diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/polynomial/polytemplate.py | 26 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_chebyshev.py | 9 | ||||
-rw-r--r-- | numpy/polynomial/tests/test_polynomial.py | 9 |
3 files changed, 24 insertions, 20 deletions
diff --git a/numpy/polynomial/polytemplate.py b/numpy/polynomial/polytemplate.py index 424ce7f48..c2030db1b 100644 --- a/numpy/polynomial/polytemplate.py +++ b/numpy/polynomial/polytemplate.py @@ -391,19 +391,20 @@ class $name(pu.PolyBase) : """ return self.__class__(pu.trimcoef(self.coef, tol), self.domain) - def truncate(self, size) : - """Truncate series by discarding trailing coefficients. + def truncate(self, deg) : + """Truncate series to degree `deg`. - Reduce the $name series to length `size` by removing trailing - coefficients. The value of `size` must be greater than zero. This - is most likely to be useful in least squares fits when the high - order coefficients are very small. + Return a $name series obtained from the current instance by discarding + all terms of degree greater than `deg`. The value of `deg` must be + non-negative. This operation is most likely to be useful in least squares + fits when the high order coefficients are very small. Parameters: ----------- - size : int - The series is reduced to length `size` by discarding trailing - coefficients. The value of `size` must be greater than zero. + deg : non-negative int + The series is reduced to degree `deg` by discarding the + coefficients of the higher degree terms. The value of `deg` + must be non-negative. Returns: ------- @@ -411,9 +412,10 @@ class $name(pu.PolyBase) : New instance of $name with truncated coefficients. """ - if size < 1 : - raise ValueError("size must be > 0") - if size >= len(self.coef) : + size = int(deg) + 1 + if size != deg + 1 or size < 1 : + raise ValueError("deg must be a non-negative integer") + if size >= len(self) : return self.__class__(self.coef, self.domain) else : return self.__class__(self.coef[:size], self.domain) diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py index 6f42d06b9..b2c4ee105 100644 --- a/numpy/polynomial/tests/test_chebyshev.py +++ b/numpy/polynomial/tests/test_chebyshev.py @@ -416,11 +416,12 @@ class TestChebyshevClass(TestCase) : assert_equal(p.trim(1e-5).coef, coef[:1]) def test_truncate(self) : - assert_raises(ValueError, self.p1.truncate, 0) - assert_equal(len(self.p1.truncate(4)), 3) + assert_raises(ValueError, self.p1.truncate, .5) + assert_raises(ValueError, self.p1.truncate, -1) assert_equal(len(self.p1.truncate(3)), 3) - assert_equal(len(self.p1.truncate(2)), 2) - assert_equal(len(self.p1.truncate(1)), 1) + assert_equal(len(self.p1.truncate(2)), 3) + assert_equal(len(self.p1.truncate(1)), 2) + assert_equal(len(self.p1.truncate(0)), 1) def test_copy(self) : p = self.p1.copy() diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index 34a3d10f3..7ba5b6c8f 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -390,11 +390,12 @@ class TestPolynomialClass(TestCase) : assert_equal(p.trim(1e-5).coef, coef[:1]) def test_truncate(self) : - assert_raises(ValueError, self.p1.truncate, 0) - assert_equal(len(self.p1.truncate(4)), 3) + assert_raises(ValueError, self.p1.truncate, .5) + assert_raises(ValueError, self.p1.truncate, -1) assert_equal(len(self.p1.truncate(3)), 3) - assert_equal(len(self.p1.truncate(2)), 2) - assert_equal(len(self.p1.truncate(1)), 1) + assert_equal(len(self.p1.truncate(2)), 3) + assert_equal(len(self.p1.truncate(1)), 2) + assert_equal(len(self.p1.truncate(0)), 1) def test_copy(self) : p = self.p1.copy() |