Exemplo n.º 1
0
    def test_match_add_logarithms(self):
        root = tree('log a + ln b')
        self.assertEqualPos(match_add_logarithms(root), [])

        # log(ab) is not desired if ab is not reduceable
        root = tree('log a + log b')
        self.assertEqualPos(match_add_logarithms(root), [])

        log_a, log_b = root = tree('log 2 + log 3')
        self.assertEqualPos(match_add_logarithms(root),
                [P(root, add_logarithms, (Scope(root), log_a, log_b))])

        log_a, log_b = root = tree('-log 2 - log 3')
        self.assertEqualPos(match_add_logarithms(root),
                [P(root, expand_negations, (Scope(root), log_a, log_b))])

        # log(2 / 3) is not desired because 2 / 3 cannot be reduced
        log_a, log_b = root = tree('log 2 - log 3')
        self.assertEqualPos(match_add_logarithms(root), [])

        log_a, log_b = root = tree('log 4 - log 2')
        self.assertEqualPos(match_add_logarithms(root),
                [P(root, subtract_logarithms, (Scope(root), log_a, log_b))])

        log_a, log_b = root = tree('-log 2 + log 4')
        self.assertEqualPos(match_add_logarithms(root),
                [P(root, subtract_logarithms, (Scope(root), log_b, log_a))])
Exemplo n.º 2
0
 def test_match_const_deriv_multiplication_multiple_constants(self):
     root = tree('d/dx 2x * 3')
     (l2, x), l3 = root[0]
     scope = Scope(root[0])
     self.assertEqualPos(match_const_deriv_multiplication(root),
             [P(root, const_deriv_multiplication, (scope, l2, x)),
              P(root, const_deriv_multiplication, (scope, l3, x))])
Exemplo n.º 3
0
 def test_find_possibilities_sort(self):
     (ab, cd), e = root = tree('(a + b)(c + d)e')
     self.assertEqualPos(find_possibilities(root), [
         P(root, expand_single, (Scope(root), cd, e)),
         P(root, expand_single, (Scope(root), ab, e)),
         P(root, expand_double, (Scope(root), ab, cd))
     ])
Exemplo n.º 4
0
    def test_match_division_in_denominator(self):
        a, ((b, c), d) = root = tree('a / (b / c + d)')
        self.assertEqualPos(match_division_in_denominator(root),
                            [P(root, multiply_with_term, (c, ))])

        a, ((d, (b, c)), e) = root = tree('a / (d + b / c + e)')
        self.assertEqualPos(match_division_in_denominator(root),
                            [P(root, multiply_with_term, (c, ))])
Exemplo n.º 5
0
    def test_match_goniometric_chain_rule(self):
        root, x2 = tree('d/dx sin(x ^ 2), x ^ 2')
        self.assertEqualPos(match_goniometric(root),
                [P(root, chain_rule, (x2, sinus, ()))])

        root = tree('d/dx cos(x ^ 2)')
        self.assertEqualPos(match_goniometric(root),
                [P(root, chain_rule, (x2, cosinus, ()))])
Exemplo n.º 6
0
    def test_match_reduce_sqrt_dividers(self):
        root = tree('sqrt(8)')
        self.assertEqualPos(match_reduce_sqrt(root),
                            [P(root, split_dividers, (4, 2))])

        root = tree('sqrt(27)')
        self.assertEqualPos(match_reduce_sqrt(root),
                            [P(root, split_dividers, (9, 3))])
Exemplo n.º 7
0
    def test_match_zero_derivative(self):
        root = tree('d/dy x')
        self.assertEqualPos(match_zero_derivative(root),
                [P(root, zero_derivative)])

        root = tree('d/dx 2')
        self.assertEqualPos(match_zero_derivative(root),
                [P(root, zero_derivative)])
Exemplo n.º 8
0
    def test_match_one_derivative(self):
        root = tree('d/dx x')
        self.assertEqualPos(match_one_derivative(root),
                [P(root, one_derivative)])

        root = tree('d/dx x')
        self.assertEqualPos(match_one_derivative(root),
                [P(root, one_derivative)])
Exemplo n.º 9
0
    def test_match_exponent_to_root(self):
        root = tree('a ^ (1 / 2)')
        self.assertEqualPos(match_exponent_to_root(root),
                            [P(root, exponent_to_root)])

        root = tree('a ^ (n / 2)')
        self.assertEqualPos(match_exponent_to_root(root),
                            [P(root, exponent_to_root)])
Exemplo n.º 10
0
    def test_match_multiply_one(self):
        l1, x = root = tree('1x')
        self.assertEqual(match_multiply_numerics(root),
                         [P(root, multiply_one, (Scope(root), l1))])

        (x, l1), x = root = tree('x * 1x')
        self.assertEqual(match_multiply_numerics(root),
                         [P(root, multiply_one, (Scope(root), l1))])
Exemplo n.º 11
0
    def test_match_factor_out_constant(self):
        root, c, cx = tree('int cx dx, c, cx')
        self.assertEqualPos(match_factor_out_constant(root),
                            [P(root, factor_out_constant, (Scope(cx), c))])

        root = tree('int -x2 dx')
        self.assertEqualPos(match_factor_out_constant(root),
                            [P(root, factor_out_integral_negation)])
Exemplo n.º 12
0
    def test_match_move_term_absolute(self):
        root = tree('|x| = 2')
        self.assertEqualPos(match_move_term(root),
                            [P(root, split_absolute_equation)])

        root = tree('|x - 1| = 2')
        self.assertEqualPos(match_move_term(root),
                            [P(root, split_absolute_equation)])
Exemplo n.º 13
0
    def test_match_divide_fractions(self):
        (a, b), c = root = tree('a / b / c')
        self.assertEqualPos(match_divide_fractions(root),
                            [P(root, divide_fraction, (a, b, c))])

        root = tree('a / (b / c)')
        self.assertEqualPos(match_divide_fractions(root),
                            [P(root, divide_by_fraction, (a, b, c))])
Exemplo n.º 14
0
    def test_match_move_term_subtract(self):
        root, a = tree('x + a = b, a')
        self.assertEqualPos(match_move_term(root),
                            [P(root, subtract_term, (a, ))])

        root, cx = tree('x = b + cx, cx')
        self.assertEqualPos(match_move_term(root),
                            [P(root, subtract_term, (cx, ))])
Exemplo n.º 15
0
    def test_match_variable_power(self):
        root, x, l2 = tree('d/dx x ^ 2, x, 2')
        self.assertEqualPos(match_variable_power(root),
                [P(root, variable_root)])

        root = tree('d/dx 2 ^ x')
        self.assertEqualPos(match_variable_power(root),
                [P(root, variable_exponent)])
Exemplo n.º 16
0
    def test_match_factor_out_exponent(self):
        for root in tree('log(a ^ 2), log(2 ^ a), log(a ^ a), log(2 ^ 2)'):
            self.assertEqualPos(match_factor_out_exponent(root),
                    [P(root, factor_out_exponent)])

        root = tree('log(a ^ -b)')
        self.assertEqualPos(match_factor_out_exponent(root),
                [P(root, split_negative_exponent),
                 P(root, factor_out_exponent)])
Exemplo n.º 17
0
    def test_match_add_exponents_multiple_identifiers(self):
        a, b, p, q = tree('a,b,p,q')
        ((a0, b0), a1), b1 = root = a**p * b**p * a**q * b**q

        possibilities = match_add_exponents(root)
        self.assertEqualPos(possibilities, [
            P(root, add_exponents, (Scope(root), a0, a1, a, p, q)),
            P(root, add_exponents, (Scope(root), b0, b1, b, p, q))
        ])
Exemplo n.º 18
0
    def test_match_const_deriv_multiplication(self):
        root = tree('d/dx 2x')
        l2, x = root[0]
        self.assertEqualPos(match_const_deriv_multiplication(root),
                [P(root, const_deriv_multiplication, (Scope(root[0]), l2, x))])

        (x, y), x = root = tree('d/dx xy')
        self.assertEqualPos(match_const_deriv_multiplication(root),
                [P(root, const_deriv_multiplication, (Scope(root[0]), y, x))])
Exemplo n.º 19
0
    def test_match_goniometric(self):
        root = tree('d/dx sin(x)')
        self.assertEqualPos(match_goniometric(root), [P(root, sinus)])

        root = tree('d/dx cos(x)')
        self.assertEqualPos(match_goniometric(root), [P(root, cosinus)])

        root = tree('d/dx tan(x)')
        self.assertEqualPos(match_goniometric(root), [P(root, tangens)])
Exemplo n.º 20
0
    def test_match_combine_groups_n_const(self):
        ((l2, a0), (l3, a1)), (l4, a2) = (m0, m1), m2 = root = tree('2a+3a+4a')

        possibilities = match_combine_groups(root)
        self.assertEqualPos(possibilities, [
            P(root, combine_groups, (Scope(root), l2, a0, m0, l3, a1, m1)),
            P(root, combine_groups, (Scope(root), l2, a0, m0, l4, a2, m2)),
            P(root, combine_groups, (Scope(root), l3, a1, m1, l4, a2, m2))
        ])
Exemplo n.º 21
0
    def test_match_negated_parameter(self):
        s, c = tree('sin -t, cos -t')
        t = s[0]

        self.assertEqualPos(match_negated_parameter(s), \
                [P(s, negated_sinus_parameter, (t,))])

        self.assertEqualPos(match_negated_parameter(c), \
                [P(c, negated_cosinus_parameter, (t,))])
Exemplo n.º 22
0
    def test_match_remove_division_negation(self):
        root = tree('-(-a + b) / c')
        self.assertEqualPos(
            match_remove_division_negation(root),
            [P(root, remove_division_negation, (True, root[0]))])

        root = tree('-a / (-b + c)')
        self.assertEqualPos(
            match_remove_division_negation(root),
            [P(root, remove_division_negation, (False, root[1]))])
Exemplo n.º 23
0
    def test_match_add_exponents_ternary(self):
        a, p, q, r = tree('a,p,q,r')
        (n0, n1), n2 = root = a**p * a**q * a**r

        possibilities = match_add_exponents(root)
        self.assertEqualPos(possibilities, [
            P(root, add_exponents, (Scope(root), n0, n1, a, p, q)),
            P(root, add_exponents, (Scope(root), n0, n2, a, p, r)),
            P(root, add_exponents, (Scope(root), n1, n2, a, q, r))
        ])
Exemplo n.º 24
0
    def test_match_constant_exponent(self):
        a0, a1, a2 = tree('a ^ 0, a ^ 1, a ^ 2')

        self.assertEqualPos(match_constant_exponent(a0),
                            [P(a0, remove_power_of_zero, ())])

        self.assertEqualPos(match_constant_exponent(a1),
                            [P(a1, remove_power_of_one, ())])

        self.assertEqualPos(match_constant_exponent(a2), [])
Exemplo n.º 25
0
    def test_match_add_numerics(self):
        l1, l2 = root = tree('1 + 2')
        possibilities = match_add_numerics(root)
        self.assertEqualPos(possibilities,
                            [P(root, add_numerics, (Scope(root), l1, l2))])

        (l1, b), l2 = root = tree('1 + b + 2')
        possibilities = match_add_numerics(root)
        self.assertEqualPos(possibilities,
                            [P(root, add_numerics, (Scope(root), l1, l2))])
Exemplo n.º 26
0
    def test_match_negated_division_single(self):
        l1, l2 = root = tree('-1 / 2')
        self.assertEqualPos(match_negated_division(root), [])

        l1, l2 = root = tree('(-1) / 2')
        self.assertEqualPos(match_negated_division(root),
                            [P(root, negated_nominator)])

        l1, l2 = root = tree('1 / -2')
        self.assertEqualPos(match_negated_division(root),
                            [P(root, negated_denominator)])
Exemplo n.º 27
0
    def test_match_negated_factor(self):
        a, b = root = tree('a * -b')
        self.assertEqualPos(match_negated_factor(root),
                            [P(root, negated_factor, (Scope(root), b))])

        (a, b), c = root = tree('a * (-b) * -c')
        scope = Scope(root)
        self.assertEqualPos(match_negated_factor(root), [
            P(root, negated_factor, (scope, b)),
            P(root, negated_factor, (scope, c))
        ])
Exemplo n.º 28
0
    def test_match_sum_product_rule_sum(self):
        root = tree('d/dx (x ^ 2 + x)')
        x2, x = f = root[0]
        self.assertEqualPos(match_sum_product_rule(root),
                [P(root, sum_rule, (Scope(f), x2)),
                 P(root, sum_rule, (Scope(f), x))])

        root = tree('d/dx (x ^ 2 + 3 + x)')
        self.assertEqualPos(match_sum_product_rule(root),
                [P(root, sum_rule, (Scope(root[0]), x2)),
                 P(root, sum_rule, (Scope(root[0]), x))])
Exemplo n.º 29
0
    def test_match_sum_rule_integral(self):
        (f, g), x = root = tree('int (2x + 3x) dx')
        self.assertEqualPos(match_sum_rule_integral(root),
                            [P(root, sum_rule_integral, (Scope(root[0]), f))])

        ((f, g), h), x = root = tree('int (2x + 3x + 4x) dx')
        self.assertEqualPos(match_sum_rule_integral(root), [
            P(root, sum_rule_integral, (Scope(root[0]), f)),
            P(root, sum_rule_integral, (Scope(root[0]), g)),
            P(root, sum_rule_integral, (Scope(root[0]), h))
        ])
Exemplo n.º 30
0
    def test_match_constant_integral(self):
        root = tree('int x dx')
        self.assertEqualPos(match_constant_integral(root),
                            [P(root, single_variable_integral)])

        root = tree('int 2')
        self.assertEqualPos(match_constant_integral(root),
                            [P(root, constant_integral)])

        root = tree('int c dx')
        self.assertEqualPos(match_constant_integral(root),
                            [P(root, constant_integral)])