summaryrefslogtreecommitdiff
path: root/numpy/polynomial
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2010-05-21 05:35:58 +0000
committerCharles Harris <charlesr.harris@gmail.com>2010-05-21 05:35:58 +0000
commit2041434cbe99115c4c26a7910904769d9bca5c7b (patch)
tree3cc7131cd35da8b5605e321543a8238eca22d81d /numpy/polynomial
parent87e2eecd808c9770355437ca871b70fe9f87b243 (diff)
downloadnumpy-2041434cbe99115c4c26a7910904769d9bca5c7b.tar.gz
ENH:
1) Let {poly,cheb}int accept 0 for the number of integrations. 2) Let {poly,cheb}(int,der} accept floating integers for number of integrations or derivations, raise ValueError otherwise. 3) Add tests for same.
Diffstat (limited to 'numpy/polynomial')
-rw-r--r--numpy/polynomial/chebyshev.py47
-rw-r--r--numpy/polynomial/polynomial.py53
-rw-r--r--numpy/polynomial/tests/test_chebyshev.py10
-rw-r--r--numpy/polynomial/tests/test_polynomial.py7
4 files changed, 78 insertions, 39 deletions
diff --git a/numpy/polynomial/chebyshev.py b/numpy/polynomial/chebyshev.py
index 662f664a9..2b6d7b4bb 100644
--- a/numpy/polynomial/chebyshev.py
+++ b/numpy/polynomial/chebyshev.py
@@ -846,25 +846,29 @@ def chebder(cs, m=1, scl=1) :
array([ 12., 96.])
"""
- # cs is a trimmed copy
- [cs] = pu.as_series([cs])
- if m < 0 :
- raise ValueError, "The order of derivation must be positive"
+ cnt = int(m)
+
+ if cnt != m:
+ raise ValueError, "The order of derivation must be integer"
+ if cnt < 0 :
+ raise ValueError, "The order of derivation must be non-negative"
if not np.isscalar(scl) :
raise ValueError, "The scl parameter must be a scalar"
- if m == 0 :
+ # cs is a trimmed copy
+ [cs] = pu.as_series([cs])
+ if cnt == 0:
return cs
- elif m >= len(cs) :
+ elif cnt >= len(cs):
return cs[:1]*0
else :
zs = _cseries_to_zseries(cs)
- for i in range(m) :
+ for i in range(cnt):
zs = _zseries_der(zs)*scl
return _zseries_to_cseries(zs)
-def chebint(cs, m=1, k=[], lbnd=0, scl=1) :
+def chebint(cs, m=1, k=[], lbnd=0, scl=1):
"""
Integrate a Chebyshev series.
@@ -941,11 +945,15 @@ def chebint(cs, m=1, k=[], lbnd=0, scl=1) :
array([-1., 1., -1., -1.])
"""
+ cnt = int(m)
if np.isscalar(k) :
k = [k]
- if m < 1 :
- raise ValueError, "The order of integration must be positive"
- if len(k) > m :
+
+ if cnt != m:
+ raise ValueError, "The order of integration must be integer"
+ if cnt < 0 :
+ raise ValueError, "The order of integration must be non-negative"
+ if len(k) > cnt :
raise ValueError, "Too many integration constants"
if not np.isscalar(lbnd) :
raise ValueError, "The lbnd parameter must be a scalar"
@@ -954,13 +962,16 @@ def chebint(cs, m=1, k=[], lbnd=0, scl=1) :
# cs is a trimmed copy
[cs] = pu.as_series([cs])
- k = list(k) + [0]*(m - len(k))
- for i in range(m) :
- zs = _cseries_to_zseries(cs)*scl
- zs = _zseries_int(zs)
- cs = _zseries_to_cseries(zs)
- cs[0] += k[i] - chebval(lbnd, cs)
- return cs
+ if cnt == 0:
+ return cs
+ else:
+ k = list(k) + [0]*(cnt - len(k))
+ for i in range(cnt) :
+ zs = _cseries_to_zseries(cs)*scl
+ zs = _zseries_int(zs)
+ cs = _zseries_to_cseries(zs)
+ cs[0] += k[i] - chebval(lbnd, cs)
+ return cs
def chebval(x, cs):
"""Evaluate a Chebyshev series.
diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py
index 60b28c41f..9f98ad4ca 100644
--- a/numpy/polynomial/polynomial.py
+++ b/numpy/polynomial/polynomial.py
@@ -410,7 +410,7 @@ def polypow(cs, pow, maxpower=None) :
prd = np.convolve(prd, cs)
return prd
-def polyder(cs, m=1, scl=1) :
+def polyder(cs, m=1, scl=1):
"""
Differentiate a polynomial.
@@ -454,25 +454,29 @@ def polyder(cs, m=1, scl=1) :
array([ 6., 24.])
"""
- # cs is a trimmed copy
- [cs] = pu.as_series([cs])
- if m < 0 :
- raise ValueError, "The order of derivation must be positive"
- if not np.isscalar(scl) :
+ cnt = int(m)
+
+ if cnt != m:
+ raise ValueError, "The order of derivation must be integer"
+ if cnt < 0:
+ raise ValueError, "The order of derivation must be non-negative"
+ if not np.isscalar(scl):
raise ValueError, "The scl parameter must be a scalar"
- if m == 0 :
+ # cs is a trimmed copy
+ [cs] = pu.as_series([cs])
+ if cnt == 0:
return cs
- elif m >= len(cs) :
+ elif cnt >= len(cs):
return cs[:1]*0
else :
n = len(cs)
d = np.arange(n)*scl
- for i in range(m) :
+ for i in range(cnt):
cs[i:] *= d[:n-i]
return cs[i+1:].copy()
-def polyint(cs, m=1, k=[], lbnd=0, scl=1) :
+def polyint(cs, m=1, k=[], lbnd=0, scl=1):
"""
Integrate a polynomial.
@@ -544,11 +548,15 @@ def polyint(cs, m=1, k=[], lbnd=0, scl=1) :
array([ 0., -2., -2., -2.])
"""
+ cnt = int(m)
if np.isscalar(k) :
k = [k]
- if m < 1 :
- raise ValueError, "The order of integration must be positive"
- if len(k) > m :
+
+ if cnt != m:
+ raise ValueError, "The order of integration must be integer"
+ if cnt < 0 :
+ raise ValueError, "The order of integration must be non-negative"
+ if len(k) > cnt :
raise ValueError, "Too many integration constants"
if not np.isscalar(lbnd) :
raise ValueError, "The lbnd parameter must be a scalar"
@@ -557,14 +565,17 @@ def polyint(cs, m=1, k=[], lbnd=0, scl=1) :
# cs is a trimmed copy
[cs] = pu.as_series([cs])
- k = list(k) + [0]*(m - len(k))
- fac = np.arange(1, len(cs) + m)/scl
- ret = np.zeros(len(cs) + m, dtype=cs.dtype)
- ret[m:] = cs
- for i in range(m) :
- ret[m - i:] /= fac[:len(cs) + i]
- ret[m - i - 1] += k[i] - polyval(lbnd, ret[m - i - 1:])
- return ret
+ if cnt == 0:
+ return cs
+ else:
+ k = list(k) + [0]*(cnt - len(k))
+ fac = np.arange(1, len(cs) + cnt)/scl
+ ret = np.zeros(len(cs) + cnt, dtype=cs.dtype)
+ ret[cnt:] = cs
+ for i in range(cnt) :
+ ret[cnt - i:] /= fac[:len(cs) + i]
+ ret[cnt - i - 1] += k[i] - polyval(lbnd, ret[cnt - i - 1:])
+ return ret
def polyval(x, cs):
"""
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py
index 10e3fdafa..6f42d06b9 100644
--- a/numpy/polynomial/tests/test_chebyshev.py
+++ b/numpy/polynomial/tests/test_chebyshev.py
@@ -136,6 +136,7 @@ class TestCalculus(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, ch.chebint, [0], 1, lbnd=[0,0])
@@ -211,18 +212,22 @@ class TestCalculus(TestCase) :
def test_chebder(self) :
# check exceptions
+ assert_raises(ValueError, ch.chebder, [0], .5)
assert_raises(ValueError, ch.chebder, [0], -1)
+
# check that zeroth deriviative does nothing
for i in range(5) :
tgt = [1] + [0]*i
res = ch.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 = [1] + [0]*i
res = ch.chebder(ch.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) :
@@ -258,6 +263,7 @@ class TestMisc(TestCase) :
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)
@@ -269,6 +275,7 @@ class TestMisc(TestCase) :
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)
@@ -276,6 +283,7 @@ class TestMisc(TestCase) :
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)
+
# Test fit
x = np.linspace(0,2)
y = f(x)
@@ -290,8 +298,10 @@ class TestMisc(TestCase) :
def test_chebtrim(self) :
coef = [2, -1, 1, 0]
+
# Test exceptions
assert_raises(ValueError, ch.chebtrim, coef, -1)
+
# Test results
assert_equal(ch.chebtrim(coef), coef[:-1])
assert_equal(ch.chebtrim(coef, 1), coef[:-3])
diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py
index 936375a80..81286d01b 100644
--- a/numpy/polynomial/tests/test_polynomial.py
+++ b/numpy/polynomial/tests/test_polynomial.py
@@ -121,6 +121,7 @@ class TestCalculus(TestCase) :
def test_polyint(self) :
# check exceptions
+ assert_raises(ValueError, poly.polyint, [0], .5)
assert_raises(ValueError, poly.polyint, [0], -1)
assert_raises(ValueError, poly.polyint, [0], 1, [0,0])
assert_raises(ValueError, poly.polyint, [0], 1, lbnd=[0,0])
@@ -191,6 +192,7 @@ class TestCalculus(TestCase) :
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
@@ -241,6 +243,7 @@ class TestMisc(TestCase) :
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)
@@ -252,6 +255,7 @@ class TestMisc(TestCase) :
def test_polyfit(self) :
def f(x) :
return x*(x - 1)*(x - 2)
+
# Test exceptions
assert_raises(ValueError, poly.polyfit, [1], [1], -1)
assert_raises(TypeError, poly.polyfit, [[1]], [1], 0)
@@ -259,6 +263,7 @@ class TestMisc(TestCase) :
assert_raises(TypeError, poly.polyfit, [1], [[[1]]], 0)
assert_raises(TypeError, poly.polyfit, [1, 2], [1], 0)
assert_raises(TypeError, poly.polyfit, [1], [1, 2], 0)
+
# Test fit
x = np.linspace(0,2)
y = f(x)
@@ -273,8 +278,10 @@ class TestMisc(TestCase) :
def test_polytrim(self) :
coef = [2, -1, 1, 0]
+
# Test exceptions
assert_raises(ValueError, poly.polytrim, coef, -1)
+
# Test results
assert_equal(poly.polytrim(coef), coef[:-1])
assert_equal(poly.polytrim(coef, 1), coef[:-3])