Exemple #1
0
    def test_infinity(self):
        expressions = [('2^3000', N('^', L(2), L(3000))),
                       ('2^-3000', N('^', L(2), -L(3000)))]
        #               ('2^99999999999', None),
        #               ('2^-99999999999', 0.0)]

        run_expressions(Parser, expressions)
Exemple #2
0
    def test_extract_fraction_terms_leaf(self):
        root, expect = tree('(ba) / a, a / a * b / 1')
        n, d = root
        self.assertEqual(
            extract_fraction_terms(root,
                                   (Scope(n), Scope(N(OP_MUL, d)), n[1], d)),
            expect)

        root, expect = tree('a / (ab), a / a * 1 / b')
        n, d = root
        self.assertEqual(
            extract_fraction_terms(root,
                                   (Scope(N(OP_MUL, n)), Scope(d), n, d[0])),
            expect)
Exemple #3
0
    def test_match_extract_fraction_terms(self):
        root, a, b, c = tree('(ab) / (ca), a, b, c')
        n, d = root
        self.assertEqualPos(
            match_extract_fraction_terms(root),
            [P(root, divide_fraction_by_term, (Scope(n), Scope(d), a, a))])

        lscp = lambda l: Scope(N(OP_MUL, l))

        n, d = root = tree('(ab) / a')
        self.assertEqualPos(
            match_extract_fraction_terms(root),
            [P(root, divide_fraction_by_term, (Scope(n), lscp(d), a, a))])

        n, d = root = tree('a / (ab)')
        self.assertEqualPos(
            match_extract_fraction_terms(root),
            [P(root, divide_fraction_by_term, (lscp(n), Scope(d), a, a))])

        n, d = root = tree('(abc) / (cba)')
        self.assertEqualPos(match_extract_fraction_terms(root), [
            P(root, divide_fraction_by_term, (Scope(n), Scope(d), a, a)),
            P(root, divide_fraction_by_term, (Scope(n), Scope(d), b, b)),
            P(root, divide_fraction_by_term, (Scope(n), Scope(d), c, c))
        ])

        root = tree('a / a')
        self.assertEqualPos(match_extract_fraction_terms(root), [])

        (ap, b), aq = n, d = root = tree('(a ^ p * b) / a ^ q')
        self.assertEqualPos(
            match_extract_fraction_terms(root),
            [P(root, extract_fraction_terms, (Scope(n), lscp(d), ap, aq))])

        (a, b), aq = n, d = root = tree('(ab) / a ^ q')
        self.assertEqualPos(
            match_extract_fraction_terms(root),
            [P(root, extract_fraction_terms, (Scope(n), lscp(d), a, aq))])

        (ap, b), a = n, d = root = tree('(a ^ p * b) / a')
        self.assertEqualPos(
            match_extract_fraction_terms(root),
            [P(root, extract_fraction_terms, (Scope(n), lscp(d), ap, a))])

        (l2, a), l3 = n, d = root = tree('(2a) / 3')
        self.assertEqualPos(match_extract_fraction_terms(root),
                            [P(root, extract_nominator_term, (2, a))])

        a, l3 = n, d = root = tree('a / 3')
        self.assertEqualPos(match_extract_fraction_terms(root),
                            [P(root, extract_nominator_term, (1, a))])

        root = tree('(2 * 4) / 3')
        self.assertEqualPos(match_extract_fraction_terms(root), [])

        n, d = root = tree('(2a) / 2')
        self.assertEqualPos(match_extract_fraction_terms(root), [
            P(root, extract_nominator_term, (2, a)),
            P(root, divide_fraction_by_term, (Scope(n), lscp(d), 2, 2))
        ])
Exemple #4
0
    def test_basic_on_exp(self):
        expressions = [('4', L(4)),
                       ('3+4', L(3) + L(4)), ('3-4', L(3) + -L(4)),
                       ('3/4', L(3) / L(4)), ('-4', -L(4)),
                       ('3^4', N('^', L(3), L(4))), ('(2)', L(2))]

        run_expressions(Parser, expressions)
Exemple #5
0
    def test_concat_easy(self):
        expressions = [
            ('xy', N('*', L('x'), L('y'))),
            ('2x', N('*', L(2), L('x'))),
            ('x4', N('*', L('x'), L(4))),
            ('3 4', N('*', L(3), L(4))),
            ('(x)4', N('*', L('x'), L(4))),
            ('(3+4)2', N('*', N('+', L(3), L(4)), L(2))),
        ]

        run_expressions(Parser, expressions)
Exemple #6
0
    def test_nary_node(self):
        a, b, c, d = tree('a,b,c,d')

        self.assertEqualNodes(nary_node('+', [a]), a)
        self.assertEqualNodes(nary_node('+', [a, b]), N('+', a, b))
        self.assertEqualNodes(nary_node('+', [a, b, c]),
                              N('+', N('+', a, b), c))
        self.assertEqualNodes(nary_node('+', [a, b, c, d]),
                              N('+', N('+', N('+', a, b), c), d))
Exemple #7
0
    def test___lt__(self):
        self.assertTrue(L(1) < L(2))
        self.assertFalse(L(1) < L(1))
        self.assertFalse(L(2) < L(1))

        self.assertTrue(L(2) < N('+', L(1), L(2)))
        self.assertFalse(N('+', L(1), L(2)) < L(1))

        self.assertTrue(N('^', L('a'), L(2)) < N('^', L('a'), L(3)))
        self.assertTrue(N('^', L(2), L('a')) < N('^', L(3), L('a')))
        self.assertTrue(
            N('*', L(2), N('^', L('a'), L('b'))) < N('*', L(3),
                                                     N('^', L('a'), L('b'))))
        self.assertFalse(N('^', L('a'), L(3)) < N('^', L('a'), L(2)))
Exemple #8
0
 def setUp(self):
     self.l = [L(1), N('*', L(2), L(3)), L(4), L(5)]
     self.n, self.f = tree('a + b + cd,f')
     (self.a, self.b), self.cd = self.n
     self.c, self.d = self.cd
     self.scope = Scope(self.n)
Exemple #9
0
 def test_constructor(self):
     assert ParserWrapper(Parser).run(['1+4']) \
             == N('+', L(1), L(4))
Exemple #10
0
 def test_extract_polynome_properties_power(self):
     power = N('^', L('a'), L(2))
     self.assertEqual(power.extract_polynome_properties(),
                      (L(1), L('a'), L(2)))
Exemple #11
0
 def test_is_nary(self):
     self.assertTrue(N('+', *self.l[:2]).is_nary())
     self.assertTrue(N('-', *self.l[:2]).is_nary())
     self.assertTrue(N('*', *self.l[:2]).is_nary())
     self.assertFalse(N('^', *self.l[:2]).is_nary())
Exemple #12
0
 def test_is_power(self):
     self.assertTrue(N('^', *self.l[2:]).is_power())
     self.assertFalse(N('+', *self.l[2:]).is_power())
Exemple #13
0
    def test_pow_nested(self):
        # a^b^c = a^(b^c) != (a^b)^c

        a, b, c, d, e = L('a'), L('b'), L('c'), L('d'), L('e')

        expressions = [
            ('a^b^c', N('^', a, N('^', b, c))),
            ('-1^b^c', -N('^', L(1), N('^', b, c))),
            ('ab^c', N('*', a, N('^', b, c))),
            ('a(b)^c', N('*', a, N('^', b, c))),
            ('a(b+c)^(d+e)', N('*', a, N('^', N('+', b, c), N('+', d, e)))),
            ('(a(b+c))^(d+e)', N('^', N('*', a, N('+', b, c)), N('+', d, e))),
        ]

        run_expressions(Parser, expressions)
Exemple #14
0
    def test_concat_intermediate(self):
        expressions = [
            ('(3+4)(5+7)', N('*', N('+', L(3), L(4)), N('+', L(5), L(7)))),
            ('(a+b)(c+d)',
             N('*', N('+', L('a'), L('b')), N('+', L('c'), L('d')))),
            ('a+b(c+d)', N('+', L('a'), N('*', L('b'), N('+', L('c'),
                                                         L('d'))))),
            ('abcd', N('*', N('*', N('*', L('a'), L('b')), L('c')), L('d'))),
            ('ab(c)d', N('*', N('*', N('*', L('a'), L('b')), L('c')), L('d'))),
            ('ab*(c)*d', N('*', N('*', N('*', L('a'), L('b')), L('c')),
                           L('d'))),
            ('ab*(c)^d', N('*', N('*', L('a'), L('b')), N('^', L('c'),
                                                          L('d')))),
        ]

        run_expressions(Parser, expressions)
Exemple #15
0
 def test_is_op(self):
     self.assertTrue(N('+', *self.l[:2]).is_op(OP_ADD))
     self.assertFalse(N('-', *self.l[:2]).is_op(OP_ADD))
Exemple #16
0
 def test_divide_fraction_by_term(self):
     (ab, a), expect = root = tree('(ab) / a, b')
     args = Scope(ab), Scope(N(OP_MUL, a)), ab[0], a
     self.assertEqual(divide_fraction_by_term(root, args), expect)
Exemple #17
0
 def test_is_leaf(self):
     self.assertTrue(L(2).is_leaf)
     self.assertFalse(N('+', *self.l[:2]).is_leaf)
Exemple #18
0
 def test_get_scope_binary(self):
     plus = N('+', *self.l[:2])
     self.assertEqual(get_scope(plus), self.l[:2])
Exemple #19
0
 def test_is_power_exponent(self):
     self.assertTrue(N('^', *self.l[2:]).is_power(5))
     self.assertFalse(N('^', *self.l[2:]).is_power(2))
Exemple #20
0
 def test_get_scope_nested_right(self):
     plus = N('+', self.l[0], N('+', *self.l[1:3]))
     self.assertEqual(get_scope(plus), self.l[:3])
Exemple #21
0
 def test_extract_polynome_properties_None(self):
     self.assertIsNone(N('+').extract_polynome_properties())
Exemple #22
0
 def test_get_scope_nested_deep(self):
     plus = N('+', N('+', N('+', *self.l[:2]), self.l[2]), self.l[3])
     self.assertEqual(get_scope(plus), self.l)
Exemple #23
0
 def test_extract_polynome_properties_coefficient_exponent_int(self):
     times = N('*', L(3), N('^', L('a'), L(2)))
     self.assertEqual(times.extract_polynome_properties(),
                      (L(3), L('a'), L(2)))
Exemple #24
0
 def test_diagnostic_test(self):
     run_expressions(Parser, [
         ('5(a-2b)', L(5) * (L('a') + -(L(2) * 'b'))),
         ('-(3a+6b)', -(L(3) * L('a') + L(6) * 'b')),
         ('18-(a-12)', L(18) + -(L('a') + -L(12))),
         ('-p-q+5(p-q)-3q-2(p-q)',
             -L('p') + -L('q') + L(5) * (L('p') + -L('q')) + -(L(3) * 'q') \
             + -(L(2) * (L('p') + -L('q')))
         ),
         ('(2+3/7)^4',
             N('^', N('+', L(2), N('/', L(3), L(7))), L(4))
         ),
         ('x^3*x^2*x',
             N('*',
               N('*',
                 N('^', L('x'), L(3)),
                 N('^', L('x'), L(2))),
               L('x')
             )
         ),
         ('-x^3*-2x^5',
             -(L('x') ** L(3) * -L(2) * L('x') ** L(5))
         ),
         ('(7x^2y^3)^2/(7x^2y^3)',
             N('/',
               N('^',
                 N('*',
                   N('*', L(7), N('^', L('x'), L(2))),
                   N('^', L('y'), L(3))
                 ),
                 L(2)),
               N('*',
                 N('*', L(7), N('^', L('x'), L(2))),
                 N('^', L('y'), L(3))
               )
             )
         ),
         ])