test_laguerre.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. """Tests for laguerre module.
  2. """
  3. from __future__ import division, absolute_import, print_function
  4. from functools import reduce
  5. import numpy as np
  6. import numpy.polynomial.laguerre as lag
  7. from numpy.polynomial.polynomial import polyval
  8. from numpy.testing import (
  9. assert_almost_equal, assert_raises, assert_equal, assert_,
  10. )
  11. L0 = np.array([1])/1
  12. L1 = np.array([1, -1])/1
  13. L2 = np.array([2, -4, 1])/2
  14. L3 = np.array([6, -18, 9, -1])/6
  15. L4 = np.array([24, -96, 72, -16, 1])/24
  16. L5 = np.array([120, -600, 600, -200, 25, -1])/120
  17. L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1])/720
  18. Llist = [L0, L1, L2, L3, L4, L5, L6]
  19. def trim(x):
  20. return lag.lagtrim(x, tol=1e-6)
  21. class TestConstants(object):
  22. def test_lagdomain(self):
  23. assert_equal(lag.lagdomain, [0, 1])
  24. def test_lagzero(self):
  25. assert_equal(lag.lagzero, [0])
  26. def test_lagone(self):
  27. assert_equal(lag.lagone, [1])
  28. def test_lagx(self):
  29. assert_equal(lag.lagx, [1, -1])
  30. class TestArithmetic(object):
  31. x = np.linspace(-3, 3, 100)
  32. def test_lagadd(self):
  33. for i in range(5):
  34. for j in range(5):
  35. msg = "At i=%d, j=%d" % (i, j)
  36. tgt = np.zeros(max(i, j) + 1)
  37. tgt[i] += 1
  38. tgt[j] += 1
  39. res = lag.lagadd([0]*i + [1], [0]*j + [1])
  40. assert_equal(trim(res), trim(tgt), err_msg=msg)
  41. def test_lagsub(self):
  42. for i in range(5):
  43. for j in range(5):
  44. msg = "At i=%d, j=%d" % (i, j)
  45. tgt = np.zeros(max(i, j) + 1)
  46. tgt[i] += 1
  47. tgt[j] -= 1
  48. res = lag.lagsub([0]*i + [1], [0]*j + [1])
  49. assert_equal(trim(res), trim(tgt), err_msg=msg)
  50. def test_lagmulx(self):
  51. assert_equal(lag.lagmulx([0]), [0])
  52. assert_equal(lag.lagmulx([1]), [1, -1])
  53. for i in range(1, 5):
  54. ser = [0]*i + [1]
  55. tgt = [0]*(i - 1) + [-i, 2*i + 1, -(i + 1)]
  56. assert_almost_equal(lag.lagmulx(ser), tgt)
  57. def test_lagmul(self):
  58. # check values of result
  59. for i in range(5):
  60. pol1 = [0]*i + [1]
  61. val1 = lag.lagval(self.x, pol1)
  62. for j in range(5):
  63. msg = "At i=%d, j=%d" % (i, j)
  64. pol2 = [0]*j + [1]
  65. val2 = lag.lagval(self.x, pol2)
  66. pol3 = lag.lagmul(pol1, pol2)
  67. val3 = lag.lagval(self.x, pol3)
  68. assert_(len(pol3) == i + j + 1, msg)
  69. assert_almost_equal(val3, val1*val2, err_msg=msg)
  70. def test_lagdiv(self):
  71. for i in range(5):
  72. for j in range(5):
  73. msg = "At i=%d, j=%d" % (i, j)
  74. ci = [0]*i + [1]
  75. cj = [0]*j + [1]
  76. tgt = lag.lagadd(ci, cj)
  77. quo, rem = lag.lagdiv(tgt, ci)
  78. res = lag.lagadd(lag.lagmul(quo, ci), rem)
  79. assert_almost_equal(trim(res), trim(tgt), err_msg=msg)
  80. def test_lagpow(self):
  81. for i in range(5):
  82. for j in range(5):
  83. msg = "At i=%d, j=%d" % (i, j)
  84. c = np.arange(i + 1)
  85. tgt = reduce(lag.lagmul, [c]*j, np.array([1]))
  86. res = lag.lagpow(c, j)
  87. assert_equal(trim(res), trim(tgt), err_msg=msg)
  88. class TestEvaluation(object):
  89. # coefficients of 1 + 2*x + 3*x**2
  90. c1d = np.array([9., -14., 6.])
  91. c2d = np.einsum('i,j->ij', c1d, c1d)
  92. c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
  93. # some random values in [-1, 1)
  94. x = np.random.random((3, 5))*2 - 1
  95. y = polyval(x, [1., 2., 3.])
  96. def test_lagval(self):
  97. #check empty input
  98. assert_equal(lag.lagval([], [1]).size, 0)
  99. #check normal input)
  100. x = np.linspace(-1, 1)
  101. y = [polyval(x, c) for c in Llist]
  102. for i in range(7):
  103. msg = "At i=%d" % i
  104. tgt = y[i]
  105. res = lag.lagval(x, [0]*i + [1])
  106. assert_almost_equal(res, tgt, err_msg=msg)
  107. #check that shape is preserved
  108. for i in range(3):
  109. dims = [2]*i
  110. x = np.zeros(dims)
  111. assert_equal(lag.lagval(x, [1]).shape, dims)
  112. assert_equal(lag.lagval(x, [1, 0]).shape, dims)
  113. assert_equal(lag.lagval(x, [1, 0, 0]).shape, dims)
  114. def test_lagval2d(self):
  115. x1, x2, x3 = self.x
  116. y1, y2, y3 = self.y
  117. #test exceptions
  118. assert_raises(ValueError, lag.lagval2d, x1, x2[:2], self.c2d)
  119. #test values
  120. tgt = y1*y2
  121. res = lag.lagval2d(x1, x2, self.c2d)
  122. assert_almost_equal(res, tgt)
  123. #test shape
  124. z = np.ones((2, 3))
  125. res = lag.lagval2d(z, z, self.c2d)
  126. assert_(res.shape == (2, 3))
  127. def test_lagval3d(self):
  128. x1, x2, x3 = self.x
  129. y1, y2, y3 = self.y
  130. #test exceptions
  131. assert_raises(ValueError, lag.lagval3d, x1, x2, x3[:2], self.c3d)
  132. #test values
  133. tgt = y1*y2*y3
  134. res = lag.lagval3d(x1, x2, x3, self.c3d)
  135. assert_almost_equal(res, tgt)
  136. #test shape
  137. z = np.ones((2, 3))
  138. res = lag.lagval3d(z, z, z, self.c3d)
  139. assert_(res.shape == (2, 3))
  140. def test_laggrid2d(self):
  141. x1, x2, x3 = self.x
  142. y1, y2, y3 = self.y
  143. #test values
  144. tgt = np.einsum('i,j->ij', y1, y2)
  145. res = lag.laggrid2d(x1, x2, self.c2d)
  146. assert_almost_equal(res, tgt)
  147. #test shape
  148. z = np.ones((2, 3))
  149. res = lag.laggrid2d(z, z, self.c2d)
  150. assert_(res.shape == (2, 3)*2)
  151. def test_laggrid3d(self):
  152. x1, x2, x3 = self.x
  153. y1, y2, y3 = self.y
  154. #test values
  155. tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
  156. res = lag.laggrid3d(x1, x2, x3, self.c3d)
  157. assert_almost_equal(res, tgt)
  158. #test shape
  159. z = np.ones((2, 3))
  160. res = lag.laggrid3d(z, z, z, self.c3d)
  161. assert_(res.shape == (2, 3)*3)
  162. class TestIntegral(object):
  163. def test_lagint(self):
  164. # check exceptions
  165. assert_raises(ValueError, lag.lagint, [0], .5)
  166. assert_raises(ValueError, lag.lagint, [0], -1)
  167. assert_raises(ValueError, lag.lagint, [0], 1, [0, 0])
  168. assert_raises(ValueError, lag.lagint, [0], lbnd=[0])
  169. assert_raises(ValueError, lag.lagint, [0], scl=[0])
  170. assert_raises(ValueError, lag.lagint, [0], axis=.5)
  171. # test integration of zero polynomial
  172. for i in range(2, 5):
  173. k = [0]*(i - 2) + [1]
  174. res = lag.lagint([0], m=i, k=k)
  175. assert_almost_equal(res, [1, -1])
  176. # check single integration with integration constant
  177. for i in range(5):
  178. scl = i + 1
  179. pol = [0]*i + [1]
  180. tgt = [i] + [0]*i + [1/scl]
  181. lagpol = lag.poly2lag(pol)
  182. lagint = lag.lagint(lagpol, m=1, k=[i])
  183. res = lag.lag2poly(lagint)
  184. assert_almost_equal(trim(res), trim(tgt))
  185. # check single integration with integration constant and lbnd
  186. for i in range(5):
  187. scl = i + 1
  188. pol = [0]*i + [1]
  189. lagpol = lag.poly2lag(pol)
  190. lagint = lag.lagint(lagpol, m=1, k=[i], lbnd=-1)
  191. assert_almost_equal(lag.lagval(-1, lagint), i)
  192. # check single integration with integration constant and scaling
  193. for i in range(5):
  194. scl = i + 1
  195. pol = [0]*i + [1]
  196. tgt = [i] + [0]*i + [2/scl]
  197. lagpol = lag.poly2lag(pol)
  198. lagint = lag.lagint(lagpol, m=1, k=[i], scl=2)
  199. res = lag.lag2poly(lagint)
  200. assert_almost_equal(trim(res), trim(tgt))
  201. # check multiple integrations with default k
  202. for i in range(5):
  203. for j in range(2, 5):
  204. pol = [0]*i + [1]
  205. tgt = pol[:]
  206. for k in range(j):
  207. tgt = lag.lagint(tgt, m=1)
  208. res = lag.lagint(pol, m=j)
  209. assert_almost_equal(trim(res), trim(tgt))
  210. # check multiple integrations with defined k
  211. for i in range(5):
  212. for j in range(2, 5):
  213. pol = [0]*i + [1]
  214. tgt = pol[:]
  215. for k in range(j):
  216. tgt = lag.lagint(tgt, m=1, k=[k])
  217. res = lag.lagint(pol, m=j, k=list(range(j)))
  218. assert_almost_equal(trim(res), trim(tgt))
  219. # check multiple integrations with lbnd
  220. for i in range(5):
  221. for j in range(2, 5):
  222. pol = [0]*i + [1]
  223. tgt = pol[:]
  224. for k in range(j):
  225. tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1)
  226. res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1)
  227. assert_almost_equal(trim(res), trim(tgt))
  228. # check multiple integrations with scaling
  229. for i in range(5):
  230. for j in range(2, 5):
  231. pol = [0]*i + [1]
  232. tgt = pol[:]
  233. for k in range(j):
  234. tgt = lag.lagint(tgt, m=1, k=[k], scl=2)
  235. res = lag.lagint(pol, m=j, k=list(range(j)), scl=2)
  236. assert_almost_equal(trim(res), trim(tgt))
  237. def test_lagint_axis(self):
  238. # check that axis keyword works
  239. c2d = np.random.random((3, 4))
  240. tgt = np.vstack([lag.lagint(c) for c in c2d.T]).T
  241. res = lag.lagint(c2d, axis=0)
  242. assert_almost_equal(res, tgt)
  243. tgt = np.vstack([lag.lagint(c) for c in c2d])
  244. res = lag.lagint(c2d, axis=1)
  245. assert_almost_equal(res, tgt)
  246. tgt = np.vstack([lag.lagint(c, k=3) for c in c2d])
  247. res = lag.lagint(c2d, k=3, axis=1)
  248. assert_almost_equal(res, tgt)
  249. class TestDerivative(object):
  250. def test_lagder(self):
  251. # check exceptions
  252. assert_raises(ValueError, lag.lagder, [0], .5)
  253. assert_raises(ValueError, lag.lagder, [0], -1)
  254. # check that zeroth derivative does nothing
  255. for i in range(5):
  256. tgt = [0]*i + [1]
  257. res = lag.lagder(tgt, m=0)
  258. assert_equal(trim(res), trim(tgt))
  259. # check that derivation is the inverse of integration
  260. for i in range(5):
  261. for j in range(2, 5):
  262. tgt = [0]*i + [1]
  263. res = lag.lagder(lag.lagint(tgt, m=j), m=j)
  264. assert_almost_equal(trim(res), trim(tgt))
  265. # check derivation with scaling
  266. for i in range(5):
  267. for j in range(2, 5):
  268. tgt = [0]*i + [1]
  269. res = lag.lagder(lag.lagint(tgt, m=j, scl=2), m=j, scl=.5)
  270. assert_almost_equal(trim(res), trim(tgt))
  271. def test_lagder_axis(self):
  272. # check that axis keyword works
  273. c2d = np.random.random((3, 4))
  274. tgt = np.vstack([lag.lagder(c) for c in c2d.T]).T
  275. res = lag.lagder(c2d, axis=0)
  276. assert_almost_equal(res, tgt)
  277. tgt = np.vstack([lag.lagder(c) for c in c2d])
  278. res = lag.lagder(c2d, axis=1)
  279. assert_almost_equal(res, tgt)
  280. class TestVander(object):
  281. # some random values in [-1, 1)
  282. x = np.random.random((3, 5))*2 - 1
  283. def test_lagvander(self):
  284. # check for 1d x
  285. x = np.arange(3)
  286. v = lag.lagvander(x, 3)
  287. assert_(v.shape == (3, 4))
  288. for i in range(4):
  289. coef = [0]*i + [1]
  290. assert_almost_equal(v[..., i], lag.lagval(x, coef))
  291. # check for 2d x
  292. x = np.array([[1, 2], [3, 4], [5, 6]])
  293. v = lag.lagvander(x, 3)
  294. assert_(v.shape == (3, 2, 4))
  295. for i in range(4):
  296. coef = [0]*i + [1]
  297. assert_almost_equal(v[..., i], lag.lagval(x, coef))
  298. def test_lagvander2d(self):
  299. # also tests lagval2d for non-square coefficient array
  300. x1, x2, x3 = self.x
  301. c = np.random.random((2, 3))
  302. van = lag.lagvander2d(x1, x2, [1, 2])
  303. tgt = lag.lagval2d(x1, x2, c)
  304. res = np.dot(van, c.flat)
  305. assert_almost_equal(res, tgt)
  306. # check shape
  307. van = lag.lagvander2d([x1], [x2], [1, 2])
  308. assert_(van.shape == (1, 5, 6))
  309. def test_lagvander3d(self):
  310. # also tests lagval3d for non-square coefficient array
  311. x1, x2, x3 = self.x
  312. c = np.random.random((2, 3, 4))
  313. van = lag.lagvander3d(x1, x2, x3, [1, 2, 3])
  314. tgt = lag.lagval3d(x1, x2, x3, c)
  315. res = np.dot(van, c.flat)
  316. assert_almost_equal(res, tgt)
  317. # check shape
  318. van = lag.lagvander3d([x1], [x2], [x3], [1, 2, 3])
  319. assert_(van.shape == (1, 5, 24))
  320. class TestFitting(object):
  321. def test_lagfit(self):
  322. def f(x):
  323. return x*(x - 1)*(x - 2)
  324. # Test exceptions
  325. assert_raises(ValueError, lag.lagfit, [1], [1], -1)
  326. assert_raises(TypeError, lag.lagfit, [[1]], [1], 0)
  327. assert_raises(TypeError, lag.lagfit, [], [1], 0)
  328. assert_raises(TypeError, lag.lagfit, [1], [[[1]]], 0)
  329. assert_raises(TypeError, lag.lagfit, [1, 2], [1], 0)
  330. assert_raises(TypeError, lag.lagfit, [1], [1, 2], 0)
  331. assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[[1]])
  332. assert_raises(TypeError, lag.lagfit, [1], [1], 0, w=[1, 1])
  333. assert_raises(ValueError, lag.lagfit, [1], [1], [-1,])
  334. assert_raises(ValueError, lag.lagfit, [1], [1], [2, -1, 6])
  335. assert_raises(TypeError, lag.lagfit, [1], [1], [])
  336. # Test fit
  337. x = np.linspace(0, 2)
  338. y = f(x)
  339. #
  340. coef3 = lag.lagfit(x, y, 3)
  341. assert_equal(len(coef3), 4)
  342. assert_almost_equal(lag.lagval(x, coef3), y)
  343. coef3 = lag.lagfit(x, y, [0, 1, 2, 3])
  344. assert_equal(len(coef3), 4)
  345. assert_almost_equal(lag.lagval(x, coef3), y)
  346. #
  347. coef4 = lag.lagfit(x, y, 4)
  348. assert_equal(len(coef4), 5)
  349. assert_almost_equal(lag.lagval(x, coef4), y)
  350. coef4 = lag.lagfit(x, y, [0, 1, 2, 3, 4])
  351. assert_equal(len(coef4), 5)
  352. assert_almost_equal(lag.lagval(x, coef4), y)
  353. #
  354. coef2d = lag.lagfit(x, np.array([y, y]).T, 3)
  355. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  356. coef2d = lag.lagfit(x, np.array([y, y]).T, [0, 1, 2, 3])
  357. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  358. # test weighting
  359. w = np.zeros_like(x)
  360. yw = y.copy()
  361. w[1::2] = 1
  362. y[0::2] = 0
  363. wcoef3 = lag.lagfit(x, yw, 3, w=w)
  364. assert_almost_equal(wcoef3, coef3)
  365. wcoef3 = lag.lagfit(x, yw, [0, 1, 2, 3], w=w)
  366. assert_almost_equal(wcoef3, coef3)
  367. #
  368. wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, 3, w=w)
  369. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  370. wcoef2d = lag.lagfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
  371. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  372. # test scaling with complex values x points whose square
  373. # is zero when summed.
  374. x = [1, 1j, -1, -1j]
  375. assert_almost_equal(lag.lagfit(x, x, 1), [1, -1])
  376. assert_almost_equal(lag.lagfit(x, x, [0, 1]), [1, -1])
  377. class TestCompanion(object):
  378. def test_raises(self):
  379. assert_raises(ValueError, lag.lagcompanion, [])
  380. assert_raises(ValueError, lag.lagcompanion, [1])
  381. def test_dimensions(self):
  382. for i in range(1, 5):
  383. coef = [0]*i + [1]
  384. assert_(lag.lagcompanion(coef).shape == (i, i))
  385. def test_linear_root(self):
  386. assert_(lag.lagcompanion([1, 2])[0, 0] == 1.5)
  387. class TestGauss(object):
  388. def test_100(self):
  389. x, w = lag.laggauss(100)
  390. # test orthogonality. Note that the results need to be normalized,
  391. # otherwise the huge values that can arise from fast growing
  392. # functions like Laguerre can be very confusing.
  393. v = lag.lagvander(x, 99)
  394. vv = np.dot(v.T * w, v)
  395. vd = 1/np.sqrt(vv.diagonal())
  396. vv = vd[:, None] * vv * vd
  397. assert_almost_equal(vv, np.eye(100))
  398. # check that the integral of 1 is correct
  399. tgt = 1.0
  400. assert_almost_equal(w.sum(), tgt)
  401. class TestMisc(object):
  402. def test_lagfromroots(self):
  403. res = lag.lagfromroots([])
  404. assert_almost_equal(trim(res), [1])
  405. for i in range(1, 5):
  406. roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
  407. pol = lag.lagfromroots(roots)
  408. res = lag.lagval(roots, pol)
  409. tgt = 0
  410. assert_(len(pol) == i + 1)
  411. assert_almost_equal(lag.lag2poly(pol)[-1], 1)
  412. assert_almost_equal(res, tgt)
  413. def test_lagroots(self):
  414. assert_almost_equal(lag.lagroots([1]), [])
  415. assert_almost_equal(lag.lagroots([0, 1]), [1])
  416. for i in range(2, 5):
  417. tgt = np.linspace(0, 3, i)
  418. res = lag.lagroots(lag.lagfromroots(tgt))
  419. assert_almost_equal(trim(res), trim(tgt))
  420. def test_lagtrim(self):
  421. coef = [2, -1, 1, 0]
  422. # Test exceptions
  423. assert_raises(ValueError, lag.lagtrim, coef, -1)
  424. # Test results
  425. assert_equal(lag.lagtrim(coef), coef[:-1])
  426. assert_equal(lag.lagtrim(coef, 1), coef[:-3])
  427. assert_equal(lag.lagtrim(coef, 2), [0])
  428. def test_lagline(self):
  429. assert_equal(lag.lagline(3, 4), [7, -4])
  430. def test_lag2poly(self):
  431. for i in range(7):
  432. assert_almost_equal(lag.lag2poly([0]*i + [1]), Llist[i])
  433. def test_poly2lag(self):
  434. for i in range(7):
  435. assert_almost_equal(lag.poly2lag(Llist[i]), [0]*i + [1])
  436. def test_weight(self):
  437. x = np.linspace(0, 10, 11)
  438. tgt = np.exp(-x)
  439. res = lag.lagweight(x)
  440. assert_almost_equal(res, tgt)