(self)
| 408 | class TestFitting: |
| 409 | |
| 410 | def test_chebfit(self): |
| 411 | def f(x): |
| 412 | return x*(x - 1)*(x - 2) |
| 413 | |
| 414 | def f2(x): |
| 415 | return x**4 + x**2 + 1 |
| 416 | |
| 417 | # Test exceptions |
| 418 | assert_raises(ValueError, cheb.chebfit, [1], [1], -1) |
| 419 | assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0) |
| 420 | assert_raises(TypeError, cheb.chebfit, [], [1], 0) |
| 421 | assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0) |
| 422 | assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0) |
| 423 | assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0) |
| 424 | assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]]) |
| 425 | assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1, 1]) |
| 426 | assert_raises(ValueError, cheb.chebfit, [1], [1], [-1,]) |
| 427 | assert_raises(ValueError, cheb.chebfit, [1], [1], [2, -1, 6]) |
| 428 | assert_raises(TypeError, cheb.chebfit, [1], [1], []) |
| 429 | |
| 430 | # Test fit |
| 431 | x = np.linspace(0, 2) |
| 432 | y = f(x) |
| 433 | # |
| 434 | coef3 = cheb.chebfit(x, y, 3) |
| 435 | assert_equal(len(coef3), 4) |
| 436 | assert_almost_equal(cheb.chebval(x, coef3), y) |
| 437 | coef3 = cheb.chebfit(x, y, [0, 1, 2, 3]) |
| 438 | assert_equal(len(coef3), 4) |
| 439 | assert_almost_equal(cheb.chebval(x, coef3), y) |
| 440 | # |
| 441 | coef4 = cheb.chebfit(x, y, 4) |
| 442 | assert_equal(len(coef4), 5) |
| 443 | assert_almost_equal(cheb.chebval(x, coef4), y) |
| 444 | coef4 = cheb.chebfit(x, y, [0, 1, 2, 3, 4]) |
| 445 | assert_equal(len(coef4), 5) |
| 446 | assert_almost_equal(cheb.chebval(x, coef4), y) |
| 447 | # check things still work if deg is not in strict increasing |
| 448 | coef4 = cheb.chebfit(x, y, [2, 3, 4, 1, 0]) |
| 449 | assert_equal(len(coef4), 5) |
| 450 | assert_almost_equal(cheb.chebval(x, coef4), y) |
| 451 | # |
| 452 | coef2d = cheb.chebfit(x, np.array([y, y]).T, 3) |
| 453 | assert_almost_equal(coef2d, np.array([coef3, coef3]).T) |
| 454 | coef2d = cheb.chebfit(x, np.array([y, y]).T, [0, 1, 2, 3]) |
| 455 | assert_almost_equal(coef2d, np.array([coef3, coef3]).T) |
| 456 | # test weighting |
| 457 | w = np.zeros_like(x) |
| 458 | yw = y.copy() |
| 459 | w[1::2] = 1 |
| 460 | y[0::2] = 0 |
| 461 | wcoef3 = cheb.chebfit(x, yw, 3, w=w) |
| 462 | assert_almost_equal(wcoef3, coef3) |
| 463 | wcoef3 = cheb.chebfit(x, yw, [0, 1, 2, 3], w=w) |
| 464 | assert_almost_equal(wcoef3, coef3) |
| 465 | # |
| 466 | wcoef2d = cheb.chebfit(x, np.array([yw, yw]).T, 3, w=w) |
| 467 | assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T) |
nothing calls this directly
no test coverage detected