Jelajahi Sumber

Removed redundant polynome rules.

Taddeus Kroes 14 tahun lalu
induk
melakukan
76b5ebeec6
2 mengubah file dengan 0 tambahan dan 220 penghapusan
  1. 0 94
      src/rules/poly.py
  2. 0 126
      tests/test_rules_poly.py

+ 0 - 94
src/rules/poly.py

@@ -1,94 +0,0 @@
-from itertools import combinations
-
-from ..node import Scope, OP_ADD
-from ..possibilities import Possibility as P, MESSAGES
-from .numerics import add_numerics
-
-
-def match_combine_polynomes(node, verbose=False):
-    """
-    n + exp + m -> exp + (n + m)
-    k0 * v ^ n + exp + k1 * v ^ n -> exp + (k0 + k1) * v ^ n
-    """
-    assert node.is_op(OP_ADD)
-
-    p = []
-
-    # Collect all nodes that can be combined:
-    # a ^ e     = 1 * a ^ e
-    # c * a     = c * a ^ 1
-    # c * a ^ e
-    # a         = 1 * a ^ 1
-    #
-    # Identifier nodes of all polynomes, tuple format is:
-    #   (root, exponent, coefficient, literal_coefficient)
-    polys = []
-
-    if verbose:  # pragma: nocover
-        print 'match combine factors:', node
-
-    scope = Scope(node)
-
-    for n in scope:
-        polynome = n.extract_polynome_properties()
-
-        if verbose:  # pragma: nocover
-            print 'n:', n, 'polynome:', polynome
-
-        if polynome:
-            polys.append((n, polynome))
-
-    # Each combination of powers of the same value and polynome can be added
-    if len(polys) >= 2:
-        for left, right in combinations(polys, 2):
-            n0, p0 = left
-            n1, p1 = right
-            c0, r0, e0 = p0
-            c1, r1, e1 = p1
-
-            # Both numeric root and same exponent -> combine coefficients and
-            # roots, or: same root and exponent -> combine coefficients.
-            # TODO: Addition with zero, e.g. a + 0 -> a
-            #if c0 == 1 and c1 == 1 and e0 == 1 and e1 == 1 \
-            #        and all(map(lambda n: n.is_numeric(), [r0, r1])):
-            #    # 2 + 3    ->  5
-            #    # 2 + -3   ->  -1
-            #    # -2 + 3   ->  1
-            #    # -2 + -3  ->  -5
-            #    p.append(P(node, add_numerics, (scope, n0, n1, r0, r1)))
-            #el
-            if c0.is_numeric() and c1.is_numeric() and r0 == r1 and e0 == e1:
-                # 2a + 2a -> 4a
-                # a + 2a -> 3a
-                # 2a + a -> 3a
-                # a + a -> 2a
-                p.append(P(node, combine_polynomes, (scope, n0, n1,
-                                                     c0, c1, r0, e0)))
-
-    return p
-
-
-def combine_polynomes(root, args):
-    """
-    Combine two multiplications of any polynome in an n-ary plus.
-
-    Synopsis:
-    c0 * a ^ b + c1 * a ^ b -> (c0 + c1) * a ^ b
-    """
-    scope, n0, n1, c0, c1, r, e = args
-
-    # a ^ 1 -> a
-    if e == 1:
-        power = r
-    else:
-        power = r ** e
-
-    # Replace the left node with the new expression:
-    # (c0 + c1) * a ^ b
-    # a, b and c are from 'left', d is from 'right'.
-    scope.replace(n0, (c0 + c1) * power)
-
-    # Remove the right node
-    scope.remove(n1)
-
-    return scope.as_nary_node()

+ 0 - 126
tests/test_rules_poly.py

@@ -1,126 +0,0 @@
-from src.rules.poly import match_combine_polynomes, combine_polynomes
-from src.rules.numerics import add_numerics
-from src.node import Scope
-from src.possibilities import Possibility as P
-from tests.rulestestcase import RulesTestCase, tree
-
-
-class TestRulesPoly(RulesTestCase):
-
-    def test_identifiers_basic(self):
-        a1, a2 = root = tree('a+a')
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), a1, a2,
-                                                          1, 1, 'a', 1))])
-
-    def test_identifiers_normal(self):
-        a1, a2 = root = tree('a+2a')
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), a1, a2,
-                                                          1, 2, 'a', 1))])
-
-    def test_identifiers_reverse(self):
-        a1, a2 = root = tree('a+a*2')
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), a1, a2,
-                                                          1, 2, a1, 1))])
-
-    def test_identifiers_exponent(self):
-        a1, a2 = root = tree('a2+a2')
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), a1, a2,
-                                                          1, 1, 'a', 2))])
-
-    def test_identifiers_coeff_exponent_left(self):
-        a1, a2 = root = tree('2a3+a3')
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), a1, a2,
-                                                          2, 1, 'a', 3))])
-
-    def test_identifiers_coeff_exponent_both(self):
-        a1, a2 = root = tree('2a3+2a3')
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), a1, a2,
-                                                          2, 2, 'a', 3))])
-
-    def test_basic_subexpressions(self):
-        a_b, c, d = tree('a+b,c,d')
-        left, right = root = tree('(a+b)^d + (a+b)^d')
-
-        self.assertEqual(left, right)
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), left, right,
-                                                          1, 1, a_b, d))])
-
-        left, right = root = tree('5(a+b)^d + 7(a+b)^d')
-
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (Scope(root), left, right,
-                                                          5, 7, a_b, d))])
-
-        # TODO: Move to other strategy
-        #left, right = root = tree('c(a+b)^d + c(a+b)^d')
-
-        #self.assertEqual(left, right)
-        #possibilities = match_combine_polynomes(root)
-        #self.assertEqualPos(possibilities,
-        #        [P(root, combine_polynomes, (Scope(root), left, right,
-        #                                                  c, c, a_b, d))])
-
-    def test_match_add_numerics(self):
-        l0, l1, l2 = tree('0,1,2')
-        root = l0 + l1 + l2
-
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, add_numerics, (Scope(root), l0, l1, l0, l1)),
-                 P(root, add_numerics, (Scope(root), l0, l2, l0, l2)),
-                 P(root, add_numerics, (Scope(root), l1, l2, l1, l2))])
-
-    def test_match_add_numerics_explicit_powers(self):
-        l0, l1, l2 = tree('0^1,1*1,1*2^1')
-        root = l0 + l1 + l2
-
-        possibilities = match_combine_polynomes(root)
-        self.assertEqualPos(possibilities,
-                [P(root, add_numerics, (Scope(root), l0, l1, l0[0], l1[1])),
-                 P(root, add_numerics, (Scope(root), l0, l2, l0[0], l2[1][0])),
-                 P(root, add_numerics, (Scope(root), l1, l2,
-                                                     l1[1], l2[1][0]))])
-
-    def test_combine_polynomes(self):
-        # 2a + 3a -> (2 + 3) * a
-        l0, a, l1, l2 = tree('2,a,3,1')
-        root = l0 * a + l1 * a
-        left, right = root
-        replacement = combine_polynomes(root, (Scope(root), left, right,
-                                                            l0, l1, a, 1))
-        self.assertEqualNodes(replacement, (l0 + l1) * a)
-
-        # a + 3a -> (1 + 3) * a
-        root = a + l1 * a
-        left, right = root
-        replacement = combine_polynomes(root, (Scope(root), left, right,
-                                                            l2, l1, a, 1))
-        self.assertEqualNodes(replacement, (l2 + l1) * a)
-
-        # 2a + a -> (2 + 1) * a
-        root = l0 * a + a
-        left, right = root
-        replacement = combine_polynomes(root, (Scope(root), left, right,
-                                                            l0, l2, a, 1))
-        self.assertEqualNodes(replacement, (l0 + 1) * a)
-
-        # a + a -> (1 + 1) * a
-        root = a + a
-        left, right = root
-        replacement = combine_polynomes(root, (Scope(root), left, right, l2, l2, a, 1))
-        self.assertEqualNodes(replacement, (l2 + 1) * a)