Преглед изворни кода

Extended equation multiplication rule to apply for a negated left side.

Taddeus Kroes пре 14 година
родитељ
комит
b4b5542d2d
2 измењених фајлова са 23 додато и 2 уклоњено
  1. 7 1
      src/rules/lineq.py
  2. 16 1
      tests/test_rules_lineq.py

+ 7 - 1
src/rules/lineq.py

@@ -1,5 +1,6 @@
 from .utils import find_variable
-from ..node import Scope, OP_EQ, OP_ADD, OP_MUL, OP_DIV, eq
+from ..node import ExpressionLeaf as L, Scope, OP_EQ, OP_ADD, OP_MUL, OP_DIV, \
+        eq
 from ..possibilities import Possibility as P, MESSAGES
 from ..translate import _
 
@@ -22,6 +23,7 @@ def match_move_term(node):
     # Multiplication
     x / a = b  ->  x / a * a = b * a  # =>*  x = a * b
     a / x = b  ->  a / x * x = b * x  # =>*  x = a / b
+    -x = b  ->  -x * -1 = b * -1  # =>*  x = -b
     """
     assert node.is_op(OP_EQ)
 
@@ -56,6 +58,10 @@ def match_move_term(node):
     if left.is_op(OP_DIV):
         p.append(P(node, multiply_term, (left[1],)))
 
+    # Remove any negation from the left side of the equation
+    if left.negated:
+        p.append(P(node, multiply_term, (-L(1),)))
+
     return p
 
 

+ 16 - 1
tests/test_rules_lineq.py

@@ -37,6 +37,10 @@ class TestRulesLineq(RulesTestCase):
         self.assertEqualPos(match_move_term(root),
                 [P(root, multiply_term, (x,))])
 
+        root, l1 = tree('-x = b, -1')
+        self.assertEqualPos(match_move_term(root),
+                [P(root, multiply_term, (l1,))])
+
     def test_swap_sides(self):
         root, expect = tree('a = bx, bx = a')
         self.assertEqual(swap_sides(root, ()), expect)
@@ -53,7 +57,7 @@ class TestRulesLineq(RulesTestCase):
         root, a, expect = tree('x / a = b, a, x / a * a = b * a')
         self.assertEqual(multiply_term(root, (a,)), expect)
 
-    def test_match_move_term_chain(self):
+    def test_match_move_term_chain_negation(self):
         self.assertRewrite([
             '2x + 3 = -3x - 2',
             '2x + 3 - 3 = -3x - 2 - 3',
@@ -74,3 +78,14 @@ class TestRulesLineq(RulesTestCase):
             'x = -5 / 5',
             'x = -1',
         ])
+
+    def test_match_move_term_chain_advanced(self):
+        self.assertRewrite([
+            '-x = a',
+            '-x * -1 = a * -1',
+            '--x * 1 = a * -1',
+            'x * 1 = a * -1',
+            'x = a * -1',
+            'x = -a * 1',
+            'x = -a',
+        ])