def test_get_new_form4(self): poly1 = ['x', 'y', '^', '2', '-', 'x', '*', '2', '*', 'y'] poly2 = ['+', '5', 'y', '^', '2', '*', 'x', '-', '10'] new_form = {(1.0, 2.0, 0): 6.0, (1.0, 1.0, 0): -2.0, (0, 0, 0): -10.0} self.assertEqual( new_form, (get_new_form(poly1, ['x', 'y', 'z'], 1e-6) + (get_new_form(poly2, ['x', 'y', 'z'], 1e-6))).monomials)
def test_get_new_form3(self): poly = ['x', '-', 'y'] new_form = {(1.0, 0, 0): -1, (0, 1.0, 0): 1} self.assertEqual( new_form, (get_new_form(poly, ['x', 'y', 'z'], 1e-6) * get_new_form(['-', '1'], ['x', 'y', 'z'], 1e-6)).monomials)
def test_get_new_form2(self): poly12 = ['x', '+', 'y'] poly21 = ['x', '-', 'y'] new_form = {(2.0, 0, 0): 1, (0, 2.0, 0): -1} self.assertEqual( new_form, (get_new_form(poly12, ['x', 'y', 'z'], 1e-6) * (get_new_form(poly21, ['x', 'y', 'z'], 1e-6))).monomials)
def test_get_new_form(self): poly1 = [ 'x', '^', '2', 'y', '^', '2', 'z', '+', '5', '*', 'x', '*', 'z' ] poly2 = ['y', '+', '5', 'z'] new_form = { (2.0, 3.0, 1.0): 1.0, (2.0, 2.0, 2.0): 5.0, (1.0, 1.0, 1.0): 5.0, (1.0, 0, 2.0): 25 } self.assertEqual( new_form, (get_new_form(poly1, ['x', 'y', 'z'], 1e-6) * (get_new_form(poly2, ['x', 'y', 'z'], 1e-6))).monomials)
def test_get_new_form6(self): polynomial = [ '2', '*', '3', 'y', '^', '2', 'x', 'y', '^', '12', '-', '3', '*', 'x' ] new_form = {(1.0, 14.0, 0): 6.0, (1.0, 0, 0): -3.0} self.assertEqual( new_form, get_new_form(polynomial, ['x', 'y', 'z'], 1e-6).monomials)
from Getting_new_form import get_new_form, OPERATORS OPERATORS_BRACKETS = ('(', ')', '+', '-', '*', '/', '^') PRIORITY_DICT = {'(': 0, ')': 0, '+': 1, '-': 1, '*': 2, '/': 2, '^': 3} OPS = { '+': lambda w1, w2, variables, EPS: w1 + w2, '*': lambda w1, w2, variables, EPS: w1 * w2, '/': lambda w1, w2, variables, EPS: w2.__div__(w1, variables), '^': lambda w1, w2, variables, EPS: w2.__pow__(w1, variables), '-': lambda w1, w2, variables, EPS: w1 * get_new_form(['-', '1'], variables, EPS ) + w2 } def convert_to_postfix_notation(polynomial): ''' Converts polynomial to postfix notatin. ''' outputSeparated = [] stack = [] for c in polynomial: if c in OPERATORS_BRACKETS: if stack and c != '(': if c == ')': s = stack.pop() while s != '(':
def test_get_new_form5(self): polynomial = ['1', '*', 'x', '+', '3', '*', 'y', '-', '5', '*', 'z'] new_form = {(1.0, 0, 0): 1.0, (0, 1.0, 0): 3.0, (0, 0, 1): -5.0} self.assertEqual( new_form, get_new_form(polynomial, ['x', 'y', 'z'], 1e-6).monomials)