From e1783714210a39cf16a815d7eaed716a68523b6b Mon Sep 17 00:00:00 2001 From: Alexander Ignatov Date: Thu, 19 Mar 2020 01:01:20 +0200 Subject: [PATCH] Implement __neg__, __sub__, __rsub__, __isub__, __pos__ --- polynomial/__init__.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/polynomial/__init__.py b/polynomial/__init__.py index 769b0e4..1ceea57 100644 --- a/polynomial/__init__.py +++ b/polynomial/__init__.py @@ -257,6 +257,29 @@ def __imul__(self, other): self._vector = result._vector return self + def __pos__(self): + """Return +self.""" + return self + + def __neg__(self): + """Return -self.""" + result_vector = list(map(lambda k: -k, self._vector)) + return Polynomial(result_vector[::-1]) + + def __sub__(self, other): + """Return self - other.""" + return self + (-other) + + def __rsub__(self, other): + """Return other - self.""" + return other + (-self) + + def __isub__(self, other): + """Implement self -= other.""" + result = self - other + self._vector = result._vector + return self + class Monomial(Polynomial): """Implements a single-variable monomial. A single-term polynomial."""