Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __add__(self, other):
if isinstance(other, Number):
other = Expr.from_number(other)
elif isinstance(other, Term):
other = Expr.from_term(other)
if isinstance(other, Expr):
terms = self.terms_to_dict()
for op, coeff in other.terms:
if op in terms:
terms[op] += coeff
if terms[op] == 0:
del terms[op]
else:
terms[op] = coeff
return Expr.from_terms_dict(terms)
return NotImplemented
def __radd__(self, other):
return other + Expr.from_term(self)
def __sub__(self, other):
return Expr.from_term(self) - other
def to_expr(self):
"""Convert to Expr."""
return Expr.from_term(self)
def __add__(self, other):
return Expr.from_term(self) + other
def __rsub__(self, other):
return other - Expr.from_term(self)