3 #include <IExprTreeNode.h>
26 template <
typename NumericType>
121 if(
right !=
nullptr){
139 NumericType
const x, NumericType
const y
142 case OP_ADD:
return x + y;
143 case OP_SUB:
return x - y;
144 case OP_MUL:
return x * y;
145 case OP_DIV:
return x / y;
146 case OP_POW:
return std::pow(x, y);
149 int const n = (int) y;
150 int const nAbs = std::abs(n);
151 for (
int i = 1; i < nAbs; ++i) out *= x;
152 if(n < 0)
return 1.0/out;
155 case OP_atan2:
return std::atan2(x, y);
157 std::stringstream ss;
158 ss <<
"UnivarExprTreeNode failed to do operation with "
159 <<
"x = " << x <<
", y = " << y;
171 case f_exp:
return std::exp(x);
172 case f_ln:
return std::log(x);
173 case f_sqrt:
return std::sqrt(x);
174 case f_abs:
return std::abs(x);
175 case f_cos:
return std::cos(x);
176 case f_sin:
return std::sin(x);
177 case f_tan:
return std::tan(x);
178 case f_acos:
return std::acos(x);
179 case f_asin:
return std::asin(x);
180 case f_atan:
return std::atan(x);
181 case f_cosh:
return std::cosh(x);
182 case f_sinh:
return std::sinh(x);
183 case f_tanh:
return std::tanh(x);
185 std::stringstream ss;
186 ss <<
"UnivarExprTreeNode failed to do function with "
196 NumericType
eval(NumericType
const t)
const override{
207 std::stringstream ss;
208 ss <<
"UnivarExprTreeNode failed to eval t = " << t;
258 if(opStr ==
"+")
op = OP_ADD;
259 else if(opStr ==
"-")
op = OP_SUB;
260 else if(opStr ==
"*")
op = OP_MUL;
261 else if(opStr ==
"/")
op = OP_DIV;
262 else if(opStr ==
"^")
op = OP_POW;
263 else if(opStr ==
"atan2")
op = OP_atan2;
265 std::stringstream ss;
266 ss <<
"UnivarExprTreeNode::setOperator(std::string const &) "
267 <<
"failed due to an unexpected operator: \"" << opStr <<
"\"";
Base class for Helios exceptions.
Definition: HeliosException.h:12
Binary tree node interface that must be implemented by any class providing binary tree node based fun...
Definition: IBinaryTreeNode.h:10
Interface extending the Binary Tree node definition to become a Expression Tree node....
Definition: IExprTreeNode.h:19
Class implementing a Univariate Expression Tree Node.
Definition: UnivarExprTreeNode.h:27
NumericType doOperation(NumericType const x, NumericType const y) const
Do the operation if the node is an operator node. Otherwise, something really wrong might happen....
Definition: UnivarExprTreeNode.h:138
bool isVariable() const
Check whether the node is a variable (true) or not (false)
Definition: UnivarExprTreeNode.h:252
bool isNumber() const
Check whether the node is a number (true) or not (false)
Definition: UnivarExprTreeNode.h:247
OpType op
Operator as node's element.
Definition: UnivarExprTreeNode.h:89
FunType
The different functions supported by the univariate expression tree node.
Definition: UnivarExprTreeNode.h:54
OpType
The different operators supported by the univariate expression tree node.
Definition: UnivarExprTreeNode.h:40
bool isOperator() const
Check whether the node is an operator (true) or not (false)
Definition: UnivarExprTreeNode.h:237
FunType fun
Function as node's element.
Definition: UnivarExprTreeNode.h:93
IBinaryTreeNode * getRightChild() const override
Obtain the right child of current node.
Definition: UnivarExprTreeNode.h:223
UnivarExprTreeNode(UnivarExprTreeNode *left=nullptr, UnivarExprTreeNode *right=nullptr)
Default constructor for Univariate Expression Tree Node.
Definition: UnivarExprTreeNode.h:112
SymbolType symbolType
The type of element (symbol) used by the univariate expression tree node.
Definition: UnivarExprTreeNode.h:77
UnivarExprTreeNode * right
The right child node.
Definition: UnivarExprTreeNode.h:102
SymbolType
The different types of element (symbols) supported by the univariate expression tree node.
Definition: UnivarExprTreeNode.h:35
bool isFunction() const
Check whether the node is a function (true) or not (false)
Definition: UnivarExprTreeNode.h:242
IBinaryTreeNode * getLeftChild() const override
Obtain the left child of current node.
Definition: UnivarExprTreeNode.h:218
UnivarExprTreeNode * left
The left child node.
Definition: UnivarExprTreeNode.h:98
void setOperator(std::string const &opStr)
Set the operator UnivarExprTreeNode::op from given string.
Definition: UnivarExprTreeNode.h:257
NumericType eval(NumericType const t) const override
Definition: UnivarExprTreeNode.h:196
NumericType num
Number as node's element.
Definition: UnivarExprTreeNode.h:85
bool isLeafNode() const override
Simplified check because for UnivarExprTreeNode there is no right child without a left child.
Definition: UnivarExprTreeNode.h:229
NumericType doFunction(NumericType const x) const
Compute the function's output for the given input .
Definition: UnivarExprTreeNode.h:167