MCPcopy
hub / github.com/numpy/numpy / test_chebfit

Method test_chebfit

numpy/polynomial/tests/test_chebyshev.py:430–502  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

428class TestFitting:
429
430 def test_chebfit(self):
431 def f(x):
432 return x * (x - 1) * (x - 2)
433
434 def f2(x):
435 return x**4 + x**2 + 1
436
437 # Test exceptions
438 assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
439 assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
440 assert_raises(TypeError, cheb.chebfit, [], [1], 0)
441 assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
442 assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
443 assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
444 assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
445 assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1])
446 assert_raises(ValueError, cheb.chebfit, [1], [1], [-1,])
447 assert_raises(ValueError, cheb.chebfit, [1], [1], [2, -1, 6])
448 assert_raises(TypeError, cheb.chebfit, [1], [1], [])
449
450 # Test fit
451 x = np.linspace(0, 2)
452 y = f(x)
453 #
454 coef3 = cheb.chebfit(x, y, 3)
455 assert_equal(len(coef3), 4)
456 assert_almost_equal(cheb.chebval(x, coef3), y)
457 coef3 = cheb.chebfit(x, y, [0, 1, 2, 3])
458 assert_equal(len(coef3), 4)
459 assert_almost_equal(cheb.chebval(x, coef3), y)
460 #
461 coef4 = cheb.chebfit(x, y, 4)
462 assert_equal(len(coef4), 5)
463 assert_almost_equal(cheb.chebval(x, coef4), y)
464 coef4 = cheb.chebfit(x, y, [0, 1, 2, 3, 4])
465 assert_equal(len(coef4), 5)
466 assert_almost_equal(cheb.chebval(x, coef4), y)
467 # check things still work if deg is not in strict increasing
468 coef4 = cheb.chebfit(x, y, [2, 3, 4, 1, 0])
469 assert_equal(len(coef4), 5)
470 assert_almost_equal(cheb.chebval(x, coef4), y)
471 #
472 coef2d = cheb.chebfit(x, np.array([y, y]).T, 3)
473 assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
474 coef2d = cheb.chebfit(x, np.array([y, y]).T, [0, 1, 2, 3])
475 assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
476 # test weighting
477 w = np.zeros_like(x)
478 yw = y.copy()
479 w[1::2] = 1
480 y[0::2] = 0
481 wcoef3 = cheb.chebfit(x, yw, 3, w=w)
482 assert_almost_equal(wcoef3, coef3)
483 wcoef3 = cheb.chebfit(x, yw, [0, 1, 2, 3], w=w)
484 assert_almost_equal(wcoef3, coef3)
485 #
486 wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, 3, w=w)
487 assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)

Callers

nothing calls this directly

Calls 7

assert_raisesFunction · 0.90
assert_equalFunction · 0.90
assert_almost_equalFunction · 0.90
linspaceMethod · 0.80
zeros_likeMethod · 0.80
fFunction · 0.70
copyMethod · 0.45

Tested by

no test coverage detected