diff options
Diffstat (limited to 'numpy/polynomial/tests/test_polynomial.py')
-rw-r--r-- | numpy/polynomial/tests/test_polynomial.py | 147 |
1 files changed, 72 insertions, 75 deletions
diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index 373045aae..77092cd2f 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -6,46 +6,47 @@ from __future__ import division, absolute_import, print_function import numpy as np import numpy.polynomial.polynomial as poly from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) -def trim(x) : + +def trim(x): return poly.polytrim(x, tol=1e-6) -T0 = [ 1] -T1 = [ 0, 1] -T2 = [-1, 0, 2] -T3 = [ 0, -3, 0, 4] -T4 = [ 1, 0, -8, 0, 8] -T5 = [ 0, 5, 0, -20, 0, 16] -T6 = [-1, 0, 18, 0, -48, 0, 32] -T7 = [ 0, -7, 0, 56, 0, -112, 0, 64] -T8 = [ 1, 0, -32, 0, 160, 0, -256, 0, 128] -T9 = [ 0, 9, 0, -120, 0, 432, 0, -576, 0, 256] +T0 = [1] +T1 = [0, 1] +T2 = [-1, 0, 2] +T3 = [0, -3, 0, 4] +T4 = [1, 0, -8, 0, 8] +T5 = [0, 5, 0, -20, 0, 16] +T6 = [-1, 0, 18, 0, -48, 0, 32] +T7 = [0, -7, 0, 56, 0, -112, 0, 64] +T8 = [1, 0, -32, 0, 160, 0, -256, 0, 128] +T9 = [0, 9, 0, -120, 0, 432, 0, -576, 0, 256] Tlist = [T0, T1, T2, T3, T4, T5, T6, T7, T8, T9] -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_polydomain(self) : + def test_polydomain(self): assert_equal(poly.polydomain, [-1, 1]) - def test_polyzero(self) : + def test_polyzero(self): assert_equal(poly.polyzero, [0]) - def test_polyone(self) : + def test_polyone(self): assert_equal(poly.polyone, [1]) - def test_polyx(self) : + def test_polyx(self): assert_equal(poly.polyx, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): - def test_polyadd(self) : - for i in range(5) : - for j in range(5) : + def test_polyadd(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -53,9 +54,9 @@ class TestArithmetic(TestCase) : res = poly.polyadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_polysub(self) : - for i in range(5) : - for j in range(5) : + def test_polysub(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(max(i, j) + 1) tgt[i] += 1 @@ -71,16 +72,16 @@ class TestArithmetic(TestCase) : tgt = [0]*(i + 1) + [1] assert_equal(poly.polymulx(ser), tgt) - def test_polymul(self) : - for i in range(5) : - for j in range(5) : + def test_polymul(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) tgt = np.zeros(i + j + 1) tgt[i + j] += 1 res = poly.polymul([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_polydiv(self) : + def test_polydiv(self): # check zero division assert_raises(ZeroDivisionError, poly.polydiv, [1], [0]) @@ -91,8 +92,8 @@ class TestArithmetic(TestCase) : assert_equal((quo, rem), ((1, 1), 0)) # check rest. - for i in range(5) : - for j in range(5) : + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1, 2] cj = [0]*j + [1, 2] @@ -112,15 +113,14 @@ class TestEvaluation(TestCase): x = np.random.random((3, 5))*2 - 1 y = poly.polyval(x, [1., 2., 3.]) - - def test_polyval(self) : + def test_polyval(self): #check empty input assert_equal(poly.polyval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [x**i for i in range(5)] - for i in range(5) : + for i in range(5): tgt = y[i] res = poly.polyval(x, [0]*i + [1]) assert_almost_equal(res, tgt) @@ -129,7 +129,7 @@ class TestEvaluation(TestCase): assert_almost_equal(res, tgt) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(poly.polyval(x, [1]).shape, dims) @@ -201,7 +201,7 @@ class TestEvaluation(TestCase): class TestIntegral(TestCase): - def test_polyint(self) : + def test_polyint(self): # check exceptions assert_raises(ValueError, poly.polyint, [0], .5) assert_raises(ValueError, poly.polyint, [0], -1) @@ -214,7 +214,7 @@ class TestIntegral(TestCase): assert_almost_equal(res, [0, 1]) # check single integration with integration constant - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [1/scl] @@ -222,14 +222,14 @@ class TestIntegral(TestCase): assert_almost_equal(trim(res), trim(tgt)) # check single integration with integration constant and lbnd - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] res = poly.polyint(pol, m=1, k=[i], lbnd=-1) assert_almost_equal(poly.polyval(-1, res), i) # check single integration with integration constant and scaling - for i in range(5) : + for i in range(5): scl = i + 1 pol = [0]*i + [1] tgt = [i] + [0]*i + [2/scl] @@ -237,41 +237,41 @@ class TestIntegral(TestCase): assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with default k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1) res = poly.polyint(pol, m=j) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with defined k - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1, k=[k]) res = poly.polyint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1) res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): pol = [0]*i + [1] tgt = pol[:] - for k in range(j) : + for k in range(j): tgt = poly.polyint(tgt, m=1, k=[k], scl=2) res = poly.polyint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -293,29 +293,29 @@ class TestIntegral(TestCase): assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_polyder(self) : + def test_polyder(self): # check exceptions assert_raises(ValueError, poly.polyder, [0], .5) assert_raises(ValueError, poly.polyder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = poly.polyder(tgt, m=0) assert_equal(trim(res), trim(tgt)) # check that derivation is the inverse of integration - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = poly.polyder(poly.polyint(tgt, m=j), m=j) assert_almost_equal(trim(res), trim(tgt)) # check derivation with scaling - for i in range(5) : - for j in range(2, 5) : + for i in range(5): + for j in range(2, 5): tgt = [0]*i + [1] res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -337,13 +337,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_polyvander(self) : + def test_polyvander(self): # check for 1d x x = np.arange(3) v = poly.polyvander(x, 3) assert_(v.shape == (3, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], poly.polyval(x, coef)) @@ -351,11 +350,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = poly.polyvander(x, 3) assert_(v.shape == (3, 2, 4)) - for i in range(4) : + for i in range(4): coef = [0]*i + [1] assert_almost_equal(v[..., i], poly.polyval(x, coef)) - def test_polyvander2d(self) : + def test_polyvander2d(self): # also tests polyval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -368,8 +367,7 @@ class TestVander(TestCase): van = poly.polyvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_polyvander3d(self) : + def test_polyvander3d(self): # also tests polyval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -398,27 +396,27 @@ class TestCompanion(TestCase): assert_(poly.polycompanion([1, 2])[0, 0] == -.5) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_polyfromroots(self) : + def test_polyfromroots(self): res = poly.polyfromroots([]) assert_almost_equal(trim(res), [1]) - for i in range(1, 5) : + for i in range(1, 5): roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2]) tgt = Tlist[i] res = poly.polyfromroots(roots)*2**(i-1) assert_almost_equal(trim(res), trim(tgt)) - def test_polyroots(self) : + def test_polyroots(self): assert_almost_equal(poly.polyroots([1]), []) assert_almost_equal(poly.polyroots([1, 2]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = poly.polyroots(poly.polyfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_polyfit(self) : - def f(x) : + def test_polyfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -460,8 +458,7 @@ class TestMisc(TestCase) : x = [1, 1j, -1, -1j] assert_almost_equal(poly.polyfit(x, x, 1), [0, 1]) - - def test_polytrim(self) : + def test_polytrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -472,7 +469,7 @@ class TestMisc(TestCase) : assert_equal(poly.polytrim(coef, 1), coef[:-3]) assert_equal(poly.polytrim(coef, 2), [0]) - def test_polyline(self) : + def test_polyline(self): assert_equal(poly.polyline(3, 4), [3, 4]) |