test_polynomial.py 20 KB

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