Commit 697592af authored by Taddeus Kroes's avatar Taddeus Kroes

Added possibility to multiply denominators of division addition.

parent 01acdf18
...@@ -70,17 +70,20 @@ MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.') ...@@ -70,17 +70,20 @@ MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.')
def match_add_constant_fractions(node): def match_add_constant_fractions(node):
""" """
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators 1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators
2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4
2 / 4 + 3 / 4 -> 5 / 4 # Equal denominators, so nominators can 2 / 4 + 3 / 4 -> 5 / 4 # Equal denominators, so nominators can
# be added # be added
2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators 2 / 4 - 3 / 4 -> -1 / 4
2 / 4 - 3 / 4 -> -1 / 4 # Equal denominators, so nominators can 1 / 2 + 3 / 4 -> 4 / 8 + 6 / 8 # Equalize denominators by multiplying
# be subtracted # them with eachother
""" """
assert node.is_op(OP_ADD) assert node.is_op(OP_ADD)
p = [] p = []
scope = Scope(node)
fractions = filter(lambda node: node.is_op(OP_DIV), Scope(node)) fractions = filter(lambda node: node.is_op(OP_DIV), scope)
for a, b in combinations(fractions, 2): for a, b in combinations(fractions, 2):
na, da = a na, da = a
...@@ -94,7 +97,12 @@ def match_add_constant_fractions(node): ...@@ -94,7 +97,12 @@ def match_add_constant_fractions(node):
# least common multiple of their denominators. Later, the # least common multiple of their denominators. Later, the
# nominators will be added # nominators will be added
denom = least_common_multiple(da.value, db.value) denom = least_common_multiple(da.value, db.value)
p.append(P(node, equalize_denominators, (a, b, denom))) p.append(P(node, equalize_denominators, (scope, a, b, denom)))
# Also, add the (non-recommended) possibility to multiply the
# denominators
p.append(P(node, equalize_denominators, (scope, a, b,
da.value * db.value)))
return p return p
...@@ -104,10 +112,9 @@ def equalize_denominators(root, args): ...@@ -104,10 +112,9 @@ def equalize_denominators(root, args):
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4
a / 2 + b / 4 -> 2a / 4 + b / 4 a / 2 + b / 4 -> 2a / 4 + b / 4
""" """
denom = args[2] scope, denom = args[::3]
scope = Scope(root)
for fraction in args[:2]: for fraction in args[1:3]:
n, d = fraction n, d = fraction
mult = denom / d.value mult = denom / d.value
...@@ -125,8 +132,8 @@ def equalize_denominators(root, args): ...@@ -125,8 +132,8 @@ def equalize_denominators(root, args):
return scope.as_nary_node() return scope.as_nary_node()
MESSAGES[equalize_denominators] = _('Equalize the denominators of division' MESSAGES[equalize_denominators] = _('Equalize the denominators of divisions'
' of {1} by {2}.') ' {2} and {3} to {4}.')
def add_nominators(root, args): def add_nominators(root, args):
......
from src.rules.fractions import match_constant_division, division_by_one, \ from src.rules.fractions import match_constant_division, division_by_one, \
division_of_zero, division_by_self, match_add_constant_fractions, \ division_of_zero, division_by_self, match_add_constant_fractions, \
equalize_denominators, add_nominators equalize_denominators, add_nominators
from src.node import Scope
from src.possibilities import Possibility as P from src.possibilities import Possibility as P
from tests.rulestestcase import RulesTestCase, tree from tests.rulestestcase import RulesTestCase, tree
...@@ -51,12 +52,14 @@ class TestRulesFractions(RulesTestCase): ...@@ -51,12 +52,14 @@ class TestRulesFractions(RulesTestCase):
n0, n1 = root = l1 / l2 + l3 / l4 n0, n1 = root = l1 / l2 + l3 / l4
possibilities = match_add_constant_fractions(root) possibilities = match_add_constant_fractions(root)
self.assertEqualPos(possibilities, self.assertEqualPos(possibilities,
[P(root, equalize_denominators, (n0, n1, 4))]) [P(root, equalize_denominators, (Scope(root), n0, n1, 4)),
P(root, equalize_denominators, (Scope(root), n0, n1, 8))])
(((n0, n1), n2), n3), n4 = root = a + l1 / l2 + b + l3 / l4 + c (((n0, n1), n2), n3), n4 = root = a + l1 / l2 + b + l3 / l4 + c
possibilities = match_add_constant_fractions(root) possibilities = match_add_constant_fractions(root)
self.assertEqualPos(possibilities, self.assertEqualPos(possibilities,
[P(root, equalize_denominators, (n1, n3, 4))]) [P(root, equalize_denominators, (Scope(root), n1, n3, 4)),
P(root, equalize_denominators, (Scope(root), n1, n3, 8))])
n0, n1 = root = l2 / l4 + l3 / l4 n0, n1 = root = l2 / l4 + l3 / l4
possibilities = match_add_constant_fractions(root) possibilities = match_add_constant_fractions(root)
...@@ -74,7 +77,8 @@ class TestRulesFractions(RulesTestCase): ...@@ -74,7 +77,8 @@ class TestRulesFractions(RulesTestCase):
(((n0, n1), n2), n3), n4 = root = a + l2 / l2 + b + (-l3 / l4) + c (((n0, n1), n2), n3), n4 = root = a + l2 / l2 + b + (-l3 / l4) + c
possibilities = match_add_constant_fractions(root) possibilities = match_add_constant_fractions(root)
self.assertEqualPos(possibilities, self.assertEqualPos(possibilities,
[P(root, equalize_denominators, (n1, n3, 4))]) [P(root, equalize_denominators, (Scope(root), n1, n3, 4)),
P(root, equalize_denominators, (Scope(root), n1, n3, 8))])
(((n0, n1), n2), n3), n4 = root = a + l2 / l4 + b + (-l3 / l4) + c (((n0, n1), n2), n3), n4 = root = a + l2 / l4 + b + (-l3 / l4) + c
possibilities = match_add_constant_fractions(root) possibilities = match_add_constant_fractions(root)
...@@ -85,22 +89,23 @@ class TestRulesFractions(RulesTestCase): ...@@ -85,22 +89,23 @@ class TestRulesFractions(RulesTestCase):
a, b, l1, l2, l3, l4 = tree('a,b,1,2,3,4') a, b, l1, l2, l3, l4 = tree('a,b,1,2,3,4')
n0, n1 = root = l1 / l2 + l3 / l4 n0, n1 = root = l1 / l2 + l3 / l4
self.assertEqualNodes(equalize_denominators(root, (n0, n1, 4)), self.assertEqualNodes(equalize_denominators(root,
l2 / l4 + l3 / l4) (Scope(root), n0, n1, 4)), l2 / l4 + l3 / l4)
n0, n1 = root = a / l2 + b / l4 n0, n1 = root = a / l2 + b / l4
self.assertEqualNodes(equalize_denominators(root, (n0, n1, 4)), self.assertEqualNodes(equalize_denominators(root,
(l2 * a) / l4 + b / l4) (Scope(root), n0, n1, 4)), (l2 * a) / l4 + b /
l4)
#2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators #2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
n0, n1 = root = l1 / l2 + (-l3 / l4) n0, n1 = root = l1 / l2 + (-l3 / l4)
self.assertEqualNodes(equalize_denominators(root, (n0, n1, 4)), self.assertEqualNodes(equalize_denominators(root,
l2 / l4 + (-l3 / l4)) (Scope(root), n0, n1, 4)), l2 / l4 + (-l3 / l4))
#2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators #2 / 2 - 3 / 4 -> 4 / 4 - 3 / 4 # Equalize denominators
n0, n1 = root = a / l2 + (-b / l4) n0, n1 = root = a / l2 + (-b / l4)
self.assertEqualNodes(equalize_denominators(root, (n0, n1, 4)), self.assertEqualNodes(equalize_denominators(root,
(l2 * a) / l4 + (-b / l4)) (Scope(root), n0, n1, 4)), (l2 * a) / l4 + (-b / l4))
def test_add_nominators(self): def test_add_nominators(self):
a, b, c = tree('a,b,c') a, b, c = tree('a,b,c')
......
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