Improved arguments of match_combine_polynomes.

match_combine_polynomes will return (n0, n1, c0, c1, r0, e0), thus avoiding the
duplicated arguments. Adjusted the unit tests as well.
parent a302f871
......@@ -125,6 +125,15 @@ class ExpressionNode(Node, ExpressionBase):
def __str__(self): # pragma: nocover
return generate_line(self)
def __eq__(self, other):
"""
Check strict equivalence.
"""
if isinstance(other, ExpressionNode):
return self.op == other.op and self.nodes == other.nodes
return False
def graph(self): # pragma: nocover
return generate_graph(self)
......@@ -205,6 +214,16 @@ class ExpressionLeaf(Leaf, ExpressionBase):
self.type = TYPE_MAP[type(args[0])]
def __eq__(self, other):
if isinstance(other, int) or isinstance(other, float) \
or isinstance(other, str):
return self.value == other
if other.is_leaf():
return self.value == other.value
return False
def extract_polynome_properties(self):
"""
An expression leaf will return the polynome tuple (1, r, 1), where r is
......
......@@ -13,12 +13,9 @@ class Possibility(object):
# TODO: Add unit tests
def __eq__(self, other):
self_arg0, self_arg1 = zip(*self.args)
other_arg0, other_arg1 = zip(*other.args)
return self.handler == other.handler \
and self_arg1 == other_arg1 \
and map(hash, self_arg0) == map(hash, other_arg0)
and hash(self.root) == hash(other.root) \
and self.args == other.args
def filter_duplicates(items):
......
......@@ -94,9 +94,18 @@ def match_combine_polynomes(node, verbose=False):
# Both numeric root and same exponent -> combine coefficients and
# roots, or: same root and exponent -> combine coefficients.
if ((r0.is_numeric() and r1.is_numeric()) or r0 == r1) \
and e0 == e1:
p.append(P(node, combine_polynomes, (left, right)))
if c0 == 1 and c1 == 1 and e0 == 1 and e1 == 1 \
and r0.is_numeric() and r1.is_numeric():
# 2 + 3 -> 5
p.append(P(node, combine_numerics, \
(left[0], right[0], r0, r1)))
elif c0.is_numeric() and c1.is_numeric() and r0 == r1 and e0 == e1:
# 2a + 2a -> 4a
# a + 2a -> 3a
# 2a + a -> 3a
# a + a -> 2a
p.append(P(node, combine_polynomes, \
(left[0], right[0], c0, c1, r0, e0)))
return p
......
import unittest
from src.rules.poly import match_combine_polynomes, combine_polynomes
from src.rules.poly import match_combine_polynomes, combine_polynomes, \
combine_numerics
from src.possibilities import Possibility as P
from src.parser import Parser
from tests.parser import ParserWrapper
......@@ -24,71 +25,81 @@ class TestRulesPoly(unittest.TestCase):
self.assertEqual(p, e)
def test_numbers(self):
return
# TODO: Move to combine numeric test
l1, l2 = root = tree('1+2')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((l1, (l1, l1, l1)),
[P(root, combine_numerics, ((l1, (l1, l1, l1)),
(l2, (l1, l2, l1))))])
def test_identifiers_basic(self):
l1, l2 = tree('1,2')
a1, a2 = root = tree('a+a')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((a1, (l1, a1, l1)),
(a2, (l1, a2, l1))))])
[P(root, combine_polynomes, (a1, a2, 1, 1, 'a', 1))])
def test_identifiers_normal(self):
l1, l2 = tree('1,2')
a1, a2 = root = tree('a+2a')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((a1, (l1, a1, l1)),
(a2, (l2, a2[1], l1))))])
[P(root, combine_polynomes, (a1, a2, 1, 2, 'a', 1))])
def test_identifiers_reverse(self):
l1, l2, la = tree('1,2,a')
return
# TODO: Move to normalisation test
a1, a2 = root = tree('a+a*2')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((a1, (l1, a1, l1)),
(a2, (l2, la, l1))))])
[P(root, combine_polynomes, (a1, a2, 1, 2, a1, 1))])
def test_identifiers_exponent(self):
l1, l2 = tree('1,2')
a1, a2 = root = tree('a2+a2')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((a1, (l1, a1[0], l2)),
(a2, (l1, a2[0], l2))))])
[P(root, combine_polynomes, (a1, a2, 1, 1, 'a', 2))])
def test_identifiers_coeff_exponent_left(self):
a1, a2 = root = tree('2a3+a3')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, (a1, a2, 2, 1, 'a', 3))])
def test_identifiers_coeff_exponent_both(self):
a1, a2 = root = tree('2a3+2a3')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, (a1, a2, 2, 2, 'a', 3))])
def test_basic_subexpressions(self):
a_b, c, d, l1, l5, l7 = tree('a+b,c,d,1,5,7')
a_b, c, d = tree('a+b,c,d')
left, right = root = tree('(a+b)^d + (a+b)^d')
self.assertEqual(left, right)
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((left, (l1, a_b, d)),
(right, (l1, a_b, d))))])
[P(root, combine_polynomes, (left, right, 1, 1, a_b, d))])
left, right = root = tree('5(a+b)^d + 7(a+b)^d')
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((left, (l5, a_b, d)),
(right, (l7, a_b, d))))])
[P(root, combine_polynomes, (left, right, 5, 7, a_b, d))])
left, right = root = tree('c(a+b)^d + c(a+b)^d')
# TODO: Move to other strategy
#left, right = root = tree('c(a+b)^d + c(a+b)^d')
self.assertEqual(left, right)
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
[P(root, combine_polynomes, ((left, (c, a_b, d)),
(right, (c, a_b, d))))])
#self.assertEqual(left, right)
#possibilities = match_combine_polynomes(root)
#self.assertEqualPos(possibilities,
# [P(root, combine_polynomes, (left, right, c, c, a_b, d))])
def test_match_combine_polynomes_numeric_combinations(self):
return
root = tree('0+1+2')
# TODO: this test fails with this code: l0, l1, l2 = tree('0,1,2')
l0, l1, l2 = root[0][0], root[0][1], root[1]
possibilities = match_combine_polynomes(root)
self.assertEqualPos(possibilities,
......
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