test_chebyshev.py 20 KB

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