diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-09-06 10:31:09 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-09-06 10:31:09 -0700 |
commit | b9b0ea5ee8270364af47d9f66ba1b28c57815d8a (patch) | |
tree | c6e19d528f570b8fb09c4ef0372ba47df20c11af | |
parent | f1e49d7b42b73e04bdb00cfab60925d7d4625cbf (diff) | |
parent | 8255df3d6cd00281f1f9b2925648dca3a0b9e036 (diff) | |
download | numpy-b9b0ea5ee8270364af47d9f66ba1b28c57815d8a.tar.gz |
Merge pull request #3676 from charris/pep8-polynomial-tests
STY: Make numpy/polynomial/tests/*.py PEP8 compliant.
-rw-r--r-- | numpy/polynomial/tests/test_chebyshev.py | 167 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_classes.py | 60 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_hermite.py | 155 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_hermite_e.py | 155 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_laguerre.py | 151 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_legendre.py | 158 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_polynomial.py | 147 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_polyutils.py | 23 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_printing.py | 11 |
9 files changed, 504 insertions, 523 deletions
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py index 70eccdd0a..82c3ba9ea 100644 --- a/numpy/polynomial/tests/test_chebyshev.py +++ b/numpy/polynomial/tests/test_chebyshev.py @@ -7,63 +7,64 @@ import numpy as np import numpy.polynomial.chebyshev as cheb from numpy.polynomial.polynomial import polyval 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 cheb.chebtrim(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 TestPrivate(TestCase) : +class TestPrivate(TestCase): - def test__cseries_to_zseries(self) : - for i in range(5) : + def test__cseries_to_zseries(self): + for i in range(5): inp = np.array([2] + [1]*i, np.double) tgt = np.array([.5]*i + [2] + [.5]*i, np.double) res = cheb._cseries_to_zseries(inp) assert_equal(res, tgt) - def test__zseries_to_cseries(self) : - for i in range(5) : + def test__zseries_to_cseries(self): + for i in range(5): inp = np.array([.5]*i + [2] + [.5]*i, np.double) tgt = np.array([2] + [1]*i, np.double) res = cheb._zseries_to_cseries(inp) assert_equal(res, tgt) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_chebdomain(self) : + def test_chebdomain(self): assert_equal(cheb.chebdomain, [-1, 1]) - def test_chebzero(self) : + def test_chebzero(self): assert_equal(cheb.chebzero, [0]) - def test_chebone(self) : + def test_chebone(self): assert_equal(cheb.chebone, [1]) - def test_chebx(self) : + def test_chebx(self): assert_equal(cheb.chebx, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): - def test_chebadd(self) : - for i in range(5) : - for j in range(5) : + def test_chebadd(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,9 +72,9 @@ class TestArithmetic(TestCase) : res = cheb.chebadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_chebsub(self) : - for i in range(5) : - for j in range(5) : + def test_chebsub(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 @@ -89,9 +90,9 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [.5, 0, .5] assert_equal(cheb.chebmulx(ser), tgt) - def test_chebmul(self) : - for i in range(5) : - for j in range(5) : + def test_chebmul(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] += .5 @@ -99,9 +100,9 @@ class TestArithmetic(TestCase) : res = cheb.chebmul([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_chebdiv(self) : - for i in range(5) : - for j in range(5) : + def test_chebdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -121,22 +122,21 @@ class TestEvaluation(TestCase): x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - - def test_chebval(self) : + def test_chebval(self): #check empty input assert_equal(cheb.chebval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Tlist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i tgt = y[i] res = cheb.chebval(x, [0]*i + [1]) assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(cheb.chebval(x, [1]).shape, dims) @@ -206,9 +206,9 @@ class TestEvaluation(TestCase): assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_chebint(self) : + def test_chebint(self): # check exceptions assert_raises(ValueError, cheb.chebint, [0], .5) assert_raises(ValueError, cheb.chebint, [0], -1) @@ -221,7 +221,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] @@ -231,7 +231,7 @@ 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] chebpol = cheb.poly2cheb(pol) @@ -239,7 +239,7 @@ class TestIntegral(TestCase) : assert_almost_equal(cheb.chebval(-1, chebint), 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] @@ -249,41 +249,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 = cheb.chebint(tgt, m=1) res = cheb.chebint(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 = cheb.chebint(tgt, m=1, k=[k]) res = cheb.chebint(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 = cheb.chebint(tgt, m=1, k=[k], lbnd=-1) res = cheb.chebint(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 = cheb.chebint(tgt, m=1, k=[k], scl=2) res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -305,29 +305,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_chebder(self) : + def test_chebder(self): # check exceptions assert_raises(ValueError, cheb.chebder, [0], .5) assert_raises(ValueError, cheb.chebder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = cheb.chebder(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 = cheb.chebder(cheb.chebint(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 = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -349,13 +349,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_chebvander(self) : + def test_chebvander(self): # check for 1d x x = np.arange(3) v = cheb.chebvander(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], cheb.chebval(x, coef)) @@ -363,11 +362,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = cheb.chebvander(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], cheb.chebval(x, coef)) - def test_chebvander2d(self) : + def test_chebvander2d(self): # also tests chebval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -380,8 +379,7 @@ class TestVander(TestCase): van = cheb.chebvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_chebvander3d(self) : + def test_chebvander3d(self): # also tests chebval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -397,8 +395,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_chebfit(self) : - def f(x) : + def test_chebfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -475,26 +473,26 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_chebfromroots(self) : + def test_chebfromroots(self): res = cheb.chebfromroots([]) 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 = [0]*i + [1] res = cheb.chebfromroots(roots)*2**(i-1) assert_almost_equal(trim(res), trim(tgt)) - def test_chebroots(self) : + def test_chebroots(self): assert_almost_equal(cheb.chebroots([1]), []) assert_almost_equal(cheb.chebroots([1, 2]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = cheb.chebroots(cheb.chebfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_chebtrim(self) : + def test_chebtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -505,15 +503,15 @@ class TestMisc(TestCase) : assert_equal(cheb.chebtrim(coef, 1), coef[:-3]) assert_equal(cheb.chebtrim(coef, 2), [0]) - def test_chebline(self) : + def test_chebline(self): assert_equal(cheb.chebline(3, 4), [3, 4]) - def test_cheb2poly(self) : - for i in range(10) : + def test_cheb2poly(self): + for i in range(10): assert_almost_equal(cheb.cheb2poly([0]*i + [1]), Tlist[i]) - def test_poly2cheb(self) : - for i in range(10) : + def test_poly2cheb(self): + for i in range(10): assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1]) def test_weight(self): @@ -537,7 +535,6 @@ class TestMisc(TestCase) : tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325] assert_almost_equal(cheb.chebpts1(4), tgt) - def test_chebpts2(self): #test exceptions assert_raises(ValueError, cheb.chebpts2, 1.5) diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py index 5708d936f..09b30a9e9 100644 --- a/numpy/polynomial/tests/test_classes.py +++ b/numpy/polynomial/tests/test_classes.py @@ -7,17 +7,17 @@ from __future__ import division, absolute_import, print_function import numpy as np from numpy.polynomial import ( - Polynomial, Legendre, Chebyshev, Laguerre, - Hermite, HermiteE) + Polynomial, Legendre, Chebyshev, Laguerre, + Hermite, HermiteE) from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite, dec) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite, dec) from numpy.testing.noseclasses import KnownFailure classes = ( - Polynomial, Legendre, Chebyshev, Laguerre, - Hermite, HermiteE) + Polynomial, Legendre, Chebyshev, Laguerre, + Hermite, HermiteE) def test_class_methods(): @@ -52,13 +52,12 @@ def test_class_methods(): yield check_trim, Poly - # # helper functions # - random = np.random.random + def assert_poly_almost_equal(p1, p2, msg=""): try: assert_(np.all(p1.domain == p2.domain)) @@ -113,7 +112,7 @@ def check_cast(Poly1, Poly2): # -def check_identity(Poly) : +def check_identity(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 x = np.linspace(d[0], d[1], 11) @@ -151,9 +150,9 @@ def check_fromroots(Poly): assert_almost_equal(p2.coef[-1], 1) -def check_fit(Poly) : +def check_fit(Poly): - def f(x) : + def f(x): return x*(x - 1)*(x - 2) x = np.linspace(0, 3) y = f(x) @@ -186,7 +185,7 @@ def check_fit(Poly) : assert_almost_equal(p1(x), p2(x)) -def check_equal(Poly) : +def check_equal(Poly): p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3]) p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3]) p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3]) @@ -197,7 +196,7 @@ def check_equal(Poly) : assert_(not p1 == p4) -def check_not_equal(Poly) : +def check_not_equal(Poly): p1 = Poly([1, 2, 3], domain=[0, 1], window=[2, 3]) p2 = Poly([1, 1, 1], domain=[0, 1], window=[2, 3]) p3 = Poly([1, 2, 3], domain=[1, 2], window=[2, 3]) @@ -208,7 +207,7 @@ def check_not_equal(Poly) : assert_(p1 != p4) -def check_add(Poly) : +def check_add(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -230,8 +229,7 @@ def check_add(Poly) : assert_raises(TypeError, p1.__add__, Polynomial([0])) - -def check_sub(Poly) : +def check_sub(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -253,7 +251,7 @@ def check_sub(Poly) : assert_raises(TypeError, p1.__sub__, Polynomial([0])) -def check_mul(Poly) : +def check_mul(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) p1 = Poly(c1) @@ -276,7 +274,7 @@ def check_mul(Poly) : assert_raises(TypeError, p1.__mul__, Polynomial([0])) -def check_floordiv(Poly) : +def check_floordiv(Poly): c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) c3 = list(random((2,)) + .5) @@ -294,15 +292,17 @@ def check_floordiv(Poly) : assert_poly_almost_equal(np.array(c4) // p2, p1) assert_poly_almost_equal(2 // p2, Poly([0])) assert_poly_almost_equal(p2 // 2, 0.5*p2) - assert_raises(TypeError, p1.__floordiv__, Poly([0], domain=Poly.domain + 1)) - assert_raises(TypeError, p1.__floordiv__, Poly([0], window=Poly.window + 1)) + assert_raises( + TypeError, p1.__floordiv__, Poly([0], domain=Poly.domain + 1)) + assert_raises( + TypeError, p1.__floordiv__, Poly([0], window=Poly.window + 1)) if Poly is Polynomial: assert_raises(TypeError, p1.__floordiv__, Chebyshev([0])) else: assert_raises(TypeError, p1.__floordiv__, Polynomial([0])) -def check_mod(Poly) : +def check_mod(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -329,7 +329,7 @@ def check_mod(Poly) : assert_raises(TypeError, p1.__mod__, Polynomial([0])) -def check_divmod(Poly) : +def check_divmod(Poly): # This checks commutation, not numerical correctness c1 = list(random((4,)) + .5) c2 = list(random((3,)) + .5) @@ -397,7 +397,7 @@ def check_copy(Poly): assert_(p1.window is not p2.window) -def check_integ(Poly) : +def check_integ(Poly): P = Polynomial # Check defaults p0 = Poly.cast(P([1*2, 2*3, 3*4])) @@ -456,19 +456,19 @@ def check_linspace(Poly): assert_almost_equal(yres, ytgt) -def check_pow(Poly) : +def check_pow(Poly): d = Poly.domain + random((2,))*.25 w = Poly.window + random((2,))*.25 tgt = Poly([1], domain=d, window=d) tst = Poly([1, 2, 3], domain=d, window=d) - for i in range(5) : + for i in range(5): assert_poly_almost_equal(tst**i, tgt) tgt = tgt * tst assert_raises(ValueError, tgt.__pow__, 1.5) assert_raises(ValueError, tgt.__pow__, -1) -def check_call(Poly) : +def check_call(Poly): P = Polynomial d = Poly.domain x = np.linspace(d[0], d[1], 11) @@ -480,7 +480,7 @@ def check_call(Poly) : assert_almost_equal(res, tgt) -def check_cutdeg(Poly) : +def check_cutdeg(Poly): p = Poly([1, 2, 3]) assert_raises(ValueError, p.cutdeg, .5) assert_raises(ValueError, p.cutdeg, -1) @@ -490,7 +490,7 @@ def check_cutdeg(Poly) : assert_equal(len(p.cutdeg(0)), 1) -def check_truncate(Poly) : +def check_truncate(Poly): p = Poly([1, 2, 3]) assert_raises(ValueError, p.truncate, .5) assert_raises(ValueError, p.truncate, 0) @@ -500,7 +500,7 @@ def check_truncate(Poly) : assert_equal(len(p.truncate(1)), 1) -def check_trim(Poly) : +def check_trim(Poly): c = [1, 1e-6, 1e-12, 0] p = Poly(c) assert_equal(p.trim().coef, c[:3]) @@ -508,7 +508,7 @@ def check_trim(Poly) : assert_equal(p.trim(1e-5).coef, c[:1]) -def check_mapparms(Poly) : +def check_mapparms(Poly): # check with defaults. Should be identity. d = Poly.domain w = Poly.window diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py index 978c9c79b..ac60007d1 100644 --- a/numpy/polynomial/tests/test_hermite.py +++ b/numpy/polynomial/tests/test_hermite.py @@ -7,48 +7,48 @@ import numpy as np import numpy.polynomial.hermite as herm from numpy.polynomial.polynomial import polyval from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) - -H0 = np.array([ 1]) -H1 = np.array([0, 2]) -H2 = np.array([ -2, 0, 4]) -H3 = np.array([0, -12, 0, 8]) -H4 = np.array([ 12, 0, -48, 0, 16]) -H5 = np.array([0, 120, 0, -160, 0, 32]) -H6 = np.array([-120, 0, 720, 0, -480, 0, 64]) -H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128]) + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) + +H0 = np.array([1]) +H1 = np.array([0, 2]) +H2 = np.array([-2, 0, 4]) +H3 = np.array([0, -12, 0, 8]) +H4 = np.array([12, 0, -48, 0, 16]) +H5 = np.array([0, 120, 0, -160, 0, 32]) +H6 = np.array([-120, 0, 720, 0, -480, 0, 64]) +H7 = np.array([0, -1680, 0, 3360, 0, -1344, 0, 128]) H8 = np.array([1680, 0, -13440, 0, 13440, 0, -3584, 0, 256]) H9 = np.array([0, 30240, 0, -80640, 0, 48384, 0, -9216, 0, 512]) Hlist = [H0, H1, H2, H3, H4, H5, H6, H7, H8, H9] -def trim(x) : +def trim(x): return herm.hermtrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_hermdomain(self) : + def test_hermdomain(self): assert_equal(herm.hermdomain, [-1, 1]) - def test_hermzero(self) : + def test_hermzero(self): assert_equal(herm.hermzero, [0]) - def test_hermone(self) : + def test_hermone(self): assert_equal(herm.hermone, [1]) - def test_hermx(self) : + def test_hermx(self): assert_equal(herm.hermx, [0, .5]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-3, 3, 100) - def test_hermadd(self) : - for i in range(5) : - for j in range(5) : + def test_hermadd(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 @@ -56,9 +56,9 @@ class TestArithmetic(TestCase) : res = herm.hermadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_hermsub(self) : - for i in range(5) : - for j in range(5) : + def test_hermsub(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 @@ -74,12 +74,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [i, 0, .5] assert_equal(herm.hermmulx(ser), tgt) - def test_hermmul(self) : + def test_hermmul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = herm.hermval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = herm.hermval(self.x, pol2) @@ -88,9 +88,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_hermdiv(self) : - for i in range(5) : - for j in range(5) : + def test_hermdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -100,7 +100,7 @@ class TestArithmetic(TestCase) : assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2.5, 1., .75]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -110,15 +110,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - - def test_hermval(self) : + def test_hermval(self): #check empty input assert_equal(herm.hermval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Hlist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -126,7 +125,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(herm.hermval(x, [1]).shape, dims) @@ -196,9 +195,9 @@ class TestEvaluation(TestCase) : assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_hermint(self) : + def test_hermint(self): # check exceptions assert_raises(ValueError, herm.hermint, [0], .5) assert_raises(ValueError, herm.hermint, [0], -1) @@ -211,7 +210,7 @@ class TestIntegral(TestCase) : assert_almost_equal(res, [0, .5]) # 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] @@ -221,7 +220,7 @@ 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] hermpol = herm.poly2herm(pol) @@ -229,7 +228,7 @@ class TestIntegral(TestCase) : assert_almost_equal(herm.hermval(-1, hermint), 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] @@ -239,41 +238,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 = herm.hermint(tgt, m=1) res = herm.hermint(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 = herm.hermint(tgt, m=1, k=[k]) res = herm.hermint(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 = herm.hermint(tgt, m=1, k=[k], lbnd=-1) res = herm.hermint(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 = herm.hermint(tgt, m=1, k=[k], scl=2) res = herm.hermint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -295,29 +294,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_hermder(self) : + def test_hermder(self): # check exceptions assert_raises(ValueError, herm.hermder, [0], .5) assert_raises(ValueError, herm.hermder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = herm.hermder(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 = herm.hermder(herm.hermint(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 = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -339,13 +338,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_hermvander(self) : + def test_hermvander(self): # check for 1d x x = np.arange(3) v = herm.hermvander(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], herm.hermval(x, coef)) @@ -353,11 +351,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = herm.hermvander(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], herm.hermval(x, coef)) - def test_hermvander2d(self) : + def test_hermvander2d(self): # also tests hermval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -370,8 +368,7 @@ class TestVander(TestCase): van = herm.hermvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_hermvander3d(self) : + def test_hermvander3d(self): # also tests hermval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -387,8 +384,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_hermfit(self) : - def f(x) : + def test_hermfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -465,12 +462,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_hermfromroots(self) : + def test_hermfromroots(self): res = herm.hermfromroots([]) 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]) pol = herm.hermfromroots(roots) res = herm.hermval(roots, pol) @@ -479,15 +476,15 @@ class TestMisc(TestCase) : assert_almost_equal(herm.herm2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_hermroots(self) : + def test_hermroots(self): assert_almost_equal(herm.hermroots([1]), []) assert_almost_equal(herm.hermroots([1, 1]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = herm.hermroots(herm.hermfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_hermtrim(self) : + def test_hermtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -498,15 +495,15 @@ class TestMisc(TestCase) : assert_equal(herm.hermtrim(coef, 1), coef[:-3]) assert_equal(herm.hermtrim(coef, 2), [0]) - def test_hermline(self) : + def test_hermline(self): assert_equal(herm.hermline(3, 4), [3, 2]) - def test_herm2poly(self) : - for i in range(10) : + def test_herm2poly(self): + for i in range(10): assert_almost_equal(herm.herm2poly([0]*i + [1]), Hlist[i]) - def test_poly2herm(self) : - for i in range(10) : + def test_poly2herm(self): + for i in range(10): assert_almost_equal(herm.poly2herm(Hlist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py index b27e8576b..5341dc7ff 100644 --- a/numpy/polynomial/tests/test_hermite_e.py +++ b/numpy/polynomial/tests/test_hermite_e.py @@ -8,44 +8,45 @@ import numpy.polynomial.hermite_e as herme from numpy.polynomial.polynomial import polyval from numpy.testing import * -He0 = np.array([ 1 ]) -He1 = np.array([ 0, 1 ]) -He2 = np.array([ -1, 0, 1 ]) -He3 = np.array([ 0, -3, 0, 1 ]) -He4 = np.array([ 3, 0, -6, 0, 1 ]) -He5 = np.array([ 0, 15, 0, -10, 0, 1 ]) -He6 = np.array([ -15, 0, 45, 0, -15, 0, 1 ]) -He7 = np.array([ 0, -105, 0, 105, 0, -21, 0, 1 ]) -He8 = np.array([ 105, 0, -420, 0, 210, 0, -28, 0, 1 ]) -He9 = np.array([ 0, 945, 0, -1260, 0, 378, 0, -36, 0, 1 ]) +He0 = np.array([1]) +He1 = np.array([0, 1]) +He2 = np.array([-1, 0, 1]) +He3 = np.array([0, -3, 0, 1]) +He4 = np.array([3, 0, -6, 0, 1]) +He5 = np.array([0, 15, 0, -10, 0, 1]) +He6 = np.array([-15, 0, 45, 0, -15, 0, 1]) +He7 = np.array([0, -105, 0, 105, 0, -21, 0, 1]) +He8 = np.array([105, 0, -420, 0, 210, 0, -28, 0, 1]) +He9 = np.array([0, 945, 0, -1260, 0, 378, 0, -36, 0, 1]) Helist = [He0, He1, He2, He3, He4, He5, He6, He7, He8, He9] -def trim(x) : + +def trim(x): return herme.hermetrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_hermedomain(self) : + def test_hermedomain(self): assert_equal(herme.hermedomain, [-1, 1]) - def test_hermezero(self) : + def test_hermezero(self): assert_equal(herme.hermezero, [0]) - def test_hermeone(self) : + def test_hermeone(self): assert_equal(herme.hermeone, [1]) - def test_hermex(self) : + def test_hermex(self): assert_equal(herme.hermex, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-3, 3, 100) - def test_hermeadd(self) : - for i in range(5) : - for j in range(5) : + def test_hermeadd(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 = herme.hermeadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_hermesub(self) : - for i in range(5) : - for j in range(5) : + def test_hermesub(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,12 +72,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [i, 0, 1] assert_equal(herme.hermemulx(ser), tgt) - def test_hermemul(self) : + def test_hermemul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = herme.hermeval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = herme.hermeval(self.x, pol2) @@ -85,9 +86,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_hermediv(self) : - for i in range(5) : - for j in range(5) : + def test_hermediv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -97,7 +98,7 @@ class TestArithmetic(TestCase) : assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([4., 2., 3.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -107,15 +108,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - - def test_hermeval(self) : + def test_hermeval(self): #check empty input assert_equal(herme.hermeval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Helist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -123,7 +123,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(herme.hermeval(x, [1]).shape, dims) @@ -195,7 +195,7 @@ class TestEvaluation(TestCase) : class TestIntegral(TestCase): - def test_hermeint(self) : + def test_hermeint(self): # check exceptions assert_raises(ValueError, herme.hermeint, [0], .5) assert_raises(ValueError, herme.hermeint, [0], -1) @@ -208,7 +208,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] @@ -218,7 +218,7 @@ 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] hermepol = herme.poly2herme(pol) @@ -226,7 +226,7 @@ class TestIntegral(TestCase): assert_almost_equal(herme.hermeval(-1, hermeint), 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] @@ -236,41 +236,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 = herme.hermeint(tgt, m=1) res = herme.hermeint(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 = herme.hermeint(tgt, m=1, k=[k]) res = herme.hermeint(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 = herme.hermeint(tgt, m=1, k=[k], lbnd=-1) res = herme.hermeint(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 = herme.hermeint(tgt, m=1, k=[k], scl=2) res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -292,31 +292,32 @@ class TestIntegral(TestCase): assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_hermeder(self) : + def test_hermeder(self): # check exceptions assert_raises(ValueError, herme.hermeder, [0], .5) assert_raises(ValueError, herme.hermeder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = herme.hermeder(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 = herme.hermeder(herme.hermeint(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 = herme.hermeder(herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5) + res = herme.hermeder( + herme.hermeint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) def test_hermeder_axis(self): @@ -336,13 +337,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_hermevander(self) : + def test_hermevander(self): # check for 1d x x = np.arange(3) v = herme.hermevander(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], herme.hermeval(x, coef)) @@ -350,11 +350,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = herme.hermevander(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], herme.hermeval(x, coef)) - def test_hermevander2d(self) : + def test_hermevander2d(self): # also tests hermeval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -367,8 +367,7 @@ class TestVander(TestCase): van = herme.hermevander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_hermevander3d(self) : + def test_hermevander3d(self): # also tests hermeval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -384,8 +383,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_hermefit(self) : - def f(x) : + def test_hermefit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -462,12 +461,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_hermefromroots(self) : + def test_hermefromroots(self): res = herme.hermefromroots([]) 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]) pol = herme.hermefromroots(roots) res = herme.hermeval(roots, pol) @@ -476,15 +475,15 @@ class TestMisc(TestCase) : assert_almost_equal(herme.herme2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_hermeroots(self) : + def test_hermeroots(self): assert_almost_equal(herme.hermeroots([1]), []) assert_almost_equal(herme.hermeroots([1, 1]), [-1]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = herme.hermeroots(herme.hermefromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_hermetrim(self) : + def test_hermetrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -495,15 +494,15 @@ class TestMisc(TestCase) : assert_equal(herme.hermetrim(coef, 1), coef[:-3]) assert_equal(herme.hermetrim(coef, 2), [0]) - def test_hermeline(self) : + def test_hermeline(self): assert_equal(herme.hermeline(3, 4), [3, 4]) - def test_herme2poly(self) : - for i in range(10) : + def test_herme2poly(self): + for i in range(10): assert_almost_equal(herme.herme2poly([0]*i + [1]), Helist[i]) - def test_poly2herme(self) : - for i in range(10) : + def test_poly2herme(self): + for i in range(10): assert_almost_equal(herme.poly2herme(Helist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py index d42bac67c..b3d8fe5ee 100644 --- a/numpy/polynomial/tests/test_laguerre.py +++ b/numpy/polynomial/tests/test_laguerre.py @@ -7,44 +7,45 @@ import numpy as np import numpy.polynomial.laguerre as lag from numpy.polynomial.polynomial import polyval 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) -L0 = np.array([1 ])/1 -L1 = np.array([1, -1 ])/1 -L2 = np.array([2, -4, 1 ])/2 -L3 = np.array([6, -18, 9, -1 ])/6 -L4 = np.array([24, -96, 72, -16, 1 ])/24 -L5 = np.array([120, -600, 600, -200, 25, -1 ])/120 -L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1 ])/720 +L0 = np.array([1])/1 +L1 = np.array([1, -1])/1 +L2 = np.array([2, -4, 1])/2 +L3 = np.array([6, -18, 9, -1])/6 +L4 = np.array([24, -96, 72, -16, 1])/24 +L5 = np.array([120, -600, 600, -200, 25, -1])/120 +L6 = np.array([720, -4320, 5400, -2400, 450, -36, 1])/720 Llist = [L0, L1, L2, L3, L4, L5, L6] -def trim(x) : + +def trim(x): return lag.lagtrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_lagdomain(self) : + def test_lagdomain(self): assert_equal(lag.lagdomain, [0, 1]) - def test_lagzero(self) : + def test_lagzero(self): assert_equal(lag.lagzero, [0]) - def test_lagone(self) : + def test_lagone(self): assert_equal(lag.lagone, [1]) - def test_lagx(self) : + def test_lagx(self): assert_equal(lag.lagx, [1, -1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-3, 3, 100) - def test_lagadd(self) : - for i in range(5) : - for j in range(5) : + def test_lagadd(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 @@ -52,9 +53,9 @@ class TestArithmetic(TestCase) : res = lag.lagadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_lagsub(self) : - for i in range(5) : - for j in range(5) : + def test_lagsub(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 @@ -70,12 +71,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [-i, 2*i + 1, -(i + 1)] assert_almost_equal(lag.lagmulx(ser), tgt) - def test_lagmul(self) : + def test_lagmul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = lag.lagval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = lag.lagval(self.x, pol2) @@ -84,9 +85,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_lagdiv(self) : - for i in range(5) : - for j in range(5) : + def test_lagdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -96,7 +97,7 @@ class TestArithmetic(TestCase) : assert_almost_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([9., -14., 6.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -106,14 +107,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - def test_lagval(self) : + def test_lagval(self): #check empty input assert_equal(lag.lagval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Llist] - for i in range(7) : + for i in range(7): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -121,7 +122,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(lag.lagval(x, [1]).shape, dims) @@ -191,9 +192,9 @@ class TestEvaluation(TestCase) : assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_lagint(self) : + def test_lagint(self): # check exceptions assert_raises(ValueError, lag.lagint, [0], .5) assert_raises(ValueError, lag.lagint, [0], -1) @@ -206,7 +207,7 @@ class TestIntegral(TestCase) : assert_almost_equal(res, [1, -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] @@ -216,7 +217,7 @@ 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] lagpol = lag.poly2lag(pol) @@ -224,7 +225,7 @@ class TestIntegral(TestCase) : assert_almost_equal(lag.lagval(-1, lagint), 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] @@ -234,41 +235,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 = lag.lagint(tgt, m=1) res = lag.lagint(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 = lag.lagint(tgt, m=1, k=[k]) res = lag.lagint(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 = lag.lagint(tgt, m=1, k=[k], lbnd=-1) res = lag.lagint(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 = lag.lagint(tgt, m=1, k=[k], scl=2) res = lag.lagint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -290,29 +291,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_lagder(self) : + def test_lagder(self): # check exceptions assert_raises(ValueError, lag.lagder, [0], .5) assert_raises(ValueError, lag.lagder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = lag.lagder(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 = lag.lagder(lag.lagint(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 = lag.lagder(lag.lagint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -334,13 +335,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_lagvander(self) : + def test_lagvander(self): # check for 1d x x = np.arange(3) v = lag.lagvander(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], lag.lagval(x, coef)) @@ -348,11 +348,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = lag.lagvander(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], lag.lagval(x, coef)) - def test_lagvander2d(self) : + def test_lagvander2d(self): # also tests lagval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -365,8 +365,7 @@ class TestVander(TestCase): van = lag.lagvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - - def test_lagvander3d(self) : + def test_lagvander3d(self): # also tests lagval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -382,8 +381,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_lagfit(self) : - def f(x) : + def test_lagfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -460,12 +459,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_lagfromroots(self) : + def test_lagfromroots(self): res = lag.lagfromroots([]) 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]) pol = lag.lagfromroots(roots) res = lag.lagval(roots, pol) @@ -474,15 +473,15 @@ class TestMisc(TestCase) : assert_almost_equal(lag.lag2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_lagroots(self) : + def test_lagroots(self): assert_almost_equal(lag.lagroots([1]), []) assert_almost_equal(lag.lagroots([0, 1]), [1]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(0, 3, i) res = lag.lagroots(lag.lagfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_lagtrim(self) : + def test_lagtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -493,15 +492,15 @@ class TestMisc(TestCase) : assert_equal(lag.lagtrim(coef, 1), coef[:-3]) assert_equal(lag.lagtrim(coef, 2), [0]) - def test_lagline(self) : + def test_lagline(self): assert_equal(lag.lagline(3, 4), [7, -4]) - def test_lag2poly(self) : - for i in range(7) : + def test_lag2poly(self): + for i in range(7): assert_almost_equal(lag.lag2poly([0]*i + [1]), Llist[i]) - def test_poly2lag(self) : - for i in range(7) : + def test_poly2lag(self): + for i in range(7): assert_almost_equal(lag.poly2lag(Llist[i]), [0]*i + [1]) def test_weight(self): diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py index ae7a85851..e248f005d 100644 --- a/numpy/polynomial/tests/test_legendre.py +++ b/numpy/polynomial/tests/test_legendre.py @@ -7,47 +7,48 @@ import numpy as np import numpy.polynomial.legendre as leg from numpy.polynomial.polynomial import polyval from numpy.testing import ( - TestCase, assert_almost_equal, assert_raises, - assert_equal, assert_, run_module_suite) - -L0 = np.array([ 1]) -L1 = np.array([ 0, 1]) -L2 = np.array([-1, 0, 3])/2 -L3 = np.array([ 0, -3, 0, 5])/2 -L4 = np.array([ 3, 0, -30, 0, 35])/8 -L5 = np.array([ 0, 15, 0, -70, 0, 63])/8 -L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16 -L7 = np.array([ 0, -35, 0, 315, 0, -693, 0, 429])/16 -L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128 -L9 = np.array([ 0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128 + TestCase, assert_almost_equal, assert_raises, + assert_equal, assert_, run_module_suite) + +L0 = np.array([1]) +L1 = np.array([0, 1]) +L2 = np.array([-1, 0, 3])/2 +L3 = np.array([0, -3, 0, 5])/2 +L4 = np.array([3, 0, -30, 0, 35])/8 +L5 = np.array([0, 15, 0, -70, 0, 63])/8 +L6 = np.array([-5, 0, 105, 0, -315, 0, 231])/16 +L7 = np.array([0, -35, 0, 315, 0, -693, 0, 429])/16 +L8 = np.array([35, 0, -1260, 0, 6930, 0, -12012, 0, 6435])/128 +L9 = np.array([0, 315, 0, -4620, 0, 18018, 0, -25740, 0, 12155])/128 Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9] -def trim(x) : + +def trim(x): return leg.legtrim(x, tol=1e-6) -class TestConstants(TestCase) : +class TestConstants(TestCase): - def test_legdomain(self) : + def test_legdomain(self): assert_equal(leg.legdomain, [-1, 1]) - def test_legzero(self) : + def test_legzero(self): assert_equal(leg.legzero, [0]) - def test_legone(self) : + def test_legone(self): assert_equal(leg.legone, [1]) - def test_legx(self) : + def test_legx(self): assert_equal(leg.legx, [0, 1]) -class TestArithmetic(TestCase) : +class TestArithmetic(TestCase): x = np.linspace(-1, 1, 100) - def test_legadd(self) : - for i in range(5) : - for j in range(5) : + def test_legadd(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 @@ -55,9 +56,9 @@ class TestArithmetic(TestCase) : res = leg.legadd([0]*i + [1], [0]*j + [1]) assert_equal(trim(res), trim(tgt), err_msg=msg) - def test_legsub(self) : - for i in range(5) : - for j in range(5) : + def test_legsub(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 @@ -74,12 +75,12 @@ class TestArithmetic(TestCase) : tgt = [0]*(i - 1) + [i/tmp, 0, (i + 1)/tmp] assert_equal(leg.legmulx(ser), tgt) - def test_legmul(self) : + def test_legmul(self): # check values of result - for i in range(5) : + for i in range(5): pol1 = [0]*i + [1] val1 = leg.legval(self.x, pol1) - for j in range(5) : + for j in range(5): msg = "At i=%d, j=%d" % (i, j) pol2 = [0]*j + [1] val2 = leg.legval(self.x, pol2) @@ -88,9 +89,9 @@ class TestArithmetic(TestCase) : assert_(len(pol3) == i + j + 1, msg) assert_almost_equal(val3, val1*val2, err_msg=msg) - def test_legdiv(self) : - for i in range(5) : - for j in range(5) : + def test_legdiv(self): + for i in range(5): + for j in range(5): msg = "At i=%d, j=%d" % (i, j) ci = [0]*i + [1] cj = [0]*j + [1] @@ -100,7 +101,7 @@ class TestArithmetic(TestCase) : assert_equal(trim(res), trim(tgt), err_msg=msg) -class TestEvaluation(TestCase) : +class TestEvaluation(TestCase): # coefficients of 1 + 2*x + 3*x**2 c1d = np.array([2., 2., 2.]) c2d = np.einsum('i,j->ij', c1d, c1d) @@ -110,14 +111,14 @@ class TestEvaluation(TestCase) : x = np.random.random((3, 5))*2 - 1 y = polyval(x, [1., 2., 3.]) - def test_legval(self) : + def test_legval(self): #check empty input assert_equal(leg.legval([], [1]).size, 0) #check normal input) x = np.linspace(-1, 1) y = [polyval(x, c) for c in Llist] - for i in range(10) : + for i in range(10): msg = "At i=%d" % i ser = np.zeros tgt = y[i] @@ -125,7 +126,7 @@ class TestEvaluation(TestCase) : assert_almost_equal(res, tgt, err_msg=msg) #check that shape is preserved - for i in range(3) : + for i in range(3): dims = [2]*i x = np.zeros(dims) assert_equal(leg.legval(x, [1]).shape, dims) @@ -195,9 +196,9 @@ class TestEvaluation(TestCase) : assert_(res.shape == (2, 3)*3) -class TestIntegral(TestCase) : +class TestIntegral(TestCase): - def test_legint(self) : + def test_legint(self): # check exceptions assert_raises(ValueError, leg.legint, [0], .5) assert_raises(ValueError, leg.legint, [0], -1) @@ -210,7 +211,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] @@ -220,7 +221,7 @@ 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] legpol = leg.poly2leg(pol) @@ -228,7 +229,7 @@ class TestIntegral(TestCase) : assert_almost_equal(leg.legval(-1, legint), 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] @@ -238,41 +239,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 = leg.legint(tgt, m=1) res = leg.legint(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 = leg.legint(tgt, m=1, k=[k]) res = leg.legint(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 = leg.legint(tgt, m=1, k=[k], lbnd=-1) res = leg.legint(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 = leg.legint(tgt, m=1, k=[k], scl=2) res = leg.legint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) @@ -294,29 +295,29 @@ class TestIntegral(TestCase) : assert_almost_equal(res, tgt) -class TestDerivative(TestCase) : +class TestDerivative(TestCase): - def test_legder(self) : + def test_legder(self): # check exceptions assert_raises(ValueError, leg.legder, [0], .5) assert_raises(ValueError, leg.legder, [0], -1) # check that zeroth deriviative does nothing - for i in range(5) : + for i in range(5): tgt = [0]*i + [1] res = leg.legder(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 = leg.legder(leg.legint(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 = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5) assert_almost_equal(trim(res), trim(tgt)) @@ -338,13 +339,12 @@ class TestVander(TestCase): # some random values in [-1, 1) x = np.random.random((3, 5))*2 - 1 - - def test_legvander(self) : + def test_legvander(self): # check for 1d x x = np.arange(3) v = leg.legvander(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], leg.legval(x, coef)) @@ -352,11 +352,11 @@ class TestVander(TestCase): x = np.array([[1, 2], [3, 4], [5, 6]]) v = leg.legvander(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], leg.legval(x, coef)) - def test_legvander2d(self) : + def test_legvander2d(self): # also tests polyval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) @@ -369,7 +369,7 @@ class TestVander(TestCase): van = leg.legvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6)) - def test_legvander3d(self) : + def test_legvander3d(self): # also tests polyval3d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3, 4)) @@ -385,8 +385,8 @@ class TestVander(TestCase): class TestFitting(TestCase): - def test_legfit(self) : - def f(x) : + def test_legfit(self): + def f(x): return x*(x - 1)*(x - 2) # Test exceptions @@ -463,12 +463,12 @@ class TestGauss(TestCase): assert_almost_equal(w.sum(), tgt) -class TestMisc(TestCase) : +class TestMisc(TestCase): - def test_legfromroots(self) : + def test_legfromroots(self): res = leg.legfromroots([]) 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]) pol = leg.legfromroots(roots) res = leg.legval(roots, pol) @@ -477,15 +477,15 @@ class TestMisc(TestCase) : assert_almost_equal(leg.leg2poly(pol)[-1], 1) assert_almost_equal(res, tgt) - def test_legroots(self) : + def test_legroots(self): assert_almost_equal(leg.legroots([1]), []) assert_almost_equal(leg.legroots([1, 2]), [-.5]) - for i in range(2, 5) : + for i in range(2, 5): tgt = np.linspace(-1, 1, i) res = leg.legroots(leg.legfromroots(tgt)) assert_almost_equal(trim(res), trim(tgt)) - def test_legtrim(self) : + def test_legtrim(self): coef = [2, -1, 1, 0] # Test exceptions @@ -496,15 +496,15 @@ class TestMisc(TestCase) : assert_equal(leg.legtrim(coef, 1), coef[:-3]) assert_equal(leg.legtrim(coef, 2), [0]) - def test_legline(self) : + def test_legline(self): assert_equal(leg.legline(3, 4), [3, 4]) - def test_leg2poly(self) : - for i in range(10) : + def test_leg2poly(self): + for i in range(10): assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i]) - def test_poly2leg(self) : - for i in range(10) : + def test_poly2leg(self): + for i in range(10): assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1]) def test_weight(self): 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]) diff --git a/numpy/polynomial/tests/test_polyutils.py b/numpy/polynomial/tests/test_polyutils.py index 11b37aa1a..c77ee2435 100644 --- a/numpy/polynomial/tests/test_polyutils.py +++ b/numpy/polynomial/tests/test_polyutils.py @@ -7,30 +7,31 @@ import numpy as np import numpy.polynomial.polyutils as pu from numpy.testing import * -class TestMisc(TestCase) : - def test_trimseq(self) : - for i in range(5) : +class TestMisc(TestCase): + + def test_trimseq(self): + for i in range(5): tgt = [1] res = pu.trimseq([1] + [0]*5) assert_equal(res, tgt) - def test_as_series(self) : + def test_as_series(self): # check exceptions assert_raises(ValueError, pu.as_series, [[]]) assert_raises(ValueError, pu.as_series, [[[1, 2]]]) assert_raises(ValueError, pu.as_series, [[1], ['a']]) # check common types types = ['i', 'd', 'O'] - for i in range(len(types)) : - for j in range(i) : + for i in range(len(types)): + for j in range(i): ci = np.ones(1, types[i]) cj = np.ones(1, types[j]) [resi, resj] = pu.as_series([ci, cj]) assert_(resi.dtype.char == resj.dtype.char) assert_(resj.dtype.char == types[i]) - def test_trimcoef(self) : + def test_trimcoef(self): coef = [2, -1, 1, 0] # Test exceptions assert_raises(ValueError, pu.trimcoef, coef, -1) @@ -40,9 +41,9 @@ class TestMisc(TestCase) : assert_equal(pu.trimcoef(coef, 2), [0]) -class TestDomain(TestCase) : +class TestDomain(TestCase): - def test_getdomain(self) : + def test_getdomain(self): # test for real values x = [1, 10, 3, -1] tgt = [-1, 10] @@ -55,7 +56,7 @@ class TestDomain(TestCase) : res = pu.getdomain(x) assert_almost_equal(res, tgt) - def test_mapdomain(self) : + def test_mapdomain(self): # test for real values dom1 = [0, 4] dom2 = [1, 3] @@ -86,7 +87,7 @@ class TestDomain(TestCase) : res = pu.mapdomain(x, dom1, dom2) assert_(isinstance(res, np.matrix)) - def test_mapparms(self) : + def test_mapparms(self): # test for real values dom1 = [0, 4] dom2 = [1, 3] diff --git a/numpy/polynomial/tests/test_printing.py b/numpy/polynomial/tests/test_printing.py index 883597cb9..86cd25732 100644 --- a/numpy/polynomial/tests/test_printing.py +++ b/numpy/polynomial/tests/test_printing.py @@ -3,37 +3,33 @@ from __future__ import division, absolute_import, print_function import numpy.polynomial as poly from numpy.testing import TestCase, run_module_suite, assert_ + class test_str(TestCase): def test_polynomial_str(self): res = str(poly.Polynomial([0, 1])) tgt = 'poly([0., 1.])' assert_(res, tgt) - def test_chebyshev_str(self): res = str(poly.Chebyshev([0, 1])) tgt = 'leg([0., 1.])' assert_(res, tgt) - def test_legendre_str(self): res = str(poly.Legendre([0, 1])) tgt = 'leg([0., 1.])' assert_(res, tgt) - def test_hermite_str(self): res = str(poly.Hermite([0, 1])) tgt = 'herm([0., 1.])' assert_(res, tgt) - def test_hermiteE_str(self): res = str(poly.HermiteE([0, 1])) tgt = 'herme([0., 1.])' assert_(res, tgt) - def test_laguerre_str(self): res = str(poly.Laguerre([0, 1])) tgt = 'lag([0., 1.])' @@ -46,31 +42,26 @@ class test_repr(TestCase): tgt = 'Polynomial([0., 1.])' assert_(res, tgt) - def test_chebyshev_str(self): res = repr(poly.Chebyshev([0, 1])) tgt = 'Chebyshev([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_legendre_repr(self): res = repr(poly.Legendre([0, 1])) tgt = 'Legendre([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_hermite_repr(self): res = repr(poly.Hermite([0, 1])) tgt = 'Hermite([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_hermiteE_repr(self): res = repr(poly.HermiteE([0, 1])) tgt = 'HermiteE([0., 1.], [-1., 1.], [-1., 1.])' assert_(res, tgt) - def test_laguerre_repr(self): res = repr(poly.Laguerre([0, 1])) tgt = 'Laguerre([0., 1.], [0., 1.], [0., 1.])' |