test_chebyshev.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. """Tests for chebyshev 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.chebyshev as cheb
  7. from numpy.polynomial.polynomial import polyval
  8. from numpy.testing import (
  9. assert_almost_equal, assert_raises, assert_equal, assert_,
  10. )
  11. def trim(x):
  12. return cheb.chebtrim(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 TestPrivate(object):
  25. def test__cseries_to_zseries(self):
  26. for i in range(5):
  27. inp = np.array([2] + [1]*i, np.double)
  28. tgt = np.array([.5]*i + [2] + [.5]*i, np.double)
  29. res = cheb._cseries_to_zseries(inp)
  30. assert_equal(res, tgt)
  31. def test__zseries_to_cseries(self):
  32. for i in range(5):
  33. inp = np.array([.5]*i + [2] + [.5]*i, np.double)
  34. tgt = np.array([2] + [1]*i, np.double)
  35. res = cheb._zseries_to_cseries(inp)
  36. assert_equal(res, tgt)
  37. class TestConstants(object):
  38. def test_chebdomain(self):
  39. assert_equal(cheb.chebdomain, [-1, 1])
  40. def test_chebzero(self):
  41. assert_equal(cheb.chebzero, [0])
  42. def test_chebone(self):
  43. assert_equal(cheb.chebone, [1])
  44. def test_chebx(self):
  45. assert_equal(cheb.chebx, [0, 1])
  46. class TestArithmetic(object):
  47. def test_chebadd(self):
  48. for i in range(5):
  49. for j in range(5):
  50. msg = "At i=%d, j=%d" % (i, j)
  51. tgt = np.zeros(max(i, j) + 1)
  52. tgt[i] += 1
  53. tgt[j] += 1
  54. res = cheb.chebadd([0]*i + [1], [0]*j + [1])
  55. assert_equal(trim(res), trim(tgt), err_msg=msg)
  56. def test_chebsub(self):
  57. for i in range(5):
  58. for j in range(5):
  59. msg = "At i=%d, j=%d" % (i, j)
  60. tgt = np.zeros(max(i, j) + 1)
  61. tgt[i] += 1
  62. tgt[j] -= 1
  63. res = cheb.chebsub([0]*i + [1], [0]*j + [1])
  64. assert_equal(trim(res), trim(tgt), err_msg=msg)
  65. def test_chebmulx(self):
  66. assert_equal(cheb.chebmulx([0]), [0])
  67. assert_equal(cheb.chebmulx([1]), [0, 1])
  68. for i in range(1, 5):
  69. ser = [0]*i + [1]
  70. tgt = [0]*(i - 1) + [.5, 0, .5]
  71. assert_equal(cheb.chebmulx(ser), tgt)
  72. def test_chebmul(self):
  73. for i in range(5):
  74. for j in range(5):
  75. msg = "At i=%d, j=%d" % (i, j)
  76. tgt = np.zeros(i + j + 1)
  77. tgt[i + j] += .5
  78. tgt[abs(i - j)] += .5
  79. res = cheb.chebmul([0]*i + [1], [0]*j + [1])
  80. assert_equal(trim(res), trim(tgt), err_msg=msg)
  81. def test_chebdiv(self):
  82. for i in range(5):
  83. for j in range(5):
  84. msg = "At i=%d, j=%d" % (i, j)
  85. ci = [0]*i + [1]
  86. cj = [0]*j + [1]
  87. tgt = cheb.chebadd(ci, cj)
  88. quo, rem = cheb.chebdiv(tgt, ci)
  89. res = cheb.chebadd(cheb.chebmul(quo, ci), rem)
  90. assert_equal(trim(res), trim(tgt), err_msg=msg)
  91. def test_chebpow(self):
  92. for i in range(5):
  93. for j in range(5):
  94. msg = "At i=%d, j=%d" % (i, j)
  95. c = np.arange(i + 1)
  96. tgt = reduce(cheb.chebmul, [c]*j, np.array([1]))
  97. res = cheb.chebpow(c, j)
  98. assert_equal(trim(res), trim(tgt), err_msg=msg)
  99. class TestEvaluation(object):
  100. # coefficients of 1 + 2*x + 3*x**2
  101. c1d = np.array([2.5, 2., 1.5])
  102. c2d = np.einsum('i,j->ij', c1d, c1d)
  103. c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
  104. # some random values in [-1, 1)
  105. x = np.random.random((3, 5))*2 - 1
  106. y = polyval(x, [1., 2., 3.])
  107. def test_chebval(self):
  108. #check empty input
  109. assert_equal(cheb.chebval([], [1]).size, 0)
  110. #check normal input)
  111. x = np.linspace(-1, 1)
  112. y = [polyval(x, c) for c in Tlist]
  113. for i in range(10):
  114. msg = "At i=%d" % i
  115. tgt = y[i]
  116. res = cheb.chebval(x, [0]*i + [1])
  117. assert_almost_equal(res, tgt, err_msg=msg)
  118. #check that shape is preserved
  119. for i in range(3):
  120. dims = [2]*i
  121. x = np.zeros(dims)
  122. assert_equal(cheb.chebval(x, [1]).shape, dims)
  123. assert_equal(cheb.chebval(x, [1, 0]).shape, dims)
  124. assert_equal(cheb.chebval(x, [1, 0, 0]).shape, dims)
  125. def test_chebval2d(self):
  126. x1, x2, x3 = self.x
  127. y1, y2, y3 = self.y
  128. #test exceptions
  129. assert_raises(ValueError, cheb.chebval2d, x1, x2[:2], self.c2d)
  130. #test values
  131. tgt = y1*y2
  132. res = cheb.chebval2d(x1, x2, self.c2d)
  133. assert_almost_equal(res, tgt)
  134. #test shape
  135. z = np.ones((2, 3))
  136. res = cheb.chebval2d(z, z, self.c2d)
  137. assert_(res.shape == (2, 3))
  138. def test_chebval3d(self):
  139. x1, x2, x3 = self.x
  140. y1, y2, y3 = self.y
  141. #test exceptions
  142. assert_raises(ValueError, cheb.chebval3d, x1, x2, x3[:2], self.c3d)
  143. #test values
  144. tgt = y1*y2*y3
  145. res = cheb.chebval3d(x1, x2, x3, self.c3d)
  146. assert_almost_equal(res, tgt)
  147. #test shape
  148. z = np.ones((2, 3))
  149. res = cheb.chebval3d(z, z, z, self.c3d)
  150. assert_(res.shape == (2, 3))
  151. def test_chebgrid2d(self):
  152. x1, x2, x3 = self.x
  153. y1, y2, y3 = self.y
  154. #test values
  155. tgt = np.einsum('i,j->ij', y1, y2)
  156. res = cheb.chebgrid2d(x1, x2, self.c2d)
  157. assert_almost_equal(res, tgt)
  158. #test shape
  159. z = np.ones((2, 3))
  160. res = cheb.chebgrid2d(z, z, self.c2d)
  161. assert_(res.shape == (2, 3)*2)
  162. def test_chebgrid3d(self):
  163. x1, x2, x3 = self.x
  164. y1, y2, y3 = self.y
  165. #test values
  166. tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
  167. res = cheb.chebgrid3d(x1, x2, x3, self.c3d)
  168. assert_almost_equal(res, tgt)
  169. #test shape
  170. z = np.ones((2, 3))
  171. res = cheb.chebgrid3d(z, z, z, self.c3d)
  172. assert_(res.shape == (2, 3)*3)
  173. class TestIntegral(object):
  174. def test_chebint(self):
  175. # check exceptions
  176. assert_raises(TypeError, cheb.chebint, [0], .5)
  177. assert_raises(ValueError, cheb.chebint, [0], -1)
  178. assert_raises(ValueError, cheb.chebint, [0], 1, [0, 0])
  179. assert_raises(ValueError, cheb.chebint, [0], lbnd=[0])
  180. assert_raises(ValueError, cheb.chebint, [0], scl=[0])
  181. assert_raises(TypeError, cheb.chebint, [0], axis=.5)
  182. # test integration of zero polynomial
  183. for i in range(2, 5):
  184. k = [0]*(i - 2) + [1]
  185. res = cheb.chebint([0], m=i, k=k)
  186. assert_almost_equal(res, [0, 1])
  187. # check single integration with integration constant
  188. for i in range(5):
  189. scl = i + 1
  190. pol = [0]*i + [1]
  191. tgt = [i] + [0]*i + [1/scl]
  192. chebpol = cheb.poly2cheb(pol)
  193. chebint = cheb.chebint(chebpol, m=1, k=[i])
  194. res = cheb.cheb2poly(chebint)
  195. assert_almost_equal(trim(res), trim(tgt))
  196. # check single integration with integration constant and lbnd
  197. for i in range(5):
  198. scl = i + 1
  199. pol = [0]*i + [1]
  200. chebpol = cheb.poly2cheb(pol)
  201. chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1)
  202. assert_almost_equal(cheb.chebval(-1, chebint), i)
  203. # check single integration with integration constant and scaling
  204. for i in range(5):
  205. scl = i + 1
  206. pol = [0]*i + [1]
  207. tgt = [i] + [0]*i + [2/scl]
  208. chebpol = cheb.poly2cheb(pol)
  209. chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2)
  210. res = cheb.cheb2poly(chebint)
  211. assert_almost_equal(trim(res), trim(tgt))
  212. # check multiple integrations with default k
  213. for i in range(5):
  214. for j in range(2, 5):
  215. pol = [0]*i + [1]
  216. tgt = pol[:]
  217. for k in range(j):
  218. tgt = cheb.chebint(tgt, m=1)
  219. res = cheb.chebint(pol, m=j)
  220. assert_almost_equal(trim(res), trim(tgt))
  221. # check multiple integrations with defined k
  222. for i in range(5):
  223. for j in range(2, 5):
  224. pol = [0]*i + [1]
  225. tgt = pol[:]
  226. for k in range(j):
  227. tgt = cheb.chebint(tgt, m=1, k=[k])
  228. res = cheb.chebint(pol, m=j, k=list(range(j)))
  229. assert_almost_equal(trim(res), trim(tgt))
  230. # check multiple integrations with lbnd
  231. for i in range(5):
  232. for j in range(2, 5):
  233. pol = [0]*i + [1]
  234. tgt = pol[:]
  235. for k in range(j):
  236. tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1)
  237. res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1)
  238. assert_almost_equal(trim(res), trim(tgt))
  239. # check multiple integrations with scaling
  240. for i in range(5):
  241. for j in range(2, 5):
  242. pol = [0]*i + [1]
  243. tgt = pol[:]
  244. for k in range(j):
  245. tgt = cheb.chebint(tgt, m=1, k=[k], scl=2)
  246. res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2)
  247. assert_almost_equal(trim(res), trim(tgt))
  248. def test_chebint_axis(self):
  249. # check that axis keyword works
  250. c2d = np.random.random((3, 4))
  251. tgt = np.vstack([cheb.chebint(c) for c in c2d.T]).T
  252. res = cheb.chebint(c2d, axis=0)
  253. assert_almost_equal(res, tgt)
  254. tgt = np.vstack([cheb.chebint(c) for c in c2d])
  255. res = cheb.chebint(c2d, axis=1)
  256. assert_almost_equal(res, tgt)
  257. tgt = np.vstack([cheb.chebint(c, k=3) for c in c2d])
  258. res = cheb.chebint(c2d, k=3, axis=1)
  259. assert_almost_equal(res, tgt)
  260. class TestDerivative(object):
  261. def test_chebder(self):
  262. # check exceptions
  263. assert_raises(TypeError, cheb.chebder, [0], .5)
  264. assert_raises(ValueError, cheb.chebder, [0], -1)
  265. # check that zeroth derivative does nothing
  266. for i in range(5):
  267. tgt = [0]*i + [1]
  268. res = cheb.chebder(tgt, m=0)
  269. assert_equal(trim(res), trim(tgt))
  270. # check that derivation is the inverse of integration
  271. for i in range(5):
  272. for j in range(2, 5):
  273. tgt = [0]*i + [1]
  274. res = cheb.chebder(cheb.chebint(tgt, m=j), m=j)
  275. assert_almost_equal(trim(res), trim(tgt))
  276. # check derivation with scaling
  277. for i in range(5):
  278. for j in range(2, 5):
  279. tgt = [0]*i + [1]
  280. res = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5)
  281. assert_almost_equal(trim(res), trim(tgt))
  282. def test_chebder_axis(self):
  283. # check that axis keyword works
  284. c2d = np.random.random((3, 4))
  285. tgt = np.vstack([cheb.chebder(c) for c in c2d.T]).T
  286. res = cheb.chebder(c2d, axis=0)
  287. assert_almost_equal(res, tgt)
  288. tgt = np.vstack([cheb.chebder(c) for c in c2d])
  289. res = cheb.chebder(c2d, axis=1)
  290. assert_almost_equal(res, tgt)
  291. class TestVander(object):
  292. # some random values in [-1, 1)
  293. x = np.random.random((3, 5))*2 - 1
  294. def test_chebvander(self):
  295. # check for 1d x
  296. x = np.arange(3)
  297. v = cheb.chebvander(x, 3)
  298. assert_(v.shape == (3, 4))
  299. for i in range(4):
  300. coef = [0]*i + [1]
  301. assert_almost_equal(v[..., i], cheb.chebval(x, coef))
  302. # check for 2d x
  303. x = np.array([[1, 2], [3, 4], [5, 6]])
  304. v = cheb.chebvander(x, 3)
  305. assert_(v.shape == (3, 2, 4))
  306. for i in range(4):
  307. coef = [0]*i + [1]
  308. assert_almost_equal(v[..., i], cheb.chebval(x, coef))
  309. def test_chebvander2d(self):
  310. # also tests chebval2d for non-square coefficient array
  311. x1, x2, x3 = self.x
  312. c = np.random.random((2, 3))
  313. van = cheb.chebvander2d(x1, x2, [1, 2])
  314. tgt = cheb.chebval2d(x1, x2, c)
  315. res = np.dot(van, c.flat)
  316. assert_almost_equal(res, tgt)
  317. # check shape
  318. van = cheb.chebvander2d([x1], [x2], [1, 2])
  319. assert_(van.shape == (1, 5, 6))
  320. def test_chebvander3d(self):
  321. # also tests chebval3d for non-square coefficient array
  322. x1, x2, x3 = self.x
  323. c = np.random.random((2, 3, 4))
  324. van = cheb.chebvander3d(x1, x2, x3, [1, 2, 3])
  325. tgt = cheb.chebval3d(x1, x2, x3, c)
  326. res = np.dot(van, c.flat)
  327. assert_almost_equal(res, tgt)
  328. # check shape
  329. van = cheb.chebvander3d([x1], [x2], [x3], [1, 2, 3])
  330. assert_(van.shape == (1, 5, 24))
  331. class TestFitting(object):
  332. def test_chebfit(self):
  333. def f(x):
  334. return x*(x - 1)*(x - 2)
  335. def f2(x):
  336. return x**4 + x**2 + 1
  337. # Test exceptions
  338. assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
  339. assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
  340. assert_raises(TypeError, cheb.chebfit, [], [1], 0)
  341. assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
  342. assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
  343. assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
  344. assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
  345. assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1])
  346. assert_raises(ValueError, cheb.chebfit, [1], [1], [-1,])
  347. assert_raises(ValueError, cheb.chebfit, [1], [1], [2, -1, 6])
  348. assert_raises(TypeError, cheb.chebfit, [1], [1], [])
  349. # Test fit
  350. x = np.linspace(0, 2)
  351. y = f(x)
  352. #
  353. coef3 = cheb.chebfit(x, y, 3)
  354. assert_equal(len(coef3), 4)
  355. assert_almost_equal(cheb.chebval(x, coef3), y)
  356. coef3 = cheb.chebfit(x, y, [0, 1, 2, 3])
  357. assert_equal(len(coef3), 4)
  358. assert_almost_equal(cheb.chebval(x, coef3), y)
  359. #
  360. coef4 = cheb.chebfit(x, y, 4)
  361. assert_equal(len(coef4), 5)
  362. assert_almost_equal(cheb.chebval(x, coef4), y)
  363. coef4 = cheb.chebfit(x, y, [0, 1, 2, 3, 4])
  364. assert_equal(len(coef4), 5)
  365. assert_almost_equal(cheb.chebval(x, coef4), y)
  366. # check things still work if deg is not in strict increasing
  367. coef4 = cheb.chebfit(x, y, [2, 3, 4, 1, 0])
  368. assert_equal(len(coef4), 5)
  369. assert_almost_equal(cheb.chebval(x, coef4), y)
  370. #
  371. coef2d = cheb.chebfit(x, np.array([y, y]).T, 3)
  372. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  373. coef2d = cheb.chebfit(x, np.array([y, y]).T, [0, 1, 2, 3])
  374. assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
  375. # test weighting
  376. w = np.zeros_like(x)
  377. yw = y.copy()
  378. w[1::2] = 1
  379. y[0::2] = 0
  380. wcoef3 = cheb.chebfit(x, yw, 3, w=w)
  381. assert_almost_equal(wcoef3, coef3)
  382. wcoef3 = cheb.chebfit(x, yw, [0, 1, 2, 3], w=w)
  383. assert_almost_equal(wcoef3, coef3)
  384. #
  385. wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, 3, w=w)
  386. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  387. wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
  388. assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
  389. # test scaling with complex values x points whose square
  390. # is zero when summed.
  391. x = [1, 1j, -1, -1j]
  392. assert_almost_equal(cheb.chebfit(x, x, 1), [0, 1])
  393. assert_almost_equal(cheb.chebfit(x, x, [0, 1]), [0, 1])
  394. # test fitting only even polynomials
  395. x = np.linspace(-1, 1)
  396. y = f2(x)
  397. coef1 = cheb.chebfit(x, y, 4)
  398. assert_almost_equal(cheb.chebval(x, coef1), y)
  399. coef2 = cheb.chebfit(x, y, [0, 2, 4])
  400. assert_almost_equal(cheb.chebval(x, coef2), y)
  401. assert_almost_equal(coef1, coef2)
  402. class TestInterpolate(object):
  403. def f(self, x):
  404. return x * (x - 1) * (x - 2)
  405. def test_raises(self):
  406. assert_raises(ValueError, cheb.chebinterpolate, self.f, -1)
  407. assert_raises(TypeError, cheb.chebinterpolate, self.f, 10.)
  408. def test_dimensions(self):
  409. for deg in range(1, 5):
  410. assert_(cheb.chebinterpolate(self.f, deg).shape == (deg + 1,))
  411. def test_approximation(self):
  412. def powx(x, p):
  413. return x**p
  414. x = np.linspace(-1, 1, 10)
  415. for deg in range(0, 10):
  416. for p in range(0, deg + 1):
  417. c = cheb.chebinterpolate(powx, deg, (p,))
  418. assert_almost_equal(cheb.chebval(x, c), powx(x, p), decimal=12)
  419. class TestCompanion(object):
  420. def test_raises(self):
  421. assert_raises(ValueError, cheb.chebcompanion, [])
  422. assert_raises(ValueError, cheb.chebcompanion, [1])
  423. def test_dimensions(self):
  424. for i in range(1, 5):
  425. coef = [0]*i + [1]
  426. assert_(cheb.chebcompanion(coef).shape == (i, i))
  427. def test_linear_root(self):
  428. assert_(cheb.chebcompanion([1, 2])[0, 0] == -.5)
  429. class TestGauss(object):
  430. def test_100(self):
  431. x, w = cheb.chebgauss(100)
  432. # test orthogonality. Note that the results need to be normalized,
  433. # otherwise the huge values that can arise from fast growing
  434. # functions like Laguerre can be very confusing.
  435. v = cheb.chebvander(x, 99)
  436. vv = np.dot(v.T * w, v)
  437. vd = 1/np.sqrt(vv.diagonal())
  438. vv = vd[:, None] * vv * vd
  439. assert_almost_equal(vv, np.eye(100))
  440. # check that the integral of 1 is correct
  441. tgt = np.pi
  442. assert_almost_equal(w.sum(), tgt)
  443. class TestMisc(object):
  444. def test_chebfromroots(self):
  445. res = cheb.chebfromroots([])
  446. assert_almost_equal(trim(res), [1])
  447. for i in range(1, 5):
  448. roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
  449. tgt = [0]*i + [1]
  450. res = cheb.chebfromroots(roots)*2**(i-1)
  451. assert_almost_equal(trim(res), trim(tgt))
  452. def test_chebroots(self):
  453. assert_almost_equal(cheb.chebroots([1]), [])
  454. assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
  455. for i in range(2, 5):
  456. tgt = np.linspace(-1, 1, i)
  457. res = cheb.chebroots(cheb.chebfromroots(tgt))
  458. assert_almost_equal(trim(res), trim(tgt))
  459. def test_chebtrim(self):
  460. coef = [2, -1, 1, 0]
  461. # Test exceptions
  462. assert_raises(ValueError, cheb.chebtrim, coef, -1)
  463. # Test results
  464. assert_equal(cheb.chebtrim(coef), coef[:-1])
  465. assert_equal(cheb.chebtrim(coef, 1), coef[:-3])
  466. assert_equal(cheb.chebtrim(coef, 2), [0])
  467. def test_chebline(self):
  468. assert_equal(cheb.chebline(3, 4), [3, 4])
  469. def test_cheb2poly(self):
  470. for i in range(10):
  471. assert_almost_equal(cheb.cheb2poly([0]*i + [1]), Tlist[i])
  472. def test_poly2cheb(self):
  473. for i in range(10):
  474. assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
  475. def test_weight(self):
  476. x = np.linspace(-1, 1, 11)[1:-1]
  477. tgt = 1./(np.sqrt(1 + x) * np.sqrt(1 - x))
  478. res = cheb.chebweight(x)
  479. assert_almost_equal(res, tgt)
  480. def test_chebpts1(self):
  481. #test exceptions
  482. assert_raises(ValueError, cheb.chebpts1, 1.5)
  483. assert_raises(ValueError, cheb.chebpts1, 0)
  484. #test points
  485. tgt = [0]
  486. assert_almost_equal(cheb.chebpts1(1), tgt)
  487. tgt = [-0.70710678118654746, 0.70710678118654746]
  488. assert_almost_equal(cheb.chebpts1(2), tgt)
  489. tgt = [-0.86602540378443871, 0, 0.86602540378443871]
  490. assert_almost_equal(cheb.chebpts1(3), tgt)
  491. tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
  492. assert_almost_equal(cheb.chebpts1(4), tgt)
  493. def test_chebpts2(self):
  494. #test exceptions
  495. assert_raises(ValueError, cheb.chebpts2, 1.5)
  496. assert_raises(ValueError, cheb.chebpts2, 1)
  497. #test points
  498. tgt = [-1, 1]
  499. assert_almost_equal(cheb.chebpts2(2), tgt)
  500. tgt = [-1, 0, 1]
  501. assert_almost_equal(cheb.chebpts2(3), tgt)
  502. tgt = [-1, -0.5, .5, 1]
  503. assert_almost_equal(cheb.chebpts2(4), tgt)
  504. tgt = [-1.0, -0.707106781187, 0, 0.707106781187, 1.0]
  505. assert_almost_equal(cheb.chebpts2(5), tgt)