def p_postfix_expression(p): """postfix_expression : postfix_expression list_apostrophe LPAREN argument_list RPAREN | postfix_expression LPAREN argument_list RPAREN | postfix_expression LPAREN RPAREN | postfix_expression EXCL | primary_expression """ # 0 1 2 3 4 5 if len(p) == 2: p[0] = p[1] elif len(p) == 3: p[0] = Fact([p[1]], {}) else: if len(p) == 6: args, opts, lists = p[4] call = translate_call(p[1], args, opts, lists) fun = call.get_fun() args = call.get_args() opts = call.get_options() dummy = Id(dummy_gen.get_dummy()) lambda_fn = Fun(dummy, Call(fun, [dummy], opts)) diff_fn = Diff([lambda_fn, Int(p[2])], {}) p[0] = Call(diff_fn, args, opts) else: if len(p) == 5: args, opts, lists = p[3] else: args, opts, lists = [], {}, [] p[0] = translate_call(p[1], args, opts, lists)
def _visit_id(self, tree: Id, *args): if tree.id in constant_functions: fun = constant_functions[tree.id] dummy_var = self.dummy_gen.get_dummy() if fun.can_be_implicit() and fun.arity() == 1: return Fun(dummy_var, fun([dummy_var], {})) return tree
def test(self): test_list = [(Id('x'), "x"), (Call(Id('f'), [Int(4)], {}), "f(Int(4))"), (Fun(Id('x'), Sum([Float(1), Id('x')], {})), "x -> Sum(Float(1), x)"), ] for (tree, string) in test_list: printer = DefaultPrinter() self.assertEqual(printer.to_str(tree), string)
def testLambdafier(self): test_list = [ (Diff([Id("Cos")], {}), Diff([Fun(Id('_0'), Cos([Id('_0')], {}))], {})), (Cos([Id('x')], {}), Cos([Id('x')], {})), (Diff([Id("Sum")], {}), Diff([Id("Sum")], {})), ] for (tree, ret) in test_list: transformer = Transformer(tree) lambdafier = CalchasTreeLambdafier(prefix="_") transformer.apply(lambdafier) self.assertEqual(transformer.get_tree(), ret)
def testToCalchas(self): x_ = sympy.symbols('x') f = sympy.symbols('f', cls=sympy.Function) test_list = [(sympy.Symbol('a'), Id('a')), (sympy.pi, pi), (1, Int(1)), (sympy.Rational(47, 10), Float(4.7)), (6, Int(6)), (30, Int(30)), (15, Int(15)), (f(12, 18), Call(Id('f'), [Int(12), Int(18)])), ({ 2: 1, 3: 2 }, DictFunctionExpression({ Int(2): Int(1), Int(3): Int(2) })), (18, Int(18)), (sympy.pi, pi), (sympy.Lambda(x_, x_), Fun(Id('x'), Id('x')))] for (tree, res) in test_list: builder = Translator() calchas_tree = builder.to_calchas_tree(tree) self.assertEqual(calchas_tree, res)
def testToSympy(self): _x = sympy.symbols('x_') f = sympy.symbols('f', cls=sympy.Function) test_list = [(Id('a'), sympy.Symbol('a')), (Id('pi'), sympy.Symbol('pi')), (pi, sympy.pi), (Int(1), 1), (Float(4.7), sympy.Rational(47, 10)), (Gcd([Int(12), Int(18)]), 6), (Sum([Int(12), Int(18)]), 30), (Sum([Int(12), Int(1), Int(2)]), 15), (Call(Id('f'), [Int(12), Int(18)]), f(12, 18)), (FactorInt([Int(18)]), { 2: 1, 3: 2 }), (Gcd([Sum([Int(18), Int(18)]), Int(18)]), 18), (pi, sympy.pi), (Fun(Id('x'), Id('x')), sympy.Lambda(_x, _x))] for (tree, res) in test_list: builder = Translator() sympy_tree = builder.to_sympy_tree(tree) self.assertEqual(sympy_tree, res)
def _visit_fun(self, tree: Fun, *args): return Fun(tree.var, self.visit(tree.expr))
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))