summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/polynomial/tests/test_chebyshev.py422
-rw-r--r--numpy/polynomial/tests/test_hermite.py234
-rw-r--r--numpy/polynomial/tests/test_hermite_e.py234
-rw-r--r--numpy/polynomial/tests/test_laguerre.py230
-rw-r--r--numpy/polynomial/tests/test_legendre.py256
-rw-r--r--numpy/polynomial/tests/test_polynomial.py182
6 files changed, 1163 insertions, 395 deletions
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py
index 3f4102db3..f0f2748b2 100644
--- a/numpy/polynomial/tests/test_chebyshev.py
+++ b/numpy/polynomial/tests/test_chebyshev.py
@@ -4,11 +4,12 @@
from __future__ import division
import numpy as np
-import numpy.polynomial.chebyshev as ch
+import numpy.polynomial.chebyshev as cheb
+from numpy.polynomial.polynomial import polyval
from numpy.testing import *
def trim(x) :
- return ch.chebtrim(x, tol=1e-6)
+ return cheb.chebtrim(x, tol=1e-6)
T0 = [ 1]
T1 = [ 0, 1]
@@ -30,30 +31,30 @@ class TestPrivate(TestCase) :
for i in range(5) :
inp = np.array([2] + [1]*i, np.double)
tgt = np.array([.5]*i + [2] + [.5]*i, np.double)
- res = ch._cseries_to_zseries(inp)
+ res = cheb._cseries_to_zseries(inp)
assert_equal(res, tgt)
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 = ch._zseries_to_cseries(inp)
+ res = cheb._zseries_to_cseries(inp)
assert_equal(res, tgt)
class TestConstants(TestCase) :
def test_chebdomain(self) :
- assert_equal(ch.chebdomain, [-1, 1])
+ assert_equal(cheb.chebdomain, [-1, 1])
def test_chebzero(self) :
- assert_equal(ch.chebzero, [0])
+ assert_equal(cheb.chebzero, [0])
def test_chebone(self) :
- assert_equal(ch.chebone, [1])
+ assert_equal(cheb.chebone, [1])
def test_chebx(self) :
- assert_equal(ch.chebx, [0, 1])
+ assert_equal(cheb.chebx, [0, 1])
class TestArithmetic(TestCase) :
@@ -65,7 +66,7 @@ class TestArithmetic(TestCase) :
tgt = np.zeros(max(i,j) + 1)
tgt[i] += 1
tgt[j] += 1
- res = ch.chebadd([0]*i + [1], [0]*j + [1])
+ res = cheb.chebadd([0]*i + [1], [0]*j + [1])
assert_equal(trim(res), trim(tgt), err_msg=msg)
def test_chebsub(self) :
@@ -75,16 +76,16 @@ class TestArithmetic(TestCase) :
tgt = np.zeros(max(i,j) + 1)
tgt[i] += 1
tgt[j] -= 1
- res = ch.chebsub([0]*i + [1], [0]*j + [1])
+ res = cheb.chebsub([0]*i + [1], [0]*j + [1])
assert_equal(trim(res), trim(tgt), err_msg=msg)
def test_chebmulx(self):
- assert_equal(ch.chebmulx([0]), [0])
- assert_equal(ch.chebmulx([1]), [0,1])
+ assert_equal(cheb.chebmulx([0]), [0])
+ assert_equal(cheb.chebmulx([1]), [0,1])
for i in range(1, 5):
ser = [0]*i + [1]
tgt = [0]*(i - 1) + [.5, 0, .5]
- assert_equal(ch.chebmulx(ser), tgt)
+ assert_equal(cheb.chebmulx(ser), tgt)
def test_chebmul(self) :
for i in range(5) :
@@ -93,7 +94,7 @@ class TestArithmetic(TestCase) :
tgt = np.zeros(i + j + 1)
tgt[i + j] += .5
tgt[abs(i - j)] += .5
- res = ch.chebmul([0]*i + [1], [0]*j + [1])
+ res = cheb.chebmul([0]*i + [1], [0]*j + [1])
assert_equal(trim(res), trim(tgt), err_msg=msg)
def test_chebdiv(self) :
@@ -102,56 +103,119 @@ class TestArithmetic(TestCase) :
msg = "At i=%d, j=%d" % (i,j)
ci = [0]*i + [1]
cj = [0]*j + [1]
- tgt = ch.chebadd(ci, cj)
- quo, rem = ch.chebdiv(tgt, ci)
- res = ch.chebadd(ch.chebmul(quo, ci), rem)
+ tgt = cheb.chebadd(ci, cj)
+ quo, rem = cheb.chebdiv(tgt, ci)
+ res = cheb.chebadd(cheb.chebmul(quo, ci), rem)
assert_equal(trim(res), trim(tgt), err_msg=msg)
- def test_chebval(self) :
- def f(x) :
- return x*(x**2 - 1)
+class TestEvaluation(TestCase):
+ # coefficients of 1 + 2*x + 3*x**2
+ c1d = np.array([2.5, 2., 1.5])
+ c2d = np.einsum('i,j->ij', c1d, c1d)
+ c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
+
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+ y = polyval(x, [1., 2., 3.])
+
+
+ def test_chebval(self) :
#check empty input
- assert_equal(ch.chebval([], [1]).size, 0)
+ assert_equal(cheb.chebval([], [1]).size, 0)
#check normal input)
- for i in range(5) :
- tgt = 1
- res = ch.chebval(1, [0]*i + [1])
- assert_almost_equal(res, tgt)
- tgt = (-1)**i
- res = ch.chebval(-1, [0]*i + [1])
- assert_almost_equal(res, tgt)
- zeros = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
- tgt = 0
- res = ch.chebval(zeros, [0]*i + [1])
- assert_almost_equal(res, tgt)
x = np.linspace(-1,1)
- tgt = f(x)
- res = ch.chebval(x, [0, -.25, 0, .25])
- assert_almost_equal(res, tgt)
+ y = [polyval(x, c) for c in Tlist]
+ 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) :
dims = [2]*i
x = np.zeros(dims)
- assert_equal(ch.chebval(x, [1]).shape, dims)
- assert_equal(ch.chebval(x, [1,0]).shape, dims)
- assert_equal(ch.chebval(x, [1,0,0]).shape, dims)
+ assert_equal(cheb.chebval(x, [1]).shape, dims)
+ assert_equal(cheb.chebval(x, [1,0]).shape, dims)
+ assert_equal(cheb.chebval(x, [1,0,0]).shape, dims)
+
+ def test_chebval2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, cheb.chebval2d, x1, x2[:2], self.c2d)
+
+ #test values
+ tgt = y1*y2
+ res = cheb.chebval2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = cheb.chebval2d(z, z, self.c2d)
+ assert_(res.shape == (2,3))
+
+ def test_chebval3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, cheb.chebval3d, x1, x2, x3[:2], self.c3d)
+ #test values
+ tgt = y1*y2*y3
+ res = cheb.chebval3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = cheb.chebval3d(z, z, z, self.c3d)
+ assert_(res.shape == (2,3))
+
+ def test_chebgrid2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j->ij', y1, y2)
+ res = cheb.chebgrid2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = cheb.chebgrid2d(z, z, self.c2d)
+ assert_(res.shape == (2, 3)*2)
+
+ def test_chebgrid3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
+ res = cheb.chebgrid3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
-class TestCalculus(TestCase) :
+ #test shape
+ z = np.ones((2,3))
+ res = cheb.chebgrid3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3)*3)
+
+
+class TestIntegral(TestCase) :
def test_chebint(self) :
# check exceptions
- assert_raises(ValueError, ch.chebint, [0], .5)
- assert_raises(ValueError, ch.chebint, [0], -1)
- assert_raises(ValueError, ch.chebint, [0], 1, [0,0])
+ assert_raises(ValueError, cheb.chebint, [0], .5)
+ assert_raises(ValueError, cheb.chebint, [0], -1)
+ assert_raises(ValueError, cheb.chebint, [0], 1, [0,0])
# test integration of zero polynomial
for i in range(2, 5):
k = [0]*(i - 2) + [1]
- res = ch.chebint([0], m=i, k=k)
+ res = cheb.chebint([0], m=i, k=k)
assert_almost_equal(res, [0, 1])
# check single integration with integration constant
@@ -159,27 +223,27 @@ class TestCalculus(TestCase) :
scl = i + 1
pol = [0]*i + [1]
tgt = [i] + [0]*i + [1/scl]
- chebpol = ch.poly2cheb(pol)
- chebint = ch.chebint(chebpol, m=1, k=[i])
- res = ch.cheb2poly(chebint)
+ chebpol = cheb.poly2cheb(pol)
+ chebint = cheb.chebint(chebpol, m=1, k=[i])
+ res = cheb.cheb2poly(chebint)
assert_almost_equal(trim(res), trim(tgt))
# check single integration with integration constant and lbnd
for i in range(5) :
scl = i + 1
pol = [0]*i + [1]
- chebpol = ch.poly2cheb(pol)
- chebint = ch.chebint(chebpol, m=1, k=[i], lbnd=-1)
- assert_almost_equal(ch.chebval(-1, chebint), i)
+ chebpol = cheb.poly2cheb(pol)
+ chebint = cheb.chebint(chebpol, m=1, k=[i], lbnd=-1)
+ assert_almost_equal(cheb.chebval(-1, chebint), i)
# check single integration with integration constant and scaling
for i in range(5) :
scl = i + 1
pol = [0]*i + [1]
tgt = [i] + [0]*i + [2/scl]
- chebpol = ch.poly2cheb(pol)
- chebint = ch.chebint(chebpol, m=1, k=[i], scl=2)
- res = ch.cheb2poly(chebint)
+ chebpol = cheb.poly2cheb(pol)
+ chebint = cheb.chebint(chebpol, m=1, k=[i], scl=2)
+ res = cheb.cheb2poly(chebint)
assert_almost_equal(trim(res), trim(tgt))
# check multiple integrations with default k
@@ -188,8 +252,8 @@ class TestCalculus(TestCase) :
pol = [0]*i + [1]
tgt = pol[:]
for k in range(j) :
- tgt = ch.chebint(tgt, m=1)
- res = ch.chebint(pol, m=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
@@ -198,8 +262,8 @@ class TestCalculus(TestCase) :
pol = [0]*i + [1]
tgt = pol[:]
for k in range(j) :
- tgt = ch.chebint(tgt, m=1, k=[k])
- res = ch.chebint(pol, m=j, k=range(j))
+ tgt = cheb.chebint(tgt, m=1, k=[k])
+ res = cheb.chebint(pol, m=j, k=range(j))
assert_almost_equal(trim(res), trim(tgt))
# check multiple integrations with lbnd
@@ -208,8 +272,8 @@ class TestCalculus(TestCase) :
pol = [0]*i + [1]
tgt = pol[:]
for k in range(j) :
- tgt = ch.chebint(tgt, m=1, k=[k], lbnd=-1)
- res = ch.chebint(pol, m=j, k=range(j), lbnd=-1)
+ tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1)
+ res = cheb.chebint(pol, m=j, k=range(j), lbnd=-1)
assert_almost_equal(trim(res), trim(tgt))
# check multiple integrations with scaling
@@ -218,174 +282,238 @@ class TestCalculus(TestCase) :
pol = [0]*i + [1]
tgt = pol[:]
for k in range(j) :
- tgt = ch.chebint(tgt, m=1, k=[k], scl=2)
- res = ch.chebint(pol, m=j, k=range(j), scl=2)
+ tgt = cheb.chebint(tgt, m=1, k=[k], scl=2)
+ res = cheb.chebint(pol, m=j, k=range(j), scl=2)
assert_almost_equal(trim(res), trim(tgt))
+ def test_chebint_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([cheb.chebint(c) for c in c2d.T]).T
+ res = cheb.chebint(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([cheb.chebint(c) for c in c2d])
+ res = cheb.chebint(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([cheb.chebint(c, k=3) for c in c2d])
+ res = cheb.chebint(c2d, k=3, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestDerivative(TestCase) :
+
def test_chebder(self) :
# check exceptions
- assert_raises(ValueError, ch.chebder, [0], .5)
- assert_raises(ValueError, ch.chebder, [0], -1)
+ 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) :
tgt = [0]*i + [1]
- res = ch.chebder(tgt, m=0)
+ 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) :
tgt = [0]*i + [1]
- res = ch.chebder(ch.chebint(tgt, m=j), m=j)
+ 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) :
tgt = [0]*i + [1]
- res = ch.chebder(ch.chebint(tgt, m=j, scl=2), m=j, scl=.5)
+ res = cheb.chebder(cheb.chebint(tgt, m=j, scl=2), m=j, scl=.5)
assert_almost_equal(trim(res), trim(tgt))
+ def test_chebder_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([cheb.chebder(c) for c in c2d.T]).T
+ res = cheb.chebder(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([cheb.chebder(c) for c in c2d])
+ res = cheb.chebder(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestVander(TestCase):
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+
+
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], cheb.chebval(x, coef))
+
+ # check for 2d x
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], cheb.chebval(x, coef))
+
+ def test_chebvander2d(self) :
+ # also tests chebval2d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3))
+ van = cheb.chebvander2d(x1, x2, [1, 2])
+ tgt = cheb.chebval2d(x1, x2, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = cheb.chebvander2d([x1], [x2], [1, 2])
+ assert_(van.shape == (1, 5, 6))
+
+
+ def test_chebvander3d(self) :
+ # also tests chebval3d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3, 4))
+ van = cheb.chebvander3d(x1, x2, x3, [1, 2, 3])
+ tgt = cheb.chebval3d(x1, x2, x3, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = cheb.chebvander3d([x1], [x2], [x3], [1, 2, 3])
+ assert_(van.shape == (1, 5, 24))
+
class TestMisc(TestCase) :
def test_chebfromroots(self) :
- res = ch.chebfromroots([])
+ res = cheb.chebfromroots([])
assert_almost_equal(trim(res), [1])
for i in range(1,5) :
roots = np.cos(np.linspace(-np.pi, 0, 2*i + 1)[1::2])
tgt = [0]*i + [1]
- res = ch.chebfromroots(roots)*2**(i-1)
+ res = cheb.chebfromroots(roots)*2**(i-1)
assert_almost_equal(trim(res),trim(tgt))
def test_chebroots(self) :
- assert_almost_equal(ch.chebroots([1]), [])
- assert_almost_equal(ch.chebroots([1, 2]), [-.5])
+ assert_almost_equal(cheb.chebroots([1]), [])
+ assert_almost_equal(cheb.chebroots([1, 2]), [-.5])
for i in range(2,5) :
tgt = np.linspace(-1, 1, i)
- res = ch.chebroots(ch.chebfromroots(tgt))
+ res = cheb.chebroots(cheb.chebfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
- def test_chebvander(self) :
- # check for 1d x
- x = np.arange(3)
- v = ch.chebvander(x, 3)
- assert_(v.shape == (3,4))
- for i in range(4) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], ch.chebval(x, coef))
-
- # check for 2d x
- x = np.array([[1,2],[3,4],[5,6]])
- v = ch.chebvander(x, 3)
- assert_(v.shape == (3,2,4))
- for i in range(4) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], ch.chebval(x, coef))
-
def test_chebfit(self) :
def f(x) :
return x*(x - 1)*(x - 2)
# Test exceptions
- assert_raises(ValueError, ch.chebfit, [1], [1], -1)
- assert_raises(TypeError, ch.chebfit, [[1]], [1], 0)
- assert_raises(TypeError, ch.chebfit, [], [1], 0)
- assert_raises(TypeError, ch.chebfit, [1], [[[1]]], 0)
- assert_raises(TypeError, ch.chebfit, [1, 2], [1], 0)
- assert_raises(TypeError, ch.chebfit, [1], [1, 2], 0)
- assert_raises(TypeError, ch.chebfit, [1], [1], 0, w=[[1]])
- assert_raises(TypeError, ch.chebfit, [1], [1], 0, w=[1,1])
+ assert_raises(ValueError, cheb.chebfit, [1], [1], -1)
+ assert_raises(TypeError, cheb.chebfit, [[1]], [1], 0)
+ assert_raises(TypeError, cheb.chebfit, [], [1], 0)
+ assert_raises(TypeError, cheb.chebfit, [1], [[[1]]], 0)
+ assert_raises(TypeError, cheb.chebfit, [1, 2], [1], 0)
+ assert_raises(TypeError, cheb.chebfit, [1], [1, 2], 0)
+ assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[[1]])
+ assert_raises(TypeError, cheb.chebfit, [1], [1], 0, w=[1,1])
# Test fit
x = np.linspace(0,2)
y = f(x)
#
- coef3 = ch.chebfit(x, y, 3)
+ coef3 = cheb.chebfit(x, y, 3)
assert_equal(len(coef3), 4)
- assert_almost_equal(ch.chebval(x, coef3), y)
+ assert_almost_equal(cheb.chebval(x, coef3), y)
#
- coef4 = ch.chebfit(x, y, 4)
+ coef4 = cheb.chebfit(x, y, 4)
assert_equal(len(coef4), 5)
- assert_almost_equal(ch.chebval(x, coef4), y)
+ assert_almost_equal(cheb.chebval(x, coef4), y)
#
- coef2d = ch.chebfit(x, np.array([y,y]).T, 3)
+ coef2d = cheb.chebfit(x, np.array([y,y]).T, 3)
assert_almost_equal(coef2d, np.array([coef3,coef3]).T)
# test weighting
w = np.zeros_like(x)
yw = y.copy()
w[1::2] = 1
y[0::2] = 0
- wcoef3 = ch.chebfit(x, yw, 3, w=w)
+ wcoef3 = cheb.chebfit(x, yw, 3, w=w)
assert_almost_equal(wcoef3, coef3)
#
- wcoef2d = ch.chebfit(x, np.array([yw,yw]).T, 3, w=w)
+ wcoef2d = cheb.chebfit(x, np.array([yw,yw]).T, 3, w=w)
assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
def test_chebtrim(self) :
coef = [2, -1, 1, 0]
# Test exceptions
- assert_raises(ValueError, ch.chebtrim, coef, -1)
+ assert_raises(ValueError, cheb.chebtrim, coef, -1)
# Test results
- assert_equal(ch.chebtrim(coef), coef[:-1])
- assert_equal(ch.chebtrim(coef, 1), coef[:-3])
- assert_equal(ch.chebtrim(coef, 2), [0])
+ assert_equal(cheb.chebtrim(coef), coef[:-1])
+ assert_equal(cheb.chebtrim(coef, 1), coef[:-3])
+ assert_equal(cheb.chebtrim(coef, 2), [0])
def test_chebline(self) :
- assert_equal(ch.chebline(3,4), [3, 4])
+ assert_equal(cheb.chebline(3,4), [3, 4])
def test_cheb2poly(self) :
for i in range(10) :
- assert_almost_equal(ch.cheb2poly([0]*i + [1]), Tlist[i])
+ assert_almost_equal(cheb.cheb2poly([0]*i + [1]), Tlist[i])
def test_poly2cheb(self) :
for i in range(10) :
- assert_almost_equal(ch.poly2cheb(Tlist[i]), [0]*i + [1])
+ assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
def test_chebpts1(self):
#test exceptions
- assert_raises(ValueError, ch.chebpts1, 1.5)
- assert_raises(ValueError, ch.chebpts1, 0)
+ assert_raises(ValueError, cheb.chebpts1, 1.5)
+ assert_raises(ValueError, cheb.chebpts1, 0)
#test points
tgt = [0]
- assert_almost_equal(ch.chebpts1(1), tgt)
+ assert_almost_equal(cheb.chebpts1(1), tgt)
tgt = [-0.70710678118654746, 0.70710678118654746]
- assert_almost_equal(ch.chebpts1(2), tgt)
+ assert_almost_equal(cheb.chebpts1(2), tgt)
tgt = [-0.86602540378443871, 0, 0.86602540378443871]
- assert_almost_equal(ch.chebpts1(3), tgt)
+ assert_almost_equal(cheb.chebpts1(3), tgt)
tgt = [-0.9238795325, -0.3826834323, 0.3826834323, 0.9238795325]
- assert_almost_equal(ch.chebpts1(4), tgt)
+ assert_almost_equal(cheb.chebpts1(4), tgt)
def test_chebpts2(self):
#test exceptions
- assert_raises(ValueError, ch.chebpts2, 1.5)
- assert_raises(ValueError, ch.chebpts2, 1)
+ assert_raises(ValueError, cheb.chebpts2, 1.5)
+ assert_raises(ValueError, cheb.chebpts2, 1)
#test points
tgt = [-1, 1]
- assert_almost_equal(ch.chebpts2(2), tgt)
+ assert_almost_equal(cheb.chebpts2(2), tgt)
tgt = [-1, 0, 1]
- assert_almost_equal(ch.chebpts2(3), tgt)
+ assert_almost_equal(cheb.chebpts2(3), tgt)
tgt = [-1, -0.5, .5, 1]
- assert_almost_equal(ch.chebpts2(4), tgt)
+ assert_almost_equal(cheb.chebpts2(4), tgt)
tgt = [-1.0, -0.707106781187, 0, 0.707106781187, 1.0]
- assert_almost_equal(ch.chebpts2(5), tgt)
+ assert_almost_equal(cheb.chebpts2(5), tgt)
class TestChebyshevClass(TestCase) :
- p1 = ch.Chebyshev([1,2,3])
- p2 = ch.Chebyshev([1,2,3], [0,1])
- p3 = ch.Chebyshev([1,2])
- p4 = ch.Chebyshev([2,2,3])
- p5 = ch.Chebyshev([3,2,3])
+ p1 = cheb.Chebyshev([1,2,3])
+ p2 = cheb.Chebyshev([1,2,3], [0,1])
+ p3 = cheb.Chebyshev([1,2])
+ p4 = cheb.Chebyshev([2,2,3])
+ p5 = cheb.Chebyshev([3,2,3])
def test_equal(self) :
assert_(self.p1 == self.p1)
@@ -402,38 +530,38 @@ class TestChebyshevClass(TestCase) :
assert_(self.p1 != [1,2,3])
def test_add(self) :
- tgt = ch.Chebyshev([2,4,6])
+ tgt = cheb.Chebyshev([2,4,6])
assert_(self.p1 + self.p1 == tgt)
assert_(self.p1 + [1,2,3] == tgt)
assert_([1,2,3] + self.p1 == tgt)
def test_sub(self) :
- tgt = ch.Chebyshev([1])
+ tgt = cheb.Chebyshev([1])
assert_(self.p4 - self.p1 == tgt)
assert_(self.p4 - [1,2,3] == tgt)
assert_([2,2,3] - self.p1 == tgt)
def test_mul(self) :
- tgt = ch.Chebyshev([7.5, 10., 8., 6., 4.5])
+ tgt = cheb.Chebyshev([7.5, 10., 8., 6., 4.5])
assert_(self.p1 * self.p1 == tgt)
assert_(self.p1 * [1,2,3] == tgt)
assert_([1,2,3] * self.p1 == tgt)
def test_floordiv(self) :
- tgt = ch.Chebyshev([1])
+ tgt = cheb.Chebyshev([1])
assert_(self.p4 // self.p1 == tgt)
assert_(self.p4 // [1,2,3] == tgt)
assert_([2,2,3] // self.p1 == tgt)
def test_mod(self) :
- tgt = ch.Chebyshev([1])
+ tgt = cheb.Chebyshev([1])
assert_((self.p4 % self.p1) == tgt)
assert_((self.p4 % [1,2,3]) == tgt)
assert_(([2,2,3] % self.p1) == tgt)
def test_divmod(self) :
- tquo = ch.Chebyshev([1])
- trem = ch.Chebyshev([2])
+ tquo = cheb.Chebyshev([1])
+ trem = cheb.Chebyshev([2])
quo, rem = divmod(self.p5, self.p1)
assert_(quo == tquo and rem == trem)
quo, rem = divmod(self.p5, [1,2,3])
@@ -442,7 +570,7 @@ class TestChebyshevClass(TestCase) :
assert_(quo == tquo and rem == trem)
def test_pow(self) :
- tgt = ch.Chebyshev([1])
+ tgt = cheb.Chebyshev([1])
for i in range(5) :
res = self.p1**i
assert_(res == tgt)
@@ -481,7 +609,7 @@ class TestChebyshevClass(TestCase) :
def test_trim(self) :
coef = [1, 1e-6, 1e-12, 0]
- p = ch.Chebyshev(coef)
+ p = cheb.Chebyshev(coef)
assert_equal(p.trim().coef, coef[:3])
assert_equal(p.trim(1e-10).coef, coef[:2])
assert_equal(p.trim(1e-5).coef, coef[:1])
@@ -500,13 +628,13 @@ class TestChebyshevClass(TestCase) :
def test_integ(self) :
p = self.p2.integ()
- assert_almost_equal(p.coef, ch.chebint([1,2,3], 1, 0, scl=.5))
+ assert_almost_equal(p.coef, cheb.chebint([1,2,3], 1, 0, scl=.5))
p = self.p2.integ(lbnd=0)
assert_almost_equal(p(0), 0)
p = self.p2.integ(1, 1)
- assert_almost_equal(p.coef, ch.chebint([1,2,3], 1, 1, scl=.5))
+ assert_almost_equal(p.coef, cheb.chebint([1,2,3], 1, 1, scl=.5))
p = self.p2.integ(2, [1, 2])
- assert_almost_equal(p.coef, ch.chebint([1,2,3], 2, [1,2], scl=.5))
+ assert_almost_equal(p.coef, cheb.chebint([1,2,3], 2, [1,2], scl=.5))
def test_deriv(self) :
p = self.p2.integ(2, [1, 2])
@@ -514,7 +642,7 @@ class TestChebyshevClass(TestCase) :
assert_almost_equal(p.deriv(2).coef, self.p2.coef)
def test_roots(self) :
- p = ch.Chebyshev(ch.poly2cheb([0, -1, 0, 1]), [0, 1])
+ p = cheb.Chebyshev(cheb.poly2cheb([0, -1, 0, 1]), [0, 1])
res = p.roots()
tgt = [0, .5, 1]
assert_almost_equal(res, tgt)
@@ -528,9 +656,9 @@ class TestChebyshevClass(TestCase) :
def test_fromroots(self) :
roots = [0, .5, 1]
- p = ch.Chebyshev.fromroots(roots, domain=[0, 1])
+ p = cheb.Chebyshev.fromroots(roots, domain=[0, 1])
res = p.coef
- tgt = ch.poly2cheb([0, -1, 0, 1])
+ tgt = cheb.poly2cheb([0, -1, 0, 1])
assert_almost_equal(res, tgt)
def test_fit(self) :
@@ -540,14 +668,14 @@ class TestChebyshevClass(TestCase) :
y = f(x)
# test default value of domain
- p = ch.Chebyshev.fit(x, y, 3)
+ p = cheb.Chebyshev.fit(x, y, 3)
assert_almost_equal(p.domain, [0,3])
# test that fit works in given domains
- p = ch.Chebyshev.fit(x, y, 3, None)
+ p = cheb.Chebyshev.fit(x, y, 3, None)
assert_almost_equal(p(x), y)
assert_almost_equal(p.domain, [0,3])
- p = ch.Chebyshev.fit(x, y, 3, [])
+ p = cheb.Chebyshev.fit(x, y, 3, [])
assert_almost_equal(p(x), y)
assert_almost_equal(p.domain, [-1, 1])
# test that fit accepts weights.
@@ -555,14 +683,14 @@ class TestChebyshevClass(TestCase) :
yw = y.copy()
w[1::2] = 1
yw[0::2] = 0
- p = ch.Chebyshev.fit(x, yw, 3, w=w)
+ p = cheb.Chebyshev.fit(x, yw, 3, w=w)
assert_almost_equal(p(x), y)
def test_identity(self) :
x = np.linspace(0,3)
- p = ch.Chebyshev.identity()
+ p = cheb.Chebyshev.identity()
assert_almost_equal(p(x), x)
- p = ch.Chebyshev.identity([1,3])
+ p = cheb.Chebyshev.identity([1,3])
assert_almost_equal(p(x), x)
#
diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py
index 1a32122c8..f0d20f8e6 100644
--- a/numpy/polynomial/tests/test_hermite.py
+++ b/numpy/polynomial/tests/test_hermite.py
@@ -1,11 +1,11 @@
-"""Tests for hermendre module.
+"""Tests for hermite module.
"""
from __future__ import division
import numpy as np
import numpy.polynomial.hermite as herm
-import numpy.polynomial.polynomial as poly
+from numpy.polynomial.polynomial import polyval
from numpy.testing import *
H0 = np.array([ 1])
@@ -43,40 +43,6 @@ class TestConstants(TestCase) :
class TestArithmetic(TestCase) :
x = np.linspace(-3, 3, 100)
- y0 = poly.polyval(x, H0)
- y1 = poly.polyval(x, H1)
- y2 = poly.polyval(x, H2)
- y3 = poly.polyval(x, H3)
- y4 = poly.polyval(x, H4)
- y5 = poly.polyval(x, H5)
- y6 = poly.polyval(x, H6)
- y7 = poly.polyval(x, H7)
- y8 = poly.polyval(x, H8)
- y9 = poly.polyval(x, H9)
- y = [y0, y1, y2, y3, y4, y5, y6, y7, y8, y9]
-
- def test_hermval(self) :
- def f(x) :
- return x*(x**2 - 1)
-
- #check empty input
- assert_equal(herm.hermval([], [1]).size, 0)
-
- #check normal input)
- for i in range(10) :
- msg = "At i=%d" % i
- ser = np.zeros
- tgt = self.y[i]
- res = herm.hermval(self.x, [0]*i + [1])
- assert_almost_equal(res, tgt, err_msg=msg)
-
- #check that shape is preserved
- for i in range(3) :
- dims = [2]*i
- x = np.zeros(dims)
- assert_equal(herm.hermval(x, [1]).shape, dims)
- assert_equal(herm.hermval(x, [1,0]).shape, dims)
- assert_equal(herm.hermval(x, [1,0,0]).shape, dims)
def test_hermadd(self) :
for i in range(5) :
@@ -132,7 +98,103 @@ class TestArithmetic(TestCase) :
assert_equal(trim(res), trim(tgt), err_msg=msg)
-class TestCalculus(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)
+ c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
+
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+ y = polyval(x, [1., 2., 3.])
+
+
+ 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) :
+ msg = "At i=%d" % i
+ ser = np.zeros
+ tgt = y[i]
+ res = herm.hermval(x, [0]*i + [1])
+ assert_almost_equal(res, tgt, err_msg=msg)
+
+ #check that shape is preserved
+ for i in range(3) :
+ dims = [2]*i
+ x = np.zeros(dims)
+ assert_equal(herm.hermval(x, [1]).shape, dims)
+ assert_equal(herm.hermval(x, [1,0]).shape, dims)
+ assert_equal(herm.hermval(x, [1,0,0]).shape, dims)
+
+ def test_hermval2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, herm.hermval2d, x1, x2[:2], self.c2d)
+
+ #test values
+ tgt = y1*y2
+ res = herm.hermval2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herm.hermval2d(z, z, self.c2d)
+ assert_(res.shape == (2,3))
+
+ def test_hermval3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, herm.hermval3d, x1, x2, x3[:2], self.c3d)
+
+ #test values
+ tgt = y1*y2*y3
+ res = herm.hermval3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herm.hermval3d(z, z, z, self.c3d)
+ assert_(res.shape == (2,3))
+
+ def test_hermgrid2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j->ij', y1, y2)
+ res = herm.hermgrid2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herm.hermgrid2d(z, z, self.c2d)
+ assert_(res.shape == (2, 3)*2)
+
+ def test_hermgrid3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
+ res = herm.hermgrid3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herm.hermgrid3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3)*3)
+
+
+class TestIntegral(TestCase) :
def test_hermint(self) :
# check exceptions
@@ -214,6 +276,21 @@ class TestCalculus(TestCase) :
res = herm.hermint(pol, m=j, k=range(j), scl=2)
assert_almost_equal(trim(res), trim(tgt))
+ def test_hermint_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([herm.hermint(c) for c in c2d.T]).T
+ res = herm.hermint(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([herm.hermint(c) for c in c2d])
+ res = herm.hermint(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestDerivative(TestCase) :
+
def test_hermder(self) :
# check exceptions
assert_raises(ValueError, herm.hermder, [0], .5)
@@ -239,6 +316,72 @@ class TestCalculus(TestCase) :
res = herm.hermder(herm.hermint(tgt, m=j, scl=2), m=j, scl=.5)
assert_almost_equal(trim(res), trim(tgt))
+ def test_hermder_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([herm.hermder(c) for c in c2d.T]).T
+ res = herm.hermder(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([herm.hermder(c) for c in c2d])
+ res = herm.hermder(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([herm.hermder(c, k=3) for c in c2d])
+ res = herm.hermder(c2d, k=3, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestVander(TestCase):
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+
+
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], herm.hermval(x, coef))
+
+ # check for 2d x
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], herm.hermval(x, coef))
+
+ def test_hermvander2d(self) :
+ # also tests hermval2d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3))
+ van = herm.hermvander2d(x1, x2, [1, 2])
+ tgt = herm.hermval2d(x1, x2, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = herm.hermvander2d([x1], [x2], [1, 2])
+ assert_(van.shape == (1, 5, 6))
+
+
+ def test_hermvander3d(self) :
+ # also tests hermval3d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3, 4))
+ van = herm.hermvander3d(x1, x2, x3, [1, 2, 3])
+ tgt = herm.hermval3d(x1, x2, x3, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = herm.hermvander3d([x1], [x2], [x3], [1, 2, 3])
+ assert_(van.shape == (1, 5, 24))
+
class TestMisc(TestCase) :
@@ -262,23 +405,6 @@ class TestMisc(TestCase) :
res = herm.hermroots(herm.hermfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], herm.hermval(x, coef))
-
- # check for 2d x
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], herm.hermval(x, coef))
-
def test_hermfit(self) :
def f(x) :
return x*(x - 1)*(x - 2)
diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py
index 850d77e4d..19d35b3d4 100644
--- a/numpy/polynomial/tests/test_hermite_e.py
+++ b/numpy/polynomial/tests/test_hermite_e.py
@@ -1,11 +1,11 @@
-"""Tests for hermeendre module.
+"""Tests for hermite_e module.
"""
from __future__ import division
import numpy as np
import numpy.polynomial.hermite_e as herme
-import numpy.polynomial.polynomial as poly
+from numpy.polynomial.polynomial import polyval
from numpy.testing import *
He0 = np.array([ 1 ])
@@ -42,40 +42,6 @@ class TestConstants(TestCase) :
class TestArithmetic(TestCase) :
x = np.linspace(-3, 3, 100)
- y0 = poly.polyval(x, He0)
- y1 = poly.polyval(x, He1)
- y2 = poly.polyval(x, He2)
- y3 = poly.polyval(x, He3)
- y4 = poly.polyval(x, He4)
- y5 = poly.polyval(x, He5)
- y6 = poly.polyval(x, He6)
- y7 = poly.polyval(x, He7)
- y8 = poly.polyval(x, He8)
- y9 = poly.polyval(x, He9)
- y = [y0, y1, y2, y3, y4, y5, y6, y7, y8, y9]
-
- def test_hermeval(self) :
- def f(x) :
- return x*(x**2 - 1)
-
- #check empty input
- assert_equal(herme.hermeval([], [1]).size, 0)
-
- #check normal input)
- for i in range(10) :
- msg = "At i=%d" % i
- ser = np.zeros
- tgt = self.y[i]
- res = herme.hermeval(self.x, [0]*i + [1])
- assert_almost_equal(res, tgt, err_msg=msg)
-
- #check that shape is preserved
- for i in range(3) :
- dims = [2]*i
- x = np.zeros(dims)
- assert_equal(herme.hermeval(x, [1]).shape, dims)
- assert_equal(herme.hermeval(x, [1,0]).shape, dims)
- assert_equal(herme.hermeval(x, [1,0,0]).shape, dims)
def test_hermeadd(self) :
for i in range(5) :
@@ -131,7 +97,103 @@ class TestArithmetic(TestCase) :
assert_equal(trim(res), trim(tgt), err_msg=msg)
-class TestCalculus(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)
+ c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
+
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+ y = polyval(x, [1., 2., 3.])
+
+
+ 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) :
+ msg = "At i=%d" % i
+ ser = np.zeros
+ tgt = y[i]
+ res = herme.hermeval(x, [0]*i + [1])
+ assert_almost_equal(res, tgt, err_msg=msg)
+
+ #check that shape is preserved
+ for i in range(3) :
+ dims = [2]*i
+ x = np.zeros(dims)
+ assert_equal(herme.hermeval(x, [1]).shape, dims)
+ assert_equal(herme.hermeval(x, [1,0]).shape, dims)
+ assert_equal(herme.hermeval(x, [1,0,0]).shape, dims)
+
+ def test_hermeval2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, herme.hermeval2d, x1, x2[:2], self.c2d)
+
+ #test values
+ tgt = y1*y2
+ res = herme.hermeval2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herme.hermeval2d(z, z, self.c2d)
+ assert_(res.shape == (2,3))
+
+ def test_hermeval3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, herme.hermeval3d, x1, x2, x3[:2], self.c3d)
+
+ #test values
+ tgt = y1*y2*y3
+ res = herme.hermeval3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herme.hermeval3d(z, z, z, self.c3d)
+ assert_(res.shape == (2,3))
+
+ def test_hermegrid2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j->ij', y1, y2)
+ res = herme.hermegrid2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herme.hermegrid2d(z, z, self.c2d)
+ assert_(res.shape == (2, 3)*2)
+
+ def test_hermegrid3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
+ res = herme.hermegrid3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = herme.hermegrid3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3)*3)
+
+
+class TestIntegral(TestCase):
def test_hermeint(self) :
# check exceptions
@@ -213,6 +275,25 @@ class TestCalculus(TestCase) :
res = herme.hermeint(pol, m=j, k=range(j), scl=2)
assert_almost_equal(trim(res), trim(tgt))
+ def test_hermeint_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([herme.hermeint(c) for c in c2d.T]).T
+ res = herme.hermeint(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([herme.hermeint(c) for c in c2d])
+ res = herme.hermeint(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([herme.hermeint(c, k=3) for c in c2d])
+ res = herme.hermeint(c2d, k=3, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestDerivative(TestCase) :
+
def test_hermeder(self) :
# check exceptions
assert_raises(ValueError, herme.hermeder, [0], .5)
@@ -238,6 +319,68 @@ class TestCalculus(TestCase) :
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):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([herme.hermeder(c) for c in c2d.T]).T
+ res = herme.hermeder(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([herme.hermeder(c) for c in c2d])
+ res = herme.hermeder(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestVander(TestCase):
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+
+
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], herme.hermeval(x, coef))
+
+ # check for 2d x
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], herme.hermeval(x, coef))
+
+ def test_hermevander2d(self) :
+ # also tests hermeval2d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3))
+ van = herme.hermevander2d(x1, x2, [1, 2])
+ tgt = herme.hermeval2d(x1, x2, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = herme.hermevander2d([x1], [x2], [1, 2])
+ assert_(van.shape == (1, 5, 6))
+
+
+ def test_hermevander3d(self) :
+ # also tests hermeval3d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3, 4))
+ van = herme.hermevander3d(x1, x2, x3, [1, 2, 3])
+ tgt = herme.hermeval3d(x1, x2, x3, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = herme.hermevander3d([x1], [x2], [x3], [1, 2, 3])
+ assert_(van.shape == (1, 5, 24))
+
class TestMisc(TestCase) :
@@ -261,23 +404,6 @@ class TestMisc(TestCase) :
res = herme.hermeroots(herme.hermefromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], herme.hermeval(x, coef))
-
- # check for 2d x
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], herme.hermeval(x, coef))
-
def test_hermefit(self) :
def f(x) :
return x*(x - 1)*(x - 2)
diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py
index 87759d5b7..90fd7afb7 100644
--- a/numpy/polynomial/tests/test_laguerre.py
+++ b/numpy/polynomial/tests/test_laguerre.py
@@ -1,11 +1,11 @@
-"""Tests for hermendre module.
+"""Tests for laguerre module.
"""
from __future__ import division
import numpy as np
import numpy.polynomial.laguerre as lag
-import numpy.polynomial.polynomial as poly
+from numpy.polynomial.polynomial import polyval
from numpy.testing import *
L0 = np.array([1 ])/1
@@ -39,37 +39,6 @@ class TestConstants(TestCase) :
class TestArithmetic(TestCase) :
x = np.linspace(-3, 3, 100)
- y0 = poly.polyval(x, L0)
- y1 = poly.polyval(x, L1)
- y2 = poly.polyval(x, L2)
- y3 = poly.polyval(x, L3)
- y4 = poly.polyval(x, L4)
- y5 = poly.polyval(x, L5)
- y6 = poly.polyval(x, L6)
- y = [y0, y1, y2, y3, y4, y5, y6]
-
- def test_lagval(self) :
- def f(x) :
- return x*(x**2 - 1)
-
- #check empty input
- assert_equal(lag.lagval([], [1]).size, 0)
-
- #check normal input)
- for i in range(7) :
- msg = "At i=%d" % i
- ser = np.zeros
- tgt = self.y[i]
- res = lag.lagval(self.x, [0]*i + [1])
- assert_almost_equal(res, tgt, err_msg=msg)
-
- #check that shape is preserved
- for i in range(3) :
- dims = [2]*i
- x = np.zeros(dims)
- assert_equal(lag.lagval(x, [1]).shape, dims)
- assert_equal(lag.lagval(x, [1,0]).shape, dims)
- assert_equal(lag.lagval(x, [1,0,0]).shape, dims)
def test_lagadd(self) :
for i in range(5) :
@@ -125,7 +94,102 @@ class TestArithmetic(TestCase) :
assert_almost_equal(trim(res), trim(tgt), err_msg=msg)
-class TestCalculus(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)
+ c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
+
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+ y = polyval(x, [1., 2., 3.])
+
+ 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) :
+ msg = "At i=%d" % i
+ ser = np.zeros
+ tgt = y[i]
+ res = lag.lagval(x, [0]*i + [1])
+ assert_almost_equal(res, tgt, err_msg=msg)
+
+ #check that shape is preserved
+ for i in range(3) :
+ dims = [2]*i
+ x = np.zeros(dims)
+ assert_equal(lag.lagval(x, [1]).shape, dims)
+ assert_equal(lag.lagval(x, [1,0]).shape, dims)
+ assert_equal(lag.lagval(x, [1,0,0]).shape, dims)
+
+ def test_lagval2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, lag.lagval2d, x1, x2[:2], self.c2d)
+
+ #test values
+ tgt = y1*y2
+ res = lag.lagval2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = lag.lagval2d(z, z, self.c2d)
+ assert_(res.shape == (2,3))
+
+ def test_lagval3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, lag.lagval3d, x1, x2, x3[:2], self.c3d)
+
+ #test values
+ tgt = y1*y2*y3
+ res = lag.lagval3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = lag.lagval3d(z, z, z, self.c3d)
+ assert_(res.shape == (2,3))
+
+ def test_laggrid2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j->ij', y1, y2)
+ res = lag.laggrid2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = lag.laggrid2d(z, z, self.c2d)
+ assert_(res.shape == (2, 3)*2)
+
+ def test_laggrid3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
+ res = lag.laggrid3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = lag.laggrid3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3)*3)
+
+
+class TestIntegral(TestCase) :
def test_lagint(self) :
# check exceptions
@@ -207,6 +271,25 @@ class TestCalculus(TestCase) :
res = lag.lagint(pol, m=j, k=range(j), scl=2)
assert_almost_equal(trim(res), trim(tgt))
+ def test_lagint_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([lag.lagint(c) for c in c2d.T]).T
+ res = lag.lagint(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([lag.lagint(c) for c in c2d])
+ res = lag.lagint(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([lag.lagint(c, k=3) for c in c2d])
+ res = lag.lagint(c2d, k=3, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestDerivative(TestCase) :
+
def test_lagder(self) :
# check exceptions
assert_raises(ValueError, lag.lagder, [0], .5)
@@ -232,6 +315,68 @@ class TestCalculus(TestCase) :
res = lag.lagder(lag.lagint(tgt, m=j, scl=2), m=j, scl=.5)
assert_almost_equal(trim(res), trim(tgt))
+ def test_lagder_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([lag.lagder(c) for c in c2d.T]).T
+ res = lag.lagder(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([lag.lagder(c) for c in c2d])
+ res = lag.lagder(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestVander(TestCase):
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+
+
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], lag.lagval(x, coef))
+
+ # check for 2d x
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], lag.lagval(x, coef))
+
+ def test_lagvander2d(self) :
+ # also tests lagval2d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3))
+ van = lag.lagvander2d(x1, x2, [1, 2])
+ tgt = lag.lagval2d(x1, x2, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = lag.lagvander2d([x1], [x2], [1, 2])
+ assert_(van.shape == (1, 5, 6))
+
+
+ def test_lagvander3d(self) :
+ # also tests lagval3d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3, 4))
+ van = lag.lagvander3d(x1, x2, x3, [1, 2, 3])
+ tgt = lag.lagval3d(x1, x2, x3, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = lag.lagvander3d([x1], [x2], [x3], [1, 2, 3])
+ assert_(van.shape == (1, 5, 24))
+
class TestMisc(TestCase) :
@@ -255,23 +400,6 @@ class TestMisc(TestCase) :
res = lag.lagroots(lag.lagfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], lag.lagval(x, coef))
-
- # check for 2d x
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], lag.lagval(x, coef))
-
def test_lagfit(self) :
def f(x) :
return x*(x - 1)*(x - 2)
diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py
index bcbadae2f..af64a2b32 100644
--- a/numpy/polynomial/tests/test_legendre.py
+++ b/numpy/polynomial/tests/test_legendre.py
@@ -5,21 +5,21 @@ from __future__ import division
import numpy as np
import numpy.polynomial.legendre as leg
-import numpy.polynomial.polynomial as poly
+from numpy.polynomial.polynomial import polyval
from numpy.testing import *
-P0 = np.array([ 1])
-P1 = np.array([ 0, 1])
-P2 = np.array([-1, 0, 3])/2
-P3 = np.array([ 0, -3, 0, 5])/2
-P4 = np.array([ 3, 0, -30, 0, 35])/8
-P5 = np.array([ 0, 15, 0, -70, 0, 63])/8
-P6 = np.array([-5, 0, 105, 0,-315, 0, 231])/16
-P7 = np.array([ 0,-35, 0, 315, 0, -693, 0, 429])/16
-P8 = np.array([35, 0,-1260, 0,6930, 0,-12012, 0,6435])/128
-P9 = np.array([ 0,315, 0,-4620, 0,18018, 0,-25740, 0,12155])/128
+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
-Plist = [P0, P1, P2, P3, P4, P5, P6, P7, P8, P9]
+Llist = [L0, L1, L2, L3, L4, L5, L6, L7, L8, L9]
def trim(x) :
return leg.legtrim(x, tol=1e-6)
@@ -42,40 +42,6 @@ class TestConstants(TestCase) :
class TestArithmetic(TestCase) :
x = np.linspace(-1, 1, 100)
- y0 = poly.polyval(x, P0)
- y1 = poly.polyval(x, P1)
- y2 = poly.polyval(x, P2)
- y3 = poly.polyval(x, P3)
- y4 = poly.polyval(x, P4)
- y5 = poly.polyval(x, P5)
- y6 = poly.polyval(x, P6)
- y7 = poly.polyval(x, P7)
- y8 = poly.polyval(x, P8)
- y9 = poly.polyval(x, P9)
- y = [y0, y1, y2, y3, y4, y5, y6, y7, y8, y9]
-
- def test_legval(self) :
- def f(x) :
- return x*(x**2 - 1)
-
- #check empty input
- assert_equal(leg.legval([], [1]).size, 0)
-
- #check normal input)
- for i in range(10) :
- msg = "At i=%d" % i
- ser = np.zeros
- tgt = self.y[i]
- res = leg.legval(self.x, [0]*i + [1])
- assert_almost_equal(res, tgt, err_msg=msg)
-
- #check that shape is preserved
- for i in range(3) :
- dims = [2]*i
- x = np.zeros(dims)
- assert_equal(leg.legval(x, [1]).shape, dims)
- assert_equal(leg.legval(x, [1,0]).shape, dims)
- assert_equal(leg.legval(x, [1,0,0]).shape, dims)
def test_legadd(self) :
for i in range(5) :
@@ -132,7 +98,102 @@ class TestArithmetic(TestCase) :
assert_equal(trim(res), trim(tgt), err_msg=msg)
-class TestCalculus(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)
+ c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
+
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+ y = polyval(x, [1., 2., 3.])
+
+ 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) :
+ msg = "At i=%d" % i
+ ser = np.zeros
+ tgt = y[i]
+ res = leg.legval(x, [0]*i + [1])
+ assert_almost_equal(res, tgt, err_msg=msg)
+
+ #check that shape is preserved
+ for i in range(3) :
+ dims = [2]*i
+ x = np.zeros(dims)
+ assert_equal(leg.legval(x, [1]).shape, dims)
+ assert_equal(leg.legval(x, [1,0]).shape, dims)
+ assert_equal(leg.legval(x, [1,0,0]).shape, dims)
+
+ def test_legval2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, leg.legval2d, x1, x2[:2], self.c2d)
+
+ #test values
+ tgt = y1*y2
+ res = leg.legval2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = leg.legval2d(z, z, self.c2d)
+ assert_(res.shape == (2,3))
+
+ def test_legval3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, leg.legval3d, x1, x2, x3[:2], self.c3d)
+
+ #test values
+ tgt = y1*y2*y3
+ res = leg.legval3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = leg.legval3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3))
+
+ def test_leggrid2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j->ij', y1, y2)
+ res = leg.leggrid2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = leg.leggrid2d(z, z, self.c2d)
+ assert_(res.shape == (2, 3)*2)
+
+ def test_leggrid3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
+ res = leg.leggrid3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = leg.leggrid3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3)*3)
+
+
+class TestIntegral(TestCase) :
def test_legint(self) :
# check exceptions
@@ -214,6 +275,25 @@ class TestCalculus(TestCase) :
res = leg.legint(pol, m=j, k=range(j), scl=2)
assert_almost_equal(trim(res), trim(tgt))
+ def test_legint_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([leg.legint(c) for c in c2d.T]).T
+ res = leg.legint(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([leg.legint(c) for c in c2d])
+ res = leg.legint(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([leg.legint(c, k=3) for c in c2d])
+ res = leg.legint(c2d, k=3, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestDerivative(TestCase) :
+
def test_legder(self) :
# check exceptions
assert_raises(ValueError, leg.legder, [0], .5)
@@ -239,6 +319,67 @@ class TestCalculus(TestCase) :
res = leg.legder(leg.legint(tgt, m=j, scl=2), m=j, scl=.5)
assert_almost_equal(trim(res), trim(tgt))
+ def test_legder_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([leg.legder(c) for c in c2d.T]).T
+ res = leg.legder(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([leg.legder(c) for c in c2d])
+ res = leg.legder(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestVander(TestCase):
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+
+
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], leg.legval(x, coef))
+
+ # check for 2d x
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], leg.legval(x, coef))
+
+ def test_legvander2d(self) :
+ # also tests polyval2d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3))
+ van = leg.legvander2d(x1, x2, [1, 2])
+ tgt = leg.legval2d(x1, x2, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = leg.legvander2d([x1], [x2], [1, 2])
+ assert_(van.shape == (1, 5, 6))
+
+ def test_legvander3d(self) :
+ # also tests polyval3d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3, 4))
+ van = leg.legvander3d(x1, x2, x3, [1, 2, 3])
+ tgt = leg.legval3d(x1, x2, x3, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = leg.legvander3d([x1], [x2], [x3], [1, 2, 3])
+ assert_(van.shape == (1, 5, 24))
+
class TestMisc(TestCase) :
@@ -262,23 +403,6 @@ class TestMisc(TestCase) :
res = leg.legroots(leg.legfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], leg.legval(x, coef))
-
- # check for 2d x
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], leg.legval(x, coef))
-
def test_legfit(self) :
def f(x) :
return x*(x - 1)*(x - 2)
@@ -334,11 +458,11 @@ class TestMisc(TestCase) :
def test_leg2poly(self) :
for i in range(10) :
- assert_almost_equal(leg.leg2poly([0]*i + [1]), Plist[i])
+ assert_almost_equal(leg.leg2poly([0]*i + [1]), Llist[i])
def test_poly2leg(self) :
for i in range(10) :
- assert_almost_equal(leg.poly2leg(Plist[i]), [0]*i + [1])
+ assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1])
def assert_poly_almost_equal(p1, p2):
diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py
index 5fcb86946..09028686f 100644
--- a/numpy/polynomial/tests/test_polynomial.py
+++ b/numpy/polynomial/tests/test_polynomial.py
@@ -99,20 +99,30 @@ class TestArithmetic(TestCase) :
res = poly.polyadd(poly.polymul(quo, ci), rem)
assert_equal(res, tgt, err_msg=msg)
- def test_polyval(self) :
- def f(x) :
- return x*(x**2 - 1)
+class TestEvaluation(TestCase):
+ # coefficients of 1 + 2*x + 3*x**2
+ c1d = np.array([1., 2., 3.])
+ c2d = np.einsum('i,j->ij', c1d, c1d)
+ c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d)
+
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+ y = poly.polyval(x, [1., 2., 3.])
+
+
+ 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) :
- tgt = x**i
+ tgt = y[i]
res = poly.polyval(x, [0]*i + [1])
assert_almost_equal(res, tgt)
- tgt = f(x)
+ tgt = x*(x**2 - 1)
res = poly.polyval(x, [0, -1, 0, 1])
assert_almost_equal(res, tgt)
@@ -124,8 +134,70 @@ class TestArithmetic(TestCase) :
assert_equal(poly.polyval(x, [1,0]).shape, dims)
assert_equal(poly.polyval(x, [1,0,0]).shape, dims)
+ def test_polyval2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, poly.polyval2d, x1, x2[:2], self.c2d)
+
+ #test values
+ tgt = y1*y2
+ res = poly.polyval2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = poly.polyval2d(z, z, self.c2d)
+ assert_(res.shape == (2,3))
+
+ def test_polyval3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test exceptions
+ assert_raises(ValueError, poly.polyval3d, x1, x2, x3[:2], self.c3d)
+
+ #test values
+ tgt = y1*y2*y3
+ res = poly.polyval3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = poly.polyval3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3))
+
+ def test_polygrid2d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j->ij', y1, y2)
+ res = poly.polygrid2d(x1, x2, self.c2d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = poly.polygrid2d(z, z, self.c2d)
+ assert_(res.shape == (2, 3)*2)
+
+ def test_polygrid3d(self):
+ x1, x2, x3 = self.x
+ y1, y2, y3 = self.y
+
+ #test values
+ tgt = np.einsum('i,j,k->ijk', y1, y2, y3)
+ res = poly.polygrid3d(x1, x2, x3, self.c3d)
+ assert_almost_equal(res, tgt)
+
+ #test shape
+ z = np.ones((2,3))
+ res = poly.polygrid3d(z, z, z, self.c3d)
+ assert_(res.shape == (2, 3)*3)
-class TestCalculus(TestCase) :
+
+class TestIntegral(TestCase):
def test_polyint(self) :
# check exceptions
@@ -202,6 +274,25 @@ class TestCalculus(TestCase) :
res = poly.polyint(pol, m=j, k=range(j), scl=2)
assert_almost_equal(trim(res), trim(tgt))
+ def test_polyint_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([poly.polyint(c) for c in c2d.T]).T
+ res = poly.polyint(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([poly.polyint(c) for c in c2d])
+ res = poly.polyint(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([poly.polyint(c, k=3) for c in c2d])
+ res = poly.polyint(c2d, k=3, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestDerivative(TestCase) :
+
def test_polyder(self) :
# check exceptions
assert_raises(ValueError, poly.polyder, [0], .5)
@@ -227,6 +318,68 @@ class TestCalculus(TestCase) :
res = poly.polyder(poly.polyint(tgt, m=j, scl=2), m=j, scl=.5)
assert_almost_equal(trim(res), trim(tgt))
+ def test_polyder_axis(self):
+ # check that axis keyword works
+ c2d = np.random.random((3, 4))
+
+ tgt = np.vstack([poly.polyder(c) for c in c2d.T]).T
+ res = poly.polyder(c2d, axis=0)
+ assert_almost_equal(res, tgt)
+
+ tgt = np.vstack([poly.polyder(c) for c in c2d])
+ res = poly.polyder(c2d, axis=1)
+ assert_almost_equal(res, tgt)
+
+
+class TestVander(TestCase):
+ # some random values in [-1, 1)
+ x = np.random.random((3, 5))*2 - 1
+
+
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], poly.polyval(x, coef))
+
+ # check for 2d x
+ 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) :
+ coef = [0]*i + [1]
+ assert_almost_equal(v[..., i], poly.polyval(x, coef))
+
+ def test_polyvander2d(self) :
+ # also tests polyval2d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3))
+ van = poly.polyvander2d(x1, x2, [1, 2])
+ tgt = poly.polyval2d(x1, x2, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = poly.polyvander2d([x1], [x2], [1, 2])
+ assert_(van.shape == (1, 5, 6))
+
+
+ def test_polyvander3d(self) :
+ # also tests polyval3d for non-square coefficient array
+ x1, x2, x3 = self.x
+ c = np.random.random((2, 3, 4))
+ van = poly.polyvander3d(x1, x2, x3, [1, 2, 3])
+ tgt = poly.polyval3d(x1, x2, x3, c)
+ res = np.dot(van, c.flat)
+ assert_almost_equal(res, tgt)
+
+ # check shape
+ van = poly.polyvander3d([x1], [x2], [x3], [1, 2, 3])
+ assert_(van.shape == (1, 5, 24))
+
class TestMisc(TestCase) :
@@ -247,23 +400,6 @@ class TestMisc(TestCase) :
res = poly.polyroots(poly.polyfromroots(tgt))
assert_almost_equal(trim(res), trim(tgt))
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], poly.polyval(x, coef))
-
- # check for 2d x
- 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) :
- coef = [0]*i + [1]
- assert_almost_equal(v[...,i], poly.polyval(x, coef))
-
def test_polyfit(self) :
def f(x) :
return x*(x - 1)*(x - 2)