示例#1
0
    def substract(a, b):
        """Returns the expression resultant of substracting terms a and b."""

        a = Expression(a, no_vars_intended=True)
        b = Expression(b, no_vars_intended=True)

        if isanumber(a.expression) or isanumber(b.expression):
            raise NonAlgebraicOperationError

        if len(a.terms) > 1 or len(b.terms) > 1:
            raise InvalidOperationError(a, b)

        if TermOperations.getpower(
                a.expression) is not TermOperations.getpower(
                    b.expression) or a.variables != b.variables:
            result = a.expression + '-' + b.expression
            return Expression.beautify(result)

        a_coefficient = a.get_number(0, frac_to_number=True)
        b_coefficient = b.get_number(0, frac_to_number=True)

        result = str(num(a_coefficient) - num(b_coefficient))
        result = if_assign(result == '1', "", result)
        result = if_assign(result == '-1', "-", result)

        result += "".join(a.variables) + '**' + str(
            TermOperations.getpower(a.expression))
        return Expression.beautify(result)
示例#2
0
    def add(a, b, non_algebraic=False):
        """Returns the expression resultant of adding terms a and b."""

        a = Expression(a, no_vars_intended=True)
        b = Expression(b, no_vars_intended=True)

        if isanumber(a.expression) or isanumber(b.expression):

            if non_algebraic is True:
                return num(num(a.expression) + num(b.expression))
            raise NonAlgebraicOperationError

        if len(a.terms) > 1 or len(b.terms) > 1:
            raise InvalidOperationError(a, b)

        if TermOperations.getpower(
                a.expression) is not TermOperations.getpower(
                    b.expression) or a.variables != b.variables:
            operator = if_assign(b.expression.startswith('-'), '', '+')
            return Expression.beautify(a.expression + operator + b.expression)

        a_coefficient = a.get_number(0)
        b_coefficient = b.get_number(0)

        result = str(num(a_coefficient) + num(b_coefficient))
        result = if_assign(result == '1', "", result)
        result = if_assign(result == '-1', "-", result)

        result += "".join(a.variables) + '**' + str(
            TermOperations.getpower(a.expression))
        if result.endswith('**1'):
            result = result.replace('**1', '')
        return Expression.beautify(result)
示例#3
0
    def multiply(a, b):
        """Multiplies terms a and b."""

        a = Expression(a, no_vars_intended=True)
        b = Expression(b, no_vars_intended=True)

        power = str(
            if_assign(
                TermOperations.getpower(a.expression) >=
                TermOperations.getpower(b.expression),
                TermOperations.getpower(a.expression),
                TermOperations.getpower(b.expression)))
        variables = set(a.variables + b.variables)

        if len(TermOperations.commonvars(a.expression, b.expression)) > 0:
            power = str(
                int(TermOperations.getpower(a.expression)) +
                int(TermOperations.getpower(b.expression)))

        power = if_assign(power == '1', '', power)

        if power == '1':
            return "".join(variables)

        a_coefficient = a.get_number(0)
        b_coefficient = b.get_number(0)

        result = str(int(a_coefficient) *
                     int(b_coefficient)) + "".join(variables) + '**' + power
        return Expression.beautify(result)
示例#4
0
    def divide(a, b):
        a = Expression(a)
        b = Expression(b)

        variables = set(a.variables + b.variables)
        power = '**' + str(
            int(TermOperations.getpower(a.expression)) -
            int(TermOperations.getpower(b.expression)))
        power = if_assign(power == '**1', '', power)

        if power == '**0':
            return "/".join(variables)

        a_coefficient = a.get_number(0)
        b_coefficient = b.get_number(0)

        result = str(num(
            int(a_coefficient) / int(b_coefficient))) + "/".join(variables)
        result = if_assign(power != '', '(' + result + ')' + power, result)
        return Expression.beautify(result)