summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/polynomial/tests/test_chebyshev.py68
-rw-r--r--numpy/polynomial/tests/test_hermite.py74
-rw-r--r--numpy/polynomial/tests/test_hermite_e.py70
-rw-r--r--numpy/polynomial/tests/test_laguerre.py74
-rw-r--r--numpy/polynomial/tests/test_legendre.py74
5 files changed, 254 insertions, 106 deletions
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py
index f0f2748b2..56d720f2d 100644
--- a/numpy/polynomial/tests/test_chebyshev.py
+++ b/numpy/polynomial/tests/test_chebyshev.py
@@ -6,7 +6,9 @@ from __future__ import division
import numpy as np
import numpy.polynomial.chebyshev as cheb
from numpy.polynomial.polynomial import polyval
-from numpy.testing import *
+from numpy.testing import (
+ TestCase, assert_almost_equal, assert_raises,
+ assert_equal, assert_, run_module_suite)
def trim(x) :
return cheb.chebtrim(x, tol=1e-6)
@@ -393,24 +395,7 @@ class TestVander(TestCase):
assert_(van.shape == (1, 5, 24))
-class TestMisc(TestCase) :
-
- def test_chebfromroots(self) :
- 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 = cheb.chebfromroots(roots)*2**(i-1)
- assert_almost_equal(trim(res),trim(tgt))
-
- def test_chebroots(self) :
- 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 = cheb.chebroots(cheb.chebfromroots(tgt))
- assert_almost_equal(trim(res), trim(tgt))
+class TestFitting(TestCase):
def test_chebfit(self) :
def f(x) :
@@ -451,6 +436,45 @@ class TestMisc(TestCase) :
wcoef2d = cheb.chebfit(x, np.array([yw,yw]).T, 3, w=w)
assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
+
+class TestGauss(TestCase):
+
+ def test_100(self):
+ x, w = cheb.chebgauss(100)
+
+ # test orthogonality. Note that the results need to be normalized,
+ # otherwise the huge values that can arise from fast growing
+ # functions like Laguerre can be very confusing.
+ v = cheb.chebvander(x, 99)
+ vv = np.dot(v.T * w, v)
+ vd = 1/np.sqrt(vv.diagonal())
+ vv = vd[:,None] * vv * vd
+ assert_almost_equal(vv, np.eye(100))
+
+ # check that the integral of 1 is correct
+ tgt = np.pi
+ assert_almost_equal(w.sum(), tgt)
+
+
+class TestMisc(TestCase) :
+
+ def test_chebfromroots(self) :
+ 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 = cheb.chebfromroots(roots)*2**(i-1)
+ assert_almost_equal(trim(res),trim(tgt))
+
+ def test_chebroots(self) :
+ 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 = cheb.chebroots(cheb.chebfromroots(tgt))
+ assert_almost_equal(trim(res), trim(tgt))
+
def test_chebtrim(self) :
coef = [2, -1, 1, 0]
@@ -473,6 +497,12 @@ class TestMisc(TestCase) :
for i in range(10) :
assert_almost_equal(cheb.poly2cheb(Tlist[i]), [0]*i + [1])
+ def test_weight(self):
+ x = np.linspace(-1, 1, 11)[1:-1]
+ tgt = 1./(np.sqrt(1 + x) * np.sqrt(1 - x))
+ res = cheb.chebweight(x)
+ assert_almost_equal(res, tgt)
+
def test_chebpts1(self):
#test exceptions
assert_raises(ValueError, cheb.chebpts1, 1.5)
diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py
index 87b64a69a..b40d3b944 100644
--- a/numpy/polynomial/tests/test_hermite.py
+++ b/numpy/polynomial/tests/test_hermite.py
@@ -6,7 +6,9 @@ from __future__ import division
import numpy as np
import numpy.polynomial.hermite as herm
from numpy.polynomial.polynomial import polyval
-from numpy.testing import *
+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])
@@ -383,27 +385,7 @@ class TestVander(TestCase):
assert_(van.shape == (1, 5, 24))
-class TestMisc(TestCase) :
-
- def test_hermfromroots(self) :
- res = herm.hermfromroots([])
- 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])
- pol = herm.hermfromroots(roots)
- res = herm.hermval(roots, pol)
- tgt = 0
- assert_(len(pol) == i + 1)
- assert_almost_equal(herm.herm2poly(pol)[-1], 1)
- assert_almost_equal(res, tgt)
-
- def test_hermroots(self) :
- assert_almost_equal(herm.hermroots([1]), [])
- assert_almost_equal(herm.hermroots([1, 1]), [-.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))
+class TestFitting(TestCase):
def test_hermfit(self) :
def f(x) :
@@ -444,6 +426,48 @@ class TestMisc(TestCase) :
wcoef2d = herm.hermfit(x, np.array([yw,yw]).T, 3, w=w)
assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
+
+class TestGauss(TestCase):
+
+ def test_100(self):
+ x, w = herm.hermgauss(100)
+
+ # test orthogonality. Note that the results need to be normalized,
+ # otherwise the huge values that can arise from fast growing
+ # functions like Laguerre can be very confusing.
+ v = herm.hermvander(x, 99)
+ vv = np.dot(v.T * w, v)
+ vd = 1/np.sqrt(vv.diagonal())
+ vv = vd[:,None] * vv * vd
+ assert_almost_equal(vv, np.eye(100))
+
+ # check that the integral of 1 is correct
+ tgt = np.sqrt(np.pi)
+ assert_almost_equal(w.sum(), tgt)
+
+
+class TestMisc(TestCase) :
+
+ def test_hermfromroots(self) :
+ res = herm.hermfromroots([])
+ 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])
+ pol = herm.hermfromroots(roots)
+ res = herm.hermval(roots, pol)
+ tgt = 0
+ assert_(len(pol) == i + 1)
+ assert_almost_equal(herm.herm2poly(pol)[-1], 1)
+ assert_almost_equal(res, tgt)
+
+ def test_hermroots(self) :
+ assert_almost_equal(herm.hermroots([1]), [])
+ assert_almost_equal(herm.hermroots([1, 1]), [-.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) :
coef = [2, -1, 1, 0]
@@ -466,6 +490,12 @@ class TestMisc(TestCase) :
for i in range(10) :
assert_almost_equal(herm.poly2herm(Hlist[i]), [0]*i + [1])
+ def test_weight(self):
+ x = np.linspace(-5, 5, 11)
+ tgt = np.exp(-x**2)
+ res = herm.hermweight(x)
+ assert_almost_equal(res, tgt)
+
def assert_poly_almost_equal(p1, p2):
assert_almost_equal(p1.coef, p2.coef)
diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py
index a29dcd240..18e42416c 100644
--- a/numpy/polynomial/tests/test_hermite_e.py
+++ b/numpy/polynomial/tests/test_hermite_e.py
@@ -382,27 +382,7 @@ class TestVander(TestCase):
assert_(van.shape == (1, 5, 24))
-class TestMisc(TestCase) :
-
- def test_hermefromroots(self) :
- res = herme.hermefromroots([])
- 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])
- pol = herme.hermefromroots(roots)
- res = herme.hermeval(roots, pol)
- tgt = 0
- assert_(len(pol) == i + 1)
- assert_almost_equal(herme.herme2poly(pol)[-1], 1)
- assert_almost_equal(res, tgt)
-
- def test_hermeroots(self) :
- assert_almost_equal(herme.hermeroots([1]), [])
- assert_almost_equal(herme.hermeroots([1, 1]), [-1])
- 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))
+class TestFitting(TestCase):
def test_hermefit(self) :
def f(x) :
@@ -443,6 +423,48 @@ class TestMisc(TestCase) :
wcoef2d = herme.hermefit(x, np.array([yw,yw]).T, 3, w=w)
assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
+
+class TestGauss(TestCase):
+
+ def test_100(self):
+ x, w = herme.hermegauss(100)
+
+ # test orthogonality. Note that the results need to be normalized,
+ # otherwise the huge values that can arise from fast growing
+ # functions like Laguerre can be very confusing.
+ v = herme.hermevander(x, 99)
+ vv = np.dot(v.T * w, v)
+ vd = 1/np.sqrt(vv.diagonal())
+ vv = vd[:,None] * vv * vd
+ assert_almost_equal(vv, np.eye(100))
+
+ # check that the integral of 1 is correct
+ tgt = np.sqrt(2*np.pi)
+ assert_almost_equal(w.sum(), tgt)
+
+
+class TestMisc(TestCase) :
+
+ def test_hermefromroots(self) :
+ res = herme.hermefromroots([])
+ 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])
+ pol = herme.hermefromroots(roots)
+ res = herme.hermeval(roots, pol)
+ tgt = 0
+ assert_(len(pol) == i + 1)
+ assert_almost_equal(herme.herme2poly(pol)[-1], 1)
+ assert_almost_equal(res, tgt)
+
+ def test_hermeroots(self) :
+ assert_almost_equal(herme.hermeroots([1]), [])
+ assert_almost_equal(herme.hermeroots([1, 1]), [-1])
+ 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) :
coef = [2, -1, 1, 0]
@@ -465,6 +487,12 @@ class TestMisc(TestCase) :
for i in range(10) :
assert_almost_equal(herme.poly2herme(Helist[i]), [0]*i + [1])
+ def test_weight(self):
+ x = np.linspace(-5, 5, 11)
+ tgt = np.exp(-.5*x**2)
+ res = herme.hermeweight(x)
+ assert_almost_equal(res, tgt)
+
def assert_poly_almost_equal(p1, p2):
assert_almost_equal(p1.coef, p2.coef)
diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py
index 90fd7afb7..a95466aed 100644
--- a/numpy/polynomial/tests/test_laguerre.py
+++ b/numpy/polynomial/tests/test_laguerre.py
@@ -6,7 +6,9 @@ from __future__ import division
import numpy as np
import numpy.polynomial.laguerre as lag
from numpy.polynomial.polynomial import polyval
-from numpy.testing import *
+from numpy.testing import (
+ TestCase, assert_almost_equal, assert_raises,
+ assert_equal, assert_, run_module_suite)
L0 = np.array([1 ])/1
L1 = np.array([1 , -1 ])/1
@@ -378,27 +380,7 @@ class TestVander(TestCase):
assert_(van.shape == (1, 5, 24))
-class TestMisc(TestCase) :
-
- def test_lagfromroots(self) :
- res = lag.lagfromroots([])
- 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])
- pol = lag.lagfromroots(roots)
- res = lag.lagval(roots, pol)
- tgt = 0
- assert_(len(pol) == i + 1)
- assert_almost_equal(lag.lag2poly(pol)[-1], 1)
- assert_almost_equal(res, tgt)
-
- def test_lagroots(self) :
- assert_almost_equal(lag.lagroots([1]), [])
- assert_almost_equal(lag.lagroots([0, 1]), [1])
- 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))
+class TestFitting(TestCase):
def test_lagfit(self) :
def f(x) :
@@ -439,6 +421,48 @@ class TestMisc(TestCase) :
wcoef2d = lag.lagfit(x, np.array([yw,yw]).T, 3, w=w)
assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
+
+class TestGauss(TestCase):
+
+ def test_100(self):
+ x, w = lag.laggauss(100)
+
+ # test orthogonality. Note that the results need to be normalized,
+ # otherwise the huge values that can arise from fast growing
+ # functions like Laguerre can be very confusing.
+ v = lag.lagvander(x, 99)
+ vv = np.dot(v.T * w, v)
+ vd = 1/np.sqrt(vv.diagonal())
+ vv = vd[:,None] * vv * vd
+ assert_almost_equal(vv, np.eye(100))
+
+ # check that the integral of 1 is correct
+ tgt = 1.0
+ assert_almost_equal(w.sum(), tgt)
+
+
+class TestMisc(TestCase) :
+
+ def test_lagfromroots(self) :
+ res = lag.lagfromroots([])
+ 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])
+ pol = lag.lagfromroots(roots)
+ res = lag.lagval(roots, pol)
+ tgt = 0
+ assert_(len(pol) == i + 1)
+ assert_almost_equal(lag.lag2poly(pol)[-1], 1)
+ assert_almost_equal(res, tgt)
+
+ def test_lagroots(self) :
+ assert_almost_equal(lag.lagroots([1]), [])
+ assert_almost_equal(lag.lagroots([0, 1]), [1])
+ 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) :
coef = [2, -1, 1, 0]
@@ -461,6 +485,12 @@ class TestMisc(TestCase) :
for i in range(7) :
assert_almost_equal(lag.poly2lag(Llist[i]), [0]*i + [1])
+ def test_weight(self):
+ x = np.linspace(0, 10, 11)
+ tgt = np.exp(-x)
+ res = lag.lagweight(x)
+ assert_almost_equal(res, tgt)
+
def assert_poly_almost_equal(p1, p2):
assert_almost_equal(p1.coef, p2.coef)
diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py
index af64a2b32..d7bb238a9 100644
--- a/numpy/polynomial/tests/test_legendre.py
+++ b/numpy/polynomial/tests/test_legendre.py
@@ -6,7 +6,9 @@ from __future__ import division
import numpy as np
import numpy.polynomial.legendre as leg
from numpy.polynomial.polynomial import polyval
-from numpy.testing import *
+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])
@@ -381,27 +383,7 @@ class TestVander(TestCase):
assert_(van.shape == (1, 5, 24))
-class TestMisc(TestCase) :
-
- def test_legfromroots(self) :
- res = leg.legfromroots([])
- 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])
- pol = leg.legfromroots(roots)
- res = leg.legval(roots, pol)
- tgt = 0
- assert_(len(pol) == i + 1)
- assert_almost_equal(leg.leg2poly(pol)[-1], 1)
- assert_almost_equal(res, tgt)
-
- def test_legroots(self) :
- assert_almost_equal(leg.legroots([1]), [])
- assert_almost_equal(leg.legroots([1, 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))
+class TestFitting(TestCase):
def test_legfit(self) :
def f(x) :
@@ -442,6 +424,48 @@ class TestMisc(TestCase) :
wcoef2d = leg.legfit(x, np.array([yw,yw]).T, 3, w=w)
assert_almost_equal(wcoef2d, np.array([coef3,coef3]).T)
+
+class TestGauss(TestCase):
+
+ def test_100(self):
+ x, w = leg.leggauss(100)
+
+ # test orthogonality. Note that the results need to be normalized,
+ # otherwise the huge values that can arise from fast growing
+ # functions like Laguerre can be very confusing.
+ v = leg.legvander(x, 99)
+ vv = np.dot(v.T * w, v)
+ vd = 1/np.sqrt(vv.diagonal())
+ vv = vd[:,None] * vv * vd
+ assert_almost_equal(vv, np.eye(100))
+
+ # check that the integral of 1 is correct
+ tgt = 2.0
+ assert_almost_equal(w.sum(), tgt)
+
+
+class TestMisc(TestCase) :
+
+ def test_legfromroots(self) :
+ res = leg.legfromroots([])
+ 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])
+ pol = leg.legfromroots(roots)
+ res = leg.legval(roots, pol)
+ tgt = 0
+ assert_(len(pol) == i + 1)
+ assert_almost_equal(leg.leg2poly(pol)[-1], 1)
+ assert_almost_equal(res, tgt)
+
+ def test_legroots(self) :
+ assert_almost_equal(leg.legroots([1]), [])
+ assert_almost_equal(leg.legroots([1, 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) :
coef = [2, -1, 1, 0]
@@ -464,6 +488,12 @@ class TestMisc(TestCase) :
for i in range(10) :
assert_almost_equal(leg.poly2leg(Llist[i]), [0]*i + [1])
+ def test_weight(self):
+ x = np.linspace(-1, 1, 11)
+ tgt = 1.
+ res = leg.legweight(x)
+ assert_almost_equal(res, tgt)
+
def assert_poly_almost_equal(p1, p2):
assert_almost_equal(p1.coef, p2.coef)