Commit b4b5542d authored by Taddeus Kroes's avatar Taddeus Kroes

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

parent 24dc157d
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
......
......@@ -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',
])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment