Pārlūkot izejas kodu

Added polynome scope to application funvtion arguments.

Taddeus Kroes 14 gadi atpakaļ
vecāks
revīzija
0e08d36432
3 mainītis faili ar 44 papildinājumiem un 28 dzēšanām
  1. 4 3
      src/rules/numerics.py
  2. 7 6
      src/rules/poly.py
  3. 33 19
      tests/test_rules_poly.py

+ 4 - 3
src/rules/numerics.py

@@ -15,11 +15,12 @@ def add_numerics(root, args):
     -2 + 3   ->  1
     -2 + -3  ->  -5
     """
-    n0, n1, c0, c1 = args
-    scope = Scope(root)
+    scope, n0, n1, c0, c1 = args
 
     # Replace the left node with the new expression
-    scope.replace(n0, Leaf(c0.actual_value() + c1.actual_value()))
+    leaf = Leaf(c0.value + c1.value)
+    print 'New leaf:', c0.value, c1.value, leaf
+    scope.replace(n0, leaf.negate(c0.negated + c1.negated))
 
     # Remove the right node
     scope.remove(n1)

+ 7 - 6
src/rules/poly.py

@@ -27,7 +27,9 @@ def match_combine_polynomes(node, verbose=False):
     if verbose:  # pragma: nocover
         print 'match combine factors:', node
 
-    for n in Scope(node):
+    scope = Scope(node)
+
+    for n in scope:
         polynome = n.extract_polynome_properties()
 
         if verbose:  # pragma: nocover
@@ -53,13 +55,14 @@ def match_combine_polynomes(node, verbose=False):
                 # 2 + -3   ->  -1
                 # -2 + 3   ->  1
                 # -2 + -3  ->  -5
-                p.append(P(node, add_numerics, (n0, n1, r0, r1)))
+                p.append(P(node, add_numerics, (scope, n0, n1, r0, r1)))
             elif 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, (n0, n1, c0, c1, r0, e0)))
+                p.append(P(node, combine_polynomes, (scope, n0, n1,
+                                                     c0, c1, r0, e0)))
 
     return p
 
@@ -71,7 +74,7 @@ def combine_polynomes(root, args):
     Synopsis:
     c0 * a ^ b + c1 * a ^ b -> (c0 + c1) * a ^ b
     """
-    n0, n1, c0, c1, r, e = args
+    scope, n0, n1, c0, c1, r, e = args
 
     # a ^ 1 -> a
     if e == 1:
@@ -79,8 +82,6 @@ def combine_polynomes(root, args):
     else:
         power = r ** e
 
-    scope = Scope(root)
-
     # Replace the left node with the new expression:
     # (c0 + c1) * a ^ b
     # a, b and c are from 'left', d is from 'right'.

+ 33 - 19
tests/test_rules_poly.py

@@ -1,5 +1,6 @@
 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
 
@@ -10,37 +11,43 @@ class TestRulesPoly(RulesTestCase):
         a1, a2 = root = tree('a+a')
         possibilities = match_combine_polynomes(root)
         self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (a1, a2, 1, 1, 'a', 1))])
+                [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, (a1, a2, 1, 2, 'a', 1))])
+                [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, (a1, a2, 1, 2, a1, 1))])
+                [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, (a1, a2, 1, 1, 'a', 2))])
+                [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, (a1, a2, 2, 1, 'a', 3))])
+                [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, (a1, a2, 2, 2, 'a', 3))])
+                [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')
@@ -49,13 +56,15 @@ class TestRulesPoly(RulesTestCase):
         self.assertEqual(left, right)
         possibilities = match_combine_polynomes(root)
         self.assertEqualPos(possibilities,
-                [P(root, combine_polynomes, (left, right, 1, 1, a_b, d))])
+                [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, (left, right, 5, 7, a_b, d))])
+                [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')
@@ -63,7 +72,8 @@ class TestRulesPoly(RulesTestCase):
         #self.assertEqual(left, right)
         #possibilities = match_combine_polynomes(root)
         #self.assertEqualPos(possibilities,
-        #        [P(root, combine_polynomes, (left, right, c, c, a_b, d))])
+        #        [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')
@@ -71,9 +81,9 @@ class TestRulesPoly(RulesTestCase):
 
         possibilities = match_combine_polynomes(root)
         self.assertEqualPos(possibilities,
-                [P(root, add_numerics, (l0, l1, l0, l1)),
-                 P(root, add_numerics, (l0, l2, l0, l2)),
-                 P(root, add_numerics, (l1, l2, l1, l2))])
+                [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')
@@ -81,32 +91,36 @@ class TestRulesPoly(RulesTestCase):
 
         possibilities = match_combine_polynomes(root)
         self.assertEqualPos(possibilities,
-                [P(root, add_numerics, (l0, l1, l0[0], l1[1])),
-                 P(root, add_numerics, (l0, l2, l0[0], l2[1][0])),
-                 P(root, add_numerics, (l1, l2, l1[1], l2[1][0]))])
+                [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, (left, right, l0, l1, a, 1))
+        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, (left, right, l2, l1, a, 1))
+        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, (left, right, l0, l2, a, 1))
+        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, (left, right, l2, l2, a, 1))
+        replacement = combine_polynomes(root, (Scope(root), left, right, l2, l2, a, 1))
         self.assertEqualNodes(replacement, (l2 + 1) * a)