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