Repository navigation
Expand file tree
/
Copy pathEvaluator.cpp
More file actions
330 lines (284 loc) · 12.7 KB
/
Copy pathEvaluator.cpp
File metadata and controls
330 lines (284 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <map>
#include "LiTeX.cpp"
typedef unsigned char ubyte;
typedef signed char sbyte;
typedef std::complex<double> cmplx;
const cmplx NaN(std::nan("0"), std::nan("0"));
inline bool isNan(cmplx z) { return z != z; }
sbyte sgn(double val) {
return (0 < val) - (val < 0);
}
inline std::string CmplxToStr(cmplx z, ubyte prec = 10) {
return (z.imag() == 0 || z.real() == 0)
? (std::stringstream() << std::setprecision(prec) << (z.imag() == 0 ? z.real() : z.imag()) << (z.imag() == 0 ? "" : "i")).str()
: ("(" + (std::stringstream() << std::setprecision(prec) << z.real()).str() + (sgn(z.imag()) >= 0 ? "+" : "") + (std::stringstream() << z.imag()).str() + "i)");
}
// PARSE COMPLEX ===================================================================== PARSE COMPLEX
cmplx ParseComplex(std::string str) {
double real = 0;
double imag = 0;
ubyte start = 0;
std::string reason = "";
PARSE_START:
ubyte stop = str.size() - (str[str.size() - 1] == ')' ? 1 : 0);
start += (str[0] == '(' && start == 0 ? 1 : 0);
for (ubyte i = start; i < stop; i++) {
if (i == start && str[i] == '-') continue;
// We found the end of a number! (Or the string ended (or it's just part of sci notation))
bool isEnd = i == stop - 1;
if (str[i] == '-' || str[i] == '+' || isEnd) {
if (str[i - 1] == 'e') {
continue; // It's fine guys. Don't worry about the exponent
}
if (str[i - (isEnd ? 0 : 1)] == 'i') {
if (imag != 0) {
reason = "Multiple imaginary parts found";
goto ERROR; // Woah, we already found imag part
}
imag = std::stod(str.substr(start, i - start + (isEnd ? 0 : -1)));
break; // Let's get out of here
}
else {
if (real != 0) {
reason = "Multiple real parts found";
goto ERROR; // Woah, we already found real part
}
real = std::stod(str.substr(start, i - start + (isEnd ? 1 : 0)));
start = i + (str[i] == '+' ? 1 : 0);
if (!(i == str.size() - 1))
goto PARSE_START; // Restart parsing cuz there still might be imag part
break;
}
}
if (!(isdigit(str[i]) || str[i] == 'e' || str[i] == '.')) {
reason = "Unrecongized character " + std::string(1, str[i]);
goto ERROR;
}
}
if (real != 0 && imag != 0 &&
(str[0] != '(' && str[str.size() - 1] != ')')
) {
reason = "Complex numbers must be surrounded by parenthesis";
goto ERROR;
}
return cmplx(real, imag);
ERROR:
std::cerr << " >>> COULD NOT PARSE '" << str << "' FOR COMPLEX (" << reason << ")" << std::endl;
return NaN;
}
// FIND COMPLEX ====================================================================== FIND COMPLEX
// Avoid slicing and checking with ParseComplex, which is slow
// Returns complex-parseable string.
std::string FindCmplx(std::string str, ubyte loc, bool before) {
ubyte minusCount = 0;
ubyte plusCount = 0;
for (ubyte i = loc + (before ? -1 : 1);
before ? (i >= 0) : (i < str.size());
i += (before ? -1 : 1)
) {
if (i == loc - 1 && before && str[i] == ')') {
// Found true complex, return contents
return "(" + Between(str, i, ')', '(', true) + ")";
}
else if (!before && i == loc + 1 && str[i] == '(')
return "(" + Between(str, i, '(', ')') + ")";
if (str[i] == '+') // Must be part of exp
if (str[i - 1] != 'e' || plusCount != 0)
goto ERROR;
else
plusCount++;
else if (str[i] == '-') { // Found minus, could be negative or e-...
if (!before) {
if (i == loc + 1) {
minusCount++;
}
else if (str[i - 1] != 'e' || minusCount > 1)
return str.substr(loc + 1, i - loc - 1);
}
else {
if (str[i - 1] == 'e' && minusCount == 0)
minusCount++; // Increase minus count
else if (minusCount == 1) // Negative number
return str.substr(i, loc - i);
else
goto ERROR;
}
}
else if (str[i] == 'e' && (!(str[i + 1] == '+' || str[i + 1] == '-') || !isdigit(str[i + 2])))
goto ERROR;
// Encountered unidentified character, must be end of number
else if (!(isdigit(str[i]) || str[i] == 'e' || str[i] == '.' || (str[i] == 'i' && before && i == loc - 1)) || i == (before ? 0 : str.size() - 1))
return before ? str.substr(i, loc - i) : str.substr(loc + 1, i - loc);
}
ERROR:
std::cerr << " >>> COULD NOT FIND COMPLEX" << std::endl;
return "";
}
// ADD IMP MULT ====================================================================== ADD IMP MULT
void AddImpMultTo(std::string& eq) {
for (ubyte i = 1; i < eq.size() - 1; i++) {
if ((isdigit(eq[i]) && eq[i + 1] == '\\') || // 5\pi
(isdigit(eq[i]) && eq[i + 1] == '(') || // 2(3...
(isdigit(eq[i]) && isalpha(eq[i + 1])
&& eq[i + 1] != 'e' && eq[i + 1] != 'i') || // 9sin... but not 3e+... or 3i...
(isalpha(eq[i]) && isdigit(eq[i + 1])) || // \pi3...
(isalpha(eq[i]) && eq[i + 1] == '\\') || // \pi\e...
(eq[i] == ')' && eq[i + 1] == '\\') || // ...4)\pi
(eq[i] == ')' && eq[i + 1] == '(') || // ...3)(6...
(eq[i] == ')' && isdigit(eq[i + 1])) || // ...)7
(eq[i] == ')' && isalpha(eq[i + 1])) || // ...)x...
(eq[i] == '}' && isdigit(eq[i + 1]))) // ...2}7
eq.insert(i + 1, 1, '*');
}
}
const std::unordered_map<std::string, ubyte> trigMap = {
{ "sin", 1 },
{ "cos", 2 },
{ "tan", 3 },
{ "sec", 4 },
{ "csc", 5 },
{ "cot", 6 },
{ "sin^{-1}", 7 },
{ "cos^{-1}", 8 },
{ "tan^{-1}", 9 },
};
auto operateTrig = [](std::string func, cmplx n) {
switch (trigMap.at(func)) {
case 1: return sin(n);
case 2: return cos(n);
case 3: return tan(n);
case 4: return cmplx(1,0) / sin(n);
case 5: return cmplx(1,0) / cos(n);
case 6: return cmplx(1,0) / tan(n);
case 7: return asin(n);
case 8: return acos(n);
case 9: return atan(n);
}
return NaN;
};
auto operate = [](ubyte op, cmplx a, cmplx b) {
switch (op) {
case 0: return a / b;
case 1: return a * b;
case 2: return a - b;
case 3: return a + b;
}
return NaN;
};
// FIND TRIGS ======================================================================== FIND TRIGS
std::tuple<std::string, ubyte> FindTrigs(std::string eq, std::array<std::string, 9> trigs) {
for (ubyte i = 0; i < size(eq); i++) {
for (ubyte n = 0; n < trigs.size(); n++) {
if (eq.substr(i, trigs[n].size()) == trigs[n])
return std::tuple<std::string, ubyte>(trigs[n], i);
}
}
std::cerr << " >>> COULD NOT FIND TRIG IN EQ: '" << eq << "'" << std::endl;
return std::tuple<std::string, ubyte>("", 0xFF);
}
// EVALUATE ========================================================================== EVALUATE
cmplx Evaluate(std::string eq) {
std::string reason = "";
{ // Scope so goto ERROR will not bypass variable initialization
// SETUP EQUATION ==================================================== SETUP EQUATION
{ cmplx test; // Is eq a number? (Scope so test is del)
std::cout << "Testing whether eq is complex..." << std::endl;
if (!isNan(test = ParseComplex(eq))) return test; }
AddImpMultTo(eq);
// ` is placeholder character for renderer.
if (eq.find('`') != std::string::npos) { reason = "Equation has placeholders"; goto ERROR; }
/* TRIGONOMETRIC FUNCTIONS */ {
std::cout << "Finding trig functions..." << std::endl;
std::tuple<std::string, ubyte> loc;
while((loc = FindTrigs(eq, std::array<std::string, 9>({ "sin", "cos", "tan", "sec", "csc", "cot", "sin^{-1}", "cos^{-1}", "tan^{-1}" })))
!= std::tuple<std::string, ubyte>("", 0xFF)
) {
std::cout << "Found trig function " << std::get<0>(loc) << std::endl;
std::string contents = Between(eq, std::get<1>(loc) + std::get<0>(loc).size(), '(', ')');
eq.erase(std::get<1>(loc), std::get<0>(loc).size() + contents.size() + 2);
eq.insert(std::get<1>(loc), CmplxToStr(operateTrig(std::get<0>(loc), Evaluate(contents))));
}
} // Scope so loc is freed. (I want to change type of loc from tuple -> size_t)
// LOGARITHMS
size_t loc = -1;
while((loc = eq.find('log')) != std::string::npos) {
std::string base = Between(eq, loc + 4, '{', '}');
std::string contents = Between(eq, loc + base.size() + 6, '(', ')');
eq.erase(loc, base.size() + contents.size() + 8);
eq.insert(loc, CmplxToStr(log(Evaluate(contents)) / log(Evaluate(base))));
// Use change-of-base formula (inefficient, but too lazy to write my own variable base thing)
}
// PARENTHESIS
loc = -1;
while ((loc = eq.find('(', loc + 1)) != std::string::npos) {
std::string contents = Between(eq, loc, '(', ')');
std::cout << "Found parenthesis with contents: " << contents << std::endl;
cmplx toRepl = ParseComplex("(" + contents + ")");
if (isNan(toRepl)) toRepl = Evaluate(contents);
eq.erase(loc, contents.size() + 2);
eq.insert(loc, CmplxToStr(toRepl));
}
// RADICALS
loc = -1;
while ((loc = eq.find("\\sqrt", loc + 1)) != std::string::npos) {
std::string nth = Between(eq, loc + 5, '{', '}');
std::string root = Between(eq, loc + 7 + nth.size(), '{', '}');
eq.erase(loc, nth.size() + 7 + root.size());
eq.insert(loc, CmplxToStr(pow(Evaluate(root), cmplx(1, 0) / Evaluate(nth))));
}
// FRACTIONS
loc = -1;
while ((loc = eq.find("\\frac", loc + 1)) != std::string::npos) {
std::string numer = Between(eq, loc + 5, '{', '}');
std::string denom = Between(eq, loc + 7 + numer.size(), '{', '}');
eq.erase(loc, numer.size() + 7 + denom.size());
eq.insert(loc, CmplxToStr(Evaluate(numer) / Evaluate(denom)));
}
// EXPONENTS
loc = -1;
while ((loc = eq.find('^', loc + 1)) != std::string::npos) {
std::string exp = Between(eq, loc + 1, '{', '}');
std::string base = FindCmplx(eq, loc, true);
std::cout << "Found exponent with base " << base << " and exponent " << exp << std::endl;
eq.erase(loc - base.size(), base.size() + 3 + exp.size());
eq.insert(loc - base.size(),
CmplxToStr(std::pow(ParseComplex(base), Evaluate(exp))));
}
// OPERATORS ========================================================= OPERATORS
std::cout << "Finding operators..." << std::endl;
ubyte curOp = 0;
size_t progress = 0;
bool DNC = false;
const char ops[] = { '/', '*', '-', '+' };
while (DNC || isNan(ParseComplex(eq))) {
if ((progress = eq.find(ops[curOp]), progress) == std::string::npos) {
curOp++;
progress = 0;
DNC = true;
continue;
}
DNC = false;
std::string first = FindCmplx(eq, progress, true);
std::string second = FindCmplx(eq, progress, false);
std::cout << "Found operator " << ops[curOp] << " with first " << first << " and second " << second << std::endl;
std::cout << "Old eq: " << eq;
eq.erase(progress - first.size(), first.size() + 1 + second.size());
std::cout << "; Replacing operation, new eq: '" << eq << '\'' << std::endl;
std::string toReplace = CmplxToStr(operate(curOp, ParseComplex(first), ParseComplex(second)));
std::cout << "Result of operation: " << CmplxToStr(operate(curOp, ParseComplex(first), ParseComplex(second))) << std::endl;
eq.insert(progress - first.size(), toReplace);
progress += (toReplace.size() - first.size()) + 1;
}
return ParseComplex(eq);
} // End error scope (Not evaluate)
ERROR:
std::cerr << " >>> COULD NOT EVALUATE EQ'" << eq << "' (" << reason << ")" << std::endl;
return NaN;
} // End evaluate