Scipy have a lot of special functions, in particular Bessel functions jn
(always denoted by uppercase letter J_n(x)) and spherical Bessel functions spherical_jn
(denoted by lowercase letter j_n(x)). On the other hand mpmath have quadosc
, a special method for integrate rapidly oscillating functions, like jn
and spherical_jn
. The problem I had obtained is that seems that quadosc
from mpmath not support, e.g, jn
from scipy as an input to make this integral. I means, if I use quad
imported from numpy, so nothing error of TypeError is obtained, but quad
is not very appropriate to evaluate integrals of J_n(x) or j_n(x) when x is very large.
(***) In the site SymPy find by "Oscillatory quadrature (quadosc)", this example come from there.
from mpmath import findroot, quadosc, inf, j0
j0zero = lambda n: findroot(j0, pi*(n-0.25)) # ***
I = quadosc(j0, [0, inf], zeros=j0zero)
print(I)
I = 1.0 # OK, this is the correct answer.
But if I use J_n(x) imported from numpy:
from scipy.special import jn
f = lambda x: jn(0,x)
j0zero = lambda n: findroot(f, pi*(n-0.25))
II = quadosc(f, [0, inf], zeros=j0zero)
print(II)
then I got the following error (Edited: added the Traceback)
TypeError Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
927 try:
--> 928 fx = f(*x0)
929 multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
<ipython-input-449-aeebd9a1e908> in <lambda>(x)
2
----> 3 f = lambda x: jn(0,x)
4 j0zero = lambda n: findroot(f, pi*(n-0.25))
TypeError: ufunc 'jv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-449-aeebd9a1e908> in <module>
3 f = lambda x: jn(0,x)
4 j0zero = lambda n: findroot(f, pi*(n-0.25))
----> 5 II = quadosc(f, [0, inf], zeros=j0zero)
6 print(II)
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/quadrature.py in quadosc(ctx, f, interval, omega, period, zeros)
998 # raise ValueError("zeros do not appear to be correctly indexed")
999 n = 1
-> 1000 s = ctx.quadgl(f, [a, zeros(n)])
1001 def term(k):
1002 return ctx.quadgl(f, [zeros(k), zeros(k+1)]
~/anaconda3/lib/python3.7/site-packages/mpmath/calculus/optimization.py in findroot(ctx, f, x0, solver, tol, verbose, verify, **kwargs)
929 multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
930 except TypeError:
--> 931 fx = f(x0[0])
932 multidimensional = False
933 if 'multidimensional' in kwargs:
On the other hand, if I use quad
then I got
from scipy.integrate import quad
f = lambda x: jn(0,x)
III = quad(f,0,inf)[0]
print(III)
III = -21.154674722694516 # What is an incorrect answer.
So how can I use a function jn
that come from scipy inside a quadosc
of mpmath? How can I fix this error? Thanks for all help.