test_spherical_bessel.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #
  2. # Tests of spherical Bessel functions.
  3. #
  4. import numpy as np
  5. from numpy.testing import (assert_almost_equal, assert_allclose,
  6. assert_array_almost_equal, suppress_warnings)
  7. import pytest
  8. from numpy import sin, cos, sinh, cosh, exp, inf, nan, r_, pi
  9. from scipy.special import spherical_jn, spherical_yn, spherical_in, spherical_kn
  10. from scipy.integrate import quad
  11. class TestSphericalJn:
  12. def test_spherical_jn_exact(self):
  13. # https://dlmf.nist.gov/10.49.E3
  14. # Note: exact expression is numerically stable only for small
  15. # n or z >> n.
  16. x = np.array([0.12, 1.23, 12.34, 123.45, 1234.5])
  17. assert_allclose(spherical_jn(2, x),
  18. (-1/x + 3/x**3)*sin(x) - 3/x**2*cos(x))
  19. def test_spherical_jn_recurrence_complex(self):
  20. # https://dlmf.nist.gov/10.51.E1
  21. n = np.array([1, 2, 3, 7, 12])
  22. x = 1.1 + 1.5j
  23. assert_allclose(spherical_jn(n - 1, x) + spherical_jn(n + 1, x),
  24. (2*n + 1)/x*spherical_jn(n, x))
  25. def test_spherical_jn_recurrence_real(self):
  26. # https://dlmf.nist.gov/10.51.E1
  27. n = np.array([1, 2, 3, 7, 12])
  28. x = 0.12
  29. assert_allclose(spherical_jn(n - 1, x) + spherical_jn(n + 1,x),
  30. (2*n + 1)/x*spherical_jn(n, x))
  31. def test_spherical_jn_inf_real(self):
  32. # https://dlmf.nist.gov/10.52.E3
  33. n = 6
  34. x = np.array([-inf, inf])
  35. assert_allclose(spherical_jn(n, x), np.array([0, 0]))
  36. def test_spherical_jn_inf_complex(self):
  37. # https://dlmf.nist.gov/10.52.E3
  38. n = 7
  39. x = np.array([-inf + 0j, inf + 0j, inf*(1+1j)])
  40. with suppress_warnings() as sup:
  41. sup.filter(RuntimeWarning, "invalid value encountered in multiply")
  42. assert_allclose(spherical_jn(n, x), np.array([0, 0, inf*(1+1j)]))
  43. def test_spherical_jn_large_arg_1(self):
  44. # https://github.com/scipy/scipy/issues/2165
  45. # Reference value computed using mpmath, via
  46. # besselj(n + mpf(1)/2, z)*sqrt(pi/(2*z))
  47. assert_allclose(spherical_jn(2, 3350.507), -0.00029846226538040747)
  48. def test_spherical_jn_large_arg_2(self):
  49. # https://github.com/scipy/scipy/issues/1641
  50. # Reference value computed using mpmath, via
  51. # besselj(n + mpf(1)/2, z)*sqrt(pi/(2*z))
  52. assert_allclose(spherical_jn(2, 10000), 3.0590002633029811e-05)
  53. def test_spherical_jn_at_zero(self):
  54. # https://dlmf.nist.gov/10.52.E1
  55. # But note that n = 0 is a special case: j0 = sin(x)/x -> 1
  56. n = np.array([0, 1, 2, 5, 10, 100])
  57. x = 0
  58. assert_allclose(spherical_jn(n, x), np.array([1, 0, 0, 0, 0, 0]))
  59. class TestSphericalYn:
  60. def test_spherical_yn_exact(self):
  61. # https://dlmf.nist.gov/10.49.E5
  62. # Note: exact expression is numerically stable only for small
  63. # n or z >> n.
  64. x = np.array([0.12, 1.23, 12.34, 123.45, 1234.5])
  65. assert_allclose(spherical_yn(2, x),
  66. (1/x - 3/x**3)*cos(x) - 3/x**2*sin(x))
  67. def test_spherical_yn_recurrence_real(self):
  68. # https://dlmf.nist.gov/10.51.E1
  69. n = np.array([1, 2, 3, 7, 12])
  70. x = 0.12
  71. assert_allclose(spherical_yn(n - 1, x) + spherical_yn(n + 1,x),
  72. (2*n + 1)/x*spherical_yn(n, x))
  73. def test_spherical_yn_recurrence_complex(self):
  74. # https://dlmf.nist.gov/10.51.E1
  75. n = np.array([1, 2, 3, 7, 12])
  76. x = 1.1 + 1.5j
  77. assert_allclose(spherical_yn(n - 1, x) + spherical_yn(n + 1, x),
  78. (2*n + 1)/x*spherical_yn(n, x))
  79. def test_spherical_yn_inf_real(self):
  80. # https://dlmf.nist.gov/10.52.E3
  81. n = 6
  82. x = np.array([-inf, inf])
  83. assert_allclose(spherical_yn(n, x), np.array([0, 0]))
  84. def test_spherical_yn_inf_complex(self):
  85. # https://dlmf.nist.gov/10.52.E3
  86. n = 7
  87. x = np.array([-inf + 0j, inf + 0j, inf*(1+1j)])
  88. with suppress_warnings() as sup:
  89. sup.filter(RuntimeWarning, "invalid value encountered in multiply")
  90. assert_allclose(spherical_yn(n, x), np.array([0, 0, inf*(1+1j)]))
  91. def test_spherical_yn_at_zero(self):
  92. # https://dlmf.nist.gov/10.52.E2
  93. n = np.array([0, 1, 2, 5, 10, 100])
  94. x = 0
  95. assert_allclose(spherical_yn(n, x), np.full(n.shape, -inf))
  96. def test_spherical_yn_at_zero_complex(self):
  97. # Consistently with numpy:
  98. # >>> -np.cos(0)/0
  99. # -inf
  100. # >>> -np.cos(0+0j)/(0+0j)
  101. # (-inf + nan*j)
  102. n = np.array([0, 1, 2, 5, 10, 100])
  103. x = 0 + 0j
  104. assert_allclose(spherical_yn(n, x), np.full(n.shape, nan))
  105. class TestSphericalJnYnCrossProduct:
  106. def test_spherical_jn_yn_cross_product_1(self):
  107. # https://dlmf.nist.gov/10.50.E3
  108. n = np.array([1, 5, 8])
  109. x = np.array([0.1, 1, 10])
  110. left = (spherical_jn(n + 1, x) * spherical_yn(n, x) -
  111. spherical_jn(n, x) * spherical_yn(n + 1, x))
  112. right = 1/x**2
  113. assert_allclose(left, right)
  114. def test_spherical_jn_yn_cross_product_2(self):
  115. # https://dlmf.nist.gov/10.50.E3
  116. n = np.array([1, 5, 8])
  117. x = np.array([0.1, 1, 10])
  118. left = (spherical_jn(n + 2, x) * spherical_yn(n, x) -
  119. spherical_jn(n, x) * spherical_yn(n + 2, x))
  120. right = (2*n + 3)/x**3
  121. assert_allclose(left, right)
  122. class TestSphericalIn:
  123. def test_spherical_in_exact(self):
  124. # https://dlmf.nist.gov/10.49.E9
  125. x = np.array([0.12, 1.23, 12.34, 123.45])
  126. assert_allclose(spherical_in(2, x),
  127. (1/x + 3/x**3)*sinh(x) - 3/x**2*cosh(x))
  128. def test_spherical_in_recurrence_real(self):
  129. # https://dlmf.nist.gov/10.51.E4
  130. n = np.array([1, 2, 3, 7, 12])
  131. x = 0.12
  132. assert_allclose(spherical_in(n - 1, x) - spherical_in(n + 1,x),
  133. (2*n + 1)/x*spherical_in(n, x))
  134. def test_spherical_in_recurrence_complex(self):
  135. # https://dlmf.nist.gov/10.51.E1
  136. n = np.array([1, 2, 3, 7, 12])
  137. x = 1.1 + 1.5j
  138. assert_allclose(spherical_in(n - 1, x) - spherical_in(n + 1,x),
  139. (2*n + 1)/x*spherical_in(n, x))
  140. def test_spherical_in_inf_real(self):
  141. # https://dlmf.nist.gov/10.52.E3
  142. n = 5
  143. x = np.array([-inf, inf])
  144. assert_allclose(spherical_in(n, x), np.array([-inf, inf]))
  145. def test_spherical_in_inf_complex(self):
  146. # https://dlmf.nist.gov/10.52.E5
  147. # Ideally, i1n(n, 1j*inf) = 0 and i1n(n, (1+1j)*inf) = (1+1j)*inf, but
  148. # this appears impossible to achieve because C99 regards any complex
  149. # value with at least one infinite part as a complex infinity, so
  150. # 1j*inf cannot be distinguished from (1+1j)*inf. Therefore, nan is
  151. # the correct return value.
  152. n = 7
  153. x = np.array([-inf + 0j, inf + 0j, inf*(1+1j)])
  154. assert_allclose(spherical_in(n, x), np.array([-inf, inf, nan]))
  155. def test_spherical_in_at_zero(self):
  156. # https://dlmf.nist.gov/10.52.E1
  157. # But note that n = 0 is a special case: i0 = sinh(x)/x -> 1
  158. n = np.array([0, 1, 2, 5, 10, 100])
  159. x = 0
  160. assert_allclose(spherical_in(n, x), np.array([1, 0, 0, 0, 0, 0]))
  161. class TestSphericalKn:
  162. def test_spherical_kn_exact(self):
  163. # https://dlmf.nist.gov/10.49.E13
  164. x = np.array([0.12, 1.23, 12.34, 123.45])
  165. assert_allclose(spherical_kn(2, x),
  166. pi/2*exp(-x)*(1/x + 3/x**2 + 3/x**3))
  167. def test_spherical_kn_recurrence_real(self):
  168. # https://dlmf.nist.gov/10.51.E4
  169. n = np.array([1, 2, 3, 7, 12])
  170. x = 0.12
  171. assert_allclose((-1)**(n - 1)*spherical_kn(n - 1, x) - (-1)**(n + 1)*spherical_kn(n + 1,x),
  172. (-1)**n*(2*n + 1)/x*spherical_kn(n, x))
  173. def test_spherical_kn_recurrence_complex(self):
  174. # https://dlmf.nist.gov/10.51.E4
  175. n = np.array([1, 2, 3, 7, 12])
  176. x = 1.1 + 1.5j
  177. assert_allclose((-1)**(n - 1)*spherical_kn(n - 1, x) - (-1)**(n + 1)*spherical_kn(n + 1,x),
  178. (-1)**n*(2*n + 1)/x*spherical_kn(n, x))
  179. def test_spherical_kn_inf_real(self):
  180. # https://dlmf.nist.gov/10.52.E6
  181. n = 5
  182. x = np.array([-inf, inf])
  183. assert_allclose(spherical_kn(n, x), np.array([-inf, 0]))
  184. def test_spherical_kn_inf_complex(self):
  185. # https://dlmf.nist.gov/10.52.E6
  186. # The behavior at complex infinity depends on the sign of the real
  187. # part: if Re(z) >= 0, then the limit is 0; if Re(z) < 0, then it's
  188. # z*inf. This distinction cannot be captured, so we return nan.
  189. n = 7
  190. x = np.array([-inf + 0j, inf + 0j, inf*(1+1j)])
  191. assert_allclose(spherical_kn(n, x), np.array([-inf, 0, nan]))
  192. def test_spherical_kn_at_zero(self):
  193. # https://dlmf.nist.gov/10.52.E2
  194. n = np.array([0, 1, 2, 5, 10, 100])
  195. x = 0
  196. assert_allclose(spherical_kn(n, x), np.full(n.shape, inf))
  197. def test_spherical_kn_at_zero_complex(self):
  198. # https://dlmf.nist.gov/10.52.E2
  199. n = np.array([0, 1, 2, 5, 10, 100])
  200. x = 0 + 0j
  201. assert_allclose(spherical_kn(n, x), np.full(n.shape, nan))
  202. class SphericalDerivativesTestCase:
  203. def fundamental_theorem(self, n, a, b):
  204. integral, tolerance = quad(lambda z: self.df(n, z), a, b)
  205. assert_allclose(integral,
  206. self.f(n, b) - self.f(n, a),
  207. atol=tolerance)
  208. @pytest.mark.slow
  209. def test_fundamental_theorem_0(self):
  210. self.fundamental_theorem(0, 3.0, 15.0)
  211. @pytest.mark.slow
  212. def test_fundamental_theorem_7(self):
  213. self.fundamental_theorem(7, 0.5, 1.2)
  214. class TestSphericalJnDerivatives(SphericalDerivativesTestCase):
  215. def f(self, n, z):
  216. return spherical_jn(n, z)
  217. def df(self, n, z):
  218. return spherical_jn(n, z, derivative=True)
  219. def test_spherical_jn_d_zero(self):
  220. n = np.array([0, 1, 2, 3, 7, 15])
  221. assert_allclose(spherical_jn(n, 0, derivative=True),
  222. np.array([0, 1/3, 0, 0, 0, 0]))
  223. class TestSphericalYnDerivatives(SphericalDerivativesTestCase):
  224. def f(self, n, z):
  225. return spherical_yn(n, z)
  226. def df(self, n, z):
  227. return spherical_yn(n, z, derivative=True)
  228. class TestSphericalInDerivatives(SphericalDerivativesTestCase):
  229. def f(self, n, z):
  230. return spherical_in(n, z)
  231. def df(self, n, z):
  232. return spherical_in(n, z, derivative=True)
  233. def test_spherical_in_d_zero(self):
  234. n = np.array([1, 2, 3, 7, 15])
  235. assert_allclose(spherical_in(n, 0, derivative=True),
  236. np.zeros(5))
  237. class TestSphericalKnDerivatives(SphericalDerivativesTestCase):
  238. def f(self, n, z):
  239. return spherical_kn(n, z)
  240. def df(self, n, z):
  241. return spherical_kn(n, z, derivative=True)
  242. class TestSphericalOld:
  243. # These are tests from the TestSpherical class of test_basic.py,
  244. # rewritten to use spherical_* instead of sph_* but otherwise unchanged.
  245. def test_sph_in(self):
  246. # This test reproduces test_basic.TestSpherical.test_sph_in.
  247. i1n = np.empty((2,2))
  248. x = 0.2
  249. i1n[0][0] = spherical_in(0, x)
  250. i1n[0][1] = spherical_in(1, x)
  251. i1n[1][0] = spherical_in(0, x, derivative=True)
  252. i1n[1][1] = spherical_in(1, x, derivative=True)
  253. inp0 = (i1n[0][1])
  254. inp1 = (i1n[0][0] - 2.0/0.2 * i1n[0][1])
  255. assert_array_almost_equal(i1n[0],np.array([1.0066800127054699381,
  256. 0.066933714568029540839]),12)
  257. assert_array_almost_equal(i1n[1],[inp0,inp1],12)
  258. def test_sph_in_kn_order0(self):
  259. x = 1.
  260. sph_i0 = np.empty((2,))
  261. sph_i0[0] = spherical_in(0, x)
  262. sph_i0[1] = spherical_in(0, x, derivative=True)
  263. sph_i0_expected = np.array([np.sinh(x)/x,
  264. np.cosh(x)/x-np.sinh(x)/x**2])
  265. assert_array_almost_equal(r_[sph_i0], sph_i0_expected)
  266. sph_k0 = np.empty((2,))
  267. sph_k0[0] = spherical_kn(0, x)
  268. sph_k0[1] = spherical_kn(0, x, derivative=True)
  269. sph_k0_expected = np.array([0.5*pi*exp(-x)/x,
  270. -0.5*pi*exp(-x)*(1/x+1/x**2)])
  271. assert_array_almost_equal(r_[sph_k0], sph_k0_expected)
  272. def test_sph_jn(self):
  273. s1 = np.empty((2,3))
  274. x = 0.2
  275. s1[0][0] = spherical_jn(0, x)
  276. s1[0][1] = spherical_jn(1, x)
  277. s1[0][2] = spherical_jn(2, x)
  278. s1[1][0] = spherical_jn(0, x, derivative=True)
  279. s1[1][1] = spherical_jn(1, x, derivative=True)
  280. s1[1][2] = spherical_jn(2, x, derivative=True)
  281. s10 = -s1[0][1]
  282. s11 = s1[0][0]-2.0/0.2*s1[0][1]
  283. s12 = s1[0][1]-3.0/0.2*s1[0][2]
  284. assert_array_almost_equal(s1[0],[0.99334665397530607731,
  285. 0.066400380670322230863,
  286. 0.0026590560795273856680],12)
  287. assert_array_almost_equal(s1[1],[s10,s11,s12],12)
  288. def test_sph_kn(self):
  289. kn = np.empty((2,3))
  290. x = 0.2
  291. kn[0][0] = spherical_kn(0, x)
  292. kn[0][1] = spherical_kn(1, x)
  293. kn[0][2] = spherical_kn(2, x)
  294. kn[1][0] = spherical_kn(0, x, derivative=True)
  295. kn[1][1] = spherical_kn(1, x, derivative=True)
  296. kn[1][2] = spherical_kn(2, x, derivative=True)
  297. kn0 = -kn[0][1]
  298. kn1 = -kn[0][0]-2.0/0.2*kn[0][1]
  299. kn2 = -kn[0][1]-3.0/0.2*kn[0][2]
  300. assert_array_almost_equal(kn[0],[6.4302962978445670140,
  301. 38.581777787067402086,
  302. 585.15696310385559829],12)
  303. assert_array_almost_equal(kn[1],[kn0,kn1,kn2],9)
  304. def test_sph_yn(self):
  305. sy1 = spherical_yn(2, 0.2)
  306. sy2 = spherical_yn(0, 0.2)
  307. assert_almost_equal(sy1,-377.52483,5) # previous values in the system
  308. assert_almost_equal(sy2,-4.9003329,5)
  309. sphpy = (spherical_yn(0, 0.2) - 2*spherical_yn(2, 0.2))/3
  310. sy3 = spherical_yn(1, 0.2, derivative=True)
  311. assert_almost_equal(sy3,sphpy,4) # compare correct derivative val. (correct =-system val).