Esempio n. 1
0
 def testSubstitution2(self):
     e = parse_expr("INT x:[0,1]. exp(6*x)")
     e = rules.Substitution1("u", parse_expr("6 * x")).eval(e)
     e = rules.Linearity().eval(e)
     e = rules.OnSubterm(rules.CommonIntegral()).eval(e)
     e = rules.Simplify().eval(e)
     self.assertEqual(str(e.normalize()), "-1/6 + 1/6 * exp(6)")
Esempio n. 2
0
    def eval(self, e, loc=[]):
        if not isinstance(e, Integral):
            return e

        res = []
        factors = decompose_expr_factor(e.body)

        if len(factors) == 1:
            factors.append(Const(1))

        for i in range(len(factors)):
            h = factors[i]
            rest_factor = [f for f in factors if f != h]
            G = functools.reduce(operator.mul, rest_factor)
            H = rules.CommonIntegral().eval(
                Integral(e.var, e.lower, e.upper, h))
            if H.body != h or h == exp(Var(e.var)):
                u = G
                v = H.body
                try:  # can't deriv abs now
                    new_integral = rules.IntegrationByParts(u, v).eval(e)
                except:
                    continue
                step = [calc.IntegrationByPartsStep(new_integral, u, v, loc)]
                res.append((new_integral, step))

        return res
Esempio n. 3
0
 def testSubstitution(self):
     e = parse_expr("INT x:[0,1]. (3 * x + 1) ^ (-2)")
     e = rules.Substitution1("u", parse_expr("3 * x + 1")).eval(e)
     e = rules.Linearity().eval(e)
     e = rules.OnSubterm(rules.CommonIntegral()).eval(e)
     e = rules.Simplify().eval(e)
     self.assertEqual(e, Const(Fraction("1/4")))
Esempio n. 4
0
 def testIntegrationByParts(self):
     e = parse_expr("INT x:[-1,2]. x * exp(x)")
     e = rules.IntegrationByParts(parse_expr("x"),
                                  parse_expr("exp(x)")).eval(e)
     e = rules.Simplify().eval(e)
     e = rules.Linearity().eval(e)
     e = rules.OnSubterm(rules.CommonIntegral()).eval(e)
     e = rules.Simplify().eval(e)
     self.assertEqual(str(e.normalize()), "2 * exp(-1) + exp(2)")
Esempio n. 5
0
    def testOnSubterm(self):
        test_data = [
            ("(INT x:[a,b]. 1) + 2 * (INT x:[a,b]. x) + (INT x:[a,b]. x ^ 2)",
             "([x]_x=a,b) + 2 * ([x ^ 2 / 2]_x=a,b) + [x ^ 3 / 3]_x=a,b"),
            ("INT x:[0,pi]. cos(x) ^ 4", "INT x:[0,pi]. cos(x) ^ 4"),
            ("(INT x:[0,pi]. cos(x) ^ 2) - (INT x:[0,pi]. cos(x) ^ 4)",
             "(INT x:[0,pi]. cos(x) ^ 2) - (INT x:[0,pi]. cos(x) ^ 4)")
        ]

        rule = rules.OnSubterm(rules.CommonIntegral())
        for s, s2 in test_data:
            s = parse_expr(s)
            s2 = parse_expr(s2)
            self.assertEqual(rule.eval(s), s2)
Esempio n. 6
0
    def testCommonIntegral(self):
        test_data = [("INT x:[a,b]. 1", "[x]_x=a,b"),
                     ("INT x:[a,b]. 2", "[2 * x]_x=a,b"),
                     ("INT x:[a,b]. x", "[x ^ 2 / 2]_x=a,b"),
                     ("INT x:[a,b]. x ^ 2", "[x ^ 3 / 3]_x=a,b"),
                     ("INT x:[a,b]. x ^ 3", "[x ^ 4 / 4]_x=a,b"),
                     ("INT x:[a,b]. x ^ -1", "[log(abs(x))]_x=a,b"),
                     ("INT x:[a,b]. 1 / x", "[log(abs(x))]_x=a,b"),
                     ("INT x:[a,b]. 1 / x ^ 2", "[x ^ -1 / -1]_x=a,b"),
                     ("INT x:[a,b]. sin(x)", "[-cos(x)]_x=a,b"),
                     ("INT x:[a,b]. cos(x)", "[sin(x)]_x=a,b"),
                     ("INT x:[a,b]. 1 / (x ^ 2 + 1)", "[atan(x)]_x=a,b"),
                     ("INT t:[a,b]. 8 ^ (1/2)", "[2 * sqrt(2) * t]_t=a,b"),
                     ("INT x:[a,b]. (1 + x^2)^(-1)", "[atan(x)]_x=a,b"),
                     ("INT x:[0, pi/4]. csc(x) ^ 2", "[-cot(x)]_x=0,1/4 * pi")]

        rule = rules.CommonIntegral()
        for s, s2 in test_data:
            s = parse_expr(s)
            self.assertEqual(str(rule.eval(s.normalize())), s2)
Esempio n. 7
0
 def testIntegral1(self):
     e = parse_expr("INT x:[2,3]. 2 * x + x ^ 2")
     e = rules.Linearity().eval(e)
     e = rules.OnSubterm(rules.CommonIntegral()).eval(e)
     e = rules.Simplify().eval(e)
     self.assertEqual(e, Const(Fraction(34, 3)))
Esempio n. 8
0
 def eval(self, e):
     new_e = rules.OnSubterm(rules.CommonIntegral()).eval(e)
     steps = []
     if new_e != e:
         steps.append(calc.CommonIntegralStep(new_e))
     return new_e, steps