def __add__(self, value): if isinstance(value, Monomial) and count_variables( value.variables) == count_variables(self.variables): if self.coefficient + value.coefficient == 1 and len( self.variables) == 1: return algebra.Variable(name=self.variables) return Monomial(coefficient=self.coefficient + value.coefficient, variables=self.variables) elif isinstance(value, algebra.Variable): if value.name == self.variables: return Monomial(coefficient=self.coefficient + 1, variables=self.variables) else: return algebra.Polynomial( monomials=[self, algebra.Monomial(variables=value.name)]) elif isinstance(value, Fraction): value.numerator += self * value.denominator return value elif isinstance(value, Monomial): return algebra.Polynomial(monomials=[ algebra.Monomial(coefficient=self.coefficient, variables=self.variables), value ]) elif isinstance(value, int): return algebra.Polynomial(monomials=[ self, ], numerical_term=value) raise TypeError(f'Cannot add Monomial to {type(value)}')
def __sub__(self, value): if isinstance(value, algebra.Monomial) and value.variables == self.name: return algebra.Monomial(coefficient=1 - value.coefficient, variables=self.name) elif isinstance(value, Variable) and value.name == self.name: return 0 elif isinstance(value, int): return algebra.Polynomial(monomials=[algebra.Monomial(variables=self.name), ], numerical_term=-value) elif isinstance(value, Fraction): return Fraction(value.numerator - self * value.denominator, value.denominator) raise ValueError(f'Cannot subtract Variable to {type(value)}')
def __mul__(self, value): if isinstance(value, algebra.Monomial): return algebra.Monomial(variables=value.variables + self.name, coefficient=value.coefficient) elif isinstance(value, int): return algebra.Monomial(variables=self.name, coefficient=value) elif isinstance(value, Variable): return algebra.Monomial(variables=self.name + value.name) elif isinstance(value, Fraction): return algebra.Monomial(variables=self.name, coefficient=value) elif isinstance(value, algebra.Polynomial): return value * self raise TypeError(f'Cannot multiply Variable by {type(value)}')
def __sub__(self, value): if isinstance(value, algebra.Monomial): result = Polynomial(numerical_term=self.numerical_term) found = False for monomial in self.monomials: if algebra.monomial.count_variables(monomial.variables) == algebra.monomial.count_variables( value.variables): if not found: if monomial.coefficient - value.coefficient != 0: result.monomials.append(monomial - value) found = True else: result.monomials.append(monomial) if not found: result.monomials.append(value) return result elif isinstance(value, Polynomial): result = Polynomial() for monomial in self.monomials: result -= monomial for monomial in value.monomials: result -= monomial result.numerical_term += self.numerical_term - value.numerical_term return result elif isinstance(value, int): self.numerical_term -= value return self elif isinstance(value, algebra.Variable): return self - algebra.Monomial(variables=value.name, coefficient=-1) elif isinstance(value, Fraction): frac = Fraction(1, value.denominator) * value.numerator + self return frac raise TypeError(f'Cannot subtract a Polynomial to {type(value)}')
def __add__(self, value): if isinstance(value, algebra.Monomial): result = Polynomial(numerical_term=self.numerical_term) found = False for monomial in self.monomials: if algebra.monomial.count_variables(monomial.variables) == algebra.monomial.count_variables( value.variables): if not found: if monomial.coefficient + value.coefficient != 0: result.monomials.append(monomial + value) found = True else: result.monomials.append(monomial) if not found: result.monomials.append(value) return result elif isinstance(value, Polynomial): result = algebra.Polynomial() for monomial in self.monomials: result += monomial for monomial in value.monomials: result += monomial result += self.numerical_term + value.numerical_term return result elif isinstance(value, int): self.numerical_term += value return self elif isinstance(value, algebra.Variable): return self + algebra.Monomial(variables=value.name) elif isinstance(value, Fraction): return Fraction(value.numerator - (value.denominator * self), value.denominator) raise TypeError(f'Cannot add a Polynomial to {type(value)}')
def __add__(self, value): if isinstance(value, algebra.Monomial): if value.variables == self.name: return algebra.Monomial(coefficient=1 + value.coefficient, variables=self.name) else: return algebra.Polynomial(monomials=[algebra.Monomial(variables=self.name), value]) elif isinstance(value, Variable): if value.name == self.name: return algebra.Monomial(coefficient=2, variables=self.name) else: return algebra.Polynomial( monomials=[algebra.Monomial(variables=self.name), algebra.Monomial(variables=value.name)]) elif isinstance(value, int): return algebra.Polynomial(monomials=[algebra.Monomial(variables=self.name)], numerical_term=value) elif isinstance(value, Fraction): return Fraction(value.numerator + self * value.denominator, value.denominator) raise TypeError(f'Cannot add Variable to {type(value)}')
def __neg__(self): return algebra.Monomial(variables=self.name, coefficient=-1)
def __pow__(self, value, mod=None): if mod: raise NotImplementedError return algebra.Monomial(variables=self.name * value)