Biorbd Release_1.13.0
Loading...
Searching...
No Matches
Equation.cpp
1#define BIORBD_API_EXPORTS
2#include "Utils/Equation.h"
3
4#ifdef _WIN32
5#include <string>
6#endif
7
8#include <math.h>
9
10#include "Utils/Error.h"
11
12using namespace BIORBD_NAMESPACE;
13
14std::vector<utils::Equation> utils::Equation::prepareMathSymbols() {
15 // Classify in the order of priority of the operations
16 std::vector<utils::Equation> symbols;
17 symbols.push_back("(");
18 symbols.push_back(")");
19 symbols.push_back("e");
20 symbols.push_back("/");
21 symbols.push_back("*");
22 symbols.push_back("+");
23 symbols.push_back("-");
24
25 return symbols;
26}
27
29
30utils::Equation::Equation(const char* c) : utils::String(c) {}
31
33
34utils::Equation::Equation(const std::basic_string<char>& c)
35 : utils::String(c) {}
36
37std::vector<utils::Equation> utils::Equation::splitIntoEquation(
38 utils::Equation wholeEq,
39 const std::map<utils::Equation, double>& variables) {
40 // output variable
41 std::vector<utils::Equation> eq;
42
43 // Replace the variables by a number
44 replaceVar(wholeEq, variables);
45
46 // Declaration of the arithmetic markers
47 const std::vector<utils::Equation>& symbols(prepareMathSymbols());
48
49 // All along the string
50 while (1) {
51 int firstIdx(
52 static_cast<int>(wholeEq.size()) + 1); // Assign an index too big
53 size_t toStop(0); // Set stop at 0
54 // Go through each symbol to see if it's in the equation
55 for (size_t i = 0; i < symbols.size(); ++i) {
56 int idx = static_cast<int>(wholeEq.find(symbols[i]));
57 if (idx < 0) { // If not, write it down
58 ++toStop;
59 continue;
60 }
61 if (idx < firstIdx &&
62 idx != -1) { // If yes, look if it's before another symbol
63 firstIdx = idx;
64 }
65 }
66 if (toStop == symbols.size()) { // If no symbols left, exit the while loop
67 eq.push_back(wholeEq);
68 break;
69 }
70 if (wholeEq.size() == 1) {
71 eq.push_back(wholeEq(0));
72 break;
73 }
74 if (firstIdx + 1 == static_cast<int>(wholeEq.size())) {
75 eq.push_back(wholeEq.substr(0, static_cast<size_t>(firstIdx)));
76 eq.push_back(wholeEq(static_cast<size_t>(firstIdx)));
77 break;
78 } else if (firstIdx == 0) {
79 if (!wholeEq(0).compare("-")) {
80 // If the equation starts with "-"
81 std::vector<utils::Equation> tp(
82 splitIntoEquation(wholeEq.substr(1), variables));
83 if (eq.size() != 0 && !eq[eq.size() - 1].compare("e")) {
84 // special case of scientific notation (1e-x)
85 eq.push_back(wholeEq(0));
86 wholeEq = wholeEq.substr(1);
87 } else if (!tp[0].compare("(")) {
88 // Resolve now the parenthese to apply the minus after
89 size_t idx = wholeEq.find_first_of(")");
90 utils::Equation newWholeEq(wholeEq.substr(2, idx - 2));
91 double res(
92 -1 * evaluateEquation(splitIntoEquation(newWholeEq, variables)));
93 wholeEq = to_string(res) + wholeEq.substr(idx + 1);
94 } else {
95 tp[0] = "-" + tp[0];
96 eq.insert(eq.end(), tp.begin(), tp.end());
97 return eq;
98 }
99 } else if (!wholeEq(0).compare("+")) {
100 // If the equation starts with +
101 std::vector<utils::Equation> tp(
102 splitIntoEquation(wholeEq.substr(1), variables));
103 if (eq.size() !=
104 0) { // If there's nothing before, it's false in all cases 1ex
105 eq.push_back("+");
106 }
107 eq.insert(eq.end(), tp.begin(), tp.end());
108 return eq;
109 } else {
110 eq.push_back(wholeEq(0));
111 wholeEq = wholeEq.substr(1);
112 }
113 } else {
114 eq.push_back(wholeEq.substr(
115 0, static_cast<size_t>(firstIdx))); // Take everything before symbol
116 eq.push_back(wholeEq(static_cast<size_t>(firstIdx))); // Get the symbol
117 wholeEq = wholeEq.substr(
118 static_cast<size_t>(firstIdx) +
119 1); // Keep everything after the symbol and restart
120 }
121 }
122
123 // Replace constant with numbers
124 replaceCste(eq);
125
126 // Return the equation
127 return eq;
128}
129
130void utils::Equation::replaceCste(std::vector<utils::Equation>& eq) {
131 for (size_t i = 0; i < eq.size(); ++i)
132 if (!eq[i].tolower().compare("pi")) {
133 eq[i] = to_string(M_PI);
134 }
135}
136
138 utils::Equation& eq,
139 const std::map<utils::Equation, double>& variables) {
140 for (auto var : variables)
141 while (eq.find(var.first) != std::string::npos) {
142 size_t pos(eq.find(var.first));
143 size_t length(var.first.length());
144 eq = eq.substr(0, pos) + "(" + to_string(var.second) + ")" +
145 eq.substr(pos + length);
146 }
147}
148
149double utils::Equation::evaluateEquation(std::vector<Equation> wholeEq) {
150 return evaluateEquation(wholeEq, 0);
151}
152
154 std::vector<Equation> eq,
155 size_t math) {
156 // If everything was done
157 if (eq.size() == 1) {
158 return stod(eq[0]);
159 }
160
161 // Declaration of the arithmetic markers
162 const std::vector<utils::Equation>& symbols(prepareMathSymbols());
163 std::vector<utils::Equation> eq2;
164 bool continuer(true);
165
166 for (size_t j = 0; j < eq.size(); ++j) {
167 if (!eq[j].compare(symbols[math]) && continuer) {
168 if (j == 0 &&
169 (!symbols[math].compare("+") || !symbols[math].compare("-"))) {
170 // Crush the previous value
171 if (!symbols[math].compare("+")) {
172 eq2[j - 1] = to_string(0.0 + stod(eq[j + 1]));
173 } else if (!symbols[math].compare("-")) {
174 eq2[j - 1] = to_string(0.0 - stod(eq[j + 1]));
175 }
176 } else {
177 // Crush the previous value
178 if (!symbols[math].compare("(")) {
179 std::vector<utils::Equation> eq_tp;
180 bool foundIdx(false);
181 int cmpValues(0);
182 size_t cmpOpen(0);
183 for (size_t k = j + 1; k < eq.size(); ++k) {
184 if (!eq[k].compare("(")) {
185 cmpOpen++;
186 } else if (!eq[k].compare(")")) {
187 if (cmpOpen == 0) {
188 foundIdx = true;
189 break;
190 } else {
191 cmpOpen--;
192 }
193 }
194
195 eq_tp.push_back(eq[k]);
196 ++cmpValues;
197 }
198 utils::Error::check(foundIdx, "You must close brackets!");
199
200 eq2.push_back(to_string(evaluateEquation(eq_tp)));
201 j += static_cast<size_t>(cmpValues);
202 } else if (!symbols[math].compare("/")) {
203 eq2[j - 1] = to_string(stod(eq[j - 1]) / stod(eq[j + 1]));
204 } else if (!symbols[math].compare("*")) {
205 eq2[j - 1] = to_string(stod(eq[j - 1]) * stod(eq[j + 1]));
206 } else if (!symbols[math].compare("+")) {
207 eq2[j - 1] = to_string(stod(eq[j - 1]) + stod(eq[j + 1]));
208 } else if (!symbols[math].compare("-")) {
209 eq2[j - 1] = to_string(stod(eq[j - 1]) - stod(eq[j + 1]));
210 } else if (!symbols[math].compare("e")) {
211 if (!eq[j + 1].compare("-")) {
212 eq2[j - 1] =
213 to_string(stod(eq[j - 1]) * pow(10, -1 * stod(eq[j + 2])));
214 ++j;
215 } else if (!eq[j + 1].compare("+")) {
216 eq2[j - 1] = to_string(stod(eq[j - 1]) * pow(10, stod(eq[j + 2])));
217 ++j;
218 } else {
219 eq2[j - 1] = to_string(stod(eq[j - 1]) * pow(10, stod(eq[j + 1])));
220 }
221 }
222 }
223
224 j += 2;
225 continuer = false;
226 }
227 if (j < eq.size()) {
228 eq2.push_back(eq[j]);
229 }
230 }
231
232 if (continuer) {
233 return evaluateEquation(eq2, ++math);
234 } else {
235 return evaluateEquation(eq2, math);
236 }
237}
238
240 utils::Equation wholeEq,
241 const std::map<Equation, double>& variables) {
242 return evaluateEquation(splitIntoEquation(wholeEq, variables));
243}
245 std::map<utils::Equation, double> dumb;
246 return evaluateEquation(splitIntoEquation(wholeEq, dumb));
247}
Strings that are to be interpreted as equation that can be evaluated.
Definition Equation.h:17
static std::vector< Equation > prepareMathSymbols()
Prepare the mathematical symbols.
Definition Equation.cpp:14
Equation()
Construct Equation.
Definition Equation.cpp:28
static void replaceCste(std::vector< Equation > &eq)
Replace constants in the split equation by a number.
Definition Equation.cpp:130
static std::vector< Equation > splitIntoEquation(Equation wholeEq, const std::map< Equation, double > &variables)
Split equation into smaller parts, down to number or math symbols.
Definition Equation.cpp:37
static double evaluateEquation(std::vector< Equation > wholeEq)
Evaluate and return an equation.
Definition Equation.cpp:149
static void replaceVar(Equation &eq, const std::map< Equation, double > &variables)
Replace the varirables in the equation by their values.
Definition Equation.cpp:137
static void check(bool cond, const String &message)
Assert that raises the error message if false.
Definition Error.cpp:10
Wrapper around the std::string class with augmented functionality.
Definition String.h:18
static String tolower(const String &str)
Convert a string to a lower case string.
Definition String.cpp:76
String()
Construct string.
Definition String.cpp:13
static String to_string(double val)
Overload of the to_string C++11 function to allow for X-digits precision.
Definition String.cpp:92