def __add__(self, other): """Addition""" # If we can turn the other into a rational do that if type(other) in [int, str, float, Rational]: other = QPoly([cast_to_rational(other)]) L = poly_add(self.coef, other.coef) return QPoly(L) elif type(other) == QPoly: L = poly_add(self.coef, other.coef) return QPoly(L) else: return NotImplemented
def __add__(self, other): """Addition""" # If we can turn the other into a rational do that if type(other) == int: other = ZPoly([other], self.M) L = poly_add(self.coef, other.coef) return ZPoly(L, self.M) elif type(other) == ZPoly: L = poly_add(self.coef, other.coef) return ZPoly(L, self.M) else: return NotImplemented
def __rsub__(self, other): """Subtraction is NOT commutative""" if type(other) in [int, str, float, Rational]: other = QPoly([cast_to_rational(other)]) L = poly_add(self.coef, [-c for c in other.coef]) return QPoly(L)
def __rsub__(self, other): """Subtraction is NOT commutative""" if type(other) == int: other = ZPoly([other], self.M) L = poly_add(self.coef, [-c for c in other.coef]) return ZPoly(L, self.M)
def __sub__(self, other): """Subtraction""" if type(other) not in [int, ZPoly]: return NotImplemented if type(other) == int: other = ZPoly([other], self.M) L = poly_add(self.coef, [-c for c in other.coef]) return ZPoly(L, self.M)