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

numpy-svn at scipy.org numpy-svn at scipy.org
Sun May 23 18:02:11 EDT 2010


Author: charris
Date: 2010-05-23 17:02:11 -0500 (Sun, 23 May 2010)
New Revision: 8428

Modified:
   trunk/numpy/polynomial/polytemplate.py
   trunk/numpy/polynomial/tests/test_chebyshev.py
   trunk/numpy/polynomial/tests/test_polynomial.py
Log:
ENH: Add reduce method to polynomial.Chebyshev and
polynomial.Polynomial. This method behaves like truncate except
it takes the degree of the result instead of the number of
coefficients.

Modified: trunk/numpy/polynomial/polytemplate.py
===================================================================
--- trunk/numpy/polynomial/polytemplate.py	2010-05-23 22:02:08 UTC (rev 8427)
+++ trunk/numpy/polynomial/polytemplate.py	2010-05-23 22:02:11 UTC (rev 8428)
@@ -304,9 +304,42 @@
     #
 
     def degree(self) :
-        """The degree of the series."""
+        """The degree of the series.
+
+        Notes
+        -----
+        .. versionadded:: 2.0.0
+
+        """
         return len(self) - 1
 
+    def reduce(self, deg) :
+        """Reduce the degree of the series.
+
+        Reduce the degree of the $name series to `deg` by discarding the
+        high order terms. If `deg` is greater than the current degree a
+        copy of the current series is returned. This can be useful in least
+        squares where the coefficients of the high degree terms may be very
+        small.
+
+        Parameters:
+        -----------
+        deg : non-negative int
+            The series is reduced to degree `deg` by discarding the high
+            order terms. The value of `deg` must be a non-negative integer.
+
+        Returns:
+        -------
+        new_instance : $name
+            New instance of $name with reduced degree.
+
+        Notes
+        -----
+        .. versionadded:: 2.0.0
+
+        """
+        return self.truncate(deg + 1)
+
     def convert(self, domain=None, kind=None) :
         """Convert to different class and/or domain.
 

Modified: trunk/numpy/polynomial/tests/test_chebyshev.py
===================================================================
--- trunk/numpy/polynomial/tests/test_chebyshev.py	2010-05-23 22:02:08 UTC (rev 8427)
+++ trunk/numpy/polynomial/tests/test_chebyshev.py	2010-05-23 22:02:11 UTC (rev 8428)
@@ -402,6 +402,14 @@
     def test_degree(self) :
         assert_equal(self.p1.degree(), 2)
 
+    def test_reduce(self) :
+        assert_raises(ValueError, self.p1.reduce, .5)
+        assert_raises(ValueError, self.p1.reduce, -1)
+        assert_equal(len(self.p1.reduce(3)), 3)
+        assert_equal(len(self.p1.reduce(2)), 3)
+        assert_equal(len(self.p1.reduce(1)), 2)
+        assert_equal(len(self.p1.reduce(0)), 1)
+
     def test_convert(self) :
         x = np.linspace(-1,1)
         p = self.p1.convert(domain=[0,1])

Modified: trunk/numpy/polynomial/tests/test_polynomial.py
===================================================================
--- trunk/numpy/polynomial/tests/test_polynomial.py	2010-05-23 22:02:08 UTC (rev 8427)
+++ trunk/numpy/polynomial/tests/test_polynomial.py	2010-05-23 22:02:11 UTC (rev 8428)
@@ -373,6 +373,14 @@
     def test_degree(self) :
         assert_equal(self.p1.degree(), 2)
 
+    def test_reduce(self) :
+        assert_raises(ValueError, self.p1.reduce, .5)
+        assert_raises(ValueError, self.p1.reduce, -1)
+        assert_equal(len(self.p1.reduce(3)), 3)
+        assert_equal(len(self.p1.reduce(2)), 3)
+        assert_equal(len(self.p1.reduce(1)), 2)
+        assert_equal(len(self.p1.reduce(0)), 1)
+
     def test_convert(self) :
         x = np.linspace(-1,1)
         p = self.p1.convert(domain=[0,1])




More information about the Numpy-svn mailing list