diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2019-04-16 08:49:59 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-16 08:49:59 -0600 |
commit | 6f5638a4913b077964d024b1d033b0333f3c0a71 (patch) | |
tree | dd8041d48e8cd9b3cc5ddcdab9e0ba851a0b4a9a | |
parent | cc94f360febdef0e6c4183c50555ba82e60ccff6 (diff) | |
parent | 9af2340580bcbacc06b1079df3e9b8abf90b7657 (diff) | |
download | numpy-6f5638a4913b077964d024b1d033b0333f3c0a71.tar.gz |
Merge pull request #13348 from eric-wieser/poly1d-fixes-fixes-fixes-fixes
BUG: Return the coefficients array directly
-rw-r--r-- | numpy/lib/polynomial.py | 10 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 18 |
2 files changed, 17 insertions, 11 deletions
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 8c6f69cb0..1f08abf36 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -1111,8 +1111,14 @@ class poly1d(object): @property def coeffs(self): - """ A copy of the polynomial coefficients """ - return self._coeffs.copy() + """ The polynomial coefficients """ + return self._coeffs + + @coeffs.setter + def coeffs(self, value): + # allowing this makes p.coeffs *= 2 legal + if value is not self._coeffs: + raise AttributeError("Cannot set attribute") @property def variable(self): diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 77414ba7c..89759bd83 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -246,16 +246,16 @@ class TestPolynomial(object): assert_equal(r.coeffs.dtype, np.complex128) assert_equal(q*a + r, b) - def test_poly_coeffs_immutable(self): - """ Coefficients should not be modifiable """ + def test_poly_coeffs_mutable(self): + """ Coefficients should be modifiable """ p = np.poly1d([1, 2, 3]) - try: - # despite throwing an exception, this used to change state - p.coeffs += 1 - except Exception: - pass - assert_equal(p.coeffs, [1, 2, 3]) + p.coeffs += 1 + assert_equal(p.coeffs, [2, 3, 4]) p.coeffs[2] += 10 - assert_equal(p.coeffs, [1, 2, 3]) + assert_equal(p.coeffs, [2, 3, 14]) + + # this never used to be allowed - let's not add features to deprecated + # APIs + assert_raises(AttributeError, setattr, p, 'coeffs', np.array(1)) |