Added possibilities to fraction rules.

parent bfd41939
......@@ -28,11 +28,11 @@ def match_constant_division(node):
# 0 / a
if nominator == 0:
p.append(P(node, division_of_zero))
p.append(P(node, division_of_zero, (denominator,)))
# a / a
if nominator == denominator:
p.append(P(node, division_by_self))
p.append(P(node, division_by_self, (nominator,)))
return p
......@@ -44,6 +44,9 @@ def division_by_one(root, args):
return args[0]
MESSAGES[division_by_one] = _('Division of {1} by 1 reduces to {1}.')
def division_of_zero(root, args):
"""
0 / a -> 0
......@@ -51,6 +54,9 @@ def division_of_zero(root, args):
return L(0)
MESSAGES[division_of_zero] = _('Division of 0 by {1} reduces to 0.')
def division_by_self(root, args):
"""
a / a -> 1
......@@ -58,6 +64,9 @@ def division_by_self(root, args):
return L(1)
MESSAGES[division_by_self] = _('Division of {1} by {1} reduces to 1.')
def match_add_constant_fractions(node):
"""
1 / 2 + 3 / 4 -> 2 / 4 + 3 / 4 # Equalize denominators
......@@ -118,6 +127,10 @@ def equalize_denominators(root, args):
return nary_node('+', scope)
MESSAGES[equalize_denominators] = _('Equalize the denominators of division'
' of {1} by {2}.')
def add_nominators(root, args):
"""
a / b + c / b -> (a + c) / b
......@@ -145,6 +158,9 @@ def add_nominators(root, args):
return nary_node('+', scope)
MESSAGES[add_nominators] = _('Add nominators of the division of {1} by {2}.')
def match_expand_and_add_fractions(node):
"""
a * b / c + d * b / c -> (a + d) * (b / c)
......
......@@ -21,11 +21,11 @@ class TestRulesFractions(RulesTestCase):
root = zero / a
possibilities = match_constant_division(root)
self.assertEqualPos(possibilities, [P(root, division_of_zero)])
self.assertEqualPos(possibilities, [P(root, division_of_zero, (a,))])
root = a / a
possibilities = match_constant_division(root)
self.assertEqualPos(possibilities, [P(root, division_by_self)])
self.assertEqualPos(possibilities, [P(root, division_by_self, (a,))])
def test_division_by_one(self):
a = tree('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