Commit ad790a21 authored by Taddeus Kroes's avatar Taddeus Kroes

duplicate_exponent now supports n-ary multiplication.

parent 44cbdd93
......@@ -85,7 +85,7 @@ def match_duplicate_exponent(node):
left, right = node
if left.is_op(OP_MUL):
return [P(node, duplicate_exponent, tuple(left) + (right,))]
return [P(node, duplicate_exponent, (left.get_scope(), right))]
return []
......@@ -157,11 +157,16 @@ def multiply_exponents(root, args):
def duplicate_exponent(root, args):
"""
(ab)^p -> a^p * b^p
(ab)^p -> a^p * b^p
(abc)^p -> a^p * b^p * c^p
"""
a, b, p = args
ab, p = args
result = ab[0] ** p
for b in ab[1:]:
result *= b ** p
return a ** p * b ** p
return result
def remove_negative_exponent(root, args):
......
......@@ -77,7 +77,7 @@ class TestRulesPowers(RulesTestCase):
possibilities = match_duplicate_exponent(root)
self.assertEqualPos(possibilities,
[P(root, duplicate_exponent, (a, b, p))])
[P(root, duplicate_exponent, ([a, b], p))])
def test_match_remove_negative_exponent(self):
a, p = tree('a,p')
......@@ -121,12 +121,16 @@ class TestRulesPowers(RulesTestCase):
a ** (p * q))
def test_duplicate_exponent(self):
a, b, p = tree('a,b,p')
root = (a * b) ** p
a, b, c, p = tree('a,b,c,p')
self.assertEqualNodes(duplicate_exponent(root, (a, b, p)),
root = (a * b) ** p
self.assertEqualNodes(duplicate_exponent(root, ([a, b], p)),
a ** p * b ** p)
root = (a * b * c) ** p
self.assertEqualNodes(duplicate_exponent(root, ([a, b, c], p)),
a ** p * b ** p * c ** p)
def test_remove_negative_exponent(self):
a, p, l1 = tree('a,p,1')
root = a ** -p
......
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