test_orthogonal.py 32 KB

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