test_ellip_harm.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #
  2. # Tests for the Ellipsoidal Harmonic Function,
  3. # Distributed under the same license as SciPy itself.
  4. #
  5. import numpy as np
  6. from numpy.testing import (assert_equal, assert_almost_equal, assert_allclose,
  7. assert_, suppress_warnings)
  8. from scipy.special._testutils import assert_func_equal
  9. from scipy.special import ellip_harm, ellip_harm_2, ellip_normal
  10. from scipy.integrate import IntegrationWarning
  11. from numpy import sqrt, pi
  12. def test_ellip_potential():
  13. def change_coefficient(lambda1, mu, nu, h2, k2):
  14. x = sqrt(lambda1**2*mu**2*nu**2/(h2*k2))
  15. y = sqrt((lambda1**2 - h2)*(mu**2 - h2)*(h2 - nu**2)/(h2*(k2 - h2)))
  16. z = sqrt((lambda1**2 - k2)*(k2 - mu**2)*(k2 - nu**2)/(k2*(k2 - h2)))
  17. return x, y, z
  18. def solid_int_ellip(lambda1, mu, nu, n, p, h2, k2):
  19. return (ellip_harm(h2, k2, n, p, lambda1)*ellip_harm(h2, k2, n, p, mu)
  20. * ellip_harm(h2, k2, n, p, nu))
  21. def solid_int_ellip2(lambda1, mu, nu, n, p, h2, k2):
  22. return (ellip_harm_2(h2, k2, n, p, lambda1)
  23. * ellip_harm(h2, k2, n, p, mu)*ellip_harm(h2, k2, n, p, nu))
  24. def summation(lambda1, mu1, nu1, lambda2, mu2, nu2, h2, k2):
  25. tol = 1e-8
  26. sum1 = 0
  27. for n in range(20):
  28. xsum = 0
  29. for p in range(1, 2*n+2):
  30. xsum += (4*pi*(solid_int_ellip(lambda2, mu2, nu2, n, p, h2, k2)
  31. * solid_int_ellip2(lambda1, mu1, nu1, n, p, h2, k2)) /
  32. (ellip_normal(h2, k2, n, p)*(2*n + 1)))
  33. if abs(xsum) < 0.1*tol*abs(sum1):
  34. break
  35. sum1 += xsum
  36. return sum1, xsum
  37. def potential(lambda1, mu1, nu1, lambda2, mu2, nu2, h2, k2):
  38. x1, y1, z1 = change_coefficient(lambda1, mu1, nu1, h2, k2)
  39. x2, y2, z2 = change_coefficient(lambda2, mu2, nu2, h2, k2)
  40. res = sqrt((x2 - x1)**2 + (y2 - y1)**2 + (z2 - z1)**2)
  41. return 1/res
  42. pts = [
  43. (120, sqrt(19), 2, 41, sqrt(17), 2, 15, 25),
  44. (120, sqrt(16), 3.2, 21, sqrt(11), 2.9, 11, 20),
  45. ]
  46. with suppress_warnings() as sup:
  47. sup.filter(IntegrationWarning, "The occurrence of roundoff error")
  48. sup.filter(IntegrationWarning, "The maximum number of subdivisions")
  49. for p in pts:
  50. err_msg = repr(p)
  51. exact = potential(*p)
  52. result, last_term = summation(*p)
  53. assert_allclose(exact, result, atol=0, rtol=1e-8, err_msg=err_msg)
  54. assert_(abs(result - exact) < 10*abs(last_term), err_msg)
  55. def test_ellip_norm():
  56. def G01(h2, k2):
  57. return 4*pi
  58. def G11(h2, k2):
  59. return 4*pi*h2*k2/3
  60. def G12(h2, k2):
  61. return 4*pi*h2*(k2 - h2)/3
  62. def G13(h2, k2):
  63. return 4*pi*k2*(k2 - h2)/3
  64. def G22(h2, k2):
  65. res = (2*(h2**4 + k2**4) - 4*h2*k2*(h2**2 + k2**2) + 6*h2**2*k2**2 +
  66. sqrt(h2**2 + k2**2 - h2*k2)*(-2*(h2**3 + k2**3) + 3*h2*k2*(h2 + k2)))
  67. return 16*pi/405*res
  68. def G21(h2, k2):
  69. res = (2*(h2**4 + k2**4) - 4*h2*k2*(h2**2 + k2**2) + 6*h2**2*k2**2
  70. + sqrt(h2**2 + k2**2 - h2*k2)*(2*(h2**3 + k2**3) - 3*h2*k2*(h2 + k2)))
  71. return 16*pi/405*res
  72. def G23(h2, k2):
  73. return 4*pi*h2**2*k2*(k2 - h2)/15
  74. def G24(h2, k2):
  75. return 4*pi*h2*k2**2*(k2 - h2)/15
  76. def G25(h2, k2):
  77. return 4*pi*h2*k2*(k2 - h2)**2/15
  78. def G32(h2, k2):
  79. res = (16*(h2**4 + k2**4) - 36*h2*k2*(h2**2 + k2**2) + 46*h2**2*k2**2
  80. + sqrt(4*(h2**2 + k2**2) - 7*h2*k2)*(-8*(h2**3 + k2**3) +
  81. 11*h2*k2*(h2 + k2)))
  82. return 16*pi/13125*k2*h2*res
  83. def G31(h2, k2):
  84. res = (16*(h2**4 + k2**4) - 36*h2*k2*(h2**2 + k2**2) + 46*h2**2*k2**2
  85. + sqrt(4*(h2**2 + k2**2) - 7*h2*k2)*(8*(h2**3 + k2**3) -
  86. 11*h2*k2*(h2 + k2)))
  87. return 16*pi/13125*h2*k2*res
  88. def G34(h2, k2):
  89. res = (6*h2**4 + 16*k2**4 - 12*h2**3*k2 - 28*h2*k2**3 + 34*h2**2*k2**2
  90. + sqrt(h2**2 + 4*k2**2 - h2*k2)*(-6*h2**3 - 8*k2**3 + 9*h2**2*k2 +
  91. 13*h2*k2**2))
  92. return 16*pi/13125*h2*(k2 - h2)*res
  93. def G33(h2, k2):
  94. res = (6*h2**4 + 16*k2**4 - 12*h2**3*k2 - 28*h2*k2**3 + 34*h2**2*k2**2
  95. + sqrt(h2**2 + 4*k2**2 - h2*k2)*(6*h2**3 + 8*k2**3 - 9*h2**2*k2 -
  96. 13*h2*k2**2))
  97. return 16*pi/13125*h2*(k2 - h2)*res
  98. def G36(h2, k2):
  99. res = (16*h2**4 + 6*k2**4 - 28*h2**3*k2 - 12*h2*k2**3 + 34*h2**2*k2**2
  100. + sqrt(4*h2**2 + k2**2 - h2*k2)*(-8*h2**3 - 6*k2**3 + 13*h2**2*k2 +
  101. 9*h2*k2**2))
  102. return 16*pi/13125*k2*(k2 - h2)*res
  103. def G35(h2, k2):
  104. res = (16*h2**4 + 6*k2**4 - 28*h2**3*k2 - 12*h2*k2**3 + 34*h2**2*k2**2
  105. + sqrt(4*h2**2 + k2**2 - h2*k2)*(8*h2**3 + 6*k2**3 - 13*h2**2*k2 -
  106. 9*h2*k2**2))
  107. return 16*pi/13125*k2*(k2 - h2)*res
  108. def G37(h2, k2):
  109. return 4*pi*h2**2*k2**2*(k2 - h2)**2/105
  110. known_funcs = {(0, 1): G01, (1, 1): G11, (1, 2): G12, (1, 3): G13,
  111. (2, 1): G21, (2, 2): G22, (2, 3): G23, (2, 4): G24,
  112. (2, 5): G25, (3, 1): G31, (3, 2): G32, (3, 3): G33,
  113. (3, 4): G34, (3, 5): G35, (3, 6): G36, (3, 7): G37}
  114. def _ellip_norm(n, p, h2, k2):
  115. func = known_funcs[n, p]
  116. return func(h2, k2)
  117. _ellip_norm = np.vectorize(_ellip_norm)
  118. def ellip_normal_known(h2, k2, n, p):
  119. return _ellip_norm(n, p, h2, k2)
  120. # generate both large and small h2 < k2 pairs
  121. np.random.seed(1234)
  122. h2 = np.random.pareto(0.5, size=1)
  123. k2 = h2 * (1 + np.random.pareto(0.5, size=h2.size))
  124. points = []
  125. for n in range(4):
  126. for p in range(1, 2*n+2):
  127. points.append((h2, k2, np.full(h2.size, n), np.full(h2.size, p)))
  128. points = np.array(points)
  129. with suppress_warnings() as sup:
  130. sup.filter(IntegrationWarning, "The occurrence of roundoff error")
  131. assert_func_equal(ellip_normal, ellip_normal_known, points, rtol=1e-12)
  132. def test_ellip_harm_2():
  133. def I1(h2, k2, s):
  134. res = (ellip_harm_2(h2, k2, 1, 1, s)/(3 * ellip_harm(h2, k2, 1, 1, s))
  135. + ellip_harm_2(h2, k2, 1, 2, s)/(3 * ellip_harm(h2, k2, 1, 2, s)) +
  136. ellip_harm_2(h2, k2, 1, 3, s)/(3 * ellip_harm(h2, k2, 1, 3, s)))
  137. return res
  138. with suppress_warnings() as sup:
  139. sup.filter(IntegrationWarning, "The occurrence of roundoff error")
  140. assert_almost_equal(I1(5, 8, 10), 1/(10*sqrt((100-5)*(100-8))))
  141. # Values produced by code from arXiv:1204.0267
  142. assert_almost_equal(ellip_harm_2(5, 8, 2, 1, 10), 0.00108056853382)
  143. assert_almost_equal(ellip_harm_2(5, 8, 2, 2, 10), 0.00105820513809)
  144. assert_almost_equal(ellip_harm_2(5, 8, 2, 3, 10), 0.00106058384743)
  145. assert_almost_equal(ellip_harm_2(5, 8, 2, 4, 10), 0.00106774492306)
  146. assert_almost_equal(ellip_harm_2(5, 8, 2, 5, 10), 0.00107976356454)
  147. def test_ellip_harm():
  148. def E01(h2, k2, s):
  149. return 1
  150. def E11(h2, k2, s):
  151. return s
  152. def E12(h2, k2, s):
  153. return sqrt(abs(s*s - h2))
  154. def E13(h2, k2, s):
  155. return sqrt(abs(s*s - k2))
  156. def E21(h2, k2, s):
  157. return s*s - 1/3*((h2 + k2) + sqrt(abs((h2 + k2)*(h2 + k2)-3*h2*k2)))
  158. def E22(h2, k2, s):
  159. return s*s - 1/3*((h2 + k2) - sqrt(abs((h2 + k2)*(h2 + k2)-3*h2*k2)))
  160. def E23(h2, k2, s):
  161. return s * sqrt(abs(s*s - h2))
  162. def E24(h2, k2, s):
  163. return s * sqrt(abs(s*s - k2))
  164. def E25(h2, k2, s):
  165. return sqrt(abs((s*s - h2)*(s*s - k2)))
  166. def E31(h2, k2, s):
  167. return s*s*s - (s/5)*(2*(h2 + k2) + sqrt(4*(h2 + k2)*(h2 + k2) -
  168. 15*h2*k2))
  169. def E32(h2, k2, s):
  170. return s*s*s - (s/5)*(2*(h2 + k2) - sqrt(4*(h2 + k2)*(h2 + k2) -
  171. 15*h2*k2))
  172. def E33(h2, k2, s):
  173. return sqrt(abs(s*s - h2))*(s*s - 1/5*((h2 + 2*k2) + sqrt(abs((h2 +
  174. 2*k2)*(h2 + 2*k2) - 5*h2*k2))))
  175. def E34(h2, k2, s):
  176. return sqrt(abs(s*s - h2))*(s*s - 1/5*((h2 + 2*k2) - sqrt(abs((h2 +
  177. 2*k2)*(h2 + 2*k2) - 5*h2*k2))))
  178. def E35(h2, k2, s):
  179. return sqrt(abs(s*s - k2))*(s*s - 1/5*((2*h2 + k2) + sqrt(abs((2*h2
  180. + k2)*(2*h2 + k2) - 5*h2*k2))))
  181. def E36(h2, k2, s):
  182. return sqrt(abs(s*s - k2))*(s*s - 1/5*((2*h2 + k2) - sqrt(abs((2*h2
  183. + k2)*(2*h2 + k2) - 5*h2*k2))))
  184. def E37(h2, k2, s):
  185. return s * sqrt(abs((s*s - h2)*(s*s - k2)))
  186. assert_equal(ellip_harm(5, 8, 1, 2, 2.5, 1, 1),
  187. ellip_harm(5, 8, 1, 2, 2.5))
  188. known_funcs = {(0, 1): E01, (1, 1): E11, (1, 2): E12, (1, 3): E13,
  189. (2, 1): E21, (2, 2): E22, (2, 3): E23, (2, 4): E24,
  190. (2, 5): E25, (3, 1): E31, (3, 2): E32, (3, 3): E33,
  191. (3, 4): E34, (3, 5): E35, (3, 6): E36, (3, 7): E37}
  192. point_ref = []
  193. def ellip_harm_known(h2, k2, n, p, s):
  194. for i in range(h2.size):
  195. func = known_funcs[(int(n[i]), int(p[i]))]
  196. point_ref.append(func(h2[i], k2[i], s[i]))
  197. return point_ref
  198. np.random.seed(1234)
  199. h2 = np.random.pareto(0.5, size=30)
  200. k2 = h2*(1 + np.random.pareto(0.5, size=h2.size))
  201. s = np.random.pareto(0.5, size=h2.size)
  202. points = []
  203. for i in range(h2.size):
  204. for n in range(4):
  205. for p in range(1, 2*n+2):
  206. points.append((h2[i], k2[i], n, p, s[i]))
  207. points = np.array(points)
  208. assert_func_equal(ellip_harm, ellip_harm_known, points, rtol=1e-12)
  209. def test_ellip_harm_invalid_p():
  210. # Regression test. This should return nan.
  211. n = 4
  212. # Make p > 2*n + 1.
  213. p = 2*n + 2
  214. result = ellip_harm(0.5, 2.0, n, p, 0.2)
  215. assert np.isnan(result)