test_orthogonal.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. from __future__ import division, print_function, absolute_import
  2. import numpy as np
  3. from numpy import array, sqrt
  4. from numpy.testing import (assert_array_almost_equal, assert_equal,
  5. assert_almost_equal, assert_allclose)
  6. from pytest import raises as assert_raises
  7. from scipy._lib.six import xrange
  8. from scipy import integrate
  9. import scipy.special as sc
  10. from scipy.special import gamma
  11. import scipy.special.orthogonal as orth
  12. class TestCheby(object):
  13. def test_chebyc(self):
  14. C0 = orth.chebyc(0)
  15. C1 = orth.chebyc(1)
  16. olderr = np.seterr(all='ignore')
  17. try:
  18. C2 = orth.chebyc(2)
  19. C3 = orth.chebyc(3)
  20. C4 = orth.chebyc(4)
  21. C5 = orth.chebyc(5)
  22. finally:
  23. np.seterr(**olderr)
  24. assert_array_almost_equal(C0.c,[2],13)
  25. assert_array_almost_equal(C1.c,[1,0],13)
  26. assert_array_almost_equal(C2.c,[1,0,-2],13)
  27. assert_array_almost_equal(C3.c,[1,0,-3,0],13)
  28. assert_array_almost_equal(C4.c,[1,0,-4,0,2],13)
  29. assert_array_almost_equal(C5.c,[1,0,-5,0,5,0],13)
  30. def test_chebys(self):
  31. S0 = orth.chebys(0)
  32. S1 = orth.chebys(1)
  33. S2 = orth.chebys(2)
  34. S3 = orth.chebys(3)
  35. S4 = orth.chebys(4)
  36. S5 = orth.chebys(5)
  37. assert_array_almost_equal(S0.c,[1],13)
  38. assert_array_almost_equal(S1.c,[1,0],13)
  39. assert_array_almost_equal(S2.c,[1,0,-1],13)
  40. assert_array_almost_equal(S3.c,[1,0,-2,0],13)
  41. assert_array_almost_equal(S4.c,[1,0,-3,0,1],13)
  42. assert_array_almost_equal(S5.c,[1,0,-4,0,3,0],13)
  43. def test_chebyt(self):
  44. T0 = orth.chebyt(0)
  45. T1 = orth.chebyt(1)
  46. T2 = orth.chebyt(2)
  47. T3 = orth.chebyt(3)
  48. T4 = orth.chebyt(4)
  49. T5 = orth.chebyt(5)
  50. assert_array_almost_equal(T0.c,[1],13)
  51. assert_array_almost_equal(T1.c,[1,0],13)
  52. assert_array_almost_equal(T2.c,[2,0,-1],13)
  53. assert_array_almost_equal(T3.c,[4,0,-3,0],13)
  54. assert_array_almost_equal(T4.c,[8,0,-8,0,1],13)
  55. assert_array_almost_equal(T5.c,[16,0,-20,0,5,0],13)
  56. def test_chebyu(self):
  57. U0 = orth.chebyu(0)
  58. U1 = orth.chebyu(1)
  59. U2 = orth.chebyu(2)
  60. U3 = orth.chebyu(3)
  61. U4 = orth.chebyu(4)
  62. U5 = orth.chebyu(5)
  63. assert_array_almost_equal(U0.c,[1],13)
  64. assert_array_almost_equal(U1.c,[2,0],13)
  65. assert_array_almost_equal(U2.c,[4,0,-1],13)
  66. assert_array_almost_equal(U3.c,[8,0,-4,0],13)
  67. assert_array_almost_equal(U4.c,[16,0,-12,0,1],13)
  68. assert_array_almost_equal(U5.c,[32,0,-32,0,6,0],13)
  69. class TestGegenbauer(object):
  70. def test_gegenbauer(self):
  71. a = 5*np.random.random() - 0.5
  72. if np.any(a == 0):
  73. a = -0.2
  74. Ca0 = orth.gegenbauer(0,a)
  75. Ca1 = orth.gegenbauer(1,a)
  76. Ca2 = orth.gegenbauer(2,a)
  77. Ca3 = orth.gegenbauer(3,a)
  78. Ca4 = orth.gegenbauer(4,a)
  79. Ca5 = orth.gegenbauer(5,a)
  80. assert_array_almost_equal(Ca0.c,array([1]),13)
  81. assert_array_almost_equal(Ca1.c,array([2*a,0]),13)
  82. assert_array_almost_equal(Ca2.c,array([2*a*(a+1),0,-a]),13)
  83. assert_array_almost_equal(Ca3.c,array([4*orth.poch(a,3),0,-6*a*(a+1),
  84. 0])/3.0,11)
  85. assert_array_almost_equal(Ca4.c,array([4*orth.poch(a,4),0,-12*orth.poch(a,3),
  86. 0,3*a*(a+1)])/6.0,11)
  87. assert_array_almost_equal(Ca5.c,array([4*orth.poch(a,5),0,-20*orth.poch(a,4),
  88. 0,15*orth.poch(a,3),0])/15.0,11)
  89. class TestHermite(object):
  90. def test_hermite(self):
  91. H0 = orth.hermite(0)
  92. H1 = orth.hermite(1)
  93. H2 = orth.hermite(2)
  94. H3 = orth.hermite(3)
  95. H4 = orth.hermite(4)
  96. H5 = orth.hermite(5)
  97. assert_array_almost_equal(H0.c,[1],13)
  98. assert_array_almost_equal(H1.c,[2,0],13)
  99. assert_array_almost_equal(H2.c,[4,0,-2],13)
  100. assert_array_almost_equal(H3.c,[8,0,-12,0],13)
  101. assert_array_almost_equal(H4.c,[16,0,-48,0,12],12)
  102. assert_array_almost_equal(H5.c,[32,0,-160,0,120,0],12)
  103. def test_hermitenorm(self):
  104. # He_n(x) = 2**(-n/2) H_n(x/sqrt(2))
  105. psub = np.poly1d([1.0/sqrt(2),0])
  106. H0 = orth.hermitenorm(0)
  107. H1 = orth.hermitenorm(1)
  108. H2 = orth.hermitenorm(2)
  109. H3 = orth.hermitenorm(3)
  110. H4 = orth.hermitenorm(4)
  111. H5 = orth.hermitenorm(5)
  112. he0 = orth.hermite(0)(psub)
  113. he1 = orth.hermite(1)(psub) / sqrt(2)
  114. he2 = orth.hermite(2)(psub) / 2.0
  115. he3 = orth.hermite(3)(psub) / (2*sqrt(2))
  116. he4 = orth.hermite(4)(psub) / 4.0
  117. he5 = orth.hermite(5)(psub) / (4.0*sqrt(2))
  118. assert_array_almost_equal(H0.c,he0.c,13)
  119. assert_array_almost_equal(H1.c,he1.c,13)
  120. assert_array_almost_equal(H2.c,he2.c,13)
  121. assert_array_almost_equal(H3.c,he3.c,13)
  122. assert_array_almost_equal(H4.c,he4.c,13)
  123. assert_array_almost_equal(H5.c,he5.c,13)
  124. class _test_sh_legendre(object):
  125. def test_sh_legendre(self):
  126. # P*_n(x) = P_n(2x-1)
  127. psub = np.poly1d([2,-1])
  128. Ps0 = orth.sh_legendre(0)
  129. Ps1 = orth.sh_legendre(1)
  130. Ps2 = orth.sh_legendre(2)
  131. Ps3 = orth.sh_legendre(3)
  132. Ps4 = orth.sh_legendre(4)
  133. Ps5 = orth.sh_legendre(5)
  134. pse0 = orth.legendre(0)(psub)
  135. pse1 = orth.legendre(1)(psub)
  136. pse2 = orth.legendre(2)(psub)
  137. pse3 = orth.legendre(3)(psub)
  138. pse4 = orth.legendre(4)(psub)
  139. pse5 = orth.legendre(5)(psub)
  140. assert_array_almost_equal(Ps0.c,pse0.c,13)
  141. assert_array_almost_equal(Ps1.c,pse1.c,13)
  142. assert_array_almost_equal(Ps2.c,pse2.c,13)
  143. assert_array_almost_equal(Ps3.c,pse3.c,13)
  144. assert_array_almost_equal(Ps4.c,pse4.c,12)
  145. assert_array_almost_equal(Ps5.c,pse5.c,12)
  146. class _test_sh_chebyt(object):
  147. def test_sh_chebyt(self):
  148. # T*_n(x) = T_n(2x-1)
  149. psub = np.poly1d([2,-1])
  150. Ts0 = orth.sh_chebyt(0)
  151. Ts1 = orth.sh_chebyt(1)
  152. Ts2 = orth.sh_chebyt(2)
  153. Ts3 = orth.sh_chebyt(3)
  154. Ts4 = orth.sh_chebyt(4)
  155. Ts5 = orth.sh_chebyt(5)
  156. tse0 = orth.chebyt(0)(psub)
  157. tse1 = orth.chebyt(1)(psub)
  158. tse2 = orth.chebyt(2)(psub)
  159. tse3 = orth.chebyt(3)(psub)
  160. tse4 = orth.chebyt(4)(psub)
  161. tse5 = orth.chebyt(5)(psub)
  162. assert_array_almost_equal(Ts0.c,tse0.c,13)
  163. assert_array_almost_equal(Ts1.c,tse1.c,13)
  164. assert_array_almost_equal(Ts2.c,tse2.c,13)
  165. assert_array_almost_equal(Ts3.c,tse3.c,13)
  166. assert_array_almost_equal(Ts4.c,tse4.c,12)
  167. assert_array_almost_equal(Ts5.c,tse5.c,12)
  168. class _test_sh_chebyu(object):
  169. def test_sh_chebyu(self):
  170. # U*_n(x) = U_n(2x-1)
  171. psub = np.poly1d([2,-1])
  172. Us0 = orth.sh_chebyu(0)
  173. Us1 = orth.sh_chebyu(1)
  174. Us2 = orth.sh_chebyu(2)
  175. Us3 = orth.sh_chebyu(3)
  176. Us4 = orth.sh_chebyu(4)
  177. Us5 = orth.sh_chebyu(5)
  178. use0 = orth.chebyu(0)(psub)
  179. use1 = orth.chebyu(1)(psub)
  180. use2 = orth.chebyu(2)(psub)
  181. use3 = orth.chebyu(3)(psub)
  182. use4 = orth.chebyu(4)(psub)
  183. use5 = orth.chebyu(5)(psub)
  184. assert_array_almost_equal(Us0.c,use0.c,13)
  185. assert_array_almost_equal(Us1.c,use1.c,13)
  186. assert_array_almost_equal(Us2.c,use2.c,13)
  187. assert_array_almost_equal(Us3.c,use3.c,13)
  188. assert_array_almost_equal(Us4.c,use4.c,12)
  189. assert_array_almost_equal(Us5.c,use5.c,11)
  190. class _test_sh_jacobi(object):
  191. def test_sh_jacobi(self):
  192. # G^(p,q)_n(x) = n! gamma(n+p)/gamma(2*n+p) * P^(p-q,q-1)_n(2*x-1)
  193. conv = lambda n,p: gamma(n+1)*gamma(n+p)/gamma(2*n+p)
  194. psub = np.poly1d([2,-1])
  195. q = 4 * np.random.random()
  196. p = q-1 + 2*np.random.random()
  197. # print("shifted jacobi p,q = ", p, q)
  198. G0 = orth.sh_jacobi(0,p,q)
  199. G1 = orth.sh_jacobi(1,p,q)
  200. G2 = orth.sh_jacobi(2,p,q)
  201. G3 = orth.sh_jacobi(3,p,q)
  202. G4 = orth.sh_jacobi(4,p,q)
  203. G5 = orth.sh_jacobi(5,p,q)
  204. ge0 = orth.jacobi(0,p-q,q-1)(psub) * conv(0,p)
  205. ge1 = orth.jacobi(1,p-q,q-1)(psub) * conv(1,p)
  206. ge2 = orth.jacobi(2,p-q,q-1)(psub) * conv(2,p)
  207. ge3 = orth.jacobi(3,p-q,q-1)(psub) * conv(3,p)
  208. ge4 = orth.jacobi(4,p-q,q-1)(psub) * conv(4,p)
  209. ge5 = orth.jacobi(5,p-q,q-1)(psub) * conv(5,p)
  210. assert_array_almost_equal(G0.c,ge0.c,13)
  211. assert_array_almost_equal(G1.c,ge1.c,13)
  212. assert_array_almost_equal(G2.c,ge2.c,13)
  213. assert_array_almost_equal(G3.c,ge3.c,13)
  214. assert_array_almost_equal(G4.c,ge4.c,13)
  215. assert_array_almost_equal(G5.c,ge5.c,13)
  216. class TestCall(object):
  217. def test_call(self):
  218. poly = []
  219. for n in xrange(5):
  220. poly.extend([x.strip() for x in
  221. ("""
  222. orth.jacobi(%(n)d,0.3,0.9)
  223. orth.sh_jacobi(%(n)d,0.3,0.9)
  224. orth.genlaguerre(%(n)d,0.3)
  225. orth.laguerre(%(n)d)
  226. orth.hermite(%(n)d)
  227. orth.hermitenorm(%(n)d)
  228. orth.gegenbauer(%(n)d,0.3)
  229. orth.chebyt(%(n)d)
  230. orth.chebyu(%(n)d)
  231. orth.chebyc(%(n)d)
  232. orth.chebys(%(n)d)
  233. orth.sh_chebyt(%(n)d)
  234. orth.sh_chebyu(%(n)d)
  235. orth.legendre(%(n)d)
  236. orth.sh_legendre(%(n)d)
  237. """ % dict(n=n)).split()
  238. ])
  239. olderr = np.seterr(all='ignore')
  240. try:
  241. for pstr in poly:
  242. p = eval(pstr)
  243. assert_almost_equal(p(0.315), np.poly1d(p.coef)(0.315),
  244. err_msg=pstr)
  245. finally:
  246. np.seterr(**olderr)
  247. class TestGenlaguerre(object):
  248. def test_regression(self):
  249. assert_equal(orth.genlaguerre(1, 1, monic=False)(0), 2.)
  250. assert_equal(orth.genlaguerre(1, 1, monic=True)(0), -2.)
  251. assert_equal(orth.genlaguerre(1, 1, monic=False), np.poly1d([-1, 2]))
  252. assert_equal(orth.genlaguerre(1, 1, monic=True), np.poly1d([1, -2]))
  253. def verify_gauss_quad(root_func, eval_func, weight_func, a, b, N,
  254. rtol=1e-15, atol=1e-14):
  255. # this test is copied from numpy's TestGauss in test_hermite.py
  256. x, w, mu = root_func(N, True)
  257. n = np.arange(N)
  258. v = eval_func(n[:,np.newaxis], x)
  259. vv = np.dot(v*w, v.T)
  260. vd = 1 / np.sqrt(vv.diagonal())
  261. vv = vd[:, np.newaxis] * vv * vd
  262. assert_allclose(vv, np.eye(N), rtol, atol)
  263. # check that the integral of 1 is correct
  264. assert_allclose(w.sum(), mu, rtol, atol)
  265. # compare the results of integrating a function with quad.
  266. f = lambda x: x**3 - 3*x**2 + x - 2
  267. resI = integrate.quad(lambda x: f(x)*weight_func(x), a, b)
  268. resG = np.vdot(f(x), w)
  269. rtol = 1e-6 if 1e-6 < resI[1] else resI[1] * 10
  270. assert_allclose(resI[0], resG, rtol=rtol)
  271. def test_roots_jacobi():
  272. rf = lambda a, b: lambda n, mu: sc.roots_jacobi(n, a, b, mu)
  273. ef = lambda a, b: lambda n, x: orth.eval_jacobi(n, a, b, x)
  274. wf = lambda a, b: lambda x: (1 - x)**a * (1 + x)**b
  275. vgq = verify_gauss_quad
  276. vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1., 5)
  277. vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1.,
  278. 25, atol=1e-12)
  279. vgq(rf(-0.5, -0.75), ef(-0.5, -0.75), wf(-0.5, -0.75), -1., 1.,
  280. 100, atol=1e-11)
  281. vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 5)
  282. vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 25, atol=1.5e-13)
  283. vgq(rf(0.5, -0.5), ef(0.5, -0.5), wf(0.5, -0.5), -1., 1., 100, atol=1e-12)
  284. vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 5, atol=2e-13)
  285. vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 25, atol=2e-13)
  286. vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), -1., 1., 100, atol=1e-12)
  287. vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 5)
  288. vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 25, atol=1e-13)
  289. vgq(rf(0.9, 2), ef(0.9, 2), wf(0.9, 2), -1., 1., 100, atol=3e-13)
  290. vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 5)
  291. vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1., 25)
  292. vgq(rf(18.24, 27.3), ef(18.24, 27.3), wf(18.24, 27.3), -1., 1.,
  293. 100, atol=1e-13)
  294. vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 5, atol=1e-13)
  295. vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1., 25, atol=2e-13)
  296. vgq(rf(47.1, -0.2), ef(47.1, -0.2), wf(47.1, -0.2), -1., 1.,
  297. 100, atol=1e-11)
  298. vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 5)
  299. vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1., 25, atol=1e-13)
  300. vgq(rf(2.25, 68.9), ef(2.25, 68.9), wf(2.25, 68.9), -1., 1.,
  301. 100, atol=1e-13)
  302. # when alpha == beta == 0, P_n^{a,b}(x) == P_n(x)
  303. xj, wj = sc.roots_jacobi(6, 0.0, 0.0)
  304. xl, wl = sc.roots_legendre(6)
  305. assert_allclose(xj, xl, 1e-14, 1e-14)
  306. assert_allclose(wj, wl, 1e-14, 1e-14)
  307. # when alpha == beta != 0, P_n^{a,b}(x) == C_n^{alpha+0.5}(x)
  308. xj, wj = sc.roots_jacobi(6, 4.0, 4.0)
  309. xc, wc = sc.roots_gegenbauer(6, 4.5)
  310. assert_allclose(xj, xc, 1e-14, 1e-14)
  311. assert_allclose(wj, wc, 1e-14, 1e-14)
  312. x, w = sc.roots_jacobi(5, 2, 3, False)
  313. y, v, m = sc.roots_jacobi(5, 2, 3, True)
  314. assert_allclose(x, y, 1e-14, 1e-14)
  315. assert_allclose(w, v, 1e-14, 1e-14)
  316. muI, muI_err = integrate.quad(wf(2,3), -1, 1)
  317. assert_allclose(m, muI, rtol=muI_err)
  318. assert_raises(ValueError, sc.roots_jacobi, 0, 1, 1)
  319. assert_raises(ValueError, sc.roots_jacobi, 3.3, 1, 1)
  320. assert_raises(ValueError, sc.roots_jacobi, 3, -2, 1)
  321. assert_raises(ValueError, sc.roots_jacobi, 3, 1, -2)
  322. assert_raises(ValueError, sc.roots_jacobi, 3, -2, -2)
  323. def test_roots_sh_jacobi():
  324. rf = lambda a, b: lambda n, mu: sc.roots_sh_jacobi(n, a, b, mu)
  325. ef = lambda a, b: lambda n, x: orth.eval_sh_jacobi(n, a, b, x)
  326. wf = lambda a, b: lambda x: (1. - x)**(a - b) * (x)**(b - 1.)
  327. vgq = verify_gauss_quad
  328. vgq(rf(-0.5, 0.25), ef(-0.5, 0.25), wf(-0.5, 0.25), 0., 1., 5)
  329. vgq(rf(-0.5, 0.25), ef(-0.5, 0.25), wf(-0.5, 0.25), 0., 1.,
  330. 25, atol=1e-12)
  331. vgq(rf(-0.5, 0.25), ef(-0.5, 0.25), wf(-0.5, 0.25), 0., 1.,
  332. 100, atol=1e-11)
  333. vgq(rf(0.5, 0.5), ef(0.5, 0.5), wf(0.5, 0.5), 0., 1., 5)
  334. vgq(rf(0.5, 0.5), ef(0.5, 0.5), wf(0.5, 0.5), 0., 1., 25, atol=1e-13)
  335. vgq(rf(0.5, 0.5), ef(0.5, 0.5), wf(0.5, 0.5), 0., 1., 100, atol=1e-12)
  336. vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), 0., 1., 5)
  337. vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), 0., 1., 25, atol=1.5e-13)
  338. vgq(rf(1, 0.5), ef(1, 0.5), wf(1, 0.5), 0., 1., 100, atol=1e-12)
  339. vgq(rf(2, 0.9), ef(2, 0.9), wf(2, 0.9), 0., 1., 5)
  340. vgq(rf(2, 0.9), ef(2, 0.9), wf(2, 0.9), 0., 1., 25, atol=1e-13)
  341. vgq(rf(2, 0.9), ef(2, 0.9), wf(2, 0.9), 0., 1., 100, atol=1e-12)
  342. vgq(rf(27.3, 18.24), ef(27.3, 18.24), wf(27.3, 18.24), 0., 1., 5)
  343. vgq(rf(27.3, 18.24), ef(27.3, 18.24), wf(27.3, 18.24), 0., 1., 25)
  344. vgq(rf(27.3, 18.24), ef(27.3, 18.24), wf(27.3, 18.24), 0., 1.,
  345. 100, atol=1e-13)
  346. vgq(rf(47.1, 0.2), ef(47.1, 0.2), wf(47.1, 0.2), 0., 1., 5, atol=1e-12)
  347. vgq(rf(47.1, 0.2), ef(47.1, 0.2), wf(47.1, 0.2), 0., 1., 25, atol=1e-11)
  348. vgq(rf(47.1, 0.2), ef(47.1, 0.2), wf(47.1, 0.2), 0., 1., 100, atol=1e-10)
  349. vgq(rf(68.9, 2.25), ef(68.9, 2.25), wf(68.9, 2.25), 0., 1., 5, atol=3.5e-14)
  350. vgq(rf(68.9, 2.25), ef(68.9, 2.25), wf(68.9, 2.25), 0., 1., 25, atol=2e-13)
  351. vgq(rf(68.9, 2.25), ef(68.9, 2.25), wf(68.9, 2.25), 0., 1.,
  352. 100, atol=1e-12)
  353. x, w = sc.roots_sh_jacobi(5, 3, 2, False)
  354. y, v, m = sc.roots_sh_jacobi(5, 3, 2, True)
  355. assert_allclose(x, y, 1e-14, 1e-14)
  356. assert_allclose(w, v, 1e-14, 1e-14)
  357. muI, muI_err = integrate.quad(wf(3,2), 0, 1)
  358. assert_allclose(m, muI, rtol=muI_err)
  359. assert_raises(ValueError, sc.roots_sh_jacobi, 0, 1, 1)
  360. assert_raises(ValueError, sc.roots_sh_jacobi, 3.3, 1, 1)
  361. assert_raises(ValueError, sc.roots_sh_jacobi, 3, 1, 2) # p - q <= -1
  362. assert_raises(ValueError, sc.roots_sh_jacobi, 3, 2, -1) # q <= 0
  363. assert_raises(ValueError, sc.roots_sh_jacobi, 3, -2, -1) # both
  364. def test_roots_hermite():
  365. rootf = sc.roots_hermite
  366. evalf = orth.eval_hermite
  367. weightf = orth.hermite(5).weight_func
  368. verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 5)
  369. verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 25, atol=1e-13)
  370. verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 100, atol=1e-12)
  371. # Golub-Welsch branch
  372. x, w = sc.roots_hermite(5, False)
  373. y, v, m = sc.roots_hermite(5, True)
  374. assert_allclose(x, y, 1e-14, 1e-14)
  375. assert_allclose(w, v, 1e-14, 1e-14)
  376. muI, muI_err = integrate.quad(weightf, -np.inf, np.inf)
  377. assert_allclose(m, muI, rtol=muI_err)
  378. # Asymptotic branch (switch over at n >= 150)
  379. x, w = sc.roots_hermite(200, False)
  380. y, v, m = sc.roots_hermite(200, True)
  381. assert_allclose(x, y, 1e-14, 1e-14)
  382. assert_allclose(w, v, 1e-14, 1e-14)
  383. assert_allclose(sum(v), m, 1e-14, 1e-14)
  384. assert_raises(ValueError, sc.roots_hermite, 0)
  385. assert_raises(ValueError, sc.roots_hermite, 3.3)
  386. def test_roots_hermite_asy():
  387. # Recursion for Hermite functions
  388. def hermite_recursion(n, nodes):
  389. H = np.zeros((n, nodes.size))
  390. H[0,:] = np.pi**(-0.25) * np.exp(-0.5*nodes**2)
  391. if n > 1:
  392. H[1,:] = sqrt(2.0) * nodes * H[0,:]
  393. for k in xrange(2, n):
  394. H[k,:] = sqrt(2.0/k) * nodes * H[k-1,:] - sqrt((k-1.0)/k) * H[k-2,:]
  395. return H
  396. # This tests only the nodes
  397. def test(N, rtol=1e-15, atol=1e-14):
  398. x, w = orth._roots_hermite_asy(N)
  399. H = hermite_recursion(N+1, x)
  400. assert_allclose(H[-1,:], np.zeros(N), rtol, atol)
  401. assert_allclose(sum(w), sqrt(np.pi), rtol, atol)
  402. test(150, atol=1e-12)
  403. test(151, atol=1e-12)
  404. test(300, atol=1e-12)
  405. test(301, atol=1e-12)
  406. test(500, atol=1e-12)
  407. test(501, atol=1e-12)
  408. test(999, atol=1e-12)
  409. test(1000, atol=1e-12)
  410. test(2000, atol=1e-12)
  411. test(5000, atol=1e-12)
  412. def test_roots_hermitenorm():
  413. rootf = sc.roots_hermitenorm
  414. evalf = orth.eval_hermitenorm
  415. weightf = orth.hermitenorm(5).weight_func
  416. verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 5)
  417. verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 25, atol=1e-13)
  418. verify_gauss_quad(rootf, evalf, weightf, -np.inf, np.inf, 100, atol=1e-12)
  419. x, w = sc.roots_hermitenorm(5, False)
  420. y, v, m = sc.roots_hermitenorm(5, True)
  421. assert_allclose(x, y, 1e-14, 1e-14)
  422. assert_allclose(w, v, 1e-14, 1e-14)
  423. muI, muI_err = integrate.quad(weightf, -np.inf, np.inf)
  424. assert_allclose(m, muI, rtol=muI_err)
  425. assert_raises(ValueError, sc.roots_hermitenorm, 0)
  426. assert_raises(ValueError, sc.roots_hermitenorm, 3.3)
  427. def test_roots_gegenbauer():
  428. rootf = lambda a: lambda n, mu: sc.roots_gegenbauer(n, a, mu)
  429. evalf = lambda a: lambda n, x: orth.eval_gegenbauer(n, a, x)
  430. weightf = lambda a: lambda x: (1 - x**2)**(a - 0.5)
  431. vgq = verify_gauss_quad
  432. vgq(rootf(-0.25), evalf(-0.25), weightf(-0.25), -1., 1., 5)
  433. vgq(rootf(-0.25), evalf(-0.25), weightf(-0.25), -1., 1., 25, atol=1e-12)
  434. vgq(rootf(-0.25), evalf(-0.25), weightf(-0.25), -1., 1., 100, atol=1e-11)
  435. vgq(rootf(0.1), evalf(0.1), weightf(0.1), -1., 1., 5)
  436. vgq(rootf(0.1), evalf(0.1), weightf(0.1), -1., 1., 25, atol=1e-13)
  437. vgq(rootf(0.1), evalf(0.1), weightf(0.1), -1., 1., 100, atol=1e-12)
  438. vgq(rootf(1), evalf(1), weightf(1), -1., 1., 5)
  439. vgq(rootf(1), evalf(1), weightf(1), -1., 1., 25, atol=1e-13)
  440. vgq(rootf(1), evalf(1), weightf(1), -1., 1., 100, atol=1e-12)
  441. vgq(rootf(10), evalf(10), weightf(10), -1., 1., 5)
  442. vgq(rootf(10), evalf(10), weightf(10), -1., 1., 25, atol=1e-13)
  443. vgq(rootf(10), evalf(10), weightf(10), -1., 1., 100, atol=1e-12)
  444. vgq(rootf(50), evalf(50), weightf(50), -1., 1., 5, atol=1e-13)
  445. vgq(rootf(50), evalf(50), weightf(50), -1., 1., 25, atol=1e-12)
  446. vgq(rootf(50), evalf(50), weightf(50), -1., 1., 100, atol=1e-11)
  447. # this is a special case that the old code supported.
  448. # when alpha = 0, the gegenbauer polynomial is uniformly 0. but it goes
  449. # to a scaled down copy of T_n(x) there.
  450. vgq(rootf(0), orth.eval_chebyt, weightf(0), -1., 1., 5)
  451. vgq(rootf(0), orth.eval_chebyt, weightf(0), -1., 1., 25)
  452. vgq(rootf(0), orth.eval_chebyt, weightf(0), -1., 1., 100, atol=1e-12)
  453. x, w = sc.roots_gegenbauer(5, 2, False)
  454. y, v, m = sc.roots_gegenbauer(5, 2, True)
  455. assert_allclose(x, y, 1e-14, 1e-14)
  456. assert_allclose(w, v, 1e-14, 1e-14)
  457. muI, muI_err = integrate.quad(weightf(2), -1, 1)
  458. assert_allclose(m, muI, rtol=muI_err)
  459. assert_raises(ValueError, sc.roots_gegenbauer, 0, 2)
  460. assert_raises(ValueError, sc.roots_gegenbauer, 3.3, 2)
  461. assert_raises(ValueError, sc.roots_gegenbauer, 3, -.75)
  462. def test_roots_chebyt():
  463. weightf = orth.chebyt(5).weight_func
  464. verify_gauss_quad(sc.roots_chebyt, orth.eval_chebyt, weightf, -1., 1., 5)
  465. verify_gauss_quad(sc.roots_chebyt, orth.eval_chebyt, weightf, -1., 1., 25)
  466. verify_gauss_quad(sc.roots_chebyt, orth.eval_chebyt, weightf, -1., 1., 100, atol=1e-12)
  467. x, w = sc.roots_chebyt(5, False)
  468. y, v, m = sc.roots_chebyt(5, True)
  469. assert_allclose(x, y, 1e-14, 1e-14)
  470. assert_allclose(w, v, 1e-14, 1e-14)
  471. muI, muI_err = integrate.quad(weightf, -1, 1)
  472. assert_allclose(m, muI, rtol=muI_err)
  473. assert_raises(ValueError, sc.roots_chebyt, 0)
  474. assert_raises(ValueError, sc.roots_chebyt, 3.3)
  475. def test_chebyt_symmetry():
  476. x, w = sc.roots_chebyt(21)
  477. pos, neg = x[:10], x[11:]
  478. assert_equal(neg, -pos[::-1])
  479. assert_equal(x[10], 0)
  480. def test_roots_chebyu():
  481. weightf = orth.chebyu(5).weight_func
  482. verify_gauss_quad(sc.roots_chebyu, orth.eval_chebyu, weightf, -1., 1., 5)
  483. verify_gauss_quad(sc.roots_chebyu, orth.eval_chebyu, weightf, -1., 1., 25)
  484. verify_gauss_quad(sc.roots_chebyu, orth.eval_chebyu, weightf, -1., 1., 100)
  485. x, w = sc.roots_chebyu(5, False)
  486. y, v, m = sc.roots_chebyu(5, True)
  487. assert_allclose(x, y, 1e-14, 1e-14)
  488. assert_allclose(w, v, 1e-14, 1e-14)
  489. muI, muI_err = integrate.quad(weightf, -1, 1)
  490. assert_allclose(m, muI, rtol=muI_err)
  491. assert_raises(ValueError, sc.roots_chebyu, 0)
  492. assert_raises(ValueError, sc.roots_chebyu, 3.3)
  493. def test_roots_chebyc():
  494. weightf = orth.chebyc(5).weight_func
  495. verify_gauss_quad(sc.roots_chebyc, orth.eval_chebyc, weightf, -2., 2., 5)
  496. verify_gauss_quad(sc.roots_chebyc, orth.eval_chebyc, weightf, -2., 2., 25)
  497. verify_gauss_quad(sc.roots_chebyc, orth.eval_chebyc, weightf, -2., 2., 100, atol=1e-12)
  498. x, w = sc.roots_chebyc(5, False)
  499. y, v, m = sc.roots_chebyc(5, True)
  500. assert_allclose(x, y, 1e-14, 1e-14)
  501. assert_allclose(w, v, 1e-14, 1e-14)
  502. muI, muI_err = integrate.quad(weightf, -2, 2)
  503. assert_allclose(m, muI, rtol=muI_err)
  504. assert_raises(ValueError, sc.roots_chebyc, 0)
  505. assert_raises(ValueError, sc.roots_chebyc, 3.3)
  506. def test_roots_chebys():
  507. weightf = orth.chebys(5).weight_func
  508. verify_gauss_quad(sc.roots_chebys, orth.eval_chebys, weightf, -2., 2., 5)
  509. verify_gauss_quad(sc.roots_chebys, orth.eval_chebys, weightf, -2., 2., 25)
  510. verify_gauss_quad(sc.roots_chebys, orth.eval_chebys, weightf, -2., 2., 100)
  511. x, w = sc.roots_chebys(5, False)
  512. y, v, m = sc.roots_chebys(5, True)
  513. assert_allclose(x, y, 1e-14, 1e-14)
  514. assert_allclose(w, v, 1e-14, 1e-14)
  515. muI, muI_err = integrate.quad(weightf, -2, 2)
  516. assert_allclose(m, muI, rtol=muI_err)
  517. assert_raises(ValueError, sc.roots_chebys, 0)
  518. assert_raises(ValueError, sc.roots_chebys, 3.3)
  519. def test_roots_sh_chebyt():
  520. weightf = orth.sh_chebyt(5).weight_func
  521. verify_gauss_quad(sc.roots_sh_chebyt, orth.eval_sh_chebyt, weightf, 0., 1., 5)
  522. verify_gauss_quad(sc.roots_sh_chebyt, orth.eval_sh_chebyt, weightf, 0., 1., 25)
  523. verify_gauss_quad(sc.roots_sh_chebyt, orth.eval_sh_chebyt, weightf, 0., 1.,
  524. 100, atol=1e-13)
  525. x, w = sc.roots_sh_chebyt(5, False)
  526. y, v, m = sc.roots_sh_chebyt(5, True)
  527. assert_allclose(x, y, 1e-14, 1e-14)
  528. assert_allclose(w, v, 1e-14, 1e-14)
  529. muI, muI_err = integrate.quad(weightf, 0, 1)
  530. assert_allclose(m, muI, rtol=muI_err)
  531. assert_raises(ValueError, sc.roots_sh_chebyt, 0)
  532. assert_raises(ValueError, sc.roots_sh_chebyt, 3.3)
  533. def test_roots_sh_chebyu():
  534. weightf = orth.sh_chebyu(5).weight_func
  535. verify_gauss_quad(sc.roots_sh_chebyu, orth.eval_sh_chebyu, weightf, 0., 1., 5)
  536. verify_gauss_quad(sc.roots_sh_chebyu, orth.eval_sh_chebyu, weightf, 0., 1., 25)
  537. verify_gauss_quad(sc.roots_sh_chebyu, orth.eval_sh_chebyu, weightf, 0., 1.,
  538. 100, atol=1e-13)
  539. x, w = sc.roots_sh_chebyu(5, False)
  540. y, v, m = sc.roots_sh_chebyu(5, True)
  541. assert_allclose(x, y, 1e-14, 1e-14)
  542. assert_allclose(w, v, 1e-14, 1e-14)
  543. muI, muI_err = integrate.quad(weightf, 0, 1)
  544. assert_allclose(m, muI, rtol=muI_err)
  545. assert_raises(ValueError, sc.roots_sh_chebyu, 0)
  546. assert_raises(ValueError, sc.roots_sh_chebyu, 3.3)
  547. def test_roots_legendre():
  548. weightf = orth.legendre(5).weight_func
  549. verify_gauss_quad(sc.roots_legendre, orth.eval_legendre, weightf, -1., 1., 5)
  550. verify_gauss_quad(sc.roots_legendre, orth.eval_legendre, weightf, -1., 1.,
  551. 25, atol=1e-13)
  552. verify_gauss_quad(sc.roots_legendre, orth.eval_legendre, weightf, -1., 1.,
  553. 100, atol=1e-12)
  554. x, w = sc.roots_legendre(5, False)
  555. y, v, m = sc.roots_legendre(5, True)
  556. assert_allclose(x, y, 1e-14, 1e-14)
  557. assert_allclose(w, v, 1e-14, 1e-14)
  558. muI, muI_err = integrate.quad(weightf, -1, 1)
  559. assert_allclose(m, muI, rtol=muI_err)
  560. assert_raises(ValueError, sc.roots_legendre, 0)
  561. assert_raises(ValueError, sc.roots_legendre, 3.3)
  562. def test_roots_sh_legendre():
  563. weightf = orth.sh_legendre(5).weight_func
  564. verify_gauss_quad(sc.roots_sh_legendre, orth.eval_sh_legendre, weightf, 0., 1., 5)
  565. verify_gauss_quad(sc.roots_sh_legendre, orth.eval_sh_legendre, weightf, 0., 1.,
  566. 25, atol=1e-13)
  567. verify_gauss_quad(sc.roots_sh_legendre, orth.eval_sh_legendre, weightf, 0., 1.,
  568. 100, atol=1e-12)
  569. x, w = sc.roots_sh_legendre(5, False)
  570. y, v, m = sc.roots_sh_legendre(5, True)
  571. assert_allclose(x, y, 1e-14, 1e-14)
  572. assert_allclose(w, v, 1e-14, 1e-14)
  573. muI, muI_err = integrate.quad(weightf, 0, 1)
  574. assert_allclose(m, muI, rtol=muI_err)
  575. assert_raises(ValueError, sc.roots_sh_legendre, 0)
  576. assert_raises(ValueError, sc.roots_sh_legendre, 3.3)
  577. def test_roots_laguerre():
  578. weightf = orth.laguerre(5).weight_func
  579. verify_gauss_quad(sc.roots_laguerre, orth.eval_laguerre, weightf, 0., np.inf, 5)
  580. verify_gauss_quad(sc.roots_laguerre, orth.eval_laguerre, weightf, 0., np.inf,
  581. 25, atol=1e-13)
  582. verify_gauss_quad(sc.roots_laguerre, orth.eval_laguerre, weightf, 0., np.inf,
  583. 100, atol=1e-12)
  584. x, w = sc.roots_laguerre(5, False)
  585. y, v, m = sc.roots_laguerre(5, True)
  586. assert_allclose(x, y, 1e-14, 1e-14)
  587. assert_allclose(w, v, 1e-14, 1e-14)
  588. muI, muI_err = integrate.quad(weightf, 0, np.inf)
  589. assert_allclose(m, muI, rtol=muI_err)
  590. assert_raises(ValueError, sc.roots_laguerre, 0)
  591. assert_raises(ValueError, sc.roots_laguerre, 3.3)
  592. def test_roots_genlaguerre():
  593. rootf = lambda a: lambda n, mu: sc.roots_genlaguerre(n, a, mu)
  594. evalf = lambda a: lambda n, x: orth.eval_genlaguerre(n, a, x)
  595. weightf = lambda a: lambda x: x**a * np.exp(-x)
  596. vgq = verify_gauss_quad
  597. vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 5)
  598. vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 25, atol=1e-13)
  599. vgq(rootf(-0.5), evalf(-0.5), weightf(-0.5), 0., np.inf, 100, atol=1e-12)
  600. vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 5)
  601. vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 25, atol=1e-13)
  602. vgq(rootf(0.1), evalf(0.1), weightf(0.1), 0., np.inf, 100, atol=1e-13)
  603. vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 5)
  604. vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 25, atol=1e-13)
  605. vgq(rootf(1), evalf(1), weightf(1), 0., np.inf, 100, atol=1e-13)
  606. vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 5)
  607. vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 25, atol=1e-13)
  608. vgq(rootf(10), evalf(10), weightf(10), 0., np.inf, 100, atol=1e-12)
  609. vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 5)
  610. vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 25, atol=1e-13)
  611. vgq(rootf(50), evalf(50), weightf(50), 0., np.inf, 100, rtol=1e-14, atol=2e-13)
  612. x, w = sc.roots_genlaguerre(5, 2, False)
  613. y, v, m = sc.roots_genlaguerre(5, 2, True)
  614. assert_allclose(x, y, 1e-14, 1e-14)
  615. assert_allclose(w, v, 1e-14, 1e-14)
  616. muI, muI_err = integrate.quad(weightf(2.), 0., np.inf)
  617. assert_allclose(m, muI, rtol=muI_err)
  618. assert_raises(ValueError, sc.roots_genlaguerre, 0, 2)
  619. assert_raises(ValueError, sc.roots_genlaguerre, 3.3, 2)
  620. assert_raises(ValueError, sc.roots_genlaguerre, 3, -1.1)
  621. def test_gh_6721():
  622. # Regresssion test for gh_6721. This should not raise.
  623. sc.chebyt(65)(0.2)