Commit 6b44c68e authored by Taddeus Kroes's avatar Taddeus Kroes

Splitted a large function into multiple smaller functions.

parent 4abbe240
......@@ -281,8 +281,7 @@ class ExpressionNode(Node, ExpressionBase):
self.value = OP_VALUE_MAP[op]
self.op = op
def construct_function(self, children):
if self.op == OP_DER:
def construct_derivative(self, children):
f = children[0]
if len(children) < 2:
......@@ -296,7 +295,7 @@ class ExpressionNode(Node, ExpressionBase):
# der(x ^ 2, x) -> d/dx (x ^ 2)
return 'd/d%s (%s)' % (children[1], f)
if self.op == OP_LOG:
def construct_logarithm(self, children):
if self[0].is_op(OP_ABS):
content = children[0]
else:
......@@ -314,7 +313,7 @@ class ExpressionNode(Node, ExpressionBase):
if children[1].isdigit():
return 'log_%s%s' % (children[1], content)
if self.op == OP_INT:
def construct_integral(self, children):
# Make sure that any needed parentheses around f(x) are generated,
# and append ' dx' to it (result 'f(x) dx')
fx, x = self[:2]
......@@ -333,7 +332,7 @@ class ExpressionNode(Node, ExpressionBase):
# int_a^b x ^ 2 -> int_a^b x ^ 2 dx
return op + ' ' + operand
if self.op == OP_INT_INDEF:
def construct_indef_integral(self, children):
# [x ^ 2]_a^b
F, lbnd, ubnd = self
lbnd = str(ExpressionNode(OP_SUBSCRIPT, lbnd))
......@@ -341,9 +340,23 @@ class ExpressionNode(Node, ExpressionBase):
return '[%s]%s%s' % (F, lbnd, ubnd)
def construct_function(self, children):
if self.op == OP_ABS:
return '|%s|' % children[0]
constructors = {
OP_DER: self.construct_derivative,
OP_LOG: self.construct_logarithm,
OP_INT: self.construct_integral,
OP_INT_INDEF: self.construct_indef_integral
}
if self.op in constructors:
result = constructors[self.op](children)
if result != None:
return result
# Function with absolute value as only parameter does not need
# parentheses
if self.op in TOKEN_MAP and TOKEN_MAP[self.op] == 'FUNCTION' \
......
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