test_chebyshev.py 20 KB

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