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))])
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))])
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)) ])
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, ))])
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, ()))])
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))])
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)])
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)])
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)])
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))])
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)])
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)])
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))])
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, ))])
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)])
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)])
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)) ])
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))])
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)])
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)) ])
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,))])
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]))])
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)) ])
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), [])
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))])
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)])
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)) ])
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))])
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)) ])
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)])