(self)
| 587 | assert_almost_equal(res, tgt, 14 - int(np.log10(i))) |
| 588 | |
| 589 | def test_polyfit(self): |
| 590 | def f(x): |
| 591 | return x * (x - 1) * (x - 2) |
| 592 | |
| 593 | def f2(x): |
| 594 | return x**4 + x**2 + 1 |
| 595 | |
| 596 | # Test exceptions |
| 597 | assert_raises(ValueError, poly.polyfit, [1], [1], -1) |
| 598 | assert_raises(TypeError, poly.polyfit, [[1]], [1], 0) |
| 599 | assert_raises(TypeError, poly.polyfit, [], [1], 0) |
| 600 | assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0) |
| 601 | assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0) |
| 602 | assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0) |
| 603 | assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]]) |
| 604 | assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1]) |
| 605 | assert_raises(ValueError, poly.polyfit, [1], [1], [-1,]) |
| 606 | assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6]) |
| 607 | assert_raises(TypeError, poly.polyfit, [1], [1], []) |
| 608 | |
| 609 | # Test fit |
| 610 | x = np.linspace(0, 2) |
| 611 | y = f(x) |
| 612 | # |
| 613 | coef3 = poly.polyfit(x, y, 3) |
| 614 | assert_equal(len(coef3), 4) |
| 615 | assert_almost_equal(poly.polyval(x, coef3), y) |
| 616 | coef3 = poly.polyfit(x, y, [0, 1, 2, 3]) |
| 617 | assert_equal(len(coef3), 4) |
| 618 | assert_almost_equal(poly.polyval(x, coef3), y) |
| 619 | # |
| 620 | coef4 = poly.polyfit(x, y, 4) |
| 621 | assert_equal(len(coef4), 5) |
| 622 | assert_almost_equal(poly.polyval(x, coef4), y) |
| 623 | coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4]) |
| 624 | assert_equal(len(coef4), 5) |
| 625 | assert_almost_equal(poly.polyval(x, coef4), y) |
| 626 | # |
| 627 | coef2d = poly.polyfit(x, np.array([y, y]).T, 3) |
| 628 | assert_almost_equal(coef2d, np.array([coef3, coef3]).T) |
| 629 | coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3]) |
| 630 | assert_almost_equal(coef2d, np.array([coef3, coef3]).T) |
| 631 | # test weighting |
| 632 | w = np.zeros_like(x) |
| 633 | yw = y.copy() |
| 634 | w[1::2] = 1 |
| 635 | yw[0::2] = 0 |
| 636 | wcoef3 = poly.polyfit(x, yw, 3, w=w) |
| 637 | assert_almost_equal(wcoef3, coef3) |
| 638 | wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w) |
| 639 | assert_almost_equal(wcoef3, coef3) |
| 640 | # |
| 641 | wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w) |
| 642 | assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T) |
| 643 | wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w) |
| 644 | assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T) |
| 645 | # test scaling with complex values x points whose square |
| 646 | # is zero when summed. |
nothing calls this directly
no test coverage detected