test_legendre.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. """Tests for legendre 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.legendre as leg
  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])
  12. L1 = np.array([0, 1])
  13. L2 = np.array([-1, 0, 3])/2
  14. L3 = np.array([0, -3, 0, 5])/2
  15. L4 = np.array([3, 0, -30, 0, 35])/8
  16. L5 = np.array([0, 15, 0, -70, 0, 63])/8
  17. L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16
  18. L7 = np.array([0, -35, 0, 315, 0, -693, 0, 429])/16
  19. L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128
  20. L9 = np.array([0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128
  21. Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9]
  22. def trim(x):
  23. return leg.legtrim(x, tol=1e-6)
  24. class TestConstants(object):
  25. def test_legdomain(self):
  26. assert_equal(leg.legdomain, [-1, 1])
  27. def test_legzero(self):
  28. assert_equal(leg.legzero, [0])
  29. def test_legone(self):
  30. assert_equal(leg.legone, [1])
  31. def test_legx(self):
  32. assert_equal(leg.legx, [0, 1])
  33. class TestArithmetic(object):
  34. x = np.linspace(-1, 1, 100)
  35. def test_legadd(self):
  36. for i in range(5):
  37. for j in range(5):
  38. msg = "At i=%d, j=%d" % (i, j)
  39. tgt = np.zeros(max(i, j) + 1)
  40. tgt[i] += 1
  41. tgt[j] += 1
  42. res = leg.legadd([0]*i + [1], [0]*j + [1])
  43. assert_equal(trim(res), trim(tgt), err_msg=msg)
  44. def test_legsub(self):
  45. for i in range(5):
  46. for j in range(5):
  47. msg = "At i=%d, j=%d" % (i, j)
  48. tgt = np.zeros(max(i, j) + 1)
  49. tgt[i] += 1
  50. tgt[j] -= 1
  51. res = leg.legsub([0]*i + [1], [0]*j + [1])
  52. assert_equal(trim(res), trim(tgt), err_msg=msg)
  53. def test_legmulx(self):
  54. assert_equal(leg.legmulx([0]), [0])
  55. assert_equal(leg.legmulx([1]), [0, 1])
  56. for i in range(1, 5):
  57. tmp = 2*i + 1
  58. ser = [0]*i + [1]
  59. tgt = [0]*(i - 1) + [i/tmp, 0, (i + 1)/tmp]
  60. assert_equal(leg.legmulx(ser), tgt)
  61. def test_legmul(self):
  62. # check values of result
  63. for i in range(5):
  64. pol1 = [0]*i + [1]
  65. val1 = leg.legval(self.x, pol1)
  66. for j in range(5):
  67. msg = "At i=%d, j=%d" % (i, j)
  68. pol2 = [0]*j + [1]
  69. val2 = leg.legval(self.x, pol2)
  70. pol3 = leg.legmul(pol1, pol2)
  71. val3 = leg.legval(self.x, pol3)
  72. assert_(len(pol3) == i + j + 1, msg)
  73. assert_almost_equal(val3, val1*val2, err_msg=msg)
  74. def test_legdiv(self):
  75. for i in range(5):
  76. for j in range(5):
  77. msg = "At i=%d, j=%d" % (i, j)
  78. ci = [0]*i + [1]
  79. cj = [0]*j + [1]
  80. tgt = leg.legadd(ci, cj)
  81. quo, rem = leg.legdiv(tgt, ci)
  82. res = leg.legadd(leg.legmul(quo, ci), rem)
  83. assert_equal(trim(res), trim(tgt), err_msg=msg)
  84. def test_legpow(self):
  85. for i in range(5):
  86. for j in range(5):
  87. msg = "At i=%d, j=%d" % (i, j)
  88. c = np.arange(i + 1)
  89. tgt = reduce(leg.legmul, [c]*j, np.array([1]))
  90. res = leg.legpow(c, j)
  91. assert_equal(trim(res), trim(tgt), err_msg=msg)
  92. class TestEvaluation(object):
  93. # coefficients of 1 + 2*x + 3*x**2
  94. c1d = np.array([2., 2., 2.])
  95. c2d = np.einsum('i,j->ij', c1d, c1d)
  96. c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
  97. # some random values in [-1, 1)
  98. x = np.random.random((3, 5))*2 - 1
  99. y = polyval(x, [1., 2., 3.])
  100. def test_legval(self):
  101. #check empty input
  102. assert_equal(leg.legval([], [1]).size, 0)
  103. #check normal input)
  104. x = np.linspace(-1, 1)
  105. y = [polyval(x, c) for c in Llist]
  106. for i in range(10):
  107. msg = "At i=%d" % i
  108. tgt = y[i]
  109. res = leg.legval(x, [0]*i + [1])
  110. assert_almost_equal(res, tgt, err_msg=msg)
  111. #check that shape is preserved
  112. for i in range(3):
  113. dims = [2]*i
  114. x = np.zeros(dims)
  115. assert_equal(leg.legval(x, [1]).shape, dims)
  116. assert_equal(leg.legval(x, [1, 0]).shape, dims)
  117. assert_equal(leg.legval(x, [1, 0, 0]).shape, dims)
  118. def test_legval2d(self):
  119. x1, x2, x3 = self.x
  120. y1, y2, y3 = self.y
  121. #test exceptions
  122. assert_raises(ValueError, leg.legval2d, x1, x2[:2], self.c2d)
  123. #test values
  124. tgt = y1*y2
  125. res = leg.legval2d(x1, x2, self.c2d)
  126. assert_almost_equal(res, tgt)
  127. #test shape
  128. z = np.ones((2, 3))
  129. res = leg.legval2d(z, z, self.c2d)
  130. assert_(res.shape == (2, 3))
  131. def test_legval3d(self):
  132. x1, x2, x3 = self.x
  133. y1, y2, y3 = self.y
  134. #test exceptions
  135. assert_raises(ValueError, leg.legval3d, x1, x2, x3[:2], self.c3d)
  136. #test values
  137. tgt = y1*y2*y3
  138. res = leg.legval3d(x1, x2, x3, self.c3d)
  139. assert_almost_equal(res, tgt)
  140. #test shape
  141. z = np.ones((2, 3))
  142. res = leg.legval3d(z, z, z, self.c3d)
  143. assert_(res.shape == (2, 3))
  144. def test_leggrid2d(self):
  145. x1, x2, x3 = self.x
  146. y1, y2, y3 = self.y
  147. #test values
  148. tgt = np.einsum('i,j->ij', y1, y2)
  149. res = leg.leggrid2d(x1, x2, self.c2d)
  150. assert_almost_equal(res, tgt)
  151. #test shape
  152. z = np.ones((2, 3))
  153. res = leg.leggrid2d(z, z, self.c2d)
  154. assert_(res.shape == (2, 3)*2)
  155. def test_leggrid3d(self):
  156. x1, x2, x3 = self.x
  157. y1, y2, y3 = self.y
  158. #test values
  159. tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
  160. res = leg.leggrid3d(x1, x2, x3, self.c3d)
  161. assert_almost_equal(res, tgt)
  162. #test shape
  163. z = np.ones((2, 3))
  164. res = leg.leggrid3d(z, z, z, self.c3d)
  165. assert_(res.shape == (2, 3)*3)
  166. class TestIntegral(object):
  167. def test_legint(self):
  168. # check exceptions
  169. assert_raises(TypeError, leg.legint, [0], .5)
  170. assert_raises(ValueError, leg.legint, [0], -1)
  171. assert_raises(ValueError, leg.legint, [0], 1, [0, 0])
  172. assert_raises(ValueError, leg.legint, [0], lbnd=[0])
  173. assert_raises(ValueError, leg.legint, [0], scl=[0])
  174. assert_raises(TypeError, leg.legint, [0], axis=.5)
  175. # test integration of zero polynomial
  176. for i in range(2, 5):
  177. k = [0]*(i - 2) + [1]
  178. res = leg.legint([0], m=i, k=k)
  179. assert_almost_equal(res, [0, 1])
  180. # check single integration with integration constant
  181. for i in range(5):
  182. scl = i + 1
  183. pol = [0]*i + [1]
  184. tgt = [i] + [0]*i + [1/scl]
  185. legpol = leg.poly2leg(pol)
  186. legint = leg.legint(legpol, m=1, k=[i])
  187. res = leg.leg2poly(legint)
  188. assert_almost_equal(trim(res), trim(tgt))
  189. # check single integration with integration constant and lbnd
  190. for i in range(5):
  191. scl = i + 1
  192. pol = [0]*i + [1]
  193. legpol = leg.poly2leg(pol)
  194. legint = leg.legint(legpol, m=1, k=[i], lbnd=-1)
  195. assert_almost_equal(leg.legval(-1, legint), i)
  196. # check single integration with integration constant and scaling
  197. for i in range(5):
  198. scl = i + 1
  199. pol = [0]*i + [1]
  200. tgt = [i] + [0]*i + [2/scl]
  201. legpol = leg.poly2leg(pol)
  202. legint = leg.legint(legpol, m=1, k=[i], scl=2)
  203. res = leg.leg2poly(legint)
  204. assert_almost_equal(trim(res), trim(tgt))
  205. # check multiple integrations with default k
  206. for i in range(5):
  207. for j in range(2, 5):
  208. pol = [0]*i + [1]
  209. tgt = pol[:]
  210. for k in range(j):
  211. tgt = leg.legint(tgt, m=1)
  212. res = leg.legint(pol, m=j)
  213. assert_almost_equal(trim(res), trim(tgt))
  214. # check multiple integrations with defined k
  215. for i in range(5):
  216. for j in range(2, 5):
  217. pol = [0]*i + [1]
  218. tgt = pol[:]
  219. for k in range(j):
  220. tgt = leg.legint(tgt, m=1, k=[k])
  221. res = leg.legint(pol, m=j, k=list(range(j)))
  222. assert_almost_equal(trim(res), trim(tgt))
  223. # check multiple integrations with lbnd
  224. for i in range(5):
  225. for j in range(2, 5):
  226. pol = [0]*i + [1]
  227. tgt = pol[:]
  228. for k in range(j):
  229. tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1)
  230. res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1)
  231. assert_almost_equal(trim(res), trim(tgt))
  232. # check multiple integrations with scaling
  233. for i in range(5):
  234. for j in range(2, 5):
  235. pol = [0]*i + [1]
  236. tgt = pol[:]
  237. for k in range(j):
  238. tgt = leg.legint(tgt, m=1, k=[k], scl=2)
  239. res = leg.legint(pol, m=j, k=list(range(j)), scl=2)
  240. assert_almost_equal(trim(res), trim(tgt))
  241. def test_legint_axis(self):
  242. # check that axis keyword works
  243. c2d = np.random.random((3, 4))
  244. tgt = np.vstack([leg.legint(c) for c in c2d.T]).T
  245. res = leg.legint(c2d, axis=0)
  246. assert_almost_equal(res, tgt)
  247. tgt = np.vstack([leg.legint(c) for c in c2d])
  248. res = leg.legint(c2d, axis=1)
  249. assert_almost_equal(res, tgt)
  250. tgt = np.vstack([leg.legint(c, k=3) for c in c2d])
  251. res = leg.legint(c2d, k=3, axis=1)
  252. assert_almost_equal(res, tgt)
  253. class TestDerivative(object):
  254. def test_legder(self):
  255. # check exceptions
  256. assert_raises(TypeError, leg.legder, [0], .5)
  257. assert_raises(ValueError, leg.legder, [0], -1)
  258. # check that zeroth derivative does nothing
  259. for i in range(5):
  260. tgt = [0]*i + [1]
  261. res = leg.legder(tgt, m=0)
  262. assert_equal(trim(res), trim(tgt))
  263. # check that derivation is the inverse of integration
  264. for i in range(5):
  265. for j in range(2, 5):
  266. tgt = [0]*i + [1]
  267. res = leg.legder(leg.legint(tgt, m=j), m=j)
  268. assert_almost_equal(trim(res), trim(tgt))
  269. # check derivation with scaling
  270. for i in range(5):
  271. for j in range(2, 5):
  272. tgt = [0]*i + [1]
  273. res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5)
  274. assert_almost_equal(trim(res), trim(tgt))
  275. def test_legder_axis(self):
  276. # check that axis keyword works
  277. c2d = np.random.random((3, 4))
  278. tgt = np.vstack([leg.legder(c) for c in c2d.T]).T
  279. res = leg.legder(c2d, axis=0)
  280. assert_almost_equal(res, tgt)
  281. tgt = np.vstack([leg.legder(c) for c in c2d])
  282. res = leg.legder(c2d, axis=1)
  283. assert_almost_equal(res, tgt)
  284. class TestVander(object):
  285. # some random values in [-1, 1)
  286. x = np.random.random((3, 5))*2 - 1
  287. def test_legvander(self):
  288. # check for 1d x
  289. x = np.arange(3)
  290. v = leg.legvander(x, 3)
  291. assert_(v.shape == (3, 4))
  292. for i in range(4):
  293. coef = [0]*i + [1]
  294. assert_almost_equal(v[..., i], leg.legval(x, coef))
  295. # check for 2d x
  296. x = np.array([[1, 2], [3, 4], [5, 6]])
  297. v = leg.legvander(x, 3)
  298. assert_(v.shape == (3, 2, 4))
  299. for i in range(4):
  300. coef = [0]*i + [1]
  301. assert_almost_equal(v[..., i], leg.legval(x, coef))
  302. def test_legvander2d(self):
  303. # also tests polyval2d for non-square coefficient array
  304. x1, x2, x3 = self.x
  305. c = np.random.random((2, 3))
  306. van = leg.legvander2d(x1, x2, [1, 2])
  307. tgt = leg.legval2d(x1, x2, c)
  308. res = np.dot(van, c.flat)
  309. assert_almost_equal(res, tgt)
  310. # check shape
  311. van = leg.legvander2d([x1], [x2], [1, 2])
  312. assert_(van.shape == (1, 5, 6))
  313. def test_legvander3d(self):
  314. # also tests polyval3d for non-square coefficient array
  315. x1, x2, x3 = self.x
  316. c = np.random.random((2, 3, 4))
  317. van = leg.legvander3d(x1, x2, x3, [1, 2, 3])
  318. tgt = leg.legval3d(x1, x2, x3, c)
  319. res = np.dot(van, c.flat)
  320. assert_almost_equal(res, tgt)
  321. # check shape
  322. van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3])
  323. assert_(van.shape == (1, 5, 24))
  324. class TestFitting(object):
  325. def test_legfit(self):
  326. def f(x):
  327. return x*(x - 1)*(x - 2)
  328. def f2(x):
  329. return x**4 + x**2 + 1
  330. # Test exceptions
  331. assert_raises(ValueError, leg.legfit, [1], [1], -1)
  332. assert_raises(TypeError, leg.legfit, [[1]], [1], 0)
  333. assert_raises(TypeError, leg.legfit, [], [1], 0)
  334. assert_raises(TypeError, leg.legfit, [1], [[[1]]], 0)
  335. assert_raises(TypeError, leg.legfit, [1, 2], [1], 0)
  336. assert_raises(TypeError, leg.legfit, [1], [1, 2], 0)
  337. assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[[1]])
  338. assert_raises(TypeError, leg.legfit, [1], [1], 0, w=[1, 1])
  339. assert_raises(ValueError, leg.legfit, [1], [1], [-1,])
  340. assert_raises(ValueError, leg.legfit, [1], [1], [2, -1, 6])
  341. assert_raises(TypeError, leg.legfit, [1], [1], [])
  342. # Test fit
  343. x = np.linspace(0, 2)
  344. y = f(x)
  345. #
  346. coef3 = leg.legfit(x, y, 3)
  347. assert_equal(len(coef3), 4)
  348. assert_almost_equal(leg.legval(x, coef3), y)
  349. coef3 = leg.legfit(x, y, [0, 1, 2, 3])
  350. assert_equal(len(coef3), 4)
  351. assert_almost_equal(leg.legval(x, coef3), y)
  352. #
  353. coef4 = leg.legfit(x, y, 4)
  354. assert_equal(len(coef4), 5)
  355. assert_almost_equal(leg.legval(x, coef4), y)
  356. coef4 = leg.legfit(x, y, [0, 1, 2, 3, 4])
  357. assert_equal(len(coef4), 5)
  358. assert_almost_equal(leg.legval(x, coef4), y)
  359. # check things still work if deg is not in strict increasing
  360. coef4 = leg.legfit(x, y, [2, 3, 4, 1, 0])
  361. assert_equal(len(coef4), 5)
  362. assert_almost_equal(leg.legval(x, coef4), y)
  363. #
  364. coef2d = leg.legfit(x, np.array([y, y]).T, 3)
  365. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  366. coef2d = leg.legfit(x, np.array([y, y]).T, [0, 1, 2, 3])
  367. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  368. # test weighting
  369. w = np.zeros_like(x)
  370. yw = y.copy()
  371. w[1::2] = 1
  372. y[0::2] = 0
  373. wcoef3 = leg.legfit(x, yw, 3, w=w)
  374. assert_almost_equal(wcoef3, coef3)
  375. wcoef3 = leg.legfit(x, yw, [0, 1, 2, 3], w=w)
  376. assert_almost_equal(wcoef3, coef3)
  377. #
  378. wcoef2d = leg.legfit(x, np.array([yw, yw]).T, 3, w=w)
  379. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  380. wcoef2d = leg.legfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
  381. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  382. # test scaling with complex values x points whose square
  383. # is zero when summed.
  384. x = [1, 1j, -1, -1j]
  385. assert_almost_equal(leg.legfit(x, x, 1), [0, 1])
  386. assert_almost_equal(leg.legfit(x, x, [0, 1]), [0, 1])
  387. # test fitting only even Legendre polynomials
  388. x = np.linspace(-1, 1)
  389. y = f2(x)
  390. coef1 = leg.legfit(x, y, 4)
  391. assert_almost_equal(leg.legval(x, coef1), y)
  392. coef2 = leg.legfit(x, y, [0, 2, 4])
  393. assert_almost_equal(leg.legval(x, coef2), y)
  394. assert_almost_equal(coef1, coef2)
  395. class TestCompanion(object):
  396. def test_raises(self):
  397. assert_raises(ValueError, leg.legcompanion, [])
  398. assert_raises(ValueError, leg.legcompanion, [1])
  399. def test_dimensions(self):
  400. for i in range(1, 5):
  401. coef = [0]*i + [1]
  402. assert_(leg.legcompanion(coef).shape == (i, i))
  403. def test_linear_root(self):
  404. assert_(leg.legcompanion([1, 2])[0, 0] == -.5)
  405. class TestGauss(object):
  406. def test_100(self):
  407. x, w = leg.leggauss(100)
  408. # test orthogonality. Note that the results need to be normalized,
  409. # otherwise the huge values that can arise from fast growing
  410. # functions like Laguerre can be very confusing.
  411. v = leg.legvander(x, 99)
  412. vv = np.dot(v.T * w, v)
  413. vd = 1/np.sqrt(vv.diagonal())
  414. vv = vd[:, None] * vv * vd
  415. assert_almost_equal(vv, np.eye(100))
  416. # check that the integral of 1 is correct
  417. tgt = 2.0
  418. assert_almost_equal(w.sum(), tgt)
  419. class TestMisc(object):
  420. def test_legfromroots(self):
  421. res = leg.legfromroots([])
  422. assert_almost_equal(trim(res), [1])
  423. for i in range(1, 5):
  424. roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
  425. pol = leg.legfromroots(roots)
  426. res = leg.legval(roots, pol)
  427. tgt = 0
  428. assert_(len(pol) == i + 1)
  429. assert_almost_equal(leg.leg2poly(pol)[-1], 1)
  430. assert_almost_equal(res, tgt)
  431. def test_legroots(self):
  432. assert_almost_equal(leg.legroots([1]), [])
  433. assert_almost_equal(leg.legroots([1, 2]), [-.5])
  434. for i in range(2, 5):
  435. tgt = np.linspace(-1, 1, i)
  436. res = leg.legroots(leg.legfromroots(tgt))
  437. assert_almost_equal(trim(res), trim(tgt))
  438. def test_legtrim(self):
  439. coef = [2, -1, 1, 0]
  440. # Test exceptions
  441. assert_raises(ValueError, leg.legtrim, coef, -1)
  442. # Test results
  443. assert_equal(leg.legtrim(coef), coef[:-1])
  444. assert_equal(leg.legtrim(coef, 1), coef[:-3])
  445. assert_equal(leg.legtrim(coef, 2), [0])
  446. def test_legline(self):
  447. assert_equal(leg.legline(3, 4), [3, 4])
  448. def test_leg2poly(self):
  449. for i in range(10):
  450. assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i])
  451. def test_poly2leg(self):
  452. for i in range(10):
  453. assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1])
  454. def test_weight(self):
  455. x = np.linspace(-1, 1, 11)
  456. tgt = 1.
  457. res = leg.legweight(x)
  458. assert_almost_equal(res, tgt)