_spherical_bessel.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. from ._ufuncs import (_spherical_jn, _spherical_yn, _spherical_in,
  2. _spherical_kn, _spherical_jn_d, _spherical_yn_d,
  3. _spherical_in_d, _spherical_kn_d)
  4. def spherical_jn(n, z, derivative=False):
  5. r"""Spherical Bessel function of the first kind or its derivative.
  6. Defined as [1]_,
  7. .. math:: j_n(z) = \sqrt{\frac{\pi}{2z}} J_{n + 1/2}(z),
  8. where :math:`J_n` is the Bessel function of the first kind.
  9. Parameters
  10. ----------
  11. n : int, array_like
  12. Order of the Bessel function (n >= 0).
  13. z : complex or float, array_like
  14. Argument of the Bessel function.
  15. derivative : bool, optional
  16. If True, the value of the derivative (rather than the function
  17. itself) is returned.
  18. Returns
  19. -------
  20. jn : ndarray
  21. Notes
  22. -----
  23. For real arguments greater than the order, the function is computed
  24. using the ascending recurrence [2]_. For small real or complex
  25. arguments, the definitional relation to the cylindrical Bessel function
  26. of the first kind is used.
  27. The derivative is computed using the relations [3]_,
  28. .. math::
  29. j_n'(z) = j_{n-1}(z) - \frac{n + 1}{z} j_n(z).
  30. j_0'(z) = -j_1(z)
  31. .. versionadded:: 0.18.0
  32. References
  33. ----------
  34. .. [1] https://dlmf.nist.gov/10.47.E3
  35. .. [2] https://dlmf.nist.gov/10.51.E1
  36. .. [3] https://dlmf.nist.gov/10.51.E2
  37. .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
  38. Handbook of Mathematical Functions with Formulas,
  39. Graphs, and Mathematical Tables. New York: Dover, 1972.
  40. Examples
  41. --------
  42. The spherical Bessel functions of the first kind :math:`j_n` accept
  43. both real and complex second argument. They can return a complex type:
  44. >>> from scipy.special import spherical_jn
  45. >>> spherical_jn(0, 3+5j)
  46. (-9.878987731663194-8.021894345786002j)
  47. >>> type(spherical_jn(0, 3+5j))
  48. <class 'numpy.complex128'>
  49. We can verify the relation for the derivative from the Notes
  50. for :math:`n=3` in the interval :math:`[1, 2]`:
  51. >>> import numpy as np
  52. >>> x = np.arange(1.0, 2.0, 0.01)
  53. >>> np.allclose(spherical_jn(3, x, True),
  54. ... spherical_jn(2, x) - 4/x * spherical_jn(3, x))
  55. True
  56. The first few :math:`j_n` with real argument:
  57. >>> import matplotlib.pyplot as plt
  58. >>> x = np.arange(0.0, 10.0, 0.01)
  59. >>> fig, ax = plt.subplots()
  60. >>> ax.set_ylim(-0.5, 1.5)
  61. >>> ax.set_title(r'Spherical Bessel functions $j_n$')
  62. >>> for n in np.arange(0, 4):
  63. ... ax.plot(x, spherical_jn(n, x), label=rf'$j_{n}$')
  64. >>> plt.legend(loc='best')
  65. >>> plt.show()
  66. """
  67. if derivative:
  68. return _spherical_jn_d(n, z)
  69. else:
  70. return _spherical_jn(n, z)
  71. def spherical_yn(n, z, derivative=False):
  72. r"""Spherical Bessel function of the second kind or its derivative.
  73. Defined as [1]_,
  74. .. math:: y_n(z) = \sqrt{\frac{\pi}{2z}} Y_{n + 1/2}(z),
  75. where :math:`Y_n` is the Bessel function of the second kind.
  76. Parameters
  77. ----------
  78. n : int, array_like
  79. Order of the Bessel function (n >= 0).
  80. z : complex or float, array_like
  81. Argument of the Bessel function.
  82. derivative : bool, optional
  83. If True, the value of the derivative (rather than the function
  84. itself) is returned.
  85. Returns
  86. -------
  87. yn : ndarray
  88. Notes
  89. -----
  90. For real arguments, the function is computed using the ascending
  91. recurrence [2]_. For complex arguments, the definitional relation to
  92. the cylindrical Bessel function of the second kind is used.
  93. The derivative is computed using the relations [3]_,
  94. .. math::
  95. y_n' = y_{n-1} - \frac{n + 1}{z} y_n.
  96. y_0' = -y_1
  97. .. versionadded:: 0.18.0
  98. References
  99. ----------
  100. .. [1] https://dlmf.nist.gov/10.47.E4
  101. .. [2] https://dlmf.nist.gov/10.51.E1
  102. .. [3] https://dlmf.nist.gov/10.51.E2
  103. .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
  104. Handbook of Mathematical Functions with Formulas,
  105. Graphs, and Mathematical Tables. New York: Dover, 1972.
  106. Examples
  107. --------
  108. The spherical Bessel functions of the second kind :math:`y_n` accept
  109. both real and complex second argument. They can return a complex type:
  110. >>> from scipy.special import spherical_yn
  111. >>> spherical_yn(0, 3+5j)
  112. (8.022343088587197-9.880052589376795j)
  113. >>> type(spherical_yn(0, 3+5j))
  114. <class 'numpy.complex128'>
  115. We can verify the relation for the derivative from the Notes
  116. for :math:`n=3` in the interval :math:`[1, 2]`:
  117. >>> import numpy as np
  118. >>> x = np.arange(1.0, 2.0, 0.01)
  119. >>> np.allclose(spherical_yn(3, x, True),
  120. ... spherical_yn(2, x) - 4/x * spherical_yn(3, x))
  121. True
  122. The first few :math:`y_n` with real argument:
  123. >>> import matplotlib.pyplot as plt
  124. >>> x = np.arange(0.0, 10.0, 0.01)
  125. >>> fig, ax = plt.subplots()
  126. >>> ax.set_ylim(-2.0, 1.0)
  127. >>> ax.set_title(r'Spherical Bessel functions $y_n$')
  128. >>> for n in np.arange(0, 4):
  129. ... ax.plot(x, spherical_yn(n, x), label=rf'$y_{n}$')
  130. >>> plt.legend(loc='best')
  131. >>> plt.show()
  132. """
  133. if derivative:
  134. return _spherical_yn_d(n, z)
  135. else:
  136. return _spherical_yn(n, z)
  137. def spherical_in(n, z, derivative=False):
  138. r"""Modified spherical Bessel function of the first kind or its derivative.
  139. Defined as [1]_,
  140. .. math:: i_n(z) = \sqrt{\frac{\pi}{2z}} I_{n + 1/2}(z),
  141. where :math:`I_n` is the modified Bessel function of the first kind.
  142. Parameters
  143. ----------
  144. n : int, array_like
  145. Order of the Bessel function (n >= 0).
  146. z : complex or float, array_like
  147. Argument of the Bessel function.
  148. derivative : bool, optional
  149. If True, the value of the derivative (rather than the function
  150. itself) is returned.
  151. Returns
  152. -------
  153. in : ndarray
  154. Notes
  155. -----
  156. The function is computed using its definitional relation to the
  157. modified cylindrical Bessel function of the first kind.
  158. The derivative is computed using the relations [2]_,
  159. .. math::
  160. i_n' = i_{n-1} - \frac{n + 1}{z} i_n.
  161. i_1' = i_0
  162. .. versionadded:: 0.18.0
  163. References
  164. ----------
  165. .. [1] https://dlmf.nist.gov/10.47.E7
  166. .. [2] https://dlmf.nist.gov/10.51.E5
  167. .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
  168. Handbook of Mathematical Functions with Formulas,
  169. Graphs, and Mathematical Tables. New York: Dover, 1972.
  170. Examples
  171. --------
  172. The modified spherical Bessel functions of the first kind :math:`i_n`
  173. accept both real and complex second argument.
  174. They can return a complex type:
  175. >>> from scipy.special import spherical_in
  176. >>> spherical_in(0, 3+5j)
  177. (-1.1689867793369182-1.2697305267234222j)
  178. >>> type(spherical_in(0, 3+5j))
  179. <class 'numpy.complex128'>
  180. We can verify the relation for the derivative from the Notes
  181. for :math:`n=3` in the interval :math:`[1, 2]`:
  182. >>> import numpy as np
  183. >>> x = np.arange(1.0, 2.0, 0.01)
  184. >>> np.allclose(spherical_in(3, x, True),
  185. ... spherical_in(2, x) - 4/x * spherical_in(3, x))
  186. True
  187. The first few :math:`i_n` with real argument:
  188. >>> import matplotlib.pyplot as plt
  189. >>> x = np.arange(0.0, 6.0, 0.01)
  190. >>> fig, ax = plt.subplots()
  191. >>> ax.set_ylim(-0.5, 5.0)
  192. >>> ax.set_title(r'Modified spherical Bessel functions $i_n$')
  193. >>> for n in np.arange(0, 4):
  194. ... ax.plot(x, spherical_in(n, x), label=rf'$i_{n}$')
  195. >>> plt.legend(loc='best')
  196. >>> plt.show()
  197. """
  198. if derivative:
  199. return _spherical_in_d(n, z)
  200. else:
  201. return _spherical_in(n, z)
  202. def spherical_kn(n, z, derivative=False):
  203. r"""Modified spherical Bessel function of the second kind or its derivative.
  204. Defined as [1]_,
  205. .. math:: k_n(z) = \sqrt{\frac{\pi}{2z}} K_{n + 1/2}(z),
  206. where :math:`K_n` is the modified Bessel function of the second kind.
  207. Parameters
  208. ----------
  209. n : int, array_like
  210. Order of the Bessel function (n >= 0).
  211. z : complex or float, array_like
  212. Argument of the Bessel function.
  213. derivative : bool, optional
  214. If True, the value of the derivative (rather than the function
  215. itself) is returned.
  216. Returns
  217. -------
  218. kn : ndarray
  219. Notes
  220. -----
  221. The function is computed using its definitional relation to the
  222. modified cylindrical Bessel function of the second kind.
  223. The derivative is computed using the relations [2]_,
  224. .. math::
  225. k_n' = -k_{n-1} - \frac{n + 1}{z} k_n.
  226. k_0' = -k_1
  227. .. versionadded:: 0.18.0
  228. References
  229. ----------
  230. .. [1] https://dlmf.nist.gov/10.47.E9
  231. .. [2] https://dlmf.nist.gov/10.51.E5
  232. .. [AS] Milton Abramowitz and Irene A. Stegun, eds.
  233. Handbook of Mathematical Functions with Formulas,
  234. Graphs, and Mathematical Tables. New York: Dover, 1972.
  235. Examples
  236. --------
  237. The modified spherical Bessel functions of the second kind :math:`k_n`
  238. accept both real and complex second argument.
  239. They can return a complex type:
  240. >>> from scipy.special import spherical_kn
  241. >>> spherical_kn(0, 3+5j)
  242. (0.012985785614001561+0.003354691603137546j)
  243. >>> type(spherical_kn(0, 3+5j))
  244. <class 'numpy.complex128'>
  245. We can verify the relation for the derivative from the Notes
  246. for :math:`n=3` in the interval :math:`[1, 2]`:
  247. >>> import numpy as np
  248. >>> x = np.arange(1.0, 2.0, 0.01)
  249. >>> np.allclose(spherical_kn(3, x, True),
  250. ... - 4/x * spherical_kn(3, x) - spherical_kn(2, x))
  251. True
  252. The first few :math:`k_n` with real argument:
  253. >>> import matplotlib.pyplot as plt
  254. >>> x = np.arange(0.0, 4.0, 0.01)
  255. >>> fig, ax = plt.subplots()
  256. >>> ax.set_ylim(0.0, 5.0)
  257. >>> ax.set_title(r'Modified spherical Bessel functions $k_n$')
  258. >>> for n in np.arange(0, 4):
  259. ... ax.plot(x, spherical_kn(n, x), label=rf'$k_{n}$')
  260. >>> plt.legend(loc='best')
  261. >>> plt.show()
  262. """
  263. if derivative:
  264. return _spherical_kn_d(n, z)
  265. else:
  266. return _spherical_kn(n, z)