[Numpy-svn] r8635 - in trunk/numpy/polynomial: . tests

numpy-svn@scip... numpy-svn@scip...
Sat Aug 14 19:40:26 CDT 2010


Author: charris
Date: 2010-08-14 19:40:26 -0500 (Sat, 14 Aug 2010)
New Revision: 8635

Modified:
   trunk/numpy/polynomial/chebyshev.py
   trunk/numpy/polynomial/polynomial.py
   trunk/numpy/polynomial/tests/test_chebyshev.py
   trunk/numpy/polynomial/tests/test_polynomial.py
Log:
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.

Modified: trunk/numpy/polynomial/chebyshev.py
===================================================================
--- trunk/numpy/polynomial/chebyshev.py	2010-08-14 13:58:37 UTC (rev 8634)
+++ trunk/numpy/polynomial/chebyshev.py	2010-08-15 00:40:26 UTC (rev 8635)
@@ -852,8 +852,6 @@
         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 @@
         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.

Modified: trunk/numpy/polynomial/polynomial.py
===================================================================
--- trunk/numpy/polynomial/polynomial.py	2010-08-14 13:58:37 UTC (rev 8634)
+++ trunk/numpy/polynomial/polynomial.py	2010-08-15 00:40:26 UTC (rev 8635)
@@ -460,8 +460,6 @@
         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,25 +556,26 @@
         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):
     """
     Evaluate a polynomial.

Modified: trunk/numpy/polynomial/tests/test_chebyshev.py
===================================================================
--- trunk/numpy/polynomial/tests/test_chebyshev.py	2010-08-14 13:58:37 UTC (rev 8634)
+++ trunk/numpy/polynomial/tests/test_chebyshev.py	2010-08-15 00:40:26 UTC (rev 8635)
@@ -139,9 +139,13 @@
         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) :
             scl = i + 1

Modified: trunk/numpy/polynomial/tests/test_polynomial.py
===================================================================
--- trunk/numpy/polynomial/tests/test_polynomial.py	2010-08-14 13:58:37 UTC (rev 8634)
+++ trunk/numpy/polynomial/tests/test_polynomial.py	2010-08-15 00:40:26 UTC (rev 8635)
@@ -124,9 +124,13 @@
         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) :
             scl = i + 1



More information about the Numpy-svn mailing list