Repository navigation
Expand file tree
/
Copy pathformula_engine.cpp
More file actions
435 lines (406 loc) · 17.3 KB
/
Copy pathformula_engine.cpp
File metadata and controls
435 lines (406 loc) · 17.3 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include "formula_engine.hpp"
#include <array>
#include <cmath>
#include <cwctype>
#include <limits>
#include <string>
#include <vector>
namespace {
int formula_precedence(FormulaOp op) {
switch (op) {
case FormulaOp::Neg: return 3;
case FormulaOp::Mul:
case FormulaOp::Div: return 2;
case FormulaOp::Add:
case FormulaOp::Sub: return 1;
default: return 0;
}
}
bool formula_is_right_associative(FormulaOp op) {
return op == FormulaOp::Neg;
}
bool formula_is_function(FormulaOp op) {
return op == FormulaOp::FuncAbs || op == FormulaOp::FuncSqrt || op == FormulaOp::FuncSin ||
op == FormulaOp::FuncCos || op == FormulaOp::FuncTan || op == FormulaOp::FuncLog ||
op == FormulaOp::FuncExp;
}
bool formula_is_operator(FormulaOp op) {
return op == FormulaOp::Add || op == FormulaOp::Sub || op == FormulaOp::Mul ||
op == FormulaOp::Div || op == FormulaOp::Neg;
}
bool formula_function_from_name(const std::wstring& name, FormulaOp& out) {
if (name == L"abs") { out = FormulaOp::FuncAbs; return true; }
if (name == L"sqrt") { out = FormulaOp::FuncSqrt; return true; }
if (name == L"sin") { out = FormulaOp::FuncSin; return true; }
if (name == L"cos") { out = FormulaOp::FuncCos; return true; }
if (name == L"tan") { out = FormulaOp::FuncTan; return true; }
if (name == L"log") { out = FormulaOp::FuncLog; return true; }
if (name == L"exp") { out = FormulaOp::FuncExp; return true; }
return false;
}
bool push_error(std::wstring& error, bool english, const wchar_t* en, const wchar_t* ru) {
error = english ? en : ru;
return false;
}
} // namespace
std::wstring default_channel_formula_text() {
return L"x";
}
std::wstring normalize_formula_text(const std::wstring& text) {
std::wstring out;
out.reserve(text.size());
for (wchar_t ch : text) {
if (ch == L',') out.push_back(L'.');
else out.push_back(ch);
}
std::size_t first = 0;
while (first < out.size() && iswspace(out[first])) ++first;
std::size_t last = out.size();
while (last > first && iswspace(out[last - 1])) --last;
out = out.substr(first, last - first);
if (out.empty()) out = default_channel_formula_text();
return out;
}
bool compile_formula_rpn(const std::wstring& raw_text,
std::vector<FormulaToken>& out,
std::wstring& error,
bool english) {
const std::wstring text = normalize_formula_text(raw_text);
error.clear();
// Commit only a complete program; a rejected edit cannot leave partial RPN.
struct OutputGuard {
std::vector<FormulaToken>& out;
bool committed = false;
~OutputGuard() { if (!committed) out.clear(); }
} output_guard{out};
struct StackEntry {
FormulaOp op = FormulaOp::Add;
bool is_lparen = false;
};
out.clear();
std::vector<StackEntry> ops;
bool expect_value = true;
auto push_operator = [&](FormulaOp op) {
while (!ops.empty() && !ops.back().is_lparen && formula_is_operator(ops.back().op)) {
const int top_prec = formula_precedence(ops.back().op);
const int cur_prec = formula_precedence(op);
if (top_prec > cur_prec || (top_prec == cur_prec && !formula_is_right_associative(op))) {
out.push_back({ops.back().op, 0.0});
ops.pop_back();
} else {
break;
}
}
ops.push_back({op, false});
};
std::size_t i = 0;
while (i < text.size()) {
const wchar_t ch = text[i];
if (iswspace(ch)) { ++i; continue; }
if ((ch >= L'0' && ch <= L'9') || ch == L'.') {
if (!expect_value) return push_error(error, english, L"Missing operator.", L"Пропущен оператор.");
const wchar_t* begin = text.c_str() + i;
wchar_t* end = nullptr;
double value = wcstod(begin, &end);
if (begin == end || !std::isfinite(value)) {
return push_error(error, english, L"Invalid number in coefficient.", L"Некорректное число в коэффициенте.");
}
i = static_cast<std::size_t>(end - text.c_str());
out.push_back({FormulaOp::Number, value});
expect_value = false;
continue;
}
if (iswalpha(ch)) {
if (!expect_value) return push_error(error, english, L"Missing operator.", L"Пропущен оператор.");
std::size_t j = i;
while (j < text.size() && (iswalpha(text[j]) || iswdigit(text[j]) || text[j] == L'_')) ++j;
std::wstring name = text.substr(i, j - i);
for (wchar_t& c : name) c = static_cast<wchar_t>(towlower(c));
if (name == L"x") {
out.push_back({FormulaOp::Variable, 0.0});
expect_value = false;
} else {
FormulaOp fn = FormulaOp::FuncAbs;
if (!formula_function_from_name(name, fn)) {
error = (english ? L"Unknown function: " : L"Неизвестная функция: ") + name;
return false;
}
std::size_t next = j;
while (next < text.size() && iswspace(text[next])) ++next;
if (next == text.size() || text[next] != L'(')
return push_error(error, english, L"Function requires parentheses.", L"Для функции нужны скобки.");
ops.push_back({fn, false});
expect_value = true;
}
i = j;
continue;
}
if (ch == L'(') {
if (!expect_value) return push_error(error, english, L"Missing operator.", L"Пропущен оператор.");
ops.push_back({FormulaOp::Add, true});
++i;
expect_value = true;
continue;
}
if (ch == L')') {
if (expect_value) return push_error(error, english, L"Missing operand.", L"Пропущен аргумент.");
bool found_lparen = false;
while (!ops.empty()) {
if (ops.back().is_lparen) {
found_lparen = true;
ops.pop_back();
break;
}
out.push_back({ops.back().op, 0.0});
ops.pop_back();
}
if (!found_lparen) {
return push_error(error, english, L"Mismatched parentheses.", L"Несогласованные скобки.");
}
if (!ops.empty() && formula_is_function(ops.back().op)) {
out.push_back({ops.back().op, 0.0});
ops.pop_back();
}
++i;
expect_value = false;
continue;
}
FormulaOp op = FormulaOp::Add;
if (ch == L'+') op = FormulaOp::Add;
else if (ch == L'-') op = expect_value ? FormulaOp::Neg : FormulaOp::Sub;
else if (ch == L'*') op = FormulaOp::Mul;
else if (ch == L'/') op = FormulaOp::Div;
else {
error = (english ? L"Unsupported character in coefficient: " : L"Недопустимый символ в коэффициенте: ") + std::wstring(1, ch);
return false;
}
if (expect_value && op != FormulaOp::Neg) {
return push_error(error, english, L"Operator position is invalid.", L"Некорректное положение оператора.");
}
push_operator(op);
++i;
expect_value = true;
}
if (expect_value) {
return push_error(error, english, L"Incomplete coefficient.", L"Коэффициент не завершён.");
}
while (!ops.empty()) {
if (ops.back().is_lparen) {
return push_error(error, english, L"Mismatched parentheses.", L"Несогласованные скобки.");
}
out.push_back({ops.back().op, 0.0});
ops.pop_back();
}
std::size_t depth = 0;
for (const auto& token : out) {
if (token.op == FormulaOp::Number || token.op == FormulaOp::Variable) ++depth;
else {
const std::size_t arity = (token.op == FormulaOp::Neg || formula_is_function(token.op)) ? 1 : 2;
if (depth < arity) return push_error(error, english, L"Missing operand.", L"Пропущен аргумент.");
depth = depth - arity + 1;
}
}
if (depth != 1) return push_error(error, english, L"Invalid expression.", L"Некорректное выражение.");
output_guard.committed = true;
return true;
}
bool formula_rpn_is_identity(const std::vector<FormulaToken>& rpn) {
return rpn.size() == 1 && rpn[0].op == FormulaOp::Variable;
}
AffineFormulaInfo analyze_formula_rpn_affine(const std::vector<FormulaToken>& rpn) {
if (rpn.empty()) return {};
std::vector<AffineFormulaInfo> stack;
stack.reserve(rpn.size());
auto is_constant = [](const AffineFormulaInfo& value) {
return value.valid && value.mul == 0.0;
};
auto push = [&](AffineFormulaInfo value) -> bool {
if (!value.valid || !std::isfinite(value.mul) || !std::isfinite(value.add)) return false;
stack.push_back(value);
return true;
};
auto pop1 = [&]() -> AffineFormulaInfo {
if (stack.empty()) return {};
AffineFormulaInfo value = stack.back();
stack.pop_back();
return value;
};
auto pop2 = [&](AffineFormulaInfo& a, AffineFormulaInfo& b) -> bool {
if (stack.size() < 2) return false;
b = stack.back();
stack.pop_back();
a = stack.back();
stack.pop_back();
return true;
};
auto push_constant_fn = [&](const AffineFormulaInfo& operand, double (*fn)(double)) -> bool {
return is_constant(operand) && push({true, 0.0, fn(operand.add)});
};
for (const auto& token : rpn) {
switch (token.op) {
case FormulaOp::Number:
if (!push({true, 0.0, token.value})) return {};
break;
case FormulaOp::Variable:
if (!push({true, 1.0, 0.0})) return {};
break;
case FormulaOp::Add: {
AffineFormulaInfo a, b;
if (!pop2(a, b) || !push({a.valid && b.valid, a.mul + b.mul, a.add + b.add})) return {};
break;
}
case FormulaOp::Sub: {
AffineFormulaInfo a, b;
if (!pop2(a, b) || !push({a.valid && b.valid, a.mul - b.mul, a.add - b.add})) return {};
break;
}
case FormulaOp::Neg: {
AffineFormulaInfo v = pop1();
if (!push({v.valid, -v.mul, -v.add})) return {};
break;
}
case FormulaOp::Mul: {
AffineFormulaInfo a, b;
if (!pop2(a, b)) return {};
if (is_constant(a) && b.valid) {
if (!push({true, a.add * b.mul, a.add * b.add})) return {};
} else if (is_constant(b) && a.valid) {
if (!push({true, b.add * a.mul, b.add * a.add})) return {};
} else {
return {};
}
break;
}
case FormulaOp::Div: {
AffineFormulaInfo a, b;
if (!pop2(a, b) || !a.valid || !is_constant(b)) return {};
const double denom = b.add;
const double inv = (denom == 0.0) ? std::numeric_limits<double>::quiet_NaN() : (1.0 / denom);
if (!push({true, a.mul * inv, a.add * inv})) return {};
break;
}
case FormulaOp::FuncAbs: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) { return std::fabs(x); })) return {};
break;
}
case FormulaOp::FuncSqrt: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) {
return x < 0.0 ? std::numeric_limits<double>::quiet_NaN() : std::sqrt(x);
})) return {};
break;
}
case FormulaOp::FuncSin: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) { return std::sin(x); })) return {};
break;
}
case FormulaOp::FuncCos: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) { return std::cos(x); })) return {};
break;
}
case FormulaOp::FuncTan: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) { return std::tan(x); })) return {};
break;
}
case FormulaOp::FuncLog: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) {
return x <= 0.0 ? std::numeric_limits<double>::quiet_NaN() : std::log(x);
})) return {};
break;
}
case FormulaOp::FuncExp: {
AffineFormulaInfo v = pop1();
if (!push_constant_fn(v, [](double x) { return std::exp(x); })) return {};
break;
}
}
}
return stack.size() == 1 ? stack.back() : AffineFormulaInfo{};
}
double eval_formula_rpn(const std::vector<FormulaToken>& rpn, double x) {
if (rpn.empty()) return std::numeric_limits<double>::quiet_NaN();
if (formula_rpn_is_identity(rpn)) return x;
constexpr std::size_t kInlineStackCap = 32;
std::array<double, kInlineStackCap> inline_stack{};
std::vector<double> heap_stack;
double* stack = inline_stack.data();
std::size_t capacity = kInlineStackCap;
if (rpn.size() > kInlineStackCap) {
heap_stack.resize(rpn.size());
stack = heap_stack.data();
capacity = heap_stack.size();
}
std::size_t sp = 0;
auto push = [&](double v) -> bool {
if (sp >= capacity) return false;
stack[sp++] = v;
return true;
};
auto pop1 = [&]() -> double {
if (sp == 0) return std::numeric_limits<double>::quiet_NaN();
return stack[--sp];
};
auto pop2 = [&](double& a, double& b) -> bool {
if (sp < 2) return false;
b = stack[--sp];
a = stack[--sp];
return true;
};
for (const auto& token : rpn) {
switch (token.op) {
case FormulaOp::Number: if (!push(token.value)) return std::numeric_limits<double>::quiet_NaN(); break;
case FormulaOp::Variable: if (!push(x)) return std::numeric_limits<double>::quiet_NaN(); break;
case FormulaOp::Add: {
double a, b;
if (!pop2(a, b)) return std::numeric_limits<double>::quiet_NaN();
if (!push(a + b)) return std::numeric_limits<double>::quiet_NaN();
break;
}
case FormulaOp::Sub: {
double a, b;
if (!pop2(a, b)) return std::numeric_limits<double>::quiet_NaN();
if (!push(a - b)) return std::numeric_limits<double>::quiet_NaN();
break;
}
case FormulaOp::Mul: {
double a, b;
if (!pop2(a, b)) return std::numeric_limits<double>::quiet_NaN();
if (!push(a * b)) return std::numeric_limits<double>::quiet_NaN();
break;
}
case FormulaOp::Div: {
double a, b;
if (!pop2(a, b)) return std::numeric_limits<double>::quiet_NaN();
if (!push(b == 0.0 ? std::numeric_limits<double>::quiet_NaN() : (a / b))) {
return std::numeric_limits<double>::quiet_NaN();
}
break;
}
case FormulaOp::Neg: {
double v = pop1();
if (!std::isfinite(v) && !std::isnan(v)) return std::numeric_limits<double>::quiet_NaN();
if (!push(-v)) return std::numeric_limits<double>::quiet_NaN();
break;
}
case FormulaOp::FuncAbs: {
double v = pop1(); if (!push(std::fabs(v))) return std::numeric_limits<double>::quiet_NaN(); break;
}
case FormulaOp::FuncSqrt: {
double v = pop1(); if (!push(v < 0.0 ? std::numeric_limits<double>::quiet_NaN() : std::sqrt(v))) return std::numeric_limits<double>::quiet_NaN(); break;
}
case FormulaOp::FuncSin: { double v = pop1(); if (!push(std::sin(v))) return std::numeric_limits<double>::quiet_NaN(); break; }
case FormulaOp::FuncCos: { double v = pop1(); if (!push(std::cos(v))) return std::numeric_limits<double>::quiet_NaN(); break; }
case FormulaOp::FuncTan: { double v = pop1(); if (!push(std::tan(v))) return std::numeric_limits<double>::quiet_NaN(); break; }
case FormulaOp::FuncLog: {
double v = pop1(); if (!push(v <= 0.0 ? std::numeric_limits<double>::quiet_NaN() : std::log(v))) return std::numeric_limits<double>::quiet_NaN(); break;
}
case FormulaOp::FuncExp: { double v = pop1(); if (!push(std::exp(v))) return std::numeric_limits<double>::quiet_NaN(); break; }
}
}
return sp == 1 ? stack[0] : std::numeric_limits<double>::quiet_NaN();
}