Răsfoiți Sursa

Implemented use of Scope class in multiply_numerics.

Taddeus Kroes 14 ani în urmă
părinte
comite
7d186db7bb
2 a modificat fișierele cu 25 adăugiri și 14 ștergeri
  1. 9 10
      src/rules/numerics.py
  2. 16 4
      tests/test_rules_numerics.py

+ 9 - 10
src/rules/numerics.py

@@ -147,13 +147,12 @@ def multiply_numerics(root, args):
     else:
         substitution = -Leaf(-value)
 
-    for n in Scope(root):
-        if hash(n) == hash(n0):
-            # Replace the left node with the new expression
-            scope.append(substitution)
-            #scope.append(n)
-        elif hash(n) != hash(n1):
-            # Remove the right node
-            scope.append(n)
-
-    return nary_node('*', scope)
+    scope = Scope(root)
+
+    # Replace the left node with the new expression
+    scope.remove(n0, substitution)
+
+    # Remove the right node
+    scope.remove(n1)
+
+    return scope.as_nary_node()

+ 16 - 4
tests/test_rules_numerics.py

@@ -96,7 +96,19 @@ class TestRulesNumerics(RulesTestCase):
                               a * 6 * b)
 
     def test_multiply_numerics_negation(self):
-        #a, b = root = tree('1 - 5 * -3x - 5 * 6')
-        l1, l2 = tree('-1 * 2')
-
-        self.assertEqual(multiply_numerics(l1 * l2, (l1, l2, -1, 2)), -l2)
+        l1_neg, l2 = root = tree('-1 * 2')
+        self.assertEqualNodes(multiply_numerics(root, (l1_neg, l2, -1, 2)), -l2)
+
+        root, l6 = tree('1 - 2 * 3,6')
+        l1, neg = root
+        l2, l3 = mul = neg[0]
+        self.assertEqualNodes(multiply_numerics(mul, (l2, l3, 2, 3)), l6)
+
+        l1, mul = root = tree('1 + -2 * 3')
+        l2_neg, l3 = mul
+        self.assertEqualNodes(multiply_numerics(mul, (l2_neg, l3, -2, 3)), -l6)
+
+        root, l30 = tree('-5 * x ^ 2 - -15x - 5 * 6,30')
+        rest, mul_neg = root
+        l5_neg, l6 = mul = mul_neg[0]
+        self.assertEqualNodes(multiply_numerics(mul, (l5_neg, l6, 5, 6)), l30)