diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2010-08-15 00:40:26 +0000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2010-08-15 00:40:26 +0000 |
commit | 6b9762c0a918acb583bee7e387863123e339e46c (patch) | |
tree | 383363edd7b3ede838d6c4eba69f514d1dabd878 | |
parent | 50db7ff052aef5e0399464a6bbaa7540b83247ea (diff) | |
download | numpy-6b9762c0a918acb583bee7e387863123e339e46c.tar.gz |
BUG: Fix integration of zero polynomials.
Remove checks that prevent use of foreign scalar types for lower
bounds and integration constants.
Cleanup code a bit.
-rw-r--r-- | numpy/polynomial/chebyshev.py | 21 | ||||
-rw-r--r-- | numpy/polynomial/polynomial.py | 29 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_chebyshev.py | 8 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_polynomial.py | 8 |
4 files changed, 36 insertions, 30 deletions
diff --git a/numpy/polynomial/chebyshev.py b/numpy/polynomial/chebyshev.py index 99edecca1..533b4c293 100644 --- a/numpy/polynomial/chebyshev.py +++ b/numpy/polynomial/chebyshev.py @@ -852,8 +852,6 @@ def chebder(cs, m=1, scl=1) : 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" # cs is a trimmed copy [cs] = pu.as_series([cs]) @@ -955,23 +953,24 @@ def chebint(cs, m=1, k=[], lbnd=0, scl=1): 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" - if not np.isscalar(scl) : - raise ValueError, "The scl parameter must be a scalar" # cs is a trimmed copy [cs] = pu.as_series([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 + + k = list(k) + [0]*(cnt - len(k)) + for i in range(cnt) : + n = len(cs) + cs *= scl + if n == 1 and cs[0] == 0: + cs[0] += k[i] + else: + zs = _cseries_to_zseries(cs) zs = _zseries_int(zs) cs = _zseries_to_cseries(zs) cs[0] += k[i] - chebval(lbnd, cs) - return cs + return cs def chebval(x, cs): """Evaluate a Chebyshev series. diff --git a/numpy/polynomial/polynomial.py b/numpy/polynomial/polynomial.py index 3144d9985..e4d9e0fcb 100644 --- a/numpy/polynomial/polynomial.py +++ b/numpy/polynomial/polynomial.py @@ -460,8 +460,6 @@ def polyder(cs, m=1, scl=1): 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" # cs is a trimmed copy [cs] = pu.as_series([cs]) @@ -558,24 +556,25 @@ def polyint(cs, m=1, k=[], lbnd=0, scl=1): 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" - if not np.isscalar(scl) : - raise ValueError, "The scl parameter must be a scalar" # cs is a trimmed copy [cs] = pu.as_series([cs]) 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 + + k = list(k) + [0]*(cnt - len(k)) + for i in range(cnt): + n = len(cs) + cs *= scl + if n == 1 and cs[0] == 0: + cs[0] += k[i] + else: + tmp = np.empty(n + 1, dtype=cs.dtype) + tmp[0] = cs[0]*0 + tmp[1:] = cs/np.arange(1, n + 1) + tmp[0] += k[i] - polyval(lbnd, tmp) + cs = tmp + return cs def polyval(x, cs): """ diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py index 981481ff1..8ae1dee6f 100644 --- a/numpy/polynomial/tests/test_chebyshev.py +++ b/numpy/polynomial/tests/test_chebyshev.py @@ -139,8 +139,12 @@ class TestCalculus(TestCase) : 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]) - assert_raises(ValueError, ch.chebint, [0], 1, scl=[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) + assert_almost_equal(res, [0, 1]) # check single integration with integration constant for i in range(5) : diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index 4bfbc46d9..3a3f26861 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -124,8 +124,12 @@ class TestCalculus(TestCase) : 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]) - assert_raises(ValueError, poly.polyint, [0], 1, scl=[0,0]) + + # test integration of zero polynomial + for i in range(2, 5): + k = [0]*(i - 2) + [1] + res = poly.polyint([0], m=i, k=k) + assert_almost_equal(res, [0, 1]) # check single integration with integration constant for i in range(5) : |