示例#1
0
    def parse_expr(self, expression, variables):
        paren_indices = self.find_parens(expression)
        result = []
        if paren_indices:
            i = len(paren_indices) - 1
            while i >= 0:
                if i == len(paren_indices) - 1:
                    left = paren_indices[i][0] + 1
                    right = paren_indices[i][1]
                    inside = expression[left:right]
                    inside = self.parse_expr(inside, variables)
                    if left - 1 == 0:
                        if right == len(expression) - 1:
                            result = inside
                        else:
                            if expression[right + 1] == ' ':
                                outside = expression[right + 1: len(expression)]
                                outside = self.parse_expr(outside, variables)
                                result = polyop.poly_addition(inside, outside)
                            else:
                                multiplier_end = expression.find(' ', right)
                                if multiplier_end == -1:
                                    multiplier_end = len(expression)
                                multiplier = expression[right + 1:multiplier_end]
                                multiplier = self.parse_expr(multiplier, variables)
                                inside = polyop.poly_poly_multiplication(inside, multiplier)
                                if multiplier_end == len(expression):
                                    result = inside
                                else:
                                    outside = expression[multiplier_end:len(expression)]
                                    outside = self.parse_expr(outside, variables)
                                    result = polyop.poly_addition(inside, outside)

                    else:
                        if right == len(expression) - 1:
                            if expression[left - 2] == ' ':
                                if expression[left - 3] == '-':
                                    inside = polyop.poly_scalar_multiplication(inside, -1.0)
                                outside = expression[0:left - 4]
                                outside = self.parse_expr(outside, variables)
                                result = polyop.poly_addition(inside, outside)
                            elif expression[left - 2] == ')':
                                j = i - 1
                                paren_multiplier_start = left - 1
                                while j >= 0:
                                    if paren_indices[j][1] == paren_indices[j + 1][0] - 1:
                                        paren_multiplier_start = paren_indices[j][0]
                                    j -= 1
                                paren_multiplier = expression[paren_multiplier_start:left - 1]
                                paren_multiplier = self.parse_expr(paren_multiplier, variables)
                                inside = polyop.poly_poly_multiplication(inside, paren_multiplier)
                                if paren_multiplier_start == 0:
                                    result = inside
                                else:
                                    if expression[paren_multiplier_start - 1] == ' ':
                                        if expression[paren_multiplier_start - 2] == '-':
                                            inside = polyop.poly_scalar_multiplication(inside, -1.0)
                                        outside = expression[0:paren_multiplier_start - 3]
                                        outside = self.parse_expr(outside, variables)
                                        result = polyop.poly_addition(inside, outside)
                                    else:
                                        multiplier_start = expression.rfind(' ', 0, paren_multiplier_start)
                                        if multiplier_start == -1:
                                            multiplier_start = 0
                                        else:
                                            multiplier_start -= 1
                                        multiplier = expression[multiplier_start:paren_multiplier_start]
                                        multiplier = self.parse_expr(multiplier, variables)
                                        inside = polyop.poly_poly_multiplication(inside, multiplier)
                                        if multiplier_start == 0:
                                            result = inside
                                        else:
                                            outside = expression[0:multiplier_start - 1]
                                            outside = self.parse_expr(outside, variables)
                                            result = polyop.poly_addition(inside, outside)
                            else:
                                multiplier_start = expression.rfind(' ', 0, left - 1)
                                if multiplier_start == -1:
                                    multiplier_start = 0
                                else:
                                    multiplier_start -= 1
                                multiplier = expression[multiplier_start:left - 1]
                                multiplier = self.parse_expr(multiplier, variables)
                                inside = polyop.poly_poly_multiplication(inside, multiplier)
                                if multiplier_start == 0:
                                    result = inside
                                else:
                                    outside = expression[0:multiplier_start - 1]
                                    outside = self.parse_expr(outside, variables)
                                    result = polyop.poly_addition(inside, outside)
                        else:
                            if expression[left - 2] == ')':
                                j = i - 1
                                paren_multiplier_start = left - 1
                                while j >= 0:
                                    if paren_indices[j][1] == paren_indices[j + 1][0] - 1:
                                        paren_multiplier_start = paren_indices[j][0]
                                    j -= 1
                                paren_multiplier = expression[paren_multiplier_start:left - 1]
                                paren_multiplier = self.parse_expr(paren_multiplier, variables)
                                inside = polyop.poly_poly_multiplication(inside, paren_multiplier)
                                if paren_multiplier_start == 0 and right == len(expression) - 1:
                                    result = inside
                                else:
                                    if expression[paren_multiplier_start - 1] == ' ' and expression[right + 1] == ' ':
                                        if expression[paren_multiplier_start - 2] == '-':
                                            inside = polyop.poly_scalar_multiplication(inside, -1.0)
                                        outside = expression[0:paren_multiplier_start - 3] + expression[
                                                                                             right + 1:len(expression)]
                                        outside = self.parse_expr(outside, variables)
                                        result = polyop.poly_addition(inside, outside)
                                    elif expression[paren_multiplier_start - 1] == ' ' and expression[right + 1] != ' ':
                                        if expression[paren_multiplier_start - 2] == '-':
                                            inside = polyop.poly_scalar_multiplication(inside, -1.0)
                                        multiplier_end = expression.find(' ', right)
                                        if multiplier_end == -1:
                                            multiplier_end = len(expression)
                                        multiplier = expression[right + 1:multiplier_end]
                                        multiplier = self.parse_expr(multiplier, variables)
                                        inside = polyop.poly_poly_multiplication(inside, multiplier)
                                        if multiplier_end == len(expression):
                                            outside = expression[0:paren_multiplier_start - 3]
                                        else:
                                            outside = expression[0:paren_multiplier_start - 3] + expression[
                                                                                                 multiplier_end:len(
                                                                                                     expression)]
                                        outside = self.parse_expr(outside, variables)
                                        result = polyop.poly_addition(inside, outside)
                                    elif expression[paren_multiplier_start - 1] != ' ' and expression[right + 1] == ' ':
                                        multiplier_start = expression.rfind(' ', 0, paren_multiplier_start)
                                        if multiplier_start == -1:
                                            multiplier_start = 0
                                        else:
                                            multiplier_start -= 1
                                        multiplier = expression[multiplier_start:paren_multiplier_start]
                                        multiplier = self.parse_expr(multiplier, variables)
                                        inside = polyop.poly_poly_multiplication(inside, multiplier)
                                        if multiplier_start == 0:
                                            result = inside
                                        else:
                                            outside = expression[0:multiplier_start - 1] + expression[
                                                                                           right + 1: len(expression)]
                                            outside = self.parse_expr(outside, variables)
                                            result = polyop.poly_addition(inside, outside)
                                    elif expression[paren_multiplier_start - 1] != ' ' and expression[right + 1] != ' ':
                                        multiplier_left_start = expression.rfind(' ', 0, paren_multiplier_start)
                                        if multiplier_left_start == -1:
                                            multiplier_left_start = 0
                                        else:
                                            multiplier_left_start -= 1
                                        multiplier_right_end = expression.find(' ', right)
                                        if multiplier_right_end == -1:
                                            multiplier_right_end = len(expression)
                                        multiplier_left = expression[multiplier_left_start:paren_multiplier_start]
                                        multiplier_right = expression[right + 1:multiplier_right_end]
                                        multiplier_left = self.parse_expr(multiplier_left, variables)
                                        multiplier_right = self.parse_expr(multiplier_right, variables)
                                        inside = polyop.poly_poly_multiplication(inside, multiplier_left)
                                        inside = polyop.poly_poly_multiplication(inside, multiplier_right)
                                        if multiplier_left_start == 0 and multiplier_right_end == len(expression):
                                            result = inside
                                        elif multiplier_left_start != 0 and multiplier_right_end == len(expression):
                                            outside = expression[0:multiplier_left_start - 1]
                                            outside = self.parse_expr(outside, variables)
                                            result = polyop.poly_addition(inside, outside)
                                        elif multiplier_left_start == 0 and multiplier_right_end != len(expression):
                                            outside = expression[multiplier_right_end:len(expression)]
                                            outside = self.parse_expr(outside, variables)
                                            result = polyop.poly_addition(inside, outside)
                                        elif multiplier_left_start != 0 and multiplier_right_end != len(expression):
                                            outside = expression[0:multiplier_left_start - 1] + expression[
                                                                                                multiplier_right_end:
                                                                                                len(expression)]
                                            outside = self.parse_expr(outside, variables)
                                            result = polyop.poly_addition(inside, outside)
                            elif expression[left - 2] == ' ' and expression[right + 1] == ' ':
                                if expression[left - 3] == '-':
                                    inside = polyop.poly_scalar_multiplication(inside, -1.0)
                                outside = expression[0:left - 4] + expression[right + 1:len(expression)]
                                outside = self.parse_expr(outside, variables)
                                result = polyop.poly_addition(inside, outside)
                            elif expression[left - 2] == ' ' and expression[right + 1] != ' ':
                                if expression[left - 3] == '-':
                                    inside = polyop.poly_scalar_multiplication(inside, -1.0)
                                multiplier_end = expression.find(' ', right)
                                if multiplier_end == -1:
                                    multiplier_end = len(expression)
                                multiplier = expression[right + 1:multiplier_end]
                                multiplier = self.parse_expr(multiplier, variables)
                                inside = polyop.poly_poly_multiplication(inside, multiplier)
                                if multiplier_end == len(expression):
                                    outside = expression[0:left - 4]
                                else:
                                    outside = expression[0:left - 4] + expression[multiplier_end:len(expression)]
                                outside = self.parse_expr(outside, variables)
                                result = polyop.poly_addition(inside, outside)
                            elif expression[left - 2] != ' ' and expression[right + 1] == ' ':
                                multiplier_start = expression.rfind(' ', 0, left - 1)
                                if multiplier_start == -1:
                                    multiplier_start = 0
                                else:
                                    multiplier_start -= 1
                                multiplier = expression[multiplier_start:left - 1]
                                multiplier = self.parse_expr(multiplier, variables)
                                inside = polyop.poly_poly_multiplication(inside, multiplier)
                                if multiplier_start == 0:
                                    outside = expression[right + 1: len(expression)]
                                else:
                                    outside = expression[0:multiplier_start - 1] + expression[
                                                                                   right + 1: len(expression)]
                                outside = self.parse_expr(outside, variables)
                                result = polyop.poly_addition(inside, outside)
                            elif expression[left - 2] != ' ' and expression[right + 1] != ' ':
                                multiplier_left_start = expression.rfind(' ', 0, left - 1)
                                if multiplier_left_start == -1:
                                    multiplier_left_start = 0
                                else:
                                    multiplier_left_start -= 1
                                multiplier_right_end = expression.find(' ', right)
                                if multiplier_right_end == -1:
                                    multiplier_right_end = len(expression)
                                multiplier_left = expression[multiplier_left_start:left - 1]
                                multiplier_right = expression[right + 1:multiplier_right_end]
                                multiplier_left = self.parse_expr(multiplier_left, variables)
                                multiplier_right = self.parse_expr(multiplier_right, variables)
                                inside = polyop.poly_poly_multiplication(inside, multiplier_left)
                                inside = polyop.poly_poly_multiplication(inside, multiplier_right)
                                if multiplier_left_start == 0 and multiplier_right_end == len(expression):
                                    result = inside
                                elif multiplier_left_start != 0 and multiplier_right_end == len(expression):
                                    outside = expression[0:multiplier_left_start - 1]
                                    outside = self.parse_expr(outside, variables)
                                    result = polyop.poly_addition(inside, outside)
                                elif multiplier_left_start == 0 and multiplier_right_end != len(expression):
                                    outside = expression[multiplier_right_end:len(expression)]
                                    outside = self.parse_expr(outside, variables)
                                    result = polyop.poly_addition(inside, outside)
                                elif multiplier_left_start != 0 and multiplier_right_end != len(expression):
                                    outside = expression[0:multiplier_left_start - 1] + expression[
                                                                                        multiplier_right_end:len(
                                                                                            expression)]
                                    result = polyop.poly_addition(inside, outside)

                i -= 1
        else:
            result = self.parse_np(expression, variables)
        return polyop.poly_sort(polyop.poly_consolidate(result))
示例#2
0
                        else:
                            if varIndex != len(temp) - 1:
                                powers.append(num_parse(temp[varIndex + 2: len(temp)]))
                            else:
                                powers.append(1.0)
                    else:
                        if varIndex != len(temp) - 1:
                            powers.append(num_parse(temp[varIndex + 2: len(temp)]))
                        else:
                            powers.append(1.0)
                else:
                    powers.append(0.0)
            coefficient = temp[0:first_index]
        coefficient = num_parse(coefficient)
        coeff_power.append([coefficient, powers])

    return coeff_power


expr = "y + x^2 + y^2 + 2x^2"
expr2 = "x^2 + 5y^2"
var = ['x', 'y']
test = parse_np(expr, var)
test2 = parse_np(expr2, var)

print(test)
print(polyop.poly_consolidate(test))
print(polyop.poly_sort(polyop.poly_consolidate(test)))
print(polyop.poly_scalar_multiplication(test, -1))
print(polyop.poly_poly_multiplication(test, test2))
示例#3
0
def parse_expr(expression, variables):
    print('called')
    paren_indices = find_parens(expression)
    print('parenthesis indices:', paren_indices)
    result = []
    if paren_indices:
        i = len(paren_indices) - 1
        while i >= 0:
            if i == len(paren_indices) - 1:
                print('last index set')
                left = paren_indices[i][0] + 1
                right = paren_indices[i][1]
                inside = expression[left:right]
                print('inside:', inside)
                inside = parse_expr(inside, variables)
                print('inside (parsed):', inside)
                if left - 1 == 0:
                    if right == len(expression) - 1:
                        result = inside
                    else:
                        if expression[right + 1] == ' ':
                            outside = expression[right + 1:len(expression)]
                            print('outside:', outside)
                            outside = parse_expr(outside, variables)
                            print('outside (parsed)', outside)
                            result = polyop.poly_addition(inside, outside)
                        else:
                            multiplier_end = expression.find(' ', right)
                            if multiplier_end == -1:
                                multiplier_end = len(expression)
                            multiplier = expression[right + 1:multiplier_end]
                            print('multiplier:', multiplier)
                            multiplier = parse_expr(multiplier, variables)
                            print('multiplier (parsed):', multiplier)
                            inside = polyop.poly_poly_multiplication(
                                inside, multiplier)
                            print('multiplied inside:', inside)
                            if multiplier_end == len(expression):
                                result = inside
                            else:
                                outside = expression[
                                    multiplier_end:len(expression)]
                                print('outside:', outside)
                                outside = parse_expr(outside, variables)
                                print('outside (parsed)', outside)
                                result = polyop.poly_addition(inside, outside)

                else:
                    if right == len(expression) - 1:
                        if expression[left - 2] == ' ':
                            if expression[left - 3] == '-':
                                inside = polyop.poly_scalar_multiplication(
                                    inside, -1.0)
                            print('inside (after negative check):', inside)
                            outside = expression[0:left - 4]
                            print('outside:', outside)
                            outside = parse_expr(outside, variables)
                            print('outside (parsed)', outside)
                            result = polyop.poly_addition(inside, outside)
                        else:
                            multiplier_start = expression.rfind(
                                ' ', 0, left - 1)
                            if multiplier_start == -1:
                                multiplier_start = 0
                            else:
                                multiplier_start -= 1
                            multiplier = expression[multiplier_start:left - 1]
                            print('multiplier:', multiplier)
                            multiplier = parse_expr(multiplier, variables)
                            print('multiplier (parsed):', multiplier)
                            inside = polyop.poly_poly_multiplication(
                                inside, multiplier)
                            print('multiplied inside:', inside)
                            if multiplier_start == 0:
                                result = inside
                            else:
                                outside = expression[0:multiplier_start - 1]
                                print('outside:', outside)
                                outside = parse_expr(outside, variables)
                                print('outside (parsed)', outside)
                                result = polyop.poly_addition(inside, outside)
                    else:
                        if expression[left -
                                      2] == ' ' and expression[right +
                                                               1] == ' ':
                            if expression[left - 3] == '-':
                                inside = polyop.poly_scalar_multiplication(
                                    inside, -1.0)
                            print('inside (after negative check):', inside)
                            outside = expression[0:left - 4] + expression[
                                right + 1:len(expression)]
                            print('outside:', outside)
                            outside = parse_expr(outside, variables)
                            print('outside (parsed)', outside)
                            result = polyop.poly_addition(inside, outside)
                        elif expression[left -
                                        2] == ' ' and expression[right +
                                                                 1] != ' ':
                            if expression[left - 3] == '-':
                                inside = polyop.poly_scalar_multiplication(
                                    inside, -1.0)
                            print('inside (after negative check):', inside)
                            multiplier_end = expression.find(' ', right)
                            if multiplier_end == -1:
                                multiplier_end = len(expression)
                            multiplier = expression[right + 1:multiplier_end]
                            print('multiplier:', multiplier)
                            multiplier = parse_expr(multiplier, variables)
                            print('multiplier (parsed):', multiplier)
                            inside = polyop.poly_poly_multiplication(
                                inside, multiplier)
                            print('multiplied inside:', inside)
                            if multiplier_end == len(expression):
                                outside = expression[0:left - 4]
                            else:
                                outside = expression[0:left - 4] + expression[
                                    multiplier_end:len(expression)]
                            print('outside:', outside)
                            outside = parse_expr(outside, variables)
                            print('outside (parsed)', outside)
                            result = polyop.poly_addition(inside, outside)
                        elif expression[left - 2] != ' ' and expression[
                                right + 1] == ' ':
                            multiplier_start = expression.rfind(
                                ' ', 0, left - 1)
                            if multiplier_start == -1:
                                multiplier_start = 0
                            else:
                                multiplier_start -= 1
                            multiplier = expression[multiplier_start:left - 1]
                            print('multiplier:', multiplier)
                            multiplier = parse_expr(multiplier, variables)
                            print('multiplier (parsed):', multiplier)
                            inside = polyop.poly_poly_multiplication(
                                inside, multiplier)
                            print('multiplied inside:', inside)
                            if multiplier_start == 0:
                                result = inside
                            else:
                                outside = expression[
                                    0:multiplier_start -
                                    1] + expression[right + 1:len(expression)]
                                print('outside:', outside)
                                outside = parse_expr(outside, variables)
                                print('outside (parsed)', outside)
                                result = polyop.poly_addition(inside, outside)
                        elif expression[left - 2] != ' ' and expression[
                                right + 1] != ' ':
                            multiplier_left_start = expression.rfind(
                                ' ', 0, left - 1)
                            if multiplier_left_start == -1:
                                multiplier_left_start = 0
                            else:
                                multiplier_left_start -= 1
                            multiplier_right_end = expression.find(' ', right)
                            if multiplier_right_end == -1:
                                multiplier_right_end = len(expression)
                            multiplier_left = expression[
                                multiplier_left_start:left - 1]
                            multiplier_right = expression[
                                right + 1:multiplier_right_end]
                            print('left multiplier:', multiplier_left)
                            print('right multiplier:', multiplier_right)
                            multiplier_left = parse_expr(
                                multiplier_left, variables)
                            multiplier_right = parse_expr(
                                multiplier_right, variables)
                            print('left multiplier (parsed):', multiplier_left)
                            print('right multiplier (parsed):',
                                  multiplier_right)
                            inside = polyop.poly_poly_multiplication(
                                inside, multiplier_left)
                            inside = polyop.poly_poly_multiplication(
                                inside, multiplier_right)
                            print('multiplied inside:', inside)
                            if multiplier_left_start == 0 and multiplier_right_end == len(
                                    expression):
                                result = inside
                            elif multiplier_left_start != 0 and multiplier_right_end == len(
                                    expression):
                                outside = expression[0:multiplier_left_start -
                                                     1]
                                print('outside:', outside)
                                outside = parse_expr(outside, variables)
                                print('outside (parsed):', outside)
                                result = polyop.poly_addition(inside, outside)
                            elif multiplier_left_start == 0 and multiplier_right_end != len(
                                    expression):
                                outside = expression[
                                    multiplier_right_end:len(expression)]
                                print('outside:', outside)
                                outside = parse_expr(outside, variables)
                                print('outside (parsed)', outside)
                                result = polyop.poly_addition(inside, outside)
                            elif multiplier_left_start != 0 and multiplier_right_end != len(
                                    expression):
                                outside = expression[
                                    0:multiplier_left_start - 1] + expression[
                                        multiplier_right_end:len(expression)]
                                print('outside:', outside)
                                outside = parse_expr(outside, variables)
                                print('outside (parsed)', outside)
                                result = polyop.poly_addition(inside, outside)

            i -= 1
    else:
        print('no parentheses')
        result = parse_np(expression, variables)
    print("returning", result)
    return result
示例#4
0
def parse_expr(expression, variables):
    print('called')
    paren_indices = find_parens(expression)
    power = []
    for _ in range(len(variables)):
        power.append(0.0)
    result = [0.0, power]
    i = 0
    while i < len(paren_indices):
        print("pass")
        if i != len(paren_indices) - 1:
            if paren_indices[i][0] > paren_indices[
                    i + 1][0] and paren_indices[i][1] < paren_indices[i +
                                                                      1][1]:
                left = paren_indices[i + 1][0] + 1
                right = paren_indices[i + 1][1]
                inside = parse_expr(expression[left:right], variables)
                print("inside", inside)
                print("expr", expression)
                if right != len(expression) - 1:
                    if expression[left - 2] == ' ' and expression[
                            right + 1] != '(' and all(
                                expression[right + 1] != variable
                                for variable in variables):
                        if expression[left - 2] == '-':
                            inside = polyop.poly_scalar_multiplication(
                                inside, -1)
                        expression = expression[0:left - 4] + expression[
                            right + 1:len(expression)]
                        print(expression)
                        inside = polyop.poly_addition(
                            inside, parse_np(expression, variables))
                        print("inside", inside)
                        result = polyop.poly_addition(result, inside)
                    elif expression[left - 2] != ' ':
                        multiplier_start = expression.rfind(' ', 0, left)
                        print(expression[multiplier_start + 1:left - 1])
                        multiplier = parse_np(
                            expression[multiplier_start + 1:left - 1],
                            variables)
                        print(multiplier)
                        inside = polyop.poly_poly_multiplication(
                            inside, multiplier)
                        print(inside)
                        result = polyop.poly_addition(result, inside)

                else:
                    if expression[left - 2] == ' ':
                        if expression[left - 3] == '-':
                            inside = polyop.poly_scalar_multiplication(
                                inside, -1)
                        expression = expression[0:left - 4]
                        print(expression)
                        inside = polyop.poly_addition(
                            inside, parse_np(expression, variables))
                        print("inside", inside)
                        result = polyop.poly_addition(result, inside)
                print("expr1", expression)
                paren_indices.pop(i + 1)
            else:
                left = paren_indices[i][0] + 1
                right = paren_indices[i][1]
                inside = parse_np(expression[left:right], variables)
                result = polyop.poly_addition(result, inside)
        else:
            left = paren_indices[i][0] + 1
            right = paren_indices[i][1]
            inside = parse_np(expression[left:right], variables)
            print("inside1", inside)
            print("expr", expression)
            if right != len(expression) - 1:
                if expression[left - 2] == ' ' and expression[
                        right + 1] != '(' and all(
                            expression[right + 1] != variable
                            for variable in variables):
                    if expression[left - 3] == '-':
                        inside = polyop.poly_scalar_multiplication(inside, -1)
                    expression = expression[0:left -
                                            4] + expression[right +
                                                            1:len(expression)]
                    print(expression)
                    inside = polyop.poly_addition(
                        inside, parse_np(expression, variables))
                    print("inside", inside)
                    result = polyop.poly_addition(result, inside)
            else:
                if expression[left - 2] == ' ':
                    if expression[left - 3] == '-':
                        inside = polyop.poly_scalar_multiplication(inside, -1)
                    expression = expression[0:left - 4]
                    print(expression)
                    inside = polyop.poly_addition(
                        inside, parse_np(expression, variables))
                    print("inside", inside)
                    result = polyop.poly_addition(result, inside)

            print("inside1", inside)
        i += 1
    print("returning")
    return inside