Commit 68a227a2 authored by Taddeüs Kroes's avatar Taddeüs Kroes

sqrt(a) no longer supports an additional argument for n (n=2 by default).

parent bc8b6611
......@@ -249,27 +249,31 @@ MESSAGES[remove_negative_root] = _('{0[1]} is an odd number, so apply the ' \
def match_exponent_to_root(node):
"""
a^(1 / m) -> sqrt(a, m)
a^(n / m) -> sqrt(a^n, m)
a^(1 / 2) -> sqrt(a)
a^(n / 2) -> sqrt(a^n)
"""
assert node.is_op(OP_POW)
left, right = node
if right.is_op(OP_DIV):
return [P(node, exponent_to_root, (left,) + tuple(right))]
if right.is_op(OP_DIV) and right[1] == 2:
return [P(node, exponent_to_root)]
return []
def exponent_to_root(root, args):
"""
a^(1 / m) -> sqrt(a, m)
a^(n / m) -> sqrt(a^n, m)
a^(1 / 2) -> sqrt(a)
a^(n / 2) -> sqrt(a^n)
"""
a, n, m = args
a, (n, _) = root
return N(OP_SQRT, a if n == 1 else a ** n, m)
return N(OP_SQRT, a if n == 1 else a ** n)
MESSAGES[exponent_to_root] = \
_('Rewrite power with exponent {0[1]} to a square root.')
def match_extend_exponent(node):
......
......@@ -88,7 +88,7 @@ def quadrant_sqrt(root, args):
MESSAGES[quadrant_sqrt] = \
_('The square root of a quadrant reduces to the raised root.')
_('The root of a reduces to the raised root.')
def constant_sqrt(root, args):
......
......@@ -140,17 +140,13 @@ class TestRulesPowers(RulesTestCase):
P(root, remove_negative_root)])
def test_match_exponent_to_root(self):
a, n, m, l1 = tree('a,n,m,1')
root = tree('a ^ (1 / 2)')
self.assertEqualPos(match_exponent_to_root(root),
[P(root, exponent_to_root)])
root = a ** (n / m)
possibilities = match_exponent_to_root(root)
self.assertEqualPos(possibilities,
[P(root, exponent_to_root, (a, n, m))])
root = a ** (l1 / m)
possibilities = match_exponent_to_root(root)
self.assertEqualPos(possibilities,
[P(root, exponent_to_root, (a, 1, m))])
root = tree('a ^ (n / 2)')
self.assertEqualPos(match_exponent_to_root(root),
[P(root, exponent_to_root)])
def test_add_exponents(self):
a, p, q = tree('a,p,q')
......@@ -198,14 +194,11 @@ class TestRulesPowers(RulesTestCase):
self.assertEqualNodes(remove_negative_root(root, ()), expect)
def test_exponent_to_root(self):
a, n, m, l1 = tree('a,n,m,1')
root = a ** (n / m)
self.assertEqualNodes(exponent_to_root(root, (a, n, m)),
N('sqrt', a ** n, m))
root, expect = tree('a ^ (1 / 2), sqrt(a)')
self.assertEqualNodes(exponent_to_root(root, ()), expect)
self.assertEqualNodes(exponent_to_root(root, (a, l1, m)),
N('sqrt', a, m))
root, expect = tree('a ^ (n / 2), sqrt(a ^ n)')
self.assertEqualNodes(exponent_to_root(root, ()), expect)
def test_match_constant_exponent(self):
a0, a1, a2 = tree('a ^ 0, a ^ 1, a ^ 2')
......
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