Skip to content

Commit

Permalink
DOC: examples for ive and kve (scipy#17183)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschmitz89 committed Oct 10, 2022
1 parent 2ec389b commit 9b03471
Showing 1 changed file with 120 additions and 1 deletion.
121 changes: 120 additions & 1 deletion scipy/special/_add_newdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7196,12 +7196,15 @@ def add_newdoc(name, doc):
r"""
ive(v, z, out=None)
Exponentially scaled modified Bessel function of the first kind
Exponentially scaled modified Bessel function of the first kind.
Defined as::
ive(v, z) = iv(v, z) * exp(-abs(z.real))
For imaginary numbers without a real part, returns the unscaled
Bessel function of the first kind `iv`.
Parameters
----------
v : array_like of float
Expand Down Expand Up @@ -7239,11 +7242,68 @@ def add_newdoc(name, doc):
is used, where :math:`K_v(z)` is the modified Bessel function of the
second kind, evaluated using the AMOS routine `zbesk`.
See also
--------
iv: Modified Bessel function of the first kind
i0e: Faster implementation of this function for order 0
i1e: Faster implementation of this function for order 1
References
----------
.. [1] Donald E. Amos, "AMOS, A Portable Package for Bessel Functions
of a Complex Argument and Nonnegative Order",
http://netlib.org/amos/
Examples
--------
Evaluate the function of order 0 at one point.
>>> import numpy as np
>>> from scipy.special import iv, ive
>>> import matplotlib.pyplot as plt
>>> ive(0, 1.)
0.4657596075936404
Evaluate the function at one point for different orders by
providing a list or NumPy array as argument for the `v` parameter:
>>> ive([0, 1, 1.5], 1.)
array([0.46575961, 0.20791042, 0.10798193])
Evaluate the function at several points for order 0 by providing an
array for `z`.
>>> points = np.array([-2., 0., 3.])
>>> ive(0, points)
array([0.30850832, 1. , 0.24300035])
Evaluate the function at several points for different orders by
providing arrays for both `v` for `z`. Both arrays have to be
broadcastable to the correct shape. To calculate the orders 0, 1
and 2 for a 1D array of points:
>>> ive([[0], [1], [2]], points)
array([[ 0.30850832, 1. , 0.24300035],
[-0.21526929, 0. , 0.19682671],
[ 0.09323903, 0. , 0.11178255]])
Plot the functions of order 0 to 3 from -5 to 5.
>>> fig, ax = plt.subplots()
>>> x = np.linspace(-5., 5., 1000)
>>> for i in range(4):
... ax.plot(x, ive(i, x), label=f'$I_{i!r}(z)\cdot e^{{-|z|}}$')
>>> ax.legend()
>>> ax.set_xlabel(r"$z$")
>>> plt.show()
Exponentially scaled Bessel functions are useful for large arguments for
which the unscaled Bessel functions over- or underflow. In the
following example `iv` returns infinity whereas `ive` still returns
a finite number.
>>> iv(3, 1000.), ive(3, 1000.)
(inf, 0.01256056218254712)
""")

add_newdoc("j0",
Expand Down Expand Up @@ -8409,6 +8469,12 @@ def add_newdoc(name, doc):
Wrapper for AMOS [1]_ routine `zbesk`. For a discussion of the
algorithm used, see [2]_ and the references therein.
See Also
--------
kv : This function without exponential scaling.
k0e : Faster version of this function for order 0.
k1e : Faster version of this function for order 1.
References
----------
.. [1] Donald E. Amos, "AMOS, A Portable Package for Bessel Functions
Expand All @@ -8417,6 +8483,59 @@ def add_newdoc(name, doc):
.. [2] Donald E. Amos, "Algorithm 644: A portable package for Bessel
functions of a complex argument and nonnegative order", ACM
TOMS Vol. 12 Issue 3, Sept. 1986, p. 265
Examples
--------
Evaluate the function of order 0 at one point.
>>> import numpy as np
>>> from scipy.special import kv, kve
>>> import matplotlib.pyplot as plt
>>> kve(0, 1.)
1.1444630798068949
Evaluate the function at one point for different orders by
providing a list or NumPy array as argument for the `v` parameter:
>>> kve([0, 1, 1.5], 1.)
array([1.14446308, 1.63615349, 2.50662827])
Evaluate the function at several points for order 0 by providing an
array for `z`.
>>> points = np.array([1., 3., 10.])
>>> kve(0, points)
array([1.14446308, 0.6977616 , 0.39163193])
Evaluate the function at several points for different orders by
providing arrays for both `v` for `z`. Both arrays have to be
broadcastable to the correct shape. To calculate the orders 0, 1
and 2 for a 1D array of points:
>>> kve([[0], [1], [2]], points)
array([[1.14446308, 0.6977616 , 0.39163193],
[1.63615349, 0.80656348, 0.41076657],
[4.41677005, 1.23547058, 0.47378525]])
Plot the functions of order 0 to 3 from 0 to 5.
>>> fig, ax = plt.subplots()
>>> x = np.linspace(0., 5., 1000)
>>> for i in range(4):
... ax.plot(x, kve(i, x), label=f'$K_{i!r}(z)\cdot e^z$')
>>> ax.legend()
>>> ax.set_xlabel(r"$z$")
>>> ax.set_ylim(0, 4)
>>> ax.set_xlim(0, 5)
>>> plt.show()
Exponentially scaled Bessel functions are useful for large arguments for
which the unscaled Bessel functions over- or underflow. In the
following example `kv` returns 0 whereas `kve` still returns
a useful finite number.
>>> kv(3, 1000.), kve(3, 1000.)
(0.0, 0.03980696128440973)
""")

add_newdoc("_lanczos_sum_expg_scaled",
Expand Down

0 comments on commit 9b03471

Please sign in to comment.