1 #define BIORBD_API_EXPORTS
2 #include "Utils/Equation.h"
9 #include "Utils/Error.h"
13 std::vector<biorbd::utils::Equation> symbols;
14 symbols.push_back(
"(");
15 symbols.push_back(
")");
16 symbols.push_back(
"e");
17 symbols.push_back(
"/");
18 symbols.push_back(
"*");
19 symbols.push_back(
"+");
20 symbols.push_back(
"-");
51 const std::map<biorbd::utils::Equation, double>& variables)
54 std::vector<biorbd::utils::Equation> eq;
57 replaceVar(wholeEq, variables);
60 const std::vector<biorbd::utils::Equation>& symbols(prepareMathSymbols());
64 int firstIdx(
static_cast<int>(wholeEq.size())+1);
65 unsigned int toStop(0);
67 for (
unsigned int i=0; i<symbols.size(); ++i){
68 int idx =
static_cast<int>(wholeEq.find(symbols[i]));
73 if (idx < firstIdx && idx != -1)
76 if (toStop == symbols.size()){
77 eq.push_back(wholeEq);
80 if (wholeEq.size() == 1){
81 eq.push_back(wholeEq(0));
84 if (firstIdx+1 ==
static_cast<int>(wholeEq.size())){
85 eq.push_back(wholeEq.substr(0,
static_cast<unsigned int>(firstIdx)));
86 eq.push_back(wholeEq(
static_cast<unsigned int>(firstIdx)));
89 else if (firstIdx == 0){
90 if (!wholeEq(0).compare(
"-")){
92 std::vector<biorbd::utils::Equation> tp(splitIntoEquation(wholeEq.substr(1), variables));
93 if (eq.size() != 0 && !eq[eq.size()-1].compare(
"e") ){
95 eq.push_back(wholeEq(0));
96 wholeEq = wholeEq.substr(1);
97 }
else if (!tp[0].compare(
"(")) {
99 size_t idx = wholeEq.find_first_of(
")");
101 double res(-1*evaluateEquation(splitIntoEquation(newWholeEq, variables)));
102 wholeEq = to_string(res) + wholeEq.substr(idx+1);
105 eq.insert(eq.end(), tp.begin(), tp.end());
109 else if (!wholeEq(0).compare(
"+")){
111 std::vector<biorbd::utils::Equation> tp(splitIntoEquation(wholeEq.substr(1), variables));
114 eq.insert(eq.end(), tp.begin(), tp.end());
118 eq.push_back(wholeEq(0));
119 wholeEq = wholeEq.substr(1);
123 eq.push_back(wholeEq.substr(0,
static_cast<unsigned int>(firstIdx)));
124 eq.push_back(wholeEq(
static_cast<unsigned int>(firstIdx)));
125 wholeEq = wholeEq.substr(
static_cast<unsigned int>(firstIdx)+1);
138 std::vector<biorbd::utils::Equation> &eq)
140 for (
unsigned int i=0; i<eq.size(); ++i)
141 if (!eq[i].tolower().compare(
"pi")){
142 eq[i] = to_string(M_PI);
148 const std::map<biorbd::utils::Equation, double>& variables)
150 for (
auto var : variables)
151 while (eq.find(var.first) != std::string::npos){
152 size_t pos(eq.find(var.first));
153 size_t length(var.first.length());
154 eq = eq.substr(0, pos) +
"(" +
155 to_string(var.second) +
")" +
156 eq.substr(pos + length);
162 std::vector<biorbd::utils::Equation> wholeEq)
164 return evaluateEquation(wholeEq,0);
168 std::vector<biorbd::utils::Equation> eq,
176 const std::vector<biorbd::utils::Equation>& symbols(prepareMathSymbols());
177 std::vector<biorbd::utils::Equation> eq2;
178 bool continuer(
true);
180 for (
unsigned int j=0; j<eq.size(); ++j){
181 if (!eq[j].compare(symbols[math]) && continuer){
182 if (j==0 && (!symbols[math].compare(
"+") || !symbols[math].compare(
"-"))){
184 if (!symbols[math].compare(
"+"))
185 eq2[j-1] = to_string(0.0 + stod(eq[j+1]));
186 else if (!symbols[math].compare(
"-"))
187 eq2[j-1] = to_string(0.0 - stod(eq[j+1]));
191 if (!symbols[math].compare(
"(")){
192 std::vector<biorbd::utils::Equation> eq_tp;
193 bool foundIdx(
false);
195 unsigned int cmpOpen(0);
196 for (
unsigned int k=j+1; k<eq.size();++k){
197 if (!eq[k].compare(
"("))
199 else if (!eq[k].compare(
")")){
208 eq_tp.push_back(eq[k]);
213 eq2.push_back(to_string(evaluateEquation(eq_tp)));
214 j+=
static_cast<unsigned int>(cmpValues);
216 else if (!symbols[math].compare(
"/"))
217 eq2[j-1] = to_string(stod(eq[j-1]) / stod(eq[j+1]));
218 else if (!symbols[math].compare(
"*"))
219 eq2[j-1] = to_string(stod(eq[j-1]) * stod(eq[j+1]));
220 else if (!symbols[math].compare(
"+"))
221 eq2[j-1] = to_string(stod(eq[j-1]) + stod(eq[j+1]));
222 else if (!symbols[math].compare(
"-"))
223 eq2[j-1] = to_string(stod(eq[j-1]) - stod(eq[j+1]));
224 else if (!symbols[math].compare(
"e")){
225 if (!eq[j+1].compare(
"-")){
226 eq2[j-1] = to_string(stod(eq[j-1]) * pow(10,-1*stod(eq[j+2])));
229 else if (!eq[j+1].compare(
"+")){
230 eq2[j-1] = to_string(stod(eq[j-1]) * pow(10,stod(eq[j+2])));
234 eq2[j-1] = to_string(stod(eq[j-1]) * pow(10,stod(eq[j+1])));
243 eq2.push_back(eq[j]);
247 return evaluateEquation(eq2, ++math);
249 return evaluateEquation(eq2, math);
254 const std::map<biorbd::utils::Equation, double>& variables)
256 return evaluateEquation(splitIntoEquation(wholeEq, variables));
261 std::map<biorbd::utils::Equation, double> dumb;
262 return evaluateEquation(splitIntoEquation(wholeEq, dumb));