test_hermite_e.py 19 KB

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