test_spherical_bessel.py 14 KB

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