6663b6c9
adorian
projet complet av...
|
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
|
#include <poincare/expression.h>
#include <poincare/preferences.h>
#include <poincare/symbol.h>
#include <poincare/dynamic_hierarchy.h>
#include <poincare/static_hierarchy.h>
#include <poincare/list_data.h>
#include <poincare/matrix_data.h>
#include <poincare/undefined.h>
#include <poincare/simplification_root.h>
#include <poincare/rational.h>
#include <poincare/matrix.h>
#include <poincare/complex.h>
#include <cmath>
#include "expression_parser.hpp"
#include "expression_lexer.hpp"
int poincare_expression_yyparse(Poincare::Expression ** expressionOutput);
namespace Poincare {
#include <stdio.h>
/* Constructor & Destructor */
Expression * Expression::parse(char const * string) {
if (string[0] == 0) {
return nullptr;
}
YY_BUFFER_STATE buf = poincare_expression_yy_scan_string(string);
Expression * expression = 0;
if (poincare_expression_yyparse(&expression) != 0) {
// Parsing failed because of invalid input or memory exhaustion
if (expression != nullptr) {
delete expression;
expression = nullptr;
}
}
poincare_expression_yy_delete_buffer(buf);
return expression;
}
void Expression::ReplaceSymbolWithExpression(Expression ** expressionAddress, char symbol, Expression * expression) {
SimplificationRoot root(*expressionAddress);
root.editableOperand(0)->replaceSymbolWithExpression(symbol, expression);
*expressionAddress = root.editableOperand(0);
}
Expression * Expression::replaceSymbolWithExpression(char symbol, Expression * expression) {
for (int i = 0; i < numberOfOperands(); i++) {
editableOperand(i)->replaceSymbolWithExpression(symbol, expression);
}
return this;
}
/* Circuit breaker */
static Expression::CircuitBreaker sCircuitBreaker = nullptr;
static bool sSimplificationHasBeenInterrupted = false;
void Expression::setCircuitBreaker(CircuitBreaker cb) {
sCircuitBreaker = cb;
}
bool Expression::shouldStopProcessing() {
if (sCircuitBreaker == nullptr) {
return false;
}
if (sCircuitBreaker()) {
sSimplificationHasBeenInterrupted = true;
return true;
}
return false;
}
/* Hierarchy */
const Expression * Expression::operand(int i) const {
assert(i >= 0);
assert(i < numberOfOperands());
assert(operands()[i]->parent() == nullptr || operands()[i]->parent() == this);
return operands()[i];
}
Expression * Expression::replaceWith(Expression * newOperand, bool deleteAfterReplace) {
assert(m_parent != nullptr);
m_parent->replaceOperand(this, newOperand, deleteAfterReplace);
return newOperand;
}
void Expression::replaceOperand(const Expression * oldOperand, Expression * newOperand, bool deleteOldOperand) {
assert(newOperand != nullptr);
// Caution: handle the case where we replace an operand with a descendant of ours.
if (newOperand->hasAncestor(this)) {
newOperand->parent()->detachOperand(newOperand);
}
Expression ** op = const_cast<Expression **>(operands());
for (int i=0; i<numberOfOperands(); i++) {
if (op[i] == oldOperand) {
if (oldOperand != nullptr && oldOperand->parent() == this) {
const_cast<Expression *>(oldOperand)->setParent(nullptr);
}
if (deleteOldOperand) {
delete oldOperand;
}
if (newOperand != nullptr) {
const_cast<Expression *>(newOperand)->setParent(this);
}
op[i] = newOperand;
break;
}
}
}
void Expression::detachOperand(const Expression * e) {
Expression ** op = const_cast<Expression **>(operands());
for (int i=0; i<numberOfOperands(); i++) {
if (op[i] == e) {
detachOperandAtIndex(i);
}
}
}
void Expression::detachOperands() {
for (int i=0; i<numberOfOperands(); i++) {
detachOperandAtIndex(i);
}
}
void Expression::detachOperandAtIndex(int i) {
Expression ** op = const_cast<Expression **>(operands());
// When detachOperands is called, it's very likely that said operands have been stolen
if (op[i] != nullptr && op[i]->parent() == this) {
const_cast<Expression *>(op[i])->setParent(nullptr);
}
op[i] = nullptr;
}
void Expression::swapOperands(int i, int j) {
assert(i >= 0 && i < numberOfOperands());
assert(j >= 0 && j < numberOfOperands());
Expression ** op = const_cast<Expression **>(operands());
Expression * temp = op[i];
op[i] = op[j];
op[j] = temp;
}
bool Expression::hasAncestor(const Expression * e) const {
assert(m_parent != this);
if (m_parent == e) {
return true;
}
if (m_parent == nullptr) {
return false;
}
return m_parent->hasAncestor(e);
}
/* Properties */
bool Expression::recursivelyMatches(ExpressionTest test, Context & context) const {
if (test(this, context)) {
return true;
}
for (int i = 0; i < numberOfOperands(); i++) {
if (operand(i)->recursivelyMatches(test, context)) {
return true;
}
}
return false;
}
bool Expression::isApproximate(Context & context) const {
return recursivelyMatches([](const Expression * e, Context & context) {
return e->type() == Expression::Type::Decimal || e->type() == Expression::Type::Complex || Expression::IsMatrix(e, context) || (e->type() == Expression::Type::Symbol && static_cast<const Symbol *>(e)->isApproximate(context));
}, context);
}
bool Expression::IsMatrix(const Expression * e, Context & context) {
return e->type() == Type::Matrix || e->type() == Type::ConfidenceInterval || e->type() == Type::MatrixDimension || e->type() == Type::PredictionInterval || e->type() == Type::MatrixInverse || e->type() == Type::MatrixTranspose || (e->type() == Type::Symbol && static_cast<const Symbol *>(e)->isMatrixSymbol());
}
bool Expression::isOfType(Type * types, int length) const {
for (int i = 0; i < length; i++) {
if (type() == types[i]) {
return true;
}
}
return false;
}
bool Expression::needParenthesisWithParent(const Expression * e) const {
return false;
}
/* Comparison */
int Expression::SimplificationOrder(const Expression * e1, const Expression * e2, bool canBeInterrupted) {
if (e1->type() > e2->type()) {
if (canBeInterrupted && shouldStopProcessing()) {
return 1;
}
return -(e2->simplificationOrderGreaterType(e1, canBeInterrupted));
} else if (e1->type() == e2->type()) {
return e1->simplificationOrderSameType(e2, canBeInterrupted);
} else {
if (canBeInterrupted && shouldStopProcessing()) {
return -1;
}
return e1->simplificationOrderGreaterType(e2, canBeInterrupted);
}
}
/* Layout */
ExpressionLayout * Expression::createLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
switch (floatDisplayMode) {
case FloatDisplayMode::Default:
switch (complexFormat) {
case ComplexFormat::Default:
return privateCreateLayout(Preferences::sharedPreferences()->displayMode(), Preferences::sharedPreferences()->complexFormat());
default:
return privateCreateLayout(Preferences::sharedPreferences()->displayMode(), complexFormat);
}
default:
switch (complexFormat) {
case ComplexFormat::Default:
return privateCreateLayout(floatDisplayMode, Preferences::sharedPreferences()->complexFormat());
default:
return privateCreateLayout(floatDisplayMode, complexFormat);
}
}
}
/* Simplification */
Expression * Expression::ParseAndSimplify(const char * text, Context & context, AngleUnit angleUnit) {
Expression * exp = parse(text);
if (exp == nullptr) {
return new Undefined();
}
Simplify(&exp, context, angleUnit);
if (exp == nullptr) {
return parse(text);
}
return exp;
}
void Expression::Simplify(Expression ** expressionAddress, Context & context, AngleUnit angleUnit) {
sSimplificationHasBeenInterrupted = false;
if (angleUnit == AngleUnit::Default) {
angleUnit = Preferences::sharedPreferences()->angleUnit();
}
#if MATRIX_EXACT_REDUCING
#else
if ((*expressionAddress)->recursivelyMatches(IsMatrix, context)) {
return;
}
#endif
SimplificationRoot root(*expressionAddress);
root.editableOperand(0)->deepReduce(context, angleUnit);
root.editableOperand(0)->deepBeautify(context, angleUnit);
*expressionAddress = root.editableOperand(0);
if (sSimplificationHasBeenInterrupted) {
root.detachOperands();
delete *expressionAddress;
*expressionAddress = nullptr;
}
}
void Expression::Reduce(Expression ** expressionAddress, Context & context, AngleUnit angleUnit, bool recursively) {
SimplificationRoot root(*expressionAddress);
if (recursively) {
root.editableOperand(0)->deepReduce(context, angleUnit);
} else {
root.editableOperand(0)->shallowReduce(context,angleUnit);
}
*expressionAddress = root.editableOperand(0);
}
Expression * Expression::deepReduce(Context & context, AngleUnit angleUnit) {
assert(parent() != nullptr);
for (int i = 0; i < numberOfOperands(); i++) {
editableOperand(i)->deepReduce(context, angleUnit);
}
return shallowReduce(context, angleUnit);
}
Expression * Expression::shallowReduce(Context & context, AngleUnit angleUnit) {
for (int i = 0; i < numberOfOperands(); i++) {
if (editableOperand(i)->type() == Type::Undefined && this->type() != Type::SimplificationRoot) {
return replaceWith(new Undefined(), true);
}
}
return this;
}
Expression * Expression::deepBeautify(Context & context, AngleUnit angleUnit) {
assert(parent() != nullptr);
Expression * e = shallowBeautify(context, angleUnit);
for (int i = 0; i < e->numberOfOperands(); i++) {
e->editableOperand(i)->deepBeautify(context, angleUnit);
}
return e;
}
/* Evaluation */
template<typename T> Expression * Expression::approximate(Context& context, AngleUnit angleUnit) const {
switch (angleUnit) {
case AngleUnit::Default:
return privateApproximate(T(), context, Preferences::sharedPreferences()->angleUnit());
default:
return privateApproximate(T(), context, angleUnit);
}
}
template<typename T> T Expression::approximateToScalar(Context& context, AngleUnit angleUnit) const {
Expression * evaluation = approximate<T>(context, angleUnit);
assert(evaluation->type() == Type::Complex || evaluation->type() == Type::Matrix);
T result = NAN;
if (evaluation->type() == Type::Complex) {
result = static_cast<const Complex<T> *>(evaluation)->toScalar();
}
/*if (evaluation->type() == Type::Matrix) {
if (numberOfOperands() == 1) {
result = static_cast<const Complex<T> *>(operand(0))->toScalar();
}
}*/
delete evaluation;
return result;
}
template<typename T> T Expression::approximateToScalar(const char * text, Context& context, AngleUnit angleUnit) {
Expression * exp = ParseAndSimplify(text, context, angleUnit);
T result = exp->approximateToScalar<T>(context, angleUnit);
delete exp;
return result;
}
template<typename T> T Expression::epsilon() {
static T epsilon = sizeof(T) == sizeof(double) ? 1E-15 : 1E-7f;
return epsilon;
}
}
template Poincare::Expression * Poincare::Expression::approximate<double>(Context& context, AngleUnit angleUnit) const;
template Poincare::Expression * Poincare::Expression::approximate<float>(Context& context, AngleUnit angleUnit) const;
template double Poincare::Expression::approximateToScalar<double>(char const*, Poincare::Context&, Poincare::Expression::AngleUnit);
template float Poincare::Expression::approximateToScalar<float>(char const*, Poincare::Context&, Poincare::Expression::AngleUnit);
template double Poincare::Expression::approximateToScalar<double>(Poincare::Context&, Poincare::Expression::AngleUnit) const;
template float Poincare::Expression::approximateToScalar<float>(Poincare::Context&, Poincare::Expression::AngleUnit) const;
template double Poincare::Expression::epsilon<double>();
template float Poincare::Expression::epsilon<float>();
|