test_polynomial.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. )
  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. def test_polyvalfromroots(self):
  121. # check exception for broadcasting x values over root array with
  122. # too few dimensions
  123. assert_raises(ValueError, poly.polyvalfromroots,
  124. [1], [1], tensor=False)
  125. # check empty input
  126. assert_equal(poly.polyvalfromroots([], [1]).size, 0)
  127. assert_(poly.polyvalfromroots([], [1]).shape == (0,))
  128. # check empty input + multidimensional roots
  129. assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0)
  130. assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0))
  131. # check scalar input
  132. assert_equal(poly.polyvalfromroots(1, 1), 0)
  133. assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3,))
  134. # check normal input)
  135. x = np.linspace(-1, 1)
  136. y = [x**i for i in range(5)]
  137. for i in range(1, 5):
  138. tgt = y[i]
  139. res = poly.polyvalfromroots(x, [0]*i)
  140. assert_almost_equal(res, tgt)
  141. tgt = x*(x - 1)*(x + 1)
  142. res = poly.polyvalfromroots(x, [-1, 0, 1])
  143. assert_almost_equal(res, tgt)
  144. # check that shape is preserved
  145. for i in range(3):
  146. dims = [2]*i
  147. x = np.zeros(dims)
  148. assert_equal(poly.polyvalfromroots(x, [1]).shape, dims)
  149. assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims)
  150. assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims)
  151. # check compatibility with factorization
  152. ptest = [15, 2, -16, -2, 1]
  153. r = poly.polyroots(ptest)
  154. x = np.linspace(-1, 1)
  155. assert_almost_equal(poly.polyval(x, ptest),
  156. poly.polyvalfromroots(x, r))
  157. # check multidimensional arrays of roots and values
  158. # check tensor=False
  159. rshape = (3, 5)
  160. x = np.arange(-3, 2)
  161. r = np.random.randint(-5, 5, size=rshape)
  162. res = poly.polyvalfromroots(x, r, tensor=False)
  163. tgt = np.empty(r.shape[1:])
  164. for ii in range(tgt.size):
  165. tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii])
  166. assert_equal(res, tgt)
  167. # check tensor=True
  168. x = np.vstack([x, 2*x])
  169. res = poly.polyvalfromroots(x, r, tensor=True)
  170. tgt = np.empty(r.shape[1:] + x.shape)
  171. for ii in range(r.shape[1]):
  172. for jj in range(x.shape[0]):
  173. tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii])
  174. assert_equal(res, tgt)
  175. def test_polyval2d(self):
  176. x1, x2, x3 = self.x
  177. y1, y2, y3 = self.y
  178. #test exceptions
  179. assert_raises(ValueError, poly.polyval2d, x1, x2[:2], self.c2d)
  180. #test values
  181. tgt = y1*y2
  182. res = poly.polyval2d(x1, x2, self.c2d)
  183. assert_almost_equal(res, tgt)
  184. #test shape
  185. z = np.ones((2, 3))
  186. res = poly.polyval2d(z, z, self.c2d)
  187. assert_(res.shape == (2, 3))
  188. def test_polyval3d(self):
  189. x1, x2, x3 = self.x
  190. y1, y2, y3 = self.y
  191. #test exceptions
  192. assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d)
  193. #test values
  194. tgt = y1*y2*y3
  195. res = poly.polyval3d(x1, x2, x3, self.c3d)
  196. assert_almost_equal(res, tgt)
  197. #test shape
  198. z = np.ones((2, 3))
  199. res = poly.polyval3d(z, z, z, self.c3d)
  200. assert_(res.shape == (2, 3))
  201. def test_polygrid2d(self):
  202. x1, x2, x3 = self.x
  203. y1, y2, y3 = self.y
  204. #test values
  205. tgt = np.einsum('i,j->ij', y1, y2)
  206. res = poly.polygrid2d(x1, x2, self.c2d)
  207. assert_almost_equal(res, tgt)
  208. #test shape
  209. z = np.ones((2, 3))
  210. res = poly.polygrid2d(z, z, self.c2d)
  211. assert_(res.shape == (2, 3)*2)
  212. def test_polygrid3d(self):
  213. x1, x2, x3 = self.x
  214. y1, y2, y3 = self.y
  215. #test values
  216. tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
  217. res = poly.polygrid3d(x1, x2, x3, self.c3d)
  218. assert_almost_equal(res, tgt)
  219. #test shape
  220. z = np.ones((2, 3))
  221. res = poly.polygrid3d(z, z, z, self.c3d)
  222. assert_(res.shape == (2, 3)*3)
  223. class TestIntegral(object):
  224. def test_polyint(self):
  225. # check exceptions
  226. assert_raises(ValueError, poly.polyint, [0], .5)
  227. assert_raises(ValueError, poly.polyint, [0], -1)
  228. assert_raises(ValueError, poly.polyint, [0], 1, [0, 0])
  229. assert_raises(ValueError, poly.polyint, [0], lbnd=[0])
  230. assert_raises(ValueError, poly.polyint, [0], scl=[0])
  231. assert_raises(ValueError, poly.polyint, [0], axis=.5)
  232. # test integration of zero polynomial
  233. for i in range(2, 5):
  234. k = [0]*(i - 2) + [1]
  235. res = poly.polyint([0], m=i, k=k)
  236. assert_almost_equal(res, [0, 1])
  237. # check single integration with integration constant
  238. for i in range(5):
  239. scl = i + 1
  240. pol = [0]*i + [1]
  241. tgt = [i] + [0]*i + [1/scl]
  242. res = poly.polyint(pol, m=1, k=[i])
  243. assert_almost_equal(trim(res), trim(tgt))
  244. # check single integration with integration constant and lbnd
  245. for i in range(5):
  246. scl = i + 1
  247. pol = [0]*i + [1]
  248. res = poly.polyint(pol, m=1, k=[i], lbnd=-1)
  249. assert_almost_equal(poly.polyval(-1, res), i)
  250. # check single integration with integration constant and scaling
  251. for i in range(5):
  252. scl = i + 1
  253. pol = [0]*i + [1]
  254. tgt = [i] + [0]*i + [2/scl]
  255. res = poly.polyint(pol, m=1, k=[i], scl=2)
  256. assert_almost_equal(trim(res), trim(tgt))
  257. # check multiple integrations with default k
  258. for i in range(5):
  259. for j in range(2, 5):
  260. pol = [0]*i + [1]
  261. tgt = pol[:]
  262. for k in range(j):
  263. tgt = poly.polyint(tgt, m=1)
  264. res = poly.polyint(pol, m=j)
  265. assert_almost_equal(trim(res), trim(tgt))
  266. # check multiple integrations with defined k
  267. for i in range(5):
  268. for j in range(2, 5):
  269. pol = [0]*i + [1]
  270. tgt = pol[:]
  271. for k in range(j):
  272. tgt = poly.polyint(tgt, m=1, k=[k])
  273. res = poly.polyint(pol, m=j, k=list(range(j)))
  274. assert_almost_equal(trim(res), trim(tgt))
  275. # check multiple integrations with lbnd
  276. for i in range(5):
  277. for j in range(2, 5):
  278. pol = [0]*i + [1]
  279. tgt = pol[:]
  280. for k in range(j):
  281. tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1)
  282. res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1)
  283. assert_almost_equal(trim(res), trim(tgt))
  284. # check multiple integrations with scaling
  285. for i in range(5):
  286. for j in range(2, 5):
  287. pol = [0]*i + [1]
  288. tgt = pol[:]
  289. for k in range(j):
  290. tgt = poly.polyint(tgt, m=1, k=[k], scl=2)
  291. res = poly.polyint(pol, m=j, k=list(range(j)), scl=2)
  292. assert_almost_equal(trim(res), trim(tgt))
  293. def test_polyint_axis(self):
  294. # check that axis keyword works
  295. c2d = np.random.random((3, 4))
  296. tgt = np.vstack([poly.polyint(c) for c in c2d.T]).T
  297. res = poly.polyint(c2d, axis=0)
  298. assert_almost_equal(res, tgt)
  299. tgt = np.vstack([poly.polyint(c) for c in c2d])
  300. res = poly.polyint(c2d, axis=1)
  301. assert_almost_equal(res, tgt)
  302. tgt = np.vstack([poly.polyint(c, k=3) for c in c2d])
  303. res = poly.polyint(c2d, k=3, axis=1)
  304. assert_almost_equal(res, tgt)
  305. class TestDerivative(object):
  306. def test_polyder(self):
  307. # check exceptions
  308. assert_raises(ValueError, poly.polyder, [0], .5)
  309. assert_raises(ValueError, poly.polyder, [0], -1)
  310. # check that zeroth derivative does nothing
  311. for i in range(5):
  312. tgt = [0]*i + [1]
  313. res = poly.polyder(tgt, m=0)
  314. assert_equal(trim(res), trim(tgt))
  315. # check that derivation is the inverse of integration
  316. for i in range(5):
  317. for j in range(2, 5):
  318. tgt = [0]*i + [1]
  319. res = poly.polyder(poly.polyint(tgt, m=j), m=j)
  320. assert_almost_equal(trim(res), trim(tgt))
  321. # check derivation with scaling
  322. for i in range(5):
  323. for j in range(2, 5):
  324. tgt = [0]*i + [1]
  325. res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5)
  326. assert_almost_equal(trim(res), trim(tgt))
  327. def test_polyder_axis(self):
  328. # check that axis keyword works
  329. c2d = np.random.random((3, 4))
  330. tgt = np.vstack([poly.polyder(c) for c in c2d.T]).T
  331. res = poly.polyder(c2d, axis=0)
  332. assert_almost_equal(res, tgt)
  333. tgt = np.vstack([poly.polyder(c) for c in c2d])
  334. res = poly.polyder(c2d, axis=1)
  335. assert_almost_equal(res, tgt)
  336. class TestVander(object):
  337. # some random values in [-1, 1)
  338. x = np.random.random((3, 5))*2 - 1
  339. def test_polyvander(self):
  340. # check for 1d x
  341. x = np.arange(3)
  342. v = poly.polyvander(x, 3)
  343. assert_(v.shape == (3, 4))
  344. for i in range(4):
  345. coef = [0]*i + [1]
  346. assert_almost_equal(v[..., i], poly.polyval(x, coef))
  347. # check for 2d x
  348. x = np.array([[1, 2], [3, 4], [5, 6]])
  349. v = poly.polyvander(x, 3)
  350. assert_(v.shape == (3, 2, 4))
  351. for i in range(4):
  352. coef = [0]*i + [1]
  353. assert_almost_equal(v[..., i], poly.polyval(x, coef))
  354. def test_polyvander2d(self):
  355. # also tests polyval2d for non-square coefficient array
  356. x1, x2, x3 = self.x
  357. c = np.random.random((2, 3))
  358. van = poly.polyvander2d(x1, x2, [1, 2])
  359. tgt = poly.polyval2d(x1, x2, c)
  360. res = np.dot(van, c.flat)
  361. assert_almost_equal(res, tgt)
  362. # check shape
  363. van = poly.polyvander2d([x1], [x2], [1, 2])
  364. assert_(van.shape == (1, 5, 6))
  365. def test_polyvander3d(self):
  366. # also tests polyval3d for non-square coefficient array
  367. x1, x2, x3 = self.x
  368. c = np.random.random((2, 3, 4))
  369. van = poly.polyvander3d(x1, x2, x3, [1, 2, 3])
  370. tgt = poly.polyval3d(x1, x2, x3, c)
  371. res = np.dot(van, c.flat)
  372. assert_almost_equal(res, tgt)
  373. # check shape
  374. van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3])
  375. assert_(van.shape == (1, 5, 24))
  376. class TestCompanion(object):
  377. def test_raises(self):
  378. assert_raises(ValueError, poly.polycompanion, [])
  379. assert_raises(ValueError, poly.polycompanion, [1])
  380. def test_dimensions(self):
  381. for i in range(1, 5):
  382. coef = [0]*i + [1]
  383. assert_(poly.polycompanion(coef).shape == (i, i))
  384. def test_linear_root(self):
  385. assert_(poly.polycompanion([1, 2])[0, 0] == -.5)
  386. class TestMisc(object):
  387. def test_polyfromroots(self):
  388. res = poly.polyfromroots([])
  389. assert_almost_equal(trim(res), [1])
  390. for i in range(1, 5):
  391. roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
  392. tgt = Tlist[i]
  393. res = poly.polyfromroots(roots)*2**(i-1)
  394. assert_almost_equal(trim(res), trim(tgt))
  395. def test_polyroots(self):
  396. assert_almost_equal(poly.polyroots([1]), [])
  397. assert_almost_equal(poly.polyroots([1, 2]), [-.5])
  398. for i in range(2, 5):
  399. tgt = np.linspace(-1, 1, i)
  400. res = poly.polyroots(poly.polyfromroots(tgt))
  401. assert_almost_equal(trim(res), trim(tgt))
  402. def test_polyfit(self):
  403. def f(x):
  404. return x*(x - 1)*(x - 2)
  405. def f2(x):
  406. return x**4 + x**2 + 1
  407. # Test exceptions
  408. assert_raises(ValueError, poly.polyfit, [1], [1], -1)
  409. assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
  410. assert_raises(TypeError, poly.polyfit, [], [1], 0)
  411. assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
  412. assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
  413. assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
  414. assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
  415. assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
  416. assert_raises(ValueError, poly.polyfit, [1], [1], [-1,])
  417. assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
  418. assert_raises(TypeError, poly.polyfit, [1], [1], [])
  419. # Test fit
  420. x = np.linspace(0, 2)
  421. y = f(x)
  422. #
  423. coef3 = poly.polyfit(x, y, 3)
  424. assert_equal(len(coef3), 4)
  425. assert_almost_equal(poly.polyval(x, coef3), y)
  426. coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
  427. assert_equal(len(coef3), 4)
  428. assert_almost_equal(poly.polyval(x, coef3), y)
  429. #
  430. coef4 = poly.polyfit(x, y, 4)
  431. assert_equal(len(coef4), 5)
  432. assert_almost_equal(poly.polyval(x, coef4), y)
  433. coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
  434. assert_equal(len(coef4), 5)
  435. assert_almost_equal(poly.polyval(x, coef4), y)
  436. #
  437. coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
  438. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  439. coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
  440. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  441. # test weighting
  442. w = np.zeros_like(x)
  443. yw = y.copy()
  444. w[1::2] = 1
  445. yw[0::2] = 0
  446. wcoef3 = poly.polyfit(x, yw, 3, w=w)
  447. assert_almost_equal(wcoef3, coef3)
  448. wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
  449. assert_almost_equal(wcoef3, coef3)
  450. #
  451. wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
  452. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  453. wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
  454. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  455. # test scaling with complex values x points whose square
  456. # is zero when summed.
  457. x = [1, 1j, -1, -1j]
  458. assert_almost_equal(poly.polyfit(x, x, 1), [0, 1])
  459. assert_almost_equal(poly.polyfit(x, x, [0, 1]), [0, 1])
  460. # test fitting only even Polyendre polynomials
  461. x = np.linspace(-1, 1)
  462. y = f2(x)
  463. coef1 = poly.polyfit(x, y, 4)
  464. assert_almost_equal(poly.polyval(x, coef1), y)
  465. coef2 = poly.polyfit(x, y, [0, 2, 4])
  466. assert_almost_equal(poly.polyval(x, coef2), y)
  467. assert_almost_equal(coef1, coef2)
  468. def test_polytrim(self):
  469. coef = [2, -1, 1, 0]
  470. # Test exceptions
  471. assert_raises(ValueError, poly.polytrim, coef, -1)
  472. # Test results
  473. assert_equal(poly.polytrim(coef), coef[:-1])
  474. assert_equal(poly.polytrim(coef, 1), coef[:-3])
  475. assert_equal(poly.polytrim(coef, 2), [0])
  476. def test_polyline(self):
  477. assert_equal(poly.polyline(3, 4), [3, 4])