summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2010-05-23 22:02:11 +0000
committerCharles Harris <charlesr.harris@gmail.com>2010-05-23 22:02:11 +0000
commit4ac790852f101dc36ffc50a5eeac4e0fe157b2d7 (patch)
tree15ed5aa6ca666d70c2297542510e17477121c455 /numpy
parent6e77005137eca1e7fdd4ebd3935f7f57d6aba7bd (diff)
downloadnumpy-4ac790852f101dc36ffc50a5eeac4e0fe157b2d7.tar.gz
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.
Diffstat (limited to 'numpy')
-rw-r--r--numpy/polynomial/polytemplate.py35
-rw-r--r--numpy/polynomial/tests/test_chebyshev.py8
-rw-r--r--numpy/polynomial/tests/test_polynomial.py8
3 files changed, 50 insertions, 1 deletions
diff --git a/numpy/polynomial/polytemplate.py b/numpy/polynomial/polytemplate.py
index bf94d28ec..706f22c2d 100644
--- a/numpy/polynomial/polytemplate.py
+++ b/numpy/polynomial/polytemplate.py
@@ -304,9 +304,42 @@ class $name(pu.PolyBase) :
#
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.
diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py
index d61e8b07c..5662d0362 100644
--- a/numpy/polynomial/tests/test_chebyshev.py
+++ b/numpy/polynomial/tests/test_chebyshev.py
@@ -402,6 +402,14 @@ class TestChebyshevClass(TestCase) :
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])
diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py
index 530e5d949..15525fcd8 100644
--- a/numpy/polynomial/tests/test_polynomial.py
+++ b/numpy/polynomial/tests/test_polynomial.py
@@ -373,6 +373,14 @@ class TestPolynomialClass(TestCase) :
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])