Commit 74d5caf1 authored by Taddeus Kroes's avatar Taddeus Kroes

Added short notation for absolute values as only function parameter.

parent 56256eb4
...@@ -282,17 +282,22 @@ class ExpressionNode(Node, ExpressionBase): ...@@ -282,17 +282,22 @@ class ExpressionNode(Node, ExpressionBase):
return 'd/d%s (%s)' % (children[1], f) return 'd/d%s (%s)' % (children[1], f)
if self.op == OP_LOG: if self.op == OP_LOG:
if self[0].is_op(OP_ABS):
content = children[0]
else:
content = '(' + children[0] + ')'
# log(a, e) -> ln(a) # log(a, e) -> ln(a)
if self[1].is_identifier(E): if self[1].is_identifier(E):
return 'ln(%s)' % children[0] return 'ln%s' % content
# log(a, 10) -> log(a) # log(a, 10) -> log(a)
if self[1] == 10: if self[1] == 10:
return 'log(%s)' % children[0] return 'log%s' % content
# log(a, 2) -> log_2(a) # log(a, 2) -> log_2(a)
if children[1].isdigit(): if children[1].isdigit():
return 'log_%s(%s)' % (children[1], children[0]) return 'log_%s%s' % (children[1], content)
if self.op == OP_INT: if self.op == OP_INT:
# Make sure that any needed parentheses around f(x) are generated, # Make sure that any needed parentheses around f(x) are generated,
...@@ -324,6 +329,12 @@ class ExpressionNode(Node, ExpressionBase): ...@@ -324,6 +329,12 @@ class ExpressionNode(Node, ExpressionBase):
if self.op == OP_ABS: if self.op == OP_ABS:
return '|%s|' % children[0] return '|%s|' % children[0]
# Function with absolute value as only parameter does not need
# parentheses
if self.op in TOKEN_MAP and TOKEN_MAP[self.op] == 'FUNCTION' \
and len(self) == 1 and self[0].is_op(OP_ABS):
return self.title() + children[0]
def __str__(self): # pragma: nocover def __str__(self): # pragma: nocover
return generate_line(self) return generate_line(self)
......
...@@ -245,3 +245,7 @@ class TestNode(RulesTestCase): ...@@ -245,3 +245,7 @@ class TestNode(RulesTestCase):
self.assertEqual(str(tree('[x ^ 2]_a^b')), '[x ^ 2]_a^b') self.assertEqual(str(tree('[x ^ 2]_a^b')), '[x ^ 2]_a^b')
self.assertEqual(str(tree('[x ^ 2]_(a-b)^(a+b)')), self.assertEqual(str(tree('[x ^ 2]_(a-b)^(a+b)')),
'[x ^ 2]_(a - b)^(a + b)') '[x ^ 2]_(a - b)^(a + b)')
def test_construct_function_absolute_child(self):
self.assertEqual(str(tree('ln(|x|)')), 'ln|x|')
self.assertEqual(str(tree('sin(|x|)')), 'sin|x|')
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