17 ComputationTree::ComputationTree(
const std::string& formula,
const std::map<std::string, std::shared_ptr<FunctionInterface>>& functionNames) :
22 std::string str2 = formula;
24 str2.erase(std::remove_if(str2.begin(),
27 return std::isspace(x);
35 std::shared_ptr<Operator>
ComputationTree::readFormula_(
const std::string& formula,
const std::map<std::string, std::shared_ptr<FunctionInterface>>& functionNames)
37 unsigned int level = 0;
42 for (
size_t i = formula.size(); i > 0; --i)
44 char c = formula[i - 1];
61 if ((c ==
'+' || c ==
'-') && !(i == 1 || formula[i - 2] ==
'*' || formula[i - 2] ==
'/' 62 || formula[i - 2] ==
'+' || formula[i - 2] ==
'-' || formula[i - 2] ==
'('))
64 std::shared_ptr<Operator> left =
readFormula_(formula.substr(0, i - 1), functionNames);
65 std::shared_ptr<Operator> right =
readFormula_(formula.substr(i), functionNames);
80 for (
size_t i = formula.size(); i > 0; --i)
82 char c = formula[i - 1];
99 if (c ==
'*' || c ==
'/')
101 std::shared_ptr<Operator> left =
readFormula_(formula.substr(0, i - 1), functionNames);
102 std::shared_ptr<Operator> right =
readFormula_(formula.substr(i), functionNames);
116 if (formula[0] ==
'(')
117 return readFormula_(formula.substr(1, formula.size() - 2), functionNames);
122 shared_ptr<Operator> here;
130 if (formula[0] ==
'-')
132 std::shared_ptr<Operator> son =
readFormula_(formula.substr(1), functionNames);
137 auto it = functionNames.find(formula);
139 if (it != functionNames.end())
141 if (dynamic_pointer_cast<const SecondOrderDerivable>(it->second))
143 else if (dynamic_pointer_cast<const FirstOrderDerivable>(it->second))
150 size_t posp = formula.find(
"(");
151 if (posp == string::npos)
152 throw Exception(
"ComputationTree::readFormula_ : unknown formula : " + formula);
154 std::shared_ptr<Operator> son =
readFormula_(formula.substr(posp), functionNames);
155 string fonc = formula.substr(0, posp);
158 here = shared_ptr<Operator>(
new MathOperator(&exp,
"exp", son));
159 else if (fonc ==
"log")
160 here = shared_ptr<Operator>(
new MathOperator(&log,
"log", son));
162 throw Exception(
"ComputationTree::readFormula_ : unknown formula : " + formula);
190 std::string op =
getRoot()->output();
191 while (op.size() > 1 && op[0] ==
'(' && op[op.size() - 1] ==
')')
193 op = op.substr(1, op.size() - 2);
Implements a double operator (ie leaf in the computation tree) where value comes from a function...
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
std::string output() const
Binary arithmetic operator for numerical computation.
std::string removeWhiteSpaces(const std::string &s)
Remove all white spaces characters in a string.
const std::shared_ptr< TreeGraphImpl > getGraph() const
Implements a unary operator that applies a math (described in cmath) operator.
void createNode(Nref nodeObject)
void setRoot(const Nref newRoot)
set the root (but no checking, to be used at first construction)
std::shared_ptr< Operator > readFormula_(const std::string &formula, const std::map< std::string, std::shared_ptr< FunctionInterface >> &functionNames)
std::unique_ptr< typename AssociationGraphObserver< N, E >::NodeIterator > allNodesIterator()
virtual bool end() const =0
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
void setFather(const std::shared_ptr< N > nodeObject, const std::shared_ptr< N > fatherNodeObject, const std::shared_ptr< E > edgeObject=0)
Constant (ie leaf) operator.
Negative value for an operator.
Interface of operator for numerical computation.