test_hermite_e.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. """Tests for hermite_e module.
  2. """
  3. from functools import reduce
  4. import numpy as np
  5. import numpy.polynomial.hermite_e as herme
  6. from numpy.polynomial.polynomial import polyval
  7. from numpy.testing import (
  8. assert_almost_equal, assert_raises, assert_equal, assert_,
  9. )
  10. He0 = np.array([1])
  11. He1 = np.array([0, 1])
  12. He2 = np.array([-1, 0, 1])
  13. He3 = np.array([0, -3, 0, 1])
  14. He4 = np.array([3, 0, -6, 0, 1])
  15. He5 = np.array([0, 15, 0, -10, 0, 1])
  16. He6 = np.array([-15, 0, 45, 0, -15, 0, 1])
  17. He7 = np.array([0, -105, 0, 105, 0, -21, 0, 1])
  18. He8 = np.array([105, 0, -420, 0, 210, 0, -28, 0, 1])
  19. He9 = np.array([0, 945, 0, -1260, 0, 378, 0, -36, 0, 1])
  20. Helist = [He0, He1, He2, He3, He4, He5, He6, He7, He8, He9]
  21. def trim(x):
  22. return herme.hermetrim(x, tol=1e-6)
  23. class TestConstants:
  24. def test_hermedomain(self):
  25. assert_equal(herme.hermedomain, [-1, 1])
  26. def test_hermezero(self):
  27. assert_equal(herme.hermezero, [0])
  28. def test_hermeone(self):
  29. assert_equal(herme.hermeone, [1])
  30. def test_hermex(self):
  31. assert_equal(herme.hermex, [0, 1])
  32. class TestArithmetic:
  33. x = np.linspace(-3, 3, 100)
  34. def test_hermeadd(self):
  35. for i in range(5):
  36. for j in range(5):
  37. msg = f"At i={i}, j={j}"
  38. tgt = np.zeros(max(i, j) + 1)
  39. tgt[i] += 1
  40. tgt[j] += 1
  41. res = herme.hermeadd([0]*i + [1], [0]*j + [1])
  42. assert_equal(trim(res), trim(tgt), err_msg=msg)
  43. def test_hermesub(self):
  44. for i in range(5):
  45. for j in range(5):
  46. msg = f"At i={i}, j={j}"
  47. tgt = np.zeros(max(i, j) + 1)
  48. tgt[i] += 1
  49. tgt[j] -= 1
  50. res = herme.hermesub([0]*i + [1], [0]*j + [1])
  51. assert_equal(trim(res), trim(tgt), err_msg=msg)
  52. def test_hermemulx(self):
  53. assert_equal(herme.hermemulx([0]), [0])
  54. assert_equal(herme.hermemulx([1]), [0, 1])
  55. for i in range(1, 5):
  56. ser = [0]*i + [1]
  57. tgt = [0]*(i - 1) + [i, 0, 1]
  58. assert_equal(herme.hermemulx(ser), tgt)
  59. def test_hermemul(self):
  60. # check values of result
  61. for i in range(5):
  62. pol1 = [0]*i + [1]
  63. val1 = herme.hermeval(self.x, pol1)
  64. for j in range(5):
  65. msg = f"At i={i}, j={j}"
  66. pol2 = [0]*j + [1]
  67. val2 = herme.hermeval(self.x, pol2)
  68. pol3 = herme.hermemul(pol1, pol2)
  69. val3 = herme.hermeval(self.x, pol3)
  70. assert_(len(pol3) == i + j + 1, msg)
  71. assert_almost_equal(val3, val1*val2, err_msg=msg)
  72. def test_hermediv(self):
  73. for i in range(5):
  74. for j in range(5):
  75. msg = f"At i={i}, j={j}"
  76. ci = [0]*i + [1]
  77. cj = [0]*j + [1]
  78. tgt = herme.hermeadd(ci, cj)
  79. quo, rem = herme.hermediv(tgt, ci)
  80. res = herme.hermeadd(herme.hermemul(quo, ci), rem)
  81. assert_equal(trim(res), trim(tgt), err_msg=msg)
  82. def test_hermepow(self):
  83. for i in range(5):
  84. for j in range(5):
  85. msg = f"At i={i}, j={j}"
  86. c = np.arange(i + 1)
  87. tgt = reduce(herme.hermemul, [c]*j, np.array([1]))
  88. res = herme.hermepow(c, j)
  89. assert_equal(trim(res), trim(tgt), err_msg=msg)
  90. class TestEvaluation:
  91. # coefficients of 1 + 2*x + 3*x**2
  92. c1d = np.array([4., 2., 3.])
  93. c2d = np.einsum('i,j->ij', c1d, c1d)
  94. c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
  95. # some random values in [-1, 1)
  96. x = np.random.random((3, 5))*2 - 1
  97. y = polyval(x, [1., 2., 3.])
  98. def test_hermeval(self):
  99. #check empty input
  100. assert_equal(herme.hermeval([], [1]).size, 0)
  101. #check normal input)
  102. x = np.linspace(-1, 1)
  103. y = [polyval(x, c) for c in Helist]
  104. for i in range(10):
  105. msg = f"At i={i}"
  106. tgt = y[i]
  107. res = herme.hermeval(x, [0]*i + [1])
  108. assert_almost_equal(res, tgt, err_msg=msg)
  109. #check that shape is preserved
  110. for i in range(3):
  111. dims = [2]*i
  112. x = np.zeros(dims)
  113. assert_equal(herme.hermeval(x, [1]).shape, dims)
  114. assert_equal(herme.hermeval(x, [1, 0]).shape, dims)
  115. assert_equal(herme.hermeval(x, [1, 0, 0]).shape, dims)
  116. def test_hermeval2d(self):
  117. x1, x2, x3 = self.x
  118. y1, y2, y3 = self.y
  119. #test exceptions
  120. assert_raises(ValueError, herme.hermeval2d, x1, x2[:2], self.c2d)
  121. #test values
  122. tgt = y1*y2
  123. res = herme.hermeval2d(x1, x2, self.c2d)
  124. assert_almost_equal(res, tgt)
  125. #test shape
  126. z = np.ones((2, 3))
  127. res = herme.hermeval2d(z, z, self.c2d)
  128. assert_(res.shape == (2, 3))
  129. def test_hermeval3d(self):
  130. x1, x2, x3 = self.x
  131. y1, y2, y3 = self.y
  132. #test exceptions
  133. assert_raises(ValueError, herme.hermeval3d, x1, x2, x3[:2], self.c3d)
  134. #test values
  135. tgt = y1*y2*y3
  136. res = herme.hermeval3d(x1, x2, x3, self.c3d)
  137. assert_almost_equal(res, tgt)
  138. #test shape
  139. z = np.ones((2, 3))
  140. res = herme.hermeval3d(z, z, z, self.c3d)
  141. assert_(res.shape == (2, 3))
  142. def test_hermegrid2d(self):
  143. x1, x2, x3 = self.x
  144. y1, y2, y3 = self.y
  145. #test values
  146. tgt = np.einsum('i,j->ij', y1, y2)
  147. res = herme.hermegrid2d(x1, x2, self.c2d)
  148. assert_almost_equal(res, tgt)
  149. #test shape
  150. z = np.ones((2, 3))
  151. res = herme.hermegrid2d(z, z, self.c2d)
  152. assert_(res.shape == (2, 3)*2)
  153. def test_hermegrid3d(self):
  154. x1, x2, x3 = self.x
  155. y1, y2, y3 = self.y
  156. #test values
  157. tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
  158. res = herme.hermegrid3d(x1, x2, x3, self.c3d)
  159. assert_almost_equal(res, tgt)
  160. #test shape
  161. z = np.ones((2, 3))
  162. res = herme.hermegrid3d(z, z, z, self.c3d)
  163. assert_(res.shape == (2, 3)*3)
  164. class TestIntegral:
  165. def test_hermeint(self):
  166. # check exceptions
  167. assert_raises(TypeError, herme.hermeint, [0], .5)
  168. assert_raises(ValueError, herme.hermeint, [0], -1)
  169. assert_raises(ValueError, herme.hermeint, [0], 1, [0, 0])
  170. assert_raises(ValueError, herme.hermeint, [0], lbnd=[0])
  171. assert_raises(ValueError, herme.hermeint, [0], scl=[0])
  172. assert_raises(TypeError, herme.hermeint, [0], axis=.5)
  173. # test integration of zero polynomial
  174. for i in range(2, 5):
  175. k = [0]*(i - 2) + [1]
  176. res = herme.hermeint([0], m=i, k=k)
  177. assert_almost_equal(res, [0, 1])
  178. # check single integration with integration constant
  179. for i in range(5):
  180. scl = i + 1
  181. pol = [0]*i + [1]
  182. tgt = [i] + [0]*i + [1/scl]
  183. hermepol = herme.poly2herme(pol)
  184. hermeint = herme.hermeint(hermepol, m=1, k=[i])
  185. res = herme.herme2poly(hermeint)
  186. assert_almost_equal(trim(res), trim(tgt))
  187. # check single integration with integration constant and lbnd
  188. for i in range(5):
  189. scl = i + 1
  190. pol = [0]*i + [1]
  191. hermepol = herme.poly2herme(pol)
  192. hermeint = herme.hermeint(hermepol, m=1, k=[i], lbnd=-1)
  193. assert_almost_equal(herme.hermeval(-1, hermeint), i)
  194. # check single integration with integration constant and scaling
  195. for i in range(5):
  196. scl = i + 1
  197. pol = [0]*i + [1]
  198. tgt = [i] + [0]*i + [2/scl]
  199. hermepol = herme.poly2herme(pol)
  200. hermeint = herme.hermeint(hermepol, m=1, k=[i], scl=2)
  201. res = herme.herme2poly(hermeint)
  202. assert_almost_equal(trim(res), trim(tgt))
  203. # check multiple integrations with default k
  204. for i in range(5):
  205. for j in range(2, 5):
  206. pol = [0]*i + [1]
  207. tgt = pol[:]
  208. for k in range(j):
  209. tgt = herme.hermeint(tgt, m=1)
  210. res = herme.hermeint(pol, m=j)
  211. assert_almost_equal(trim(res), trim(tgt))
  212. # check multiple integrations with defined k
  213. for i in range(5):
  214. for j in range(2, 5):
  215. pol = [0]*i + [1]
  216. tgt = pol[:]
  217. for k in range(j):
  218. tgt = herme.hermeint(tgt, m=1, k=[k])
  219. res = herme.hermeint(pol, m=j, k=list(range(j)))
  220. assert_almost_equal(trim(res), trim(tgt))
  221. # check multiple integrations with lbnd
  222. for i in range(5):
  223. for j in range(2, 5):
  224. pol = [0]*i + [1]
  225. tgt = pol[:]
  226. for k in range(j):
  227. tgt = herme.hermeint(tgt, m=1, k=[k], lbnd=-1)
  228. res = herme.hermeint(pol, m=j, k=list(range(j)), lbnd=-1)
  229. assert_almost_equal(trim(res), trim(tgt))
  230. # check multiple integrations with scaling
  231. for i in range(5):
  232. for j in range(2, 5):
  233. pol = [0]*i + [1]
  234. tgt = pol[:]
  235. for k in range(j):
  236. tgt = herme.hermeint(tgt, m=1, k=[k], scl=2)
  237. res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2)
  238. assert_almost_equal(trim(res), trim(tgt))
  239. def test_hermeint_axis(self):
  240. # check that axis keyword works
  241. c2d = np.random.random((3, 4))
  242. tgt = np.vstack([herme.hermeint(c) for c in c2d.T]).T
  243. res = herme.hermeint(c2d, axis=0)
  244. assert_almost_equal(res, tgt)
  245. tgt = np.vstack([herme.hermeint(c) for c in c2d])
  246. res = herme.hermeint(c2d, axis=1)
  247. assert_almost_equal(res, tgt)
  248. tgt = np.vstack([herme.hermeint(c, k=3) for c in c2d])
  249. res = herme.hermeint(c2d, k=3, axis=1)
  250. assert_almost_equal(res, tgt)
  251. class TestDerivative:
  252. def test_hermeder(self):
  253. # check exceptions
  254. assert_raises(TypeError, herme.hermeder, [0], .5)
  255. assert_raises(ValueError, herme.hermeder, [0], -1)
  256. # check that zeroth derivative does nothing
  257. for i in range(5):
  258. tgt = [0]*i + [1]
  259. res = herme.hermeder(tgt, m=0)
  260. assert_equal(trim(res), trim(tgt))
  261. # check that derivation is the inverse of integration
  262. for i in range(5):
  263. for j in range(2, 5):
  264. tgt = [0]*i + [1]
  265. res = herme.hermeder(herme.hermeint(tgt, m=j), m=j)
  266. assert_almost_equal(trim(res), trim(tgt))
  267. # check derivation with scaling
  268. for i in range(5):
  269. for j in range(2, 5):
  270. tgt = [0]*i + [1]
  271. res = herme.hermeder(
  272. herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5)
  273. assert_almost_equal(trim(res), trim(tgt))
  274. def test_hermeder_axis(self):
  275. # check that axis keyword works
  276. c2d = np.random.random((3, 4))
  277. tgt = np.vstack([herme.hermeder(c) for c in c2d.T]).T
  278. res = herme.hermeder(c2d, axis=0)
  279. assert_almost_equal(res, tgt)
  280. tgt = np.vstack([herme.hermeder(c) for c in c2d])
  281. res = herme.hermeder(c2d, axis=1)
  282. assert_almost_equal(res, tgt)
  283. class TestVander:
  284. # some random values in [-1, 1)
  285. x = np.random.random((3, 5))*2 - 1
  286. def test_hermevander(self):
  287. # check for 1d x
  288. x = np.arange(3)
  289. v = herme.hermevander(x, 3)
  290. assert_(v.shape == (3, 4))
  291. for i in range(4):
  292. coef = [0]*i + [1]
  293. assert_almost_equal(v[..., i], herme.hermeval(x, coef))
  294. # check for 2d x
  295. x = np.array([[1, 2], [3, 4], [5, 6]])
  296. v = herme.hermevander(x, 3)
  297. assert_(v.shape == (3, 2, 4))
  298. for i in range(4):
  299. coef = [0]*i + [1]
  300. assert_almost_equal(v[..., i], herme.hermeval(x, coef))
  301. def test_hermevander2d(self):
  302. # also tests hermeval2d for non-square coefficient array
  303. x1, x2, x3 = self.x
  304. c = np.random.random((2, 3))
  305. van = herme.hermevander2d(x1, x2, [1, 2])
  306. tgt = herme.hermeval2d(x1, x2, c)
  307. res = np.dot(van, c.flat)
  308. assert_almost_equal(res, tgt)
  309. # check shape
  310. van = herme.hermevander2d([x1], [x2], [1, 2])
  311. assert_(van.shape == (1, 5, 6))
  312. def test_hermevander3d(self):
  313. # also tests hermeval3d for non-square coefficient array
  314. x1, x2, x3 = self.x
  315. c = np.random.random((2, 3, 4))
  316. van = herme.hermevander3d(x1, x2, x3, [1, 2, 3])
  317. tgt = herme.hermeval3d(x1, x2, x3, c)
  318. res = np.dot(van, c.flat)
  319. assert_almost_equal(res, tgt)
  320. # check shape
  321. van = herme.hermevander3d([x1], [x2], [x3], [1, 2, 3])
  322. assert_(van.shape == (1, 5, 24))
  323. class TestFitting:
  324. def test_hermefit(self):
  325. def f(x):
  326. return x*(x - 1)*(x - 2)
  327. def f2(x):
  328. return x**4 + x**2 + 1
  329. # Test exceptions
  330. assert_raises(ValueError, herme.hermefit, [1], [1], -1)
  331. assert_raises(TypeError, herme.hermefit, [[1]], [1], 0)
  332. assert_raises(TypeError, herme.hermefit, [], [1], 0)
  333. assert_raises(TypeError, herme.hermefit, [1], [[[1]]], 0)
  334. assert_raises(TypeError, herme.hermefit, [1, 2], [1], 0)
  335. assert_raises(TypeError, herme.hermefit, [1], [1, 2], 0)
  336. assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[[1]])
  337. assert_raises(TypeError, herme.hermefit, [1], [1], 0, w=[1, 1])
  338. assert_raises(ValueError, herme.hermefit, [1], [1], [-1,])
  339. assert_raises(ValueError, herme.hermefit, [1], [1], [2, -1, 6])
  340. assert_raises(TypeError, herme.hermefit, [1], [1], [])
  341. # Test fit
  342. x = np.linspace(0, 2)
  343. y = f(x)
  344. #
  345. coef3 = herme.hermefit(x, y, 3)
  346. assert_equal(len(coef3), 4)
  347. assert_almost_equal(herme.hermeval(x, coef3), y)
  348. coef3 = herme.hermefit(x, y, [0, 1, 2, 3])
  349. assert_equal(len(coef3), 4)
  350. assert_almost_equal(herme.hermeval(x, coef3), y)
  351. #
  352. coef4 = herme.hermefit(x, y, 4)
  353. assert_equal(len(coef4), 5)
  354. assert_almost_equal(herme.hermeval(x, coef4), y)
  355. coef4 = herme.hermefit(x, y, [0, 1, 2, 3, 4])
  356. assert_equal(len(coef4), 5)
  357. assert_almost_equal(herme.hermeval(x, coef4), y)
  358. # check things still work if deg is not in strict increasing
  359. coef4 = herme.hermefit(x, y, [2, 3, 4, 1, 0])
  360. assert_equal(len(coef4), 5)
  361. assert_almost_equal(herme.hermeval(x, coef4), y)
  362. #
  363. coef2d = herme.hermefit(x, np.array([y, y]).T, 3)
  364. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  365. coef2d = herme.hermefit(x, np.array([y, y]).T, [0, 1, 2, 3])
  366. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  367. # test weighting
  368. w = np.zeros_like(x)
  369. yw = y.copy()
  370. w[1::2] = 1
  371. y[0::2] = 0
  372. wcoef3 = herme.hermefit(x, yw, 3, w=w)
  373. assert_almost_equal(wcoef3, coef3)
  374. wcoef3 = herme.hermefit(x, yw, [0, 1, 2, 3], w=w)
  375. assert_almost_equal(wcoef3, coef3)
  376. #
  377. wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, 3, w=w)
  378. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  379. wcoef2d = herme.hermefit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
  380. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  381. # test scaling with complex values x points whose square
  382. # is zero when summed.
  383. x = [1, 1j, -1, -1j]
  384. assert_almost_equal(herme.hermefit(x, x, 1), [0, 1])
  385. assert_almost_equal(herme.hermefit(x, x, [0, 1]), [0, 1])
  386. # test fitting only even Legendre polynomials
  387. x = np.linspace(-1, 1)
  388. y = f2(x)
  389. coef1 = herme.hermefit(x, y, 4)
  390. assert_almost_equal(herme.hermeval(x, coef1), y)
  391. coef2 = herme.hermefit(x, y, [0, 2, 4])
  392. assert_almost_equal(herme.hermeval(x, coef2), y)
  393. assert_almost_equal(coef1, coef2)
  394. class TestCompanion:
  395. def test_raises(self):
  396. assert_raises(ValueError, herme.hermecompanion, [])
  397. assert_raises(ValueError, herme.hermecompanion, [1])
  398. def test_dimensions(self):
  399. for i in range(1, 5):
  400. coef = [0]*i + [1]
  401. assert_(herme.hermecompanion(coef).shape == (i, i))
  402. def test_linear_root(self):
  403. assert_(herme.hermecompanion([1, 2])[0, 0] == -.5)
  404. class TestGauss:
  405. def test_100(self):
  406. x, w = herme.hermegauss(100)
  407. # test orthogonality. Note that the results need to be normalized,
  408. # otherwise the huge values that can arise from fast growing
  409. # functions like Laguerre can be very confusing.
  410. v = herme.hermevander(x, 99)
  411. vv = np.dot(v.T * w, v)
  412. vd = 1/np.sqrt(vv.diagonal())
  413. vv = vd[:, None] * vv * vd
  414. assert_almost_equal(vv, np.eye(100))
  415. # check that the integral of 1 is correct
  416. tgt = np.sqrt(2*np.pi)
  417. assert_almost_equal(w.sum(), tgt)
  418. class TestMisc:
  419. def test_hermefromroots(self):
  420. res = herme.hermefromroots([])
  421. assert_almost_equal(trim(res), [1])
  422. for i in range(1, 5):
  423. roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
  424. pol = herme.hermefromroots(roots)
  425. res = herme.hermeval(roots, pol)
  426. tgt = 0
  427. assert_(len(pol) == i + 1)
  428. assert_almost_equal(herme.herme2poly(pol)[-1], 1)
  429. assert_almost_equal(res, tgt)
  430. def test_hermeroots(self):
  431. assert_almost_equal(herme.hermeroots([1]), [])
  432. assert_almost_equal(herme.hermeroots([1, 1]), [-1])
  433. for i in range(2, 5):
  434. tgt = np.linspace(-1, 1, i)
  435. res = herme.hermeroots(herme.hermefromroots(tgt))
  436. assert_almost_equal(trim(res), trim(tgt))
  437. def test_hermetrim(self):
  438. coef = [2, -1, 1, 0]
  439. # Test exceptions
  440. assert_raises(ValueError, herme.hermetrim, coef, -1)
  441. # Test results
  442. assert_equal(herme.hermetrim(coef), coef[:-1])
  443. assert_equal(herme.hermetrim(coef, 1), coef[:-3])
  444. assert_equal(herme.hermetrim(coef, 2), [0])
  445. def test_hermeline(self):
  446. assert_equal(herme.hermeline(3, 4), [3, 4])
  447. def test_herme2poly(self):
  448. for i in range(10):
  449. assert_almost_equal(herme.herme2poly([0]*i + [1]), Helist[i])
  450. def test_poly2herme(self):
  451. for i in range(10):
  452. assert_almost_equal(herme.poly2herme(Helist[i]), [0]*i + [1])
  453. def test_weight(self):
  454. x = np.linspace(-5, 5, 11)
  455. tgt = np.exp(-.5*x**2)
  456. res = herme.hermeweight(x)
  457. assert_almost_equal(res, tgt)