Ejemplo n.º 1
0
 def evaluate(self):
     """recursively iterates through all parentheses, evaluating the \
     innermost parenthetical phrase first and working its way back \
     to the outermost"""
     try:
         return float(self.phrase)
     except ValueError:
         while self.has_parenthesis():
             index = self.get_nested_index()
             if str(self.terms[index][0]) == "-":
                 #there may be some issues with the type casting here
                 try:
                     self.terms[index] = -1 * \
                     (float(RawEvaluator(eqn_helper.unshell(\
                     self.terms[index])).evaluate()))
                 except ValueError:
                     self.terms[index] = -1 * \
                     (complex(RawEvaluator(eqn_helper.unshell(\
                     self.terms[index])).evaluate()))
             else:
                 self.terms[index] = \
                 RawEvaluator(eqn_helper.unshell(self.terms[index])).evaluate()
         index = 0
         terms = self.terms.copy()
         operators = self.operators.copy()
         #iterates through the term and operator equations,
         #trimming them as it goes
         while len(terms) > 1 and len(operators) > 0:
             index = self.get_op_index(operators)
             terms[index] = self.operate(operators[index], terms[index],\
             terms[index+1])
             if index < len(terms):
                 del(terms[index+1])
             del(operators[index])
         #returns the last remaining dreg of the term equation
         return terms[0]
Ejemplo n.º 2
0
 def eval_function(self, phrase):
     """evaluates a given function by splitting it into its term and its
     function component, then using the appropriate pre-defined math
     operation"""
     #manages the sign of the output
     sign = 1.0
     if phrase[0] == "-":
         funct = phrase[1:phrase.index("(")]
         sign = -1.0
     #gets everything before the first parenthesis in the phrase
     else:
         funct = phrase[:phrase.index("(")]
     #gets / evaluates the term as the item inside the parentheses;
     #if the term is a summation, handles that separately so as not
     #to evaluate the terms inside beforehand          
     if funct != "sum":           
         term = Function(eqn_helper.unshell(phrase),\
                     cpx = self.complex, asimp = self.asimp,\
                     constants = self.constants).evaluate()
     else:
         return sign * self.eval_sum(phrase)
     try:
         return sign * eqn_helper.simple_functions[funct](term)
     except TypeError:
         return sign * eqn_helper.complex_functions[funct](term)
     except KeyError:
         if funct == "norm":
             if type(term) != type(Vector(0)):
                 term = Vector(term[0:len(term)])
             result = str(term.normalize() * sign)
             if result[0] == '<':
                 result = result[1:len(result)-1]
             return result
         else:
             if funct == 'sum':
                 print(term)
                 return self.eval_sum(str(term))
     except ValueError:
         print('Value Error.')
         return 0
     except ZeroDivisionError:
         print('Zero Division Error.')
         return 0
     return term