53 ComputationTree::ComputationTree(
const std::string& formula,
const std::map<std::string, Function*>& functionNames) :
58 std::string str2 = formula;
60 str2.erase(std::remove_if(str2.begin(),
63 return std::isspace(x);
73 unsigned int level = 0;
78 for (
size_t i = formula.size(); i > 0; --i)
80 char c = formula[i - 1];
97 if ((c ==
'+' || c ==
'-') && !(i == 1 || formula[i - 2] ==
'*' || formula[i - 2] ==
'/'
98 || formula[i - 2] ==
'+' || formula[i - 2] ==
'-' || formula[i - 2] ==
'('))
100 std::shared_ptr<Operator> left =
readFormula_(formula.substr(0, i - 1), functionNames);
101 std::shared_ptr<Operator> right =
readFormula_(formula.substr(i), functionNames);
116 for (
size_t i = formula.size(); i > 0; --i)
118 char c = formula[i - 1];
135 if (c ==
'*' || c ==
'/')
137 std::shared_ptr<Operator> left =
readFormula_(formula.substr(0, i - 1), functionNames);
138 std::shared_ptr<Operator> right =
readFormula_(formula.substr(i), functionNames);
152 if (formula[0] ==
'(')
153 return readFormula_(formula.substr(1, formula.size() - 2), functionNames);
158 shared_ptr<Operator> here;
166 if (formula[0] ==
'-')
168 std::shared_ptr<Operator> son =
readFormula_(formula.substr(1), functionNames);
173 std::map<std::string, Function*>::const_iterator it(functionNames.find(formula));
175 if (it != functionNames.end())
186 size_t posp = formula.find(
"(");
187 if (posp == string::npos)
188 throw Exception(
"ComputationTree::readFormula_ : unknown formula : " + formula);
190 std::shared_ptr<Operator> son =
readFormula_(formula.substr(posp), functionNames);
191 string fonc = formula.substr(0, posp);
194 here = shared_ptr<Operator>(
new MathOperator(&exp,
"exp", son));
195 else if (fonc ==
"log")
196 here = shared_ptr<Operator>(
new MathOperator(&log,
"log", son));
198 throw Exception(
"ComputationTree::readFormula_ : unknown formula : " + formula);
214 for ( ; it->end(); it->next())
226 std::string op =
getRoot()->output();
227 while (op.size() > 1 && op[0] ==
'(' && op[op.size() - 1] ==
')')
229 op = op.substr(1, op.size() - 2);
const std::shared_ptr< TreeGraphImpl > getGraph() const
void createNode(Nref nodeObject)
std::unique_ptr< typename AssociationGraphObserver< N, E >::NodeIterator > allNodesIterator()
void setRoot(const Nref newRoot)
set the root (but no checking, to be used at first construction)
void setFather(const std::shared_ptr< N > nodeObject, const std::shared_ptr< N > fatherNodeObject, const std::shared_ptr< E > edgeObject=0)
Binary arithmetic operator for numerical computation.
std::shared_ptr< Operator > readFormula_(const std::string &formula, const std::map< std::string, Function * > &functionNames)
std::string output() const
Constant (ie leaf) operator.
This is the abstract class for first order derivable functions.
This is the abstract class for second order derivable functions.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Implements a double operator (ie leaf in the computation tree) where value comes from a function.
Implements a unary operator that applies a math (described in cmath) operator.
Negative value for an operator.
Interface of operator for numerical computation.
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.