diff options
author | Pauli Virtanen <pav@iki.fi> | 2009-04-24 20:31:22 +0000 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2009-04-24 20:31:22 +0000 |
commit | 1a613e1c460d2e7756724b7e5c638c95519ee498 (patch) | |
tree | 31ce56bc09e829271899c784e70f6b2c80eeebbc /numpy/lib | |
parent | 653d2b27fbe3771d7b669da0aec62a0cdb0654ac (diff) | |
download | numpy-1a613e1c460d2e7756724b7e5c638c95519ee498.tar.gz |
Fixed #1095: make polyint work well with object arrays
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/polynomial.py | 7 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 23 |
2 files changed, 26 insertions, 4 deletions
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 5c3d146f8..fee498757 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -241,15 +241,14 @@ def polyint(p, m=1, k=None): "k must be a scalar or a rank-1 array of length 1 or >m." truepoly = isinstance(p, poly1d) - p = NX.asarray(p) + 0.0 + p = NX.asarray(p) if m == 0: if truepoly: return poly1d(p) return p else: - y = NX.zeros(len(p) + 1, p.dtype) - y[:-1] = p*1.0/NX.arange(len(p), 0, -1) - y[-1] = k[0] + # Note: this must work also with object and integer arrays + y = NX.concatenate((p.__truediv__(NX.arange(len(p), 0, -1)), [k[0]])) val = polyint(y, m - 1, k=k[1:]) if truepoly: return poly1d(val) diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 96dd084b2..45e68d248 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -110,6 +110,29 @@ class TestDocs(TestCase): cc = np.concatenate((c,c), axis=1) assert_almost_equal(cc, np.polyfit(x,yy,2)) + def test_objects(self): + from decimal import Decimal + p = np.poly1d([Decimal('4.0'), Decimal('3.0'), Decimal('2.0')]) + p2 = p * Decimal('1.333333333333333') + assert p2[1] == Decimal("3.9999999999999990") + p2 = p.deriv() + assert p2[1] == Decimal('8.0') + p2 = p.integ() + assert p2[3] == Decimal("1.333333333333333333333333333") + assert p2[2] == Decimal('1.5') + assert np.issubdtype(p2.coeffs.dtype, np.object_) + + def test_complex(self): + p = np.poly1d([3j, 2j, 1j]) + p2 = p.integ() + assert (p2.coeffs == [1j,1j,1j,0]).all() + p2 = p.deriv() + assert (p2.coeffs == [6j,2j]).all() + + def test_integ_coeffs(self): + p = np.poly1d([3,2,1]) + p2 = p.integ(3, k=[9,7,6]) + assert (p2.coeffs == [1/4./5.,1/3./4.,1/2./3.,9/1./2.,7,6]).all() if __name__ == "__main__": run_module_suite() |