// TODO: Expand to allow for more functions. // TODO: When parsing e^(f(z)), use Exponential class instead of Power class. // Evaluates the given expression tree on the given variable value. ComplexNumber evaluate_tree(const ExpressionTreeNode *root, const ComplexNumber z) { if (root->is_op_node()) { ExpressionTreeBinaryOp *temp = (ExpressionTreeBinaryOp *) root; ComplexNumber left = evaluate_tree(temp->get_left(), z); ComplexNumber right = evaluate_tree(temp->get_right(), z); // Find the appropriate operator, apply, then return. if (temp->get_operator() == PLUS) { return left + right; } else if (temp->get_operator() == MINUS) { return left - right; } else if (temp->get_operator() == TIMES) { return left * right; } else if (temp->get_operator() == DIVIDE) { return left / right; } else { // temp->get_operator() == EXP Power p (right); return p.eval(left); } } else if (root->is_func_node()) { ExpressionTreeFunction *temp = (ExpressionTreeFunction *) root; ComplexNumber arg = evaluate_tree(temp->get_argument(), z); // Find the appropriate function, evaluate, then return. if (temp->get_function() == LOG) { Logarithm l (1, 0); return l.eval(arg); } else if (temp->get_function() == SIN) { Sine s (1, 1, 0); return s.eval(arg); } else if (temp->get_function() == COS) { Cosine c (1, 1, 0); return c.eval(arg); } else { // temp->get_function() == TAN Tangent t (1, 1, 0); return t.eval(arg); } } else if (root->is_leaf_node()) { ExpressionTreeLeaf *temp = (ExpressionTreeLeaf *) root; return temp->get_val(); // Return the value itself } else if (root->is_const_node()) { ExpressionTreeConstant *temp = (ExpressionTreeConstant *) root; // Return the corresponding mathematical constant if (temp->get_constant() == e) { return E; } else if (temp->get_constant() == pi) { return PI; } else { // temp->get_constant() == phi return PHI; } } else { // root->is_var_node() return z; } }
ComplexNumber Tangent::eval(const ComplexNumber z) const { Sine sine (a, b, c); Cosine cosine (a, b, c); return sine.eval(z) / cosine.eval(z); }