test_polynomial.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. """Tests for polynomial 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.polynomial as poly
  7. from numpy.testing import (
  8. assert_almost_equal, assert_raises, assert_equal, assert_,
  9. assert_warns, assert_array_equal)
  10. def trim(x):
  11. return poly.polytrim(x, tol=1e-6)
  12. T0 = [1]
  13. T1 = [0, 1]
  14. T2 = [-1, 0, 2]
  15. T3 = [0, -3, 0, 4]
  16. T4 = [1, 0, -8, 0, 8]
  17. T5 = [0, 5, 0, -20, 0, 16]
  18. T6 = [-1, 0, 18, 0, -48, 0, 32]
  19. T7 = [0, -7, 0, 56, 0, -112, 0, 64]
  20. T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128]
  21. T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256]
  22. Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
  23. class TestConstants(object):
  24. def test_polydomain(self):
  25. assert_equal(poly.polydomain, [-1, 1])
  26. def test_polyzero(self):
  27. assert_equal(poly.polyzero, [0])
  28. def test_polyone(self):
  29. assert_equal(poly.polyone, [1])
  30. def test_polyx(self):
  31. assert_equal(poly.polyx, [0, 1])
  32. class TestArithmetic(object):
  33. def test_polyadd(self):
  34. for i in range(5):
  35. for j in range(5):
  36. msg = "At i=%d, j=%d" % (i, j)
  37. tgt = np.zeros(max(i, j) + 1)
  38. tgt[i] += 1
  39. tgt[j] += 1
  40. res = poly.polyadd([0]*i + [1], [0]*j + [1])
  41. assert_equal(trim(res), trim(tgt), err_msg=msg)
  42. def test_polysub(self):
  43. for i in range(5):
  44. for j in range(5):
  45. msg = "At i=%d, j=%d" % (i, j)
  46. tgt = np.zeros(max(i, j) + 1)
  47. tgt[i] += 1
  48. tgt[j] -= 1
  49. res = poly.polysub([0]*i + [1], [0]*j + [1])
  50. assert_equal(trim(res), trim(tgt), err_msg=msg)
  51. def test_polymulx(self):
  52. assert_equal(poly.polymulx([0]), [0])
  53. assert_equal(poly.polymulx([1]), [0, 1])
  54. for i in range(1, 5):
  55. ser = [0]*i + [1]
  56. tgt = [0]*(i + 1) + [1]
  57. assert_equal(poly.polymulx(ser), tgt)
  58. def test_polymul(self):
  59. for i in range(5):
  60. for j in range(5):
  61. msg = "At i=%d, j=%d" % (i, j)
  62. tgt = np.zeros(i + j + 1)
  63. tgt[i + j] += 1
  64. res = poly.polymul([0]*i + [1], [0]*j + [1])
  65. assert_equal(trim(res), trim(tgt), err_msg=msg)
  66. def test_polydiv(self):
  67. # check zero division
  68. assert_raises(ZeroDivisionError, poly.polydiv, [1], [0])
  69. # check scalar division
  70. quo, rem = poly.polydiv([2], [2])
  71. assert_equal((quo, rem), (1, 0))
  72. quo, rem = poly.polydiv([2, 2], [2])
  73. assert_equal((quo, rem), ((1, 1), 0))
  74. # check rest.
  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, 2]
  79. cj = [0]*j + [1, 2]
  80. tgt = poly.polyadd(ci, cj)
  81. quo, rem = poly.polydiv(tgt, ci)
  82. res = poly.polyadd(poly.polymul(quo, ci), rem)
  83. assert_equal(res, tgt, err_msg=msg)
  84. def test_polypow(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(poly.polymul, [c]*j, np.array([1]))
  90. res = poly.polypow(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([1., 2., 3.])
  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 = poly.polyval(x, [1., 2., 3.])
  100. def test_polyval(self):
  101. #check empty input
  102. assert_equal(poly.polyval([], [1]).size, 0)
  103. #check normal input)
  104. x = np.linspace(-1, 1)
  105. y = [x**i for i in range(5)]
  106. for i in range(5):
  107. tgt = y[i]
  108. res = poly.polyval(x, [0]*i + [1])
  109. assert_almost_equal(res, tgt)
  110. tgt = x*(x**2 - 1)
  111. res = poly.polyval(x, [0, -1, 0, 1])
  112. assert_almost_equal(res, tgt)
  113. #check that shape is preserved
  114. for i in range(3):
  115. dims = [2]*i
  116. x = np.zeros(dims)
  117. assert_equal(poly.polyval(x, [1]).shape, dims)
  118. assert_equal(poly.polyval(x, [1, 0]).shape, dims)
  119. assert_equal(poly.polyval(x, [1, 0, 0]).shape, dims)
  120. #check masked arrays are processed correctly
  121. mask = [False, True, False]
  122. mx = np.ma.array([1, 2, 3], mask=mask)
  123. res = np.polyval([7, 5, 3], mx)
  124. assert_array_equal(res.mask, mask)
  125. #check subtypes of ndarray are preserved
  126. class C(np.ndarray):
  127. pass
  128. cx = np.array([1, 2, 3]).view(C)
  129. assert_equal(type(np.polyval([2, 3, 4], cx)), C)
  130. def test_polyvalfromroots(self):
  131. # check exception for broadcasting x values over root array with
  132. # too few dimensions
  133. assert_raises(ValueError, poly.polyvalfromroots,
  134. [1], [1], tensor=False)
  135. # check empty input
  136. assert_equal(poly.polyvalfromroots([], [1]).size, 0)
  137. assert_(poly.polyvalfromroots([], [1]).shape == (0,))
  138. # check empty input + multidimensional roots
  139. assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0)
  140. assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0))
  141. # check scalar input
  142. assert_equal(poly.polyvalfromroots(1, 1), 0)
  143. assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3,))
  144. # check normal input)
  145. x = np.linspace(-1, 1)
  146. y = [x**i for i in range(5)]
  147. for i in range(1, 5):
  148. tgt = y[i]
  149. res = poly.polyvalfromroots(x, [0]*i)
  150. assert_almost_equal(res, tgt)
  151. tgt = x*(x - 1)*(x + 1)
  152. res = poly.polyvalfromroots(x, [-1, 0, 1])
  153. assert_almost_equal(res, tgt)
  154. # check that shape is preserved
  155. for i in range(3):
  156. dims = [2]*i
  157. x = np.zeros(dims)
  158. assert_equal(poly.polyvalfromroots(x, [1]).shape, dims)
  159. assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims)
  160. assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims)
  161. # check compatibility with factorization
  162. ptest = [15, 2, -16, -2, 1]
  163. r = poly.polyroots(ptest)
  164. x = np.linspace(-1, 1)
  165. assert_almost_equal(poly.polyval(x, ptest),
  166. poly.polyvalfromroots(x, r))
  167. # check multidimensional arrays of roots and values
  168. # check tensor=False
  169. rshape = (3, 5)
  170. x = np.arange(-3, 2)
  171. r = np.random.randint(-5, 5, size=rshape)
  172. res = poly.polyvalfromroots(x, r, tensor=False)
  173. tgt = np.empty(r.shape[1:])
  174. for ii in range(tgt.size):
  175. tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii])
  176. assert_equal(res, tgt)
  177. # check tensor=True
  178. x = np.vstack([x, 2*x])
  179. res = poly.polyvalfromroots(x, r, tensor=True)
  180. tgt = np.empty(r.shape[1:] + x.shape)
  181. for ii in range(r.shape[1]):
  182. for jj in range(x.shape[0]):
  183. tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii])
  184. assert_equal(res, tgt)
  185. def test_polyval2d(self):
  186. x1, x2, x3 = self.x
  187. y1, y2, y3 = self.y
  188. #test exceptions
  189. assert_raises(ValueError, poly.polyval2d, x1, x2[:2], self.c2d)
  190. #test values
  191. tgt = y1*y2
  192. res = poly.polyval2d(x1, x2, self.c2d)
  193. assert_almost_equal(res, tgt)
  194. #test shape
  195. z = np.ones((2, 3))
  196. res = poly.polyval2d(z, z, self.c2d)
  197. assert_(res.shape == (2, 3))
  198. def test_polyval3d(self):
  199. x1, x2, x3 = self.x
  200. y1, y2, y3 = self.y
  201. #test exceptions
  202. assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d)
  203. #test values
  204. tgt = y1*y2*y3
  205. res = poly.polyval3d(x1, x2, x3, self.c3d)
  206. assert_almost_equal(res, tgt)
  207. #test shape
  208. z = np.ones((2, 3))
  209. res = poly.polyval3d(z, z, z, self.c3d)
  210. assert_(res.shape == (2, 3))
  211. def test_polygrid2d(self):
  212. x1, x2, x3 = self.x
  213. y1, y2, y3 = self.y
  214. #test values
  215. tgt = np.einsum('i,j->ij', y1, y2)
  216. res = poly.polygrid2d(x1, x2, self.c2d)
  217. assert_almost_equal(res, tgt)
  218. #test shape
  219. z = np.ones((2, 3))
  220. res = poly.polygrid2d(z, z, self.c2d)
  221. assert_(res.shape == (2, 3)*2)
  222. def test_polygrid3d(self):
  223. x1, x2, x3 = self.x
  224. y1, y2, y3 = self.y
  225. #test values
  226. tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
  227. res = poly.polygrid3d(x1, x2, x3, self.c3d)
  228. assert_almost_equal(res, tgt)
  229. #test shape
  230. z = np.ones((2, 3))
  231. res = poly.polygrid3d(z, z, z, self.c3d)
  232. assert_(res.shape == (2, 3)*3)
  233. class TestIntegral(object):
  234. def test_polyint(self):
  235. # check exceptions
  236. assert_raises(TypeError, poly.polyint, [0], .5)
  237. assert_raises(ValueError, poly.polyint, [0], -1)
  238. assert_raises(ValueError, poly.polyint, [0], 1, [0, 0])
  239. assert_raises(ValueError, poly.polyint, [0], lbnd=[0])
  240. assert_raises(ValueError, poly.polyint, [0], scl=[0])
  241. assert_raises(TypeError, poly.polyint, [0], axis=.5)
  242. with assert_warns(DeprecationWarning):
  243. poly.polyint([1, 1], 1.)
  244. # test integration of zero polynomial
  245. for i in range(2, 5):
  246. k = [0]*(i - 2) + [1]
  247. res = poly.polyint([0], m=i, k=k)
  248. assert_almost_equal(res, [0, 1])
  249. # check single integration with integration constant
  250. for i in range(5):
  251. scl = i + 1
  252. pol = [0]*i + [1]
  253. tgt = [i] + [0]*i + [1/scl]
  254. res = poly.polyint(pol, m=1, k=[i])
  255. assert_almost_equal(trim(res), trim(tgt))
  256. # check single integration with integration constant and lbnd
  257. for i in range(5):
  258. scl = i + 1
  259. pol = [0]*i + [1]
  260. res = poly.polyint(pol, m=1, k=[i], lbnd=-1)
  261. assert_almost_equal(poly.polyval(-1, res), i)
  262. # check single integration with integration constant and scaling
  263. for i in range(5):
  264. scl = i + 1
  265. pol = [0]*i + [1]
  266. tgt = [i] + [0]*i + [2/scl]
  267. res = poly.polyint(pol, m=1, k=[i], scl=2)
  268. assert_almost_equal(trim(res), trim(tgt))
  269. # check multiple integrations with default k
  270. for i in range(5):
  271. for j in range(2, 5):
  272. pol = [0]*i + [1]
  273. tgt = pol[:]
  274. for k in range(j):
  275. tgt = poly.polyint(tgt, m=1)
  276. res = poly.polyint(pol, m=j)
  277. assert_almost_equal(trim(res), trim(tgt))
  278. # check multiple integrations with defined k
  279. for i in range(5):
  280. for j in range(2, 5):
  281. pol = [0]*i + [1]
  282. tgt = pol[:]
  283. for k in range(j):
  284. tgt = poly.polyint(tgt, m=1, k=[k])
  285. res = poly.polyint(pol, m=j, k=list(range(j)))
  286. assert_almost_equal(trim(res), trim(tgt))
  287. # check multiple integrations with lbnd
  288. for i in range(5):
  289. for j in range(2, 5):
  290. pol = [0]*i + [1]
  291. tgt = pol[:]
  292. for k in range(j):
  293. tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1)
  294. res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1)
  295. assert_almost_equal(trim(res), trim(tgt))
  296. # check multiple integrations with scaling
  297. for i in range(5):
  298. for j in range(2, 5):
  299. pol = [0]*i + [1]
  300. tgt = pol[:]
  301. for k in range(j):
  302. tgt = poly.polyint(tgt, m=1, k=[k], scl=2)
  303. res = poly.polyint(pol, m=j, k=list(range(j)), scl=2)
  304. assert_almost_equal(trim(res), trim(tgt))
  305. def test_polyint_axis(self):
  306. # check that axis keyword works
  307. c2d = np.random.random((3, 4))
  308. tgt = np.vstack([poly.polyint(c) for c in c2d.T]).T
  309. res = poly.polyint(c2d, axis=0)
  310. assert_almost_equal(res, tgt)
  311. tgt = np.vstack([poly.polyint(c) for c in c2d])
  312. res = poly.polyint(c2d, axis=1)
  313. assert_almost_equal(res, tgt)
  314. tgt = np.vstack([poly.polyint(c, k=3) for c in c2d])
  315. res = poly.polyint(c2d, k=3, axis=1)
  316. assert_almost_equal(res, tgt)
  317. class TestDerivative(object):
  318. def test_polyder(self):
  319. # check exceptions
  320. assert_raises(TypeError, poly.polyder, [0], .5)
  321. assert_raises(ValueError, poly.polyder, [0], -1)
  322. # check that zeroth derivative does nothing
  323. for i in range(5):
  324. tgt = [0]*i + [1]
  325. res = poly.polyder(tgt, m=0)
  326. assert_equal(trim(res), trim(tgt))
  327. # check that derivation is the inverse of integration
  328. for i in range(5):
  329. for j in range(2, 5):
  330. tgt = [0]*i + [1]
  331. res = poly.polyder(poly.polyint(tgt, m=j), m=j)
  332. assert_almost_equal(trim(res), trim(tgt))
  333. # check derivation with scaling
  334. for i in range(5):
  335. for j in range(2, 5):
  336. tgt = [0]*i + [1]
  337. res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5)
  338. assert_almost_equal(trim(res), trim(tgt))
  339. def test_polyder_axis(self):
  340. # check that axis keyword works
  341. c2d = np.random.random((3, 4))
  342. tgt = np.vstack([poly.polyder(c) for c in c2d.T]).T
  343. res = poly.polyder(c2d, axis=0)
  344. assert_almost_equal(res, tgt)
  345. tgt = np.vstack([poly.polyder(c) for c in c2d])
  346. res = poly.polyder(c2d, axis=1)
  347. assert_almost_equal(res, tgt)
  348. class TestVander(object):
  349. # some random values in [-1, 1)
  350. x = np.random.random((3, 5))*2 - 1
  351. def test_polyvander(self):
  352. # check for 1d x
  353. x = np.arange(3)
  354. v = poly.polyvander(x, 3)
  355. assert_(v.shape == (3, 4))
  356. for i in range(4):
  357. coef = [0]*i + [1]
  358. assert_almost_equal(v[..., i], poly.polyval(x, coef))
  359. # check for 2d x
  360. x = np.array([[1, 2], [3, 4], [5, 6]])
  361. v = poly.polyvander(x, 3)
  362. assert_(v.shape == (3, 2, 4))
  363. for i in range(4):
  364. coef = [0]*i + [1]
  365. assert_almost_equal(v[..., i], poly.polyval(x, coef))
  366. def test_polyvander2d(self):
  367. # also tests polyval2d for non-square coefficient array
  368. x1, x2, x3 = self.x
  369. c = np.random.random((2, 3))
  370. van = poly.polyvander2d(x1, x2, [1, 2])
  371. tgt = poly.polyval2d(x1, x2, c)
  372. res = np.dot(van, c.flat)
  373. assert_almost_equal(res, tgt)
  374. # check shape
  375. van = poly.polyvander2d([x1], [x2], [1, 2])
  376. assert_(van.shape == (1, 5, 6))
  377. def test_polyvander3d(self):
  378. # also tests polyval3d for non-square coefficient array
  379. x1, x2, x3 = self.x
  380. c = np.random.random((2, 3, 4))
  381. van = poly.polyvander3d(x1, x2, x3, [1, 2, 3])
  382. tgt = poly.polyval3d(x1, x2, x3, c)
  383. res = np.dot(van, c.flat)
  384. assert_almost_equal(res, tgt)
  385. # check shape
  386. van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3])
  387. assert_(van.shape == (1, 5, 24))
  388. class TestCompanion(object):
  389. def test_raises(self):
  390. assert_raises(ValueError, poly.polycompanion, [])
  391. assert_raises(ValueError, poly.polycompanion, [1])
  392. def test_dimensions(self):
  393. for i in range(1, 5):
  394. coef = [0]*i + [1]
  395. assert_(poly.polycompanion(coef).shape == (i, i))
  396. def test_linear_root(self):
  397. assert_(poly.polycompanion([1, 2])[0, 0] == -.5)
  398. class TestMisc(object):
  399. def test_polyfromroots(self):
  400. res = poly.polyfromroots([])
  401. assert_almost_equal(trim(res), [1])
  402. for i in range(1, 5):
  403. roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
  404. tgt = Tlist[i]
  405. res = poly.polyfromroots(roots)*2**(i-1)
  406. assert_almost_equal(trim(res), trim(tgt))
  407. def test_polyroots(self):
  408. assert_almost_equal(poly.polyroots([1]), [])
  409. assert_almost_equal(poly.polyroots([1, 2]), [-.5])
  410. for i in range(2, 5):
  411. tgt = np.linspace(-1, 1, i)
  412. res = poly.polyroots(poly.polyfromroots(tgt))
  413. assert_almost_equal(trim(res), trim(tgt))
  414. def test_polyfit(self):
  415. def f(x):
  416. return x*(x - 1)*(x - 2)
  417. def f2(x):
  418. return x**4 + x**2 + 1
  419. # Test exceptions
  420. assert_raises(ValueError, poly.polyfit, [1], [1], -1)
  421. assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
  422. assert_raises(TypeError, poly.polyfit, [], [1], 0)
  423. assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
  424. assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
  425. assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
  426. assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
  427. assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
  428. assert_raises(ValueError, poly.polyfit, [1], [1], [-1,])
  429. assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
  430. assert_raises(TypeError, poly.polyfit, [1], [1], [])
  431. # Test fit
  432. x = np.linspace(0, 2)
  433. y = f(x)
  434. #
  435. coef3 = poly.polyfit(x, y, 3)
  436. assert_equal(len(coef3), 4)
  437. assert_almost_equal(poly.polyval(x, coef3), y)
  438. coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
  439. assert_equal(len(coef3), 4)
  440. assert_almost_equal(poly.polyval(x, coef3), y)
  441. #
  442. coef4 = poly.polyfit(x, y, 4)
  443. assert_equal(len(coef4), 5)
  444. assert_almost_equal(poly.polyval(x, coef4), y)
  445. coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
  446. assert_equal(len(coef4), 5)
  447. assert_almost_equal(poly.polyval(x, coef4), y)
  448. #
  449. coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
  450. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  451. coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
  452. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  453. # test weighting
  454. w = np.zeros_like(x)
  455. yw = y.copy()
  456. w[1::2] = 1
  457. yw[0::2] = 0
  458. wcoef3 = poly.polyfit(x, yw, 3, w=w)
  459. assert_almost_equal(wcoef3, coef3)
  460. wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
  461. assert_almost_equal(wcoef3, coef3)
  462. #
  463. wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
  464. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  465. wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
  466. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  467. # test scaling with complex values x points whose square
  468. # is zero when summed.
  469. x = [1, 1j, -1, -1j]
  470. assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
  471. assert_almost_equal(poly.polyfit(x, x, [0, 1]), [0, 1])
  472. # test fitting only even Polyendre polynomials
  473. x = np.linspace(-1, 1)
  474. y = f2(x)
  475. coef1 = poly.polyfit(x, y, 4)
  476. assert_almost_equal(poly.polyval(x, coef1), y)
  477. coef2 = poly.polyfit(x, y, [0, 2, 4])
  478. assert_almost_equal(poly.polyval(x, coef2), y)
  479. assert_almost_equal(coef1, coef2)
  480. def test_polytrim(self):
  481. coef = [2, -1, 1, 0]
  482. # Test exceptions
  483. assert_raises(ValueError, poly.polytrim, coef, -1)
  484. # Test results
  485. assert_equal(poly.polytrim(coef), coef[:-1])
  486. assert_equal(poly.polytrim(coef, 1), coef[:-3])
  487. assert_equal(poly.polytrim(coef, 2), [0])
  488. def test_polyline(self):
  489. assert_equal(poly.polyline(3, 4), [3, 4])