MCPcopy Create free account
hub / github.com/numpy/numpy / test_polyfit

Method test_polyfit

numpy/polynomial/tests/test_polynomial.py:526–594  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

524 assert_almost_equal(trim(res), trim(tgt))
525
526 def test_polyfit(self):
527 def f(x):
528 return x*(x - 1)*(x - 2)
529
530 def f2(x):
531 return x**4 + x**2 + 1
532
533 # Test exceptions
534 assert_raises(ValueError, poly.polyfit, [1], [1], -1)
535 assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
536 assert_raises(TypeError, poly.polyfit, [], [1], 0)
537 assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
538 assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
539 assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
540 assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[[1]])
541 assert_raises(TypeError, poly.polyfit, [1], [1], 0, w=[1, 1])
542 assert_raises(ValueError, poly.polyfit, [1], [1], [-1,])
543 assert_raises(ValueError, poly.polyfit, [1], [1], [2, -1, 6])
544 assert_raises(TypeError, poly.polyfit, [1], [1], [])
545
546 # Test fit
547 x = np.linspace(0, 2)
548 y = f(x)
549 #
550 coef3 = poly.polyfit(x, y, 3)
551 assert_equal(len(coef3), 4)
552 assert_almost_equal(poly.polyval(x, coef3), y)
553 coef3 = poly.polyfit(x, y, [0, 1, 2, 3])
554 assert_equal(len(coef3), 4)
555 assert_almost_equal(poly.polyval(x, coef3), y)
556 #
557 coef4 = poly.polyfit(x, y, 4)
558 assert_equal(len(coef4), 5)
559 assert_almost_equal(poly.polyval(x, coef4), y)
560 coef4 = poly.polyfit(x, y, [0, 1, 2, 3, 4])
561 assert_equal(len(coef4), 5)
562 assert_almost_equal(poly.polyval(x, coef4), y)
563 #
564 coef2d = poly.polyfit(x, np.array([y, y]).T, 3)
565 assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
566 coef2d = poly.polyfit(x, np.array([y, y]).T, [0, 1, 2, 3])
567 assert_almost_equal(coef2d, np.array([coef3, coef3]).T)
568 # test weighting
569 w = np.zeros_like(x)
570 yw = y.copy()
571 w[1::2] = 1
572 yw[0::2] = 0
573 wcoef3 = poly.polyfit(x, yw, 3, w=w)
574 assert_almost_equal(wcoef3, coef3)
575 wcoef3 = poly.polyfit(x, yw, [0, 1, 2, 3], w=w)
576 assert_almost_equal(wcoef3, coef3)
577 #
578 wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, 3, w=w)
579 assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
580 wcoef2d = poly.polyfit(x, np.array([yw, yw]).T, [0, 1, 2, 3], w=w)
581 assert_almost_equal(wcoef2d, np.array([coef3, coef3]).T)
582 # test scaling with complex values x points whose square
583 # is zero when summed.

Callers

nothing calls this directly

Calls 6

assert_raisesFunction · 0.90
assert_equalFunction · 0.90
assert_almost_equalFunction · 0.90
fFunction · 0.85
linspaceMethod · 0.80
copyMethod · 0.45

Tested by

no test coverage detected