def p_multiplicative_expression(p): """multiplicative_expression : multiplicative_expression TIMES unary_expression | multiplicative_expression DIVIDE unary_expression | unary_expression """ if len(p) == 2: p[0] = p[1] else: if p[2] == '*': p[0] = Prod([p[1], p[3]], {}) elif p[2] == '/': p[0] = Prod([p[1], Pow([p[3], Int(-1)], {})], {})
def p_postfix_expression_binary(p): """postfix_expression_binary : BINARY LBRACE argument_list RBRACE LBRACE argument_list RBRACE""" # 0 1 2 3 4 5 6 7 if p[1] == '\\frac': p[0] = Prod([p[3][0][0], Pow([p[6][0][0], Int(-1)], {})], {}) elif p[1] == '\\binom': p[0] = C([p[3][0][0], p[6][0][0]], {})
def p_multiplicative_expression(p): """multiplicative_expression : multiplicative_expression TIMES unary_expression | multiplicative_expression DIVIDE unary_expression | multiplicative_expression MOD unary_expression | BIGPROD DOWN LBRACE ID EQ expression RBRACE POW LBRACE expression RBRACE unary_expression | unary_expression """ # 0 1 2 3 4 5 6 7 8 9 10 11 12 if len(p) == 13: p[0] = BigProd([p[12], translate_id(p[4]), p[6], p[10]], {}) if len(p) == 2: p[0] = p[1] else: if p[2] in ['*', '\\times']: p[0] = Prod([p[1], p[3]], {}) elif p[2] == '/': p[0] = Prod([p[1], Pow([p[3], Int(-1)], {})], {}) elif p[2] == '\\%': p[0] = Mod([p[1], p[3]], {})
def p_additive_expression(p): """additive_expression : additive_expression PLUS multiplicative_expression | additive_expression MINUS multiplicative_expression | multiplicative_expression """ if len(p) == 2: p[0] = p[1] else: if p[2] == '+': p[0] = Sum([p[1], p[3]], {}) elif p[2] == '-': p[0] = Sum([p[1], Prod([Int(-1), p[3]], {})], {})
def p_expr_unary(p): """unary_expression : MINUS unary_expression %prec NEG | PLUS unary_expression %prec NEG | pow_expression """ if len(p) == 2: p[0] = p[1] else: if p[1] == '-': p[0] = Prod([Int(-1), p[2]], {}) else: p[0] = p[2]
def testSum(self): test_list = [ (Sum([Int(1), Sum([Int(2), Int(3)], {})], {}), Sum([Int(1), Int(2), Int(3)], {})), (Sum([Int(0), Sum([Int(2), Sum([Int(3), Int(4)], {})], {})], {}), Sum([Int(2), Int(3), Int(4)], {})), (Sum([ Int(0), Int(6), Prod([Int(2), Sum([Int(3), Sum([Int(4), Int(5)], {})], {})], {}) ], {}), Sum([ Int(6), Prod([Int(2), Sum([Int(3), Int(4), Int(5)], {})], {}) ], {})), (Sum([Id('x')], {}), Id('x')) ] for (tree, ret) in test_list: transformer = Transformer(tree) simplifier = SimplifySum() transformer.apply(simplifier) self.assertEqual(transformer.get_tree(), ret)
def p_additive_expression(p): """additive_expression : additive_expression PLUS multiplicative_expression | additive_expression MINUS multiplicative_expression | BIGSUM DOWN LBRACE ID EQ expression RBRACE POW LBRACE expression RBRACE multiplicative_expression | BIGINT DOWN LBRACE ID EQ expression RBRACE POW LBRACE expression RBRACE multiplicative_expression | multiplicative_expression """ # 0 1 2 3 4 5 6 7 8 9 10 11 12 if len(p) == 13: if p[1] == "\\int": p[0] = Integrate([p[12], translate_id(p[4]), p[6], p[10]], {}) else: p[0] = Series([p[12], translate_id(p[4]), p[6], p[10]], {}) elif len(p) == 2: p[0] = p[1] else: if p[2] == '+': p[0] = Sum([p[1], p[3]], {}) elif p[2] == '-': p[0] = Sum([p[1], Prod([Int(-1), p[3]], {})], {})
def testParser(self): a = Id('a') i = Id('i') n = Id('n') x = Id('x') y = Id('y') test_list = [ ("0", Int(0)), ("sin(x)", Sin([x], {})), ("\\sin{y}", Sin([y], {})), ("\\sqrt{4}", Sqrt([Int(4)], {})), ("\\sqrt{a}", Sqrt([a], {})), ("\\sqrt[n]{a}", Pow([a, Pow([n, Int(-1)], {})], {})), ("\\frac{1}{2}", Prod([Int(1), Pow([Int(2), Int(-1)], {})], {})), ("\\binom{6}{4}", C([Int(6), Int(4)], {})), ("\\lceil sin(x) \\rceil", Ceiling([Sin([x], {})], {})), ("\\lim_{x\\to 0} sin(x)/x", Limit( [Prod([Sin([x], {}), Pow([x, Int(-1)], {})], {}), x, Int(0)], {})), ("\\sum _ { i = 0 } ^ { \\infty } (1)", Series([Int(1), i, Int(0), infinity], {})), ("\\sum _ { i = 0 } ^ { \\infty } (1/i^{2}) ", Series([ Prod( [Int(1), Pow( [Pow([i, Int(2)], {}), Int(-1)], {})], {}), i, Int(0), infinity ], {})), ("\\frac { \\log ( \\frac { 1 } { 2 } ) } { 2 ^ {2} }", Prod([ Log([Prod([Int(1), Pow([Int(2), Int(-1)], {})], {})], {}), Pow([Pow([Int(2), Int(2)], {}), Int(-1)], {}) ], {})), ("2\\times(1+3)", Prod([Int(2), Sum([Int(1), Int(3)], {})], {})), ] for e in test_list: if len(e) == 3: expr, res, d = e else: expr, res = e d = False self.assertEqual( repr(latexParser.parse(expr, lexer=latexLexer, debug=d)), repr(res))
def testParserWithImplicitBracesOrTimes(self): a = Id('a') b = Id('b') c = Id('c') d = Id('d') f = Id('f') g = Id('g') i = Id('i') j = Id('j') n = Id('n') x = Id('x') test_list = [ ("f ( a ) ", "f ( a ) ", Call(f, [a], {})), ("( a ) ", "( a ) ", a), ("( ( a ) ) ", "( ( a ) ) ", a), ("( ( a ) b ) ", "( ( a ) *b ) ", Prod([a, b], {})), ("( ( abc ) b a ^ {n} \\% b ! + c ) ", "( ( abc ) *b *a ^ { n } \\% b ! + c ) ", Sum([ Mod([ Prod([Prod([Id('abc'), b], {}), Pow([a, n], {})], {}), Fact([b], {}) ], {}), c ], {})), ("f(x)a", "f ( x ) *a ", Prod([Call(f, [x], {}), a], {})), ("g f(n)", "g *f ( n ) ", Prod([g, Call(f, [n], {})], {})), ("((f(a)(a+b)a))a(c+d)d", "( ( f ( a ) *( a + b ) *a ) ) *a ( c + d ) *d ", Prod([ Prod([ Prod([Prod( [Call(f, [a], {}), Sum([a, b], {})], {}), a], {}), Call(a, [Sum([c, d], {})], {}) ], {}), d ], {})), ("a 2", "a *2 ", Prod([a, Int(2)], {})), ("2 a", "2 *a ", Prod([Int(2), a], {})), ("2 4", "2 *4 ", Prod([Int(2), Int(4)], {})), ("2x", "2 *x ", Prod([Int(2), x], {})), ("x2", "x *2 ", Prod([x, Int(2)], {})), ("a-b", "a - b ", Sum([a, Prod([Int(-1), b], {})], {})), ("a/-b", "a / - b ", Prod([a, Pow( [Prod([Int(-1), b], {}), Int(-1)], {})], {})), ("-a/+b", "- a / + b ", Prod([Prod([Int(-1), a], {}), Pow([b, Int(-1)], {})], {})), ("a(b+c)", "a ( b + c ) ", Call(a, [Sum([b, c], {})], {})), ("42(b+c)", "42 *( b + c ) ", Prod([Int(42), Sum([b, c], {})], {})), ("1/2\\pi", "1 / 2 *\\pi ", Prod([Prod([Int(1), Pow([Int(2), Int(-1)], {})], {}), pi], {})), ("\\sum_{i=0}^\\infty i", "\sum _ { i = 0 } ^ { \infty } { i } ", Series([i, i, Int(0), infinity], {})), ("\\sum\\limits_{j=0}^\\infty j", "\sum _ { j = 0 } ^ { \infty } { j } ", Series([j, j, Int(0), infinity], {})), ("\\sum_{i=0}^\\infty(1/i^2) ", "\sum _ { i = 0 } ^ { \infty } ( 1 / i ^ { 2 } ) ", Series([ Prod( [Int(1), Pow( [Pow([i, Int(2)], {}), Int(-1)], {})], {}), i, Int(0), infinity ], {})), ("\\sqrt{a}", "\\sqrt { a } ", Sqrt([a], {})), ("\\sqrt[n]{a}", "\\sqrt [ n ] { a } ", Pow([a, Pow([n, Int(-1)], {})], {})), ("\\sqrt {\\sqrt a}", "\\sqrt { \\sqrt { a } } ", Sqrt([Sqrt([a], {})], {})), ] for e in test_list: if len(e) == 4: expr, prep, res, d = e else: expr, prep, res = e d = False expr_ = preprocess_latex(expr) self.assertEqual(expr_, prep) self.assertEqual( repr(latexParser.parse(expr_, lexer=latexLexer, debug=d)), repr(res))
def testParser(self): a = Id('a') b = Id('b') c = Id('c') e = Id('e') i = Id('i') j = Id('j') m = Id('m') n = Id('n') x = Id('x') y = Id('y') test_list = [ ("0", Int(0)), ("Sin[x]", Sin([x], {})), ("Arctan[x]", Call(Id('Arctan'), [x], {})), ("Sqrt[4]", Call(Id('Sqrt'), [Int(4)], {})), ("ArcTan[x]", Call(Id('ArcTan'), [x], {})), ("x^2", Pow([x, Int(2)], {})), ("a+b", Sum([a, b], {})), ("a+b+c", Sum([Sum([a, b], {}), c], {})), ("a*b", Prod([a, b], {})), ("a!", Fact([a], {})), ("2*a!", Prod([Int(2), Fact([a], {})], {})), ("a!!", Fact([Fact([a], {})], {})), ("-a!", Prod([Int(-1), Fact([a], {})], {})), ("b+a!", Sum([b, Fact([a], {})], {})), ("-a-a", Sum([Prod([Int(-1), a], {}), Prod([Int(-1), a], {})], {})), ("--a", Prod([Int(-1), Prod([Int(-1), a], {})], {})), ("+a", a), ("+-a", Prod([Int(-1), a], {})), ("-+a", Prod([Int(-1), a], {})), ("a*-b", Prod([a, Prod([Int(-1), b], {})], {})), ("-a*b", Prod([Prod([Int(-1), a], {}), b], {})), ("a/b", Prod([a, Pow([b, Int(-1)], {})], {})), ("a-b", Sum([a, Prod([Int(-1), b], {})], {})), ("a^b", Pow([a, b], {})), ("a^b/c", Prod([Pow([a, b], {}), Pow([c, Int(-1)], {})], {})), ("-a^b/c", Prod( [Prod([Int(-1), Pow([a, b], {})], {}), Pow([c, Int(-1)], {})], {})), ("(a+b)", Sum([a, b], {})), ("(a*b)", Prod([a, b], {})), ("(a/b)", Prod([a, Pow([b, Int(-1)], {})], {})), ("(a-b)", Sum([a, Prod([Int(-1), b], {})], {})), ("(a^b)", Pow([a, b], {})), ("(a)^b", Pow([a, b], {})), ("(a+b)*c", Prod([Sum([a, b], {}), c], {})), ("a+(b*c)", Sum([a, Prod([b, c], {})], {})), ("a+b*c", Sum([a, Prod([b, c], {})], {})), ("a^b^c", Pow([a, Pow([b, c], {})], {})), ("a*b/c", Prod([Prod([a, b], {}), Pow([c, Int(-1)], {})], {})), ("a+b/c", Sum([a, Prod([b, Pow([c, Int(-1)], {})], {})], {})), ("x^n/n!", Prod([Pow([x, n], {}), Pow([Fact([n], {}), Int(-1)], {})], {})), ("a^G[x]", Pow([a, Call(Id('G'), [x], {})], {})), ("f[x]+f[x]", Sum([Call(Id('f'), [x], {}), Call(Id('f'), [x], {})], {})), ("f[x]^G[x]", Pow([Call(Id('f'), [x], {}), Call(Id('G'), [x], {})], {})), ("Sin[x]^2+Cos[x]^2", Sum([ Pow([Sin([x], {}), Int(2)], {}), Pow([Cos([x], {}), Int(2)], {}) ], {})), ("Sum[1/i^6, {i, 1, Infinity}]", Series([ Prod( [Int(1), Pow( [Pow([i, Int(6)], {}), Int(-1)], {})], {}), i, Int(1), infinity ], {})), ("Sum[j/i^6, {i, 1, Infinity}, {j, 0 , m}]", Series([ Series([ Prod([j, Pow( [Pow([i, Int(6)], {}), Int(-1)], {})], {}), j, Int(0), m ], {}), i, Int(1), infinity ], {})), ("Integrate[1/(x^3 + 1), x]", Integrate([ Prod([ Int(1), Pow([Sum( [Pow([x, Int(3)], {}), Int(1)], {}), Int(-1)], {}) ], {}), x ], {})), ("Integrate[1/(x^3 + 1), {x, 0, 1}]", Integrate([ Prod([ Int(1), Pow([Sum( [Pow([x, Int(3)], {}), Int(1)], {}), Int(-1)], {}) ], {}), x, Int(0), Int(1) ], {})), ("Integrate[Sin[x*y], {x, 0, 1}, {y, 0, x}]", Integrate([ Integrate([Sin([Prod([x, y], {})], {}), y, Int(0), x], {}), x, Int(0), Int(1) ], {})), ("Limit[e, x->a]", Limit([e], {x: a})), ("Limit[Sin[x]/x, x->0]", Limit([Prod([Sin([x], {}), Pow([x, Int(-1)], {})], {})], {x: Int(0)})), ("Limit[(1+x/n)^n, x->Infinity]", Limit([ Pow([ Sum([Int(1), Prod([x, Pow([n, Int(-1)], {})], {})], {}), n ], {}) ], {x: infinity})), ("Solve[x^2==1, x]", Solve([Eq([Pow([x, Int(2)], {}), Int(1)], {}), x], {})), ] for test in test_list: if len(test) == 2: expr, res = test d = False else: expr, res, d = test self.assertEqual( repr( mathematicaParser.parse(expr, lexer=mathematicaLexer, debug=d)), repr(res))
def testParserWithImplicitMultiplication(self): a = Id('a') b = Id('b') c = Id('c') d = Id('d') f = Id('f') g = Id('g') n = Id('n') x = Id('x') test_list = [ ("(a)a^n%b!+c", "(a)*a^n%b!+c", Sum([Mod([Prod([a, Pow([a, n], {})], {}), Fact([b], {})], {}), c], {})), ("f(x)a", "f(x)*a", Prod([Call(f, [x], {}), a], {})), ("g f(n)", "g*f(n)", Prod([g, Call(f, [n], {})], {})), ("((f(a)(a+b)a))a(c+d)d", "((f(a)*(a+b)*a))*a(c+d)*d", Prod([ Prod([ Prod([Prod( [Call(f, [a], {}), Sum([a, b], {})], {}), a], {}), Call(a, [Sum([c, d], {})], {}) ], {}), d ], {})), ("a 2", "a*2", Prod([a, Int(2)], {})), ("2 a", "2*a", Prod([Int(2), a], {})), ("2 4", "2*4", Prod([Int(2), Int(4)], {})), ("2x", "2*x", Prod([Int(2), x], {})), ("x2", "x2", Id('x2')), ("a-b", "a-b", Sum([a, Prod([Int(-1), b], {})], {})), ("a/-b", "a/-b", Prod([a, Pow( [Prod([Int(-1), b], {}), Int(-1)], {})], {})), ("-a/+b", "-a/+b", Prod([Prod([Int(-1), a], {}), Pow([b, Int(-1)], {})], {})), ("a(b+c)", "a(b+c)", Call(a, [Sum([b, c], {})], {})), ("42(b+c)", "42*(b+c)", Prod([Int(42), Sum([b, c], {})], {})), ("1/2Pi", "1/2*Pi", Prod([Prod([Int(1), Pow([Int(2), Int(-1)], {})], {}), pi], {})), ] for e in test_list: if len(e) == 4: expr, prep, res, d = e else: expr, prep, res = e d = False expr_ = preprocess_natural(expr) self.assertEqual(expr_, prep) self.assertEqual( repr(naturalParser.parse(expr_, lexer=naturalLexer, debug=d)), repr(res))
def testParser(self): a = Id('a') b = Id('b') c = Id('c') d = Id('d') f = Id('f') g = Id('g') i = Id('i') j = Id('j') m = Id('m') n = Id('n') x = Id('x') y = Id('y') z = Id('z') _d0 = Id('_d0') __1 = Int(-1) _0 = Int(0) _1 = Int(1) _2 = Int(2) _3 = Int(3) _4 = Int(4) _6 = Int(6) _1024 = Int(1024) self.maxDiff = None test_list = [ ("0", _0), ("Sin(x)", Sin([x], {})), ("Arctan(x)", Arctan([x], {})), ("Sqrt(4)", Sqrt([_4], {})), ("ArcTan(x)", Arctan([x], {})), ("x^2", Pow([x, _2], {})), ("a+b", Sum([a, b], {})), ("a*b", Prod([a, b], {})), ("a!", Fact([a], {})), ("2*a!", Prod([_2, Fact([a], {})], {})), ("a!!", Fact([Fact([a], {})], {})), ("-a!", Prod([__1, Fact([a], {})], {})), ("b+a!", Sum([b, Fact([a], {})], {})), ("-a-a", Sum([Prod([__1, a], {}), Prod([__1, a], {})], {})), ("--a", Prod([__1, Prod([__1, a], {})], {})), ("+a", a), ("+-a", Prod([__1, a], {})), ("-+a", Prod([__1, a], {})), ("a*-b", Prod([a, Prod([__1, b], {})], {})), ("-a*b", Prod([Prod([__1, a], {}), b], {})), ("-a^b", Prod([__1, Pow([a, b], {})], {})), ("-c**d", Prod([__1, Pow([c, d], {})], {})), ("a/b", Prod([a, Pow([b, __1], {})], {})), ("a-b", Sum([a, Prod([__1, b], {})], {})), ("a^b", Pow([a, b], {})), ("a^b/c", Prod([Pow([a, b], {}), Pow([c, __1], {})], {})), ("-a^b/c", Prod([Prod([__1, Pow([a, b], {})], {}), Pow([c, __1], {})], {})), ("(a+b)", Sum([a, b], {})), ("(a*b)", Prod([a, b], {})), ("(a/b)", Prod([a, Pow([b, __1], {})], {})), ("(a-b)", Sum([a, Prod([__1, b], {})], {})), ("(a^b)", Pow([a, b], {})), ("(a)^b", Pow([a, b], {})), ("(a+b)*c", Prod([Sum([a, b], {}), c], {})), ("a+(b*c)", Sum([a, Prod([b, c], {})], {})), ("a+b*c", Sum([a, Prod([b, c], {})], {})), ("a^b^c", Pow([a, Pow([b, c], {})], {})), ("a*b/c", Prod([Prod([a, b], {}), Pow([c, __1], {})], {})), ("a+b/c", Sum([a, Prod([b, Pow([c, __1], {})], {})], {})), ("x^n/n!", Prod([Pow([x, n], {}), Pow([Fact([n], {}), __1], {})], {})), ("a^n%b!", Mod([Pow([a, n], {}), Fact([b], {})], {})), ("a!%b^c+d", Sum([Mod([Fact([a], {}), Pow([b, c], {})], {}), d], {})), ("a^g(x)", Pow([a, Call(g, [x], {})], {})), ("f(x)+f(x)", Sum([Call(f, [x], {}), Call(f, [x], {})], {})), ("f(x)^g(x)", Pow([Call(f, [x], {}), Call(g, [x], {})], {})), ("Sin(x)^2+Cos(x)^2", Sum([Pow([Sin([x], {}), _2], {}), Pow([Cos([x], {}), _2], {})], {})), ("Sum(1/i^6, i, 1, Infinity)", Series([ Prod([_1, Pow([Pow([i, _6], {}), __1], {})], {}), i, _1, infinity ], {})), ("Sum(Sum(j/i^6, i, 1, Infinity), j, 0 , m)", Series([ Series([ Prod([j, Pow([Pow([i, _6], {}), __1], {})], {}), i, _1, infinity ], {}), j, _0, m ], {})), ("Integrate(1/(x^3 + 1), x)", Integrate([ Prod([_1, Pow([Sum([Pow([x, _3], {}), _1], {}), __1], {})], {}), x ], {})), ("Integrate(1/(x^3 + 1), x, 0, 1)", Integrate([ Prod([_1, Pow([Sum([Pow([x, _3], {}), _1], {}), __1], {})], {}), x, _0, _1 ], {})), ("Integrate(Integrate(Sin(x*y), x, 0, 1), y, 0, x)", Integrate([ Integrate([Sin([Prod([x, y], {})], {}), x, _0, _1], {}), y, _0, x ], {})), ("Limit(Sin(x)/x, x, 0)", Limit([Prod([Sin([x], {}), Pow([x, __1], {})], {}), x, _0], {})), ("Limit((1+x/n)^n, x, Infinity)", Limit([ Pow([Sum([_1, Prod([x, Pow([n, __1], {})], {})], {}), n], {}), x, infinity ], {})), ("Pow(1024, 1/2)", Pow([_1024, Prod([_1, Pow([_2, __1], {})], {})], {})), ("cos'(x)", Call(Diff([Fun(_d0, Cos([_d0], {})), _1], {}), [x], {})), ("cos''''(x)", Call(Diff([Fun(_d0, Cos([_d0], {})), _4], {}), [x], {})), ("x | y", Or([x, y], {})), ("~ y", Not([y], {})), ("x & y", And([x, y], {})), ("x | ~z & ~ y", Or([x, And([Not([z], {}), Not([y], {})], {})], {})), ] for (expr, res) in test_list: self.assertEqual(repr(parse_natural(expr)), repr(res))