test_hermite_e.py 19 KB

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