Commit f057cada authored by Taddeus Kroes's avatar Taddeus Kroes

Implemented and tested numerics multiplication rewriting.

parent 9f4f5029
from itertools import combinations
from .utils import nary_node
from ..node import ExpressionLeaf as Leaf, OP_DIV, OP_MUL
from ..possibilities import Possibility as P, MESSAGES
from .utils import nary_node
def add_numerics(root, args):
"""
Combine two constants to a single constant in an n-ary addition.
Example:
2 + 3 -> 5
"""
n0, n1, c0, c1 = args
scope = root.get_scope()
# Replace the left node with the new expression
scope[scope.index(n0)] = Leaf(c0 + c1)
# Remove the right node
scope.remove(n1)
return nary_node('+', scope)
#def match_subtract_numerics(node):
# """
# 3 - 2 -> 2.0
# 3.0 - 2 -> 1.0
# 3 - 2.0 -> 1.0
# 3.0 - 2.0 -> 1.0
# """
# # TODO: This should be handled by match_combine_polynomes
# assert node.is_op(OP_MUL)
def match_divide_numerics(node):
......@@ -40,28 +73,6 @@ def match_divide_numerics(node):
return [P(node, divide_numerics, (n.value, dv))] if divide else []
def match_multiply_numerics(node):
"""
3 * 2 -> 6
3.0 * 2 -> 6.0 # FIXME: is this correct?
3 * 2.0 -> 6.0 # FIXME: is this correct?
3.0 * 2.0 -> 6.0
"""
# TODO: Finish
assert node.is_op(OP_MUL)
def match_subtract_numerics(node):
"""
3 - 2 -> 2.0
3.0 - 2 -> 1.0 # FIXME: is this correct?
3 - 2.0 -> 1.0 # FIXME: is this correct?
3.0 - 2.0 -> 1.0
"""
# TODO: Finish
assert node.is_op(OP_MUL)
def divide_numerics(root, args):
"""
Combine two constants to a single constant in a division.
......@@ -78,21 +89,42 @@ def divide_numerics(root, args):
return Leaf(n / d)
def add_numerics(root, args):
def match_multiply_numerics(node):
"""
3 * 2 -> 6
3.0 * 2 -> 6.0
3 * 2.0 -> 6.0
3.0 * 2.0 -> 6.0
"""
assert node.is_op(OP_MUL)
p = []
scope = node.get_scope()
numerics = filter(lambda n: n.is_numeric(), scope)
for args in combinations(numerics, 2):
p.append(P(node, multiply_numerics, args))
return p
def multiply_numerics(root, args):
"""
Combine two constants to a single constant in an n-ary plus.
Combine two constants to a single constant in an n-ary multiplication.
Example:
2 + 3 -> 5
2 * 3 -> 6
"""
n0, n1, c0, c1 = args
scope = root.get_scope()
n0, n1 = args
scope = []
for n in root.get_scope():
if hash(n) == hash(n0):
# Replace the left node with the new expression
scope[scope.index(n0)] = Leaf(c0 + c1)
scope.append(Leaf(n0.value * n1.value))
#scope.append(n)
elif hash(n) != hash(n1):
# Remove the right node
scope.remove(n1)
scope.append(n)
return nary_node('+', scope)
return nary_node('*', scope)
from src.rules.numerics import match_divide_numerics, divide_numerics, \
add_numerics
from src.rules.numerics import add_numerics, match_divide_numerics, \
divide_numerics, match_multiply_numerics, multiply_numerics
from src.possibilities import Possibility as P
from src.node import ExpressionLeaf as L
from tests.rulestestcase import RulesTestCase
......@@ -8,11 +8,20 @@ from tests.test_rules_poly import tree
class TestRulesNumerics(RulesTestCase):
def test_add_numerics(self):
l0, a, l1 = tree('1,a,2')
self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a)
def test_add_numerics(self):
l0, a, l1 = tree('1,a,2')
self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a)
def test_match_divide_numerics(self):
# FIXME: Parser does not recognize floats
#a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
a, b, i2, i3, i6 = tree('a,b,2,3,6')
f1, f2, f3 = L(1.0), L(2.0), L(3.0)
a, b, i2, i3, i6, f1, f2, f3 = tree('a,b,2,3,6,1.0,2.0,3.0')
root = i6 / i2
possibilities = match_divide_numerics(root)
......@@ -48,18 +57,39 @@ class TestRulesNumerics(RulesTestCase):
self.assertEqualPos(possibilities, [])
def test_divide_numerics(self):
# FIXME: Parser does not recognize floats
#i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
i2, i3, i6 = tree('2,3,6')
f2, f3 = L(2.0), L(3.0)
i2, i3, i6, f2, f3 = tree('2,3,6,2.0,3.0')
self.assertEqual(divide_numerics(i6 / i2, (6, 2)), 3)
self.assertEqual(divide_numerics(f3 / i2, (3.0, 2)), 1.5)
self.assertEqual(divide_numerics(i3 / f2, (3, 2.0)), 1.5)
self.assertEqual(divide_numerics(f3 / f2, (3.0, 2.0)), 1.5)
def test_add_numerics(self):
l0, a, l1 = tree('1,a,2')
def test_match_multiply_numerics(self):
i2, i3, i6, f2, f3, f6 = tree('2,3,6,2.0,3.0,6.0')
self.assertEqual(add_numerics(l0 + l1, (l0, l1, 1, 2)), 3)
self.assertEqual(add_numerics(l0 + a + l1, (l0, l1, 1, 2)), L(3) + a)
root = i3 * i2
self.assertEqual(match_multiply_numerics(root),
[P(root, multiply_numerics, (i3, i2))])
root = f3 * i2
self.assertEqual(match_multiply_numerics(root),
[P(root, multiply_numerics, (f3, i2))])
root = i3 * f2
self.assertEqual(match_multiply_numerics(root),
[P(root, multiply_numerics, (i3, f2))])
root = f3 * f2
self.assertEqual(match_multiply_numerics(root),
[P(root, multiply_numerics, (f3, f2))])
def test_multiply_numerics(self):
a, b, i2, i3, i6, f2, f3, f6 = tree('a,b,2,3,6,2.0,3.0,6.0')
self.assertEqual(multiply_numerics(i3 * i2, (i3, i2)), 6)
self.assertEqual(multiply_numerics(f3 * i2, (f3, i2)), 6.0)
self.assertEqual(multiply_numerics(i3 * f2, (i3, f2)), 6.0)
self.assertEqual(multiply_numerics(f3 * f2, (f3, f2)), 6.0)
self.assertEqualNodes(multiply_numerics(a * i3 * i2 * b, (i3, i2)),
a * 6 * b)
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