In this post, Antonov said Mathematica is better than scipy in integration.
However, I've tried several examples, Mathematica is always several times slower than scipy.
For example,
In[1]:= NIntegrate[x y + Sin[x^y], {x, 0, 4}, {y, 0, 4}, PrecisionGoal -> 8,
AccuracyGoal -> 8] // AbsoluteTiming
Out[1]= {2.98762, 68.8117}
In[2]:= NIntegrate[x y + Sin[x^y], {x, 0, 4}, {y, 0, 4}] // AbsoluteTiming
Out[2]= {2.10989, 68.8117}
According to this post, scipy's integration corresponds to PrecisionGoal -> 8,AccuracyGoal -> 8
Now look at scipy.
In [1]: import scipy.integrate
In [2]: import numpy as np
In [3]: %time scipy.integrate.dblquad(lambda x,y:x*y+np.sin(x**y), 0, 4,lambda x
...: :0,lambda x:4)
CPU times: user 593 ms, sys: 45.8 ms, total: 639 ms
Wall time: 638 ms
Out[3]: (68.8116996894142, 4.805091491642489e-07)
It is almost 5 times faster than 2.98762s.
What magic made scipy this fast? Any idea to make NIntegration faster?
What I actually want to is to plot integration result like this
Plot[NIntegrate[-2 Im[((-0.0006250000000000001` + ((3.5` +
0.02` I) + \[Omega] - 1.9` Cos[kx] -
2.1` Cos[ky]) ((3.5` + 0.02` I) + \[Omega] -
2.1` Cos[kx] - 1.9` Cos[ky])) ((-3.5` +
0.02` I) + \[Omega] + 2.1` Cos[kx] +
1.9` Cos[
ky]))/(-0.0006250000000000001` (-0.0006250000000000001` + \
((-3.5` + 0.02` I) + \[Omega] + 2.1` Cos[kx] +
1.9` Cos[ky]) ((-3.5` + 0.02` I) + \[Omega] +
1.9` Cos[kx] + 2.1` Cos[ky])) + ((3.5` +
0.02` I) + \[Omega] - 1.9` Cos[kx] -
2.1` Cos[ky]) ((3.5` + 0.02` I) + \[Omega] - 2.1` Cos[kx] -
1.9` Cos[
ky]) (-0.0006250000000000001` + ((-3.5` +
0.02` I) + \[Omega] + 2.1` Cos[kx] +
1.9` Cos[ky]) ((-3.5` + 0.02` I) + \[Omega] +
1.9` Cos[kx] +
2.1` Cos[
ky])))], {kx, -\[Pi], \[Pi]}, {ky, -\[Pi], \[Pi]}], {\
\[Omega].-0.5, 0.5}]
each integration took more than 3 seconds, the plotting is slow. Do I really need to call scipy inside Mathematica. This way is awkward, and there is no standard conversion function to conveniently convert long expression to python form.
NIntegrate[Integrate[x y+Sin[x^y],{y,0,4}], {x,0,4}, PrecisionGoal->8, AccuracyGoal->8]//AbsoluteTiming– matrix42 Sep 20 '18 at 06:49