diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-03-14 13:02:35 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-14 13:02:35 -0600 |
commit | dc04055d464401ccb21269b1a972466bfbe67fbe (patch) | |
tree | a618d3a65fd78ff6310f3cef28b304191bce59d0 /numpy/lib | |
parent | 3e297ba7e47c949469744ac67cf296b4315ceea9 (diff) | |
parent | 6f108ae44904026d7da2a1b71abb116284b04960 (diff) | |
download | python-numpy-dc04055d464401ccb21269b1a972466bfbe67fbe.tar.gz python-numpy-dc04055d464401ccb21269b1a972466bfbe67fbe.tar.bz2 python-numpy-dc04055d464401ccb21269b1a972466bfbe67fbe.zip |
Merge pull request #8762 from eric-wieser/poly1d-fixes
BUG: Prevent crash in poly1d.__eq__
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/polynomial.py | 74 | ||||
-rw-r--r-- | numpy/lib/tests/test_polynomial.py | 9 |
2 files changed, 54 insertions, 29 deletions
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index f00d95b6c..50e6d8db2 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -1036,17 +1036,47 @@ class poly1d(object): poly1d([ 1, -3, 2]) """ - coeffs = None - order = None - variable = None __hash__ = None - def __init__(self, c_or_r, r=0, variable=None): + @property + def coeffs(self): + """ The polynomial coefficients """ + return self._coeffs + + @property + def variable(self): + """ The name of the polynomial variable """ + return self._variable + + # calculated attributes + @property + def order(self): + """ The order or degree of the polynomial """ + return len(self._coeffs) - 1 + + @property + def roots(self): + """ The roots of the polynomial, where self(x) == 0 """ + return roots(self._coeffs) + + # alias attributes + r = roots + c = coef = coefficients = coeffs + o = order + + def __init__(self, c_or_r, r=False, variable=None): if isinstance(c_or_r, poly1d): - for key in c_or_r.__dict__.keys(): - self.__dict__[key] = c_or_r.__dict__[key] + self._variable = c_or_r._variable + self._coeffs = c_or_r._coeffs + + if set(c_or_r.__dict__) - set(self.__dict__): + msg = ("In the future extra properties will not be copied " + "across when constructing one poly1d from another") + warnings.warn(msg, FutureWarning, stacklevel=2) + self.__dict__.update(c_or_r.__dict__) + if variable is not None: - self.__dict__['variable'] = variable + self._variable = variable return if r: c_or_r = poly(c_or_r) @@ -1056,11 +1086,10 @@ class poly1d(object): c_or_r = trim_zeros(c_or_r, trim='f') if len(c_or_r) == 0: c_or_r = NX.array([0.]) - self.__dict__['coeffs'] = c_or_r - self.__dict__['order'] = len(c_or_r) - 1 + self._coeffs = c_or_r if variable is None: variable = 'x' - self.__dict__['variable'] = variable + self._variable = variable def __array__(self, t=None): if t: @@ -1199,29 +1228,17 @@ class poly1d(object): __rtruediv__ = __rdiv__ def __eq__(self, other): + if not isinstance(other, poly1d): + return NotImplemented if self.coeffs.shape != other.coeffs.shape: return False return (self.coeffs == other.coeffs).all() def __ne__(self, other): + if not isinstance(other, poly1d): + return NotImplemented return not self.__eq__(other) - def __setattr__(self, key, val): - raise ValueError("Attributes cannot be changed this way.") - - def __getattr__(self, key): - if key in ['r', 'roots']: - return roots(self.coeffs) - elif key in ['c', 'coef', 'coefficients']: - return self.coeffs - elif key in ['o']: - return self.order - else: - try: - return self.__dict__[key] - except KeyError: - raise AttributeError( - "'%s' has no attribute '%s'" % (self.__class__, key)) def __getitem__(self, val): ind = self.order - val @@ -1237,10 +1254,9 @@ class poly1d(object): raise ValueError("Does not support negative powers.") if key > self.order: zr = NX.zeros(key-self.order, self.coeffs.dtype) - self.__dict__['coeffs'] = NX.concatenate((zr, self.coeffs)) - self.__dict__['order'] = key + self._coeffs = NX.concatenate((zr, self.coeffs)) ind = 0 - self.__dict__['coeffs'][ind] = val + self._coeffs[ind] = val return def __iter__(self): diff --git a/numpy/lib/tests/test_polynomial.py b/numpy/lib/tests/test_polynomial.py index 00dffd3d3..f1e4543bb 100644 --- a/numpy/lib/tests/test_polynomial.py +++ b/numpy/lib/tests/test_polynomial.py @@ -213,6 +213,15 @@ class TestDocs(TestCase): v = np.arange(1, 21) assert_almost_equal(np.poly(v), np.poly(np.diag(v))) + def test_poly_eq(self): + p = np.poly1d([1, 2, 3]) + p2 = np.poly1d([1, 2, 4]) + assert_equal(p == None, False) + assert_equal(p != None, True) + assert_equal(p == p, True) + assert_equal(p == p2, False) + assert_equal(p != p2, True) + if __name__ == "__main__": run_module_suite() |