Menu
![](/uploads/1/2/5/8/125838452/201467853.jpg)
Active1 year, 3 months ago
I've got some code that uses the modified Bessel functions of both 1st and 2nd order (iv and kv). Annoyingly they seem to have limits, those are iv(0,713) and kv(0,697), add one to each and you get infinity and 0 respectively. This is a problem for me because I need to use values higher than this, often up to 2000 or more. When I try to divide by these I end up diving by 0 or by infinity which means I either get errors or zeros, neither of which I want.
I'm using the scipy bessel functions, are there any better functions that can cope with much smaller and much larger numbers, or a way of modifying Python to work with these big numbers. I'm unsure what the real issue here is as to why Python can't work these out much beyond 700, is it the function or is it Python?
Zeros of Bessel functions The Bessel function J (z) of the rst kind of order 2R can be written as J (z) = z 2 X1 k=0 ( 1)k ( + k+ 1)k! Z 2 2k: (1) This is a solution of the Bessel di erential equation which can be written as z2y00(z) + zy0(z) + (z2 2)y(z) = 0; 2R: (2) We will derive some basic facts about the zeros of the Bessel function J. Ipcop advanced proxy download.
I don't know if Python is already doing it but I'd only need the first 5-10 digits *10^x for example; that is to say I wouldn't need all 1000 digits, perhaps this is the problem with how Python is working it out compared to how Wolfram Alpha is working it out?
endolith12.1k1919 gold badges9090 silver badges161161 bronze badges
RapidRapid56211 gold badge88 silver badges2323 bronze badges
4 Answers
The
iv
and kv
functions in Scipy are more or less as good as you can get if using double precision machine floating point. As noted in the comments above, you are working in the range where the results overflow from the floating point range.You can use the
pv.pv.mpmath
library, which does adjustable precision (software) floating point, to get around this. (It's similar to MPFR, but in Python):24.9k77 gold badges4343 silver badges4646 bronze badges
mpmath
is a fantastic library and is the way to go for high-precision calculations. It is worth noting that these functions can be computed from their more basic constituents. Thus, you are not forced to abide by scipy's restriction and you can use a different high precision library. Minimal example below:This gives:
This will fail for the large values you are looking for unless the underlying data has high precision.
Jonathan Wakely139k1919 gold badges260260 silver badges435435 bronze badges
HookedHooked51.8k2727 gold badges144144 silver badges218218 bronze badges
Could be the problem is with the function. For large positive x, there is the asymptotic kv(nu,x) ~ e^{-x}/sqrt{x} for any nu. So for large x you end up with very small values. If you are able to work with the log of the Bessel function instead, the problems will vanish. Scilab exploits this asymptotic: its has a parameter ice which defaults to 0, but when set to 1 will return exp(x)*kv(nu,x), and this keeps everything of reasonable size.
Actually, the same is available in scipy - scipy.special.kve
ChrisRChrisR
You can do this straightforwardly using the exponentially scaled modified Bessel functions, which will not overflow. These are implemented as
special.ive
and special.kve
. For example, the modified Bessel function of the first kind, special.iv(0, 1714)
, will overflow. However, its logarithm will be perfectly well-defined, as long as you aren't taking the log of something that has already overflowed:Other functions that readily overflow are also available as logs or in scaled versions.
![Fortran zeros of bessel function worksheet Fortran zeros of bessel function worksheet](/uploads/1/2/5/8/125838452/848949306.png)
Timothy BrandtTimothy Brandt
Not the answer you're looking for? Browse other questions tagged pythonscipybessel-functions or ask your own question.
MATLAB routines for computation of Special Functions
These routines are a direct translation, performed using f2matlab, of the original FORTRAN-77 implementation of 'Computation of Special Functions.'
For detailed description of the algorithm of each program, please consult the book 'Computation of Special Functions.'
Also see Errata at:
http://ceta.mit.edu/comp_spec_func/
Also see Errata at:
http://ceta.mit.edu/comp_spec_func/
Bernoulli numbers
Euler numbers
Chebyshev, Laguerre, and Hermite polynomials
Gauss-Legendre quadrature
Gauss-Laguerre
Gauss-Hermite
gamma function.
logarithm of the gamma function.
gamma function with a complex argument.
beta function.
psi function.
psi function with a complex argument.
incomplete gamma function.
incomplete beta function.
Legendre polynomials
Legendre functions of the second kind
associated Legendre polynomials
Bessel functions of the first and second kinds
zeros of the Bessel functions of the first and second kinds
lambda functions
modified Bessel functions of the first and second kinds
Hankel functions of the first and second kinds
integral of Bessel functions J0(t) and Y0(t) from 0 to x
integral of [1-J0(t)]/t from 0 to x and Y0(t)/t from x to infinity
integral of modified Bessel functions I0(t) and K0(t) from 0 to x
integral of [1-I0(t)]/t from 0 to x and K0(t) from x to infinity
spherical Bessel functions of the first and second kinds
Riccati-Bessel functions of the first kind and second kind
modified spherical Bessel functions of the first kind and second kind
Kelvin functions
zeros of the Kelvin functions
Airy functions
integral of the Airy functions.
zeros of Airy functions
Struve functions with an arbitrary order
integral of Struve function H0(t) from 0 to x.
integral of H0(t)/t from x to infinity.
modified Struve function with an arbitrary order.
integral of modified Struve function L0(t) from 0 to x.
hypergeometric function
hypergeometric function M(a,b,z)
hypergeometric function U(a,b,x)
parabolic cylinder functions Dv(z)
parabolic cylinder functions Vv(x)
parabolic cylinder functions W(a,+/-x)
characteristic values for the Mathieu and modified Mathieu functions.
expansion coefficients for the Mathieu and modified Mathieu functions.
Mathieu functions
modified Mathieu functions of the first and second kinds
characteristic values for spheroidal wave functions.
angular spheroidal wave functions
radial spheroidal wave functions
error function.
Fresnel Integrals.
modified Fresnel integrals.
complex zeros of the error function.
complex zeros of the Fresnel Integrals.
cosine and sine integrals
complete and incomplete elliptic integrals of the first and second kinds.
complete and incomplete elliptic integrals of the third kind.
Jacobian elliptic functions.
exponential integral E1(x)
exponential integrals En(x)
exponential integral Ei(x)
Euler numbers
Chebyshev, Laguerre, and Hermite polynomials
Gauss-Legendre quadrature
Gauss-Laguerre
Gauss-Hermite
gamma function.
logarithm of the gamma function.
gamma function with a complex argument.
beta function.
psi function.
psi function with a complex argument.
incomplete gamma function.
incomplete beta function.
Legendre polynomials
Legendre functions of the second kind
associated Legendre polynomials
Bessel functions of the first and second kinds
zeros of the Bessel functions of the first and second kinds
lambda functions
modified Bessel functions of the first and second kinds
Hankel functions of the first and second kinds
integral of Bessel functions J0(t) and Y0(t) from 0 to x
integral of [1-J0(t)]/t from 0 to x and Y0(t)/t from x to infinity
integral of modified Bessel functions I0(t) and K0(t) from 0 to x
integral of [1-I0(t)]/t from 0 to x and K0(t) from x to infinity
spherical Bessel functions of the first and second kinds
Riccati-Bessel functions of the first kind and second kind
modified spherical Bessel functions of the first kind and second kind
Kelvin functions
zeros of the Kelvin functions
Airy functions
integral of the Airy functions.
zeros of Airy functions
Struve functions with an arbitrary order
integral of Struve function H0(t) from 0 to x.
integral of H0(t)/t from x to infinity.
modified Struve function with an arbitrary order.
integral of modified Struve function L0(t) from 0 to x.
hypergeometric function
hypergeometric function M(a,b,z)
hypergeometric function U(a,b,x)
parabolic cylinder functions Dv(z)
parabolic cylinder functions Vv(x)
parabolic cylinder functions W(a,+/-x)
characteristic values for the Mathieu and modified Mathieu functions.
expansion coefficients for the Mathieu and modified Mathieu functions.
Mathieu functions
modified Mathieu functions of the first and second kinds
characteristic values for spheroidal wave functions.
angular spheroidal wave functions
radial spheroidal wave functions
error function.
Fresnel Integrals.
modified Fresnel integrals.
complex zeros of the error function.
complex zeros of the Fresnel Integrals.
cosine and sine integrals
complete and incomplete elliptic integrals of the first and second kinds.
complete and incomplete elliptic integrals of the third kind.
Jacobian elliptic functions.
exponential integral E1(x)
exponential integrals En(x)
exponential integral Ei(x)
![](/uploads/1/2/5/8/125838452/201467853.jpg)