I have a (hopefully small) problem with some numerical integration algorithm, more specifically I want to integrate the imaginary part of a complex valued function, e.g. f[u_]:=Exp[-iuK]
with $K\in\mathbb{R}$. As mentioned I am only interested in Im[f]
, in the example -Sin[u K]
.
Now if I integrate with Mathematica
NIntegrate[f, {s, Min[roots[[ 1 ]], roots[[ 2 ]]],
Max[roots[[ 1 ]], roots[[ 2 ]]]}, AccuracyGoal->aGoal,
PrecisionGoal->pGoal, WorkingPrecision->wPrecision ];
I get two different results depending on f
:
- if I use
-Sin[u K]
, it returnssomenumber
- if I use
Im[f]
, it returns a list{ somenumber }
Those two have to be treated differently and that crashes my program. I have a few questions:
Why does Mathematica sometimes return lists, and sometimes values? How can I distinguish between a list and a value, i.e.
If xyz is a list then
do something
else
do something else
end
Any other ideas how one could avoid these different return "types"? The manual and anything I found hasn't been useful so far.
Answer
How about putting the results in a list and then removing unneeded braces.?
a = 76.5
b = {4, 5, 12.3}
Flatten[{a}]
Flatten[{b}]
That way you are always dealing with a list of values.
Or simply place braces around numbers (that are not already in a list):
If[NumericQ[x], x = {x}]
Comments
Post a Comment