Beispiel #1
0
    def __sub__(self, value):
        if isinstance(value, Monomial) and count_variables(
                value.variables) == count_variables(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)
            elif value.name in self.variables:
                s = subtract_str(self.variables, value.name)
                return Monomial(coefficient=self.coefficient, variables=s)
            else:
                return algebra.Polynomial(
                    monomials=[self, -Monomial(variables=value.name)])
        elif isinstance(value, int):
            return algebra.Polynomial(monomials=[
                self,
            ],
                                      numerical_term=-value)
        elif isinstance(value, Monomial):
            return algebra.Polynomial(monomials=[self, -value])
        elif isinstance(value, algebra.Polynomial):
            return value - self
        elif isinstance(value, Fraction):
            return value - self

        raise TypeError(f'Cannot subtract Monomial to {type(value)}')
Beispiel #2
0
    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)}')
Beispiel #3
0
    def __mul__(self, value):
        if isinstance(value, (algebra.Monomial, int)):
            result = algebra.Polynomial()

            for monomial in self.monomials:
                result += monomial * value

            if self.numerical_term:
                result += self.numerical_term * value

            return result
        elif isinstance(value, Polynomial):
            result = algebra.Polynomial()

            for monomial in self.monomials:
                result += monomial * value

            result += self.numerical_term * value

            return result
        elif isinstance(value, algebra.Variable):
            result = Polynomial()

            for monomial in self.monomials:
                result += monomial * value

            if self.numerical_term:
                result += self.numerical_term * value

            return result
        elif isinstance(value, Fraction):
            result = Polynomial()

            for monomial in self.monomials:
                result += monomial * value

            if self.numerical_term:
                result += self.numerical_term * value

            return result

        raise TypeError(f'Cannot multiply a Polynomial by {type(value)}')
Beispiel #4
0
    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)}')
Beispiel #5
0
    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)}')
Beispiel #6
0
    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)}')