-
Notifications
You must be signed in to change notification settings - Fork 1
/
ginacmathengine.cpp
executable file
·334 lines (260 loc) · 8.7 KB
/
ginacmathengine.cpp
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
#include "ginacmathengine.h"
#include <stdexcept>
#include <QDebug>
#include <QList>
using namespace std;
using namespace GiNaC;
SymbolicMathFunction::SymbolicMathFunction(bool undo, qint16 operands, int operandsType, MathFunction func)
{
m_undo = undo;
m_operands = operands;
m_operandsType = operandsType;
m_func = func;
}
GiNacMathEngine::GiNacMathEngine()
{
funcMap.insert("+", new SymbolicMathFunction(true, 2, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(getOp_ex(ops,1) + getOp_ex(ops,0)); } ));
funcMap.insert("-", new SymbolicMathFunction(true, 2, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(getOp_ex(ops,1) - getOp_ex(ops,0)); } ));
funcMap.insert("mul", new SymbolicMathFunction(true, 2, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(getOp_ex(ops,1) * getOp_ex(ops,0)); } ));
funcMap.insert("div", new SymbolicMathFunction(true, 2, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(getOp_ex(ops,1) / getOp_ex(ops,0)); } ));
funcMap.insert("^", new SymbolicMathFunction(true, 2, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::pow(getOp_ex(ops,1),getOp_ex(ops,0))); } ));
funcMap.insert("10^x", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::pow(10,getOp_ex(ops,0))); } ));
funcMap.insert("x^2", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::pow(getOp_ex(ops,0),2)); } ));
funcMap.insert("neg", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(getOp_ex(ops,0) * -1); } ));
funcMap.insert("%", new SymbolicMathFunction(true, 2, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(getOp_ex(ops,0) * getOp_ex(ops,1) / 100); } ));
funcMap.insert("inv", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(1 / getOp_ex(ops,0)); } ));
funcMap.insert("sqrt", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(1 / getOp_ex(ops,0)); } ));
//funcMap.insert("log", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::log(getOp_ex(ops,0)) / GiNaC::EulerEvalf()); } ));
funcMap.insert("ln", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::log(getOp_ex(ops,0))); } ));
funcMap.insert("e^x", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::exp(getOp_ex(ops,0))); } ));
funcMap.insert("factorial", new SymbolicMathFunction(true, 1, 0, [](QList<SymbolicExpr *> ops) { return (SymbolicExpr *) new GiNacExpr(GiNaC::factorial(getOp_ex(ops,0))); } ));
// non lambda function
funcMap.insert("addall", new SymbolicMathFunction(true, -1, 0, addall));
funcMap.insert("suball", new SymbolicMathFunction(true, -1, 0, suball));
/*
Backend.engineFunction("=", lambda op: sympy.N(op), operands=1)
Backend.engineFunction("simplify", lambda op: sympy.simplify(op), operands=1)
Backend.engineFunction("nthroot", lambda op: sympy.root(op[0], op[1]))
Backend.engineFunction("ln", lambda op: sympy.log(op), operands=1)
*/
}
SymbolicExpr * GiNacMathEngine::strToExpr(QString s, bool rationalMode)
{
//TODO: rational mode
parser reader;
SymbolicExpr * expr;
try{
ex e = reader(s.toStdString().c_str());
expr = new GiNacExpr(e);
return expr;
}catch(exception &p){
qDebug() << p.what();
expr = new SymbolicExpr();
return expr;
}
}
bool GiNacMathEngine::operandValid(QString o)
{
if(!o.isEmpty()){
parser reader;
try{
ex e = reader(o.toStdString().c_str());
}catch(exception &p){
qDebug() << p.what();
return false;
}
return true;
}else{
return true;
}
}
SymbolicExpr * GiNacMathEngine::addSymbol(QString e)
{
GiNaC::symbol s(e.toStdString().c_str());
GiNaC::ex expr_ex = s;
GiNacExpr * expr = new GiNacExpr(expr_ex);
return expr;
}
SymbolicExpr *GiNacMathEngine::constants(QString e)
{
//TODO
}
GiNacExpr *GiNacMathEngine::getOp(QList<SymbolicExpr *> ops, uint16 idx)
{
GiNacExpr * expr = (GiNacExpr *) ops.at(idx);
return expr;
}
GiNaC::ex GiNacMathEngine::getOp_ex(QList<SymbolicExpr *> ops, uint16 idx)
{
GiNacExpr * expr = (GiNacExpr *) ops.at(idx);
GiNaC::ex expr_ex = expr->getExpr();
return expr_ex;
}
SymbolicMathFunction * GiNacMathEngine::getFunction(QString key)
{
if(funcMap.contains(key)){
return funcMap[key];
}else{
return NULL;
}
}
SymbolicMathEngine::Features GiNacMathEngine::features()
{
Features f = NoFeatures;
f |= StringConversionFeature;
f |= RationalFeature;
f |= SymbolicFeature;
return f;
}
SymbolicExpr *GiNacMathEngine::addall(QList<SymbolicExpr *> ops)
{
GiNaC::ex e = getOp_ex(ops, ops.length() - 1);
for(int i=ops.length()-2; i >= 0; i--){
e = e + getOp_ex(ops, i);
}
return new GiNacExpr(e);
}
SymbolicExpr *GiNacMathEngine::suball(QList<SymbolicExpr *> ops)
{
GiNaC::ex e = getOp_ex(ops, ops.length() - 1);
for(int i=ops.length()-2; i >= 0; i--){
e = e - getOp_ex(ops, i);
}
return new GiNacExpr(e);
}
/*
@Backend.engineFunction(operands=1)
def cos(op):
op = convertToRadians(op)
return sympy.cos(op)
@Backend.engineFunction(operands=1)
def acos(op):
expr = sympy.acos(op)
return convertFromRadians(expr)
@Backend.engineFunction(operands=1)
def sin(op):
op = convertToRadians(op)
return sympy.sin(op)
@Backend.engineFunction(operands=1)
def asin(op):
expr = sympy.asin(op)
return convertFromRadians(expr)
@Backend.engineFunction(operands=1)
def tan(op):
op = convertToRadians(op)
return sympy.tan(op)
@Backend.engineFunction(operands=1)
def atan(op):
expr = sympy.atan(op)
return convertFromRadians(expr)
@Backend.engineFunction(operands=0)
def d4(op):
expr = functions.d4(1)
return expr
@Backend.engineFunction(operands=0)
def d6(op):
expr = functions.d6(1)
return expr
@Backend.engineFunction(operands=0)
def d8(op):
expr = functions.d8(1)
return expr
@Backend.engineFunction(operands=0)
def d10(op):
expr = functions.d10(1)
return expr
@Backend.engineFunction(operands=0)
def d12(op):
expr = functions.d12(1)
return expr
@Backend.engineFunction(operands=0)
def d20(op):
expr = functions.d20(1)
return expr
@Backend.engineFunction(operands=0)
def d100(op):
expr = functions.d100(1)
return expr
def convertToRadians(expr):
newExpr = None
if trigUnit == TrigUnit.Degrees:
newExpr = sympy.rad(expr)
elif trigUnit == TrigUnit.Gradients:
newExpr = expr * sympy.pi / 200
else:
newExpr = expr
return newExpr
def convertFromRadians(expr):
newExpr = None
if trigUnit == TrigUnit.Degrees:
newExpr = sympy.deg(expr)
elif trigUnit == TrigUnit.Gradients:
newExpr = expr * 200 / sympy.pi
else:
newExpr = expr
return newExpr
*/
/*
@Bitwise.engineFunction
def _and(op):
res = int(op[0]) & int(op[1])
return sympy.S(res)
@Bitwise.engineFunction
def _nand(op):
res = int(op[0]) & int(op[1])
res = ~res
return sympy.S(res)
@Bitwise.engineFunction
def _or(op):
res = int(op[0]) | int(op[1])
return sympy.S(res)
@Bitwise.engineFunction
def _nor(op):
res = int(op[0]) | int(op[1])
res = ~res
return sympy.S(res)
@Bitwise.engineFunction
def _and(op):
res = int(op[0]) & int(op[1])
return sympy.S(res)
@Bitwise.engineFunction
def _nand(op):
res = int(op[0]) & int(op[1])
res = ~res
return sympy.S(res)
@Bitwise.engineFunction
def _xor(op):
res = int(op[0]) ^ int(op[1])
return sympy.S(res)
@Bitwise.engineFunction
def _xnor(op):
res = int(op[0]) ^ int(op[1])
res = ~res
return sympy.S(res)
@Bitwise.engineFunction
def _shl(op):
res = int(op[0]) << int(op[1])
return sympy.S(res)
@Bitwise.engineFunction
def _shr(op):
res = int(op[0]) >> int(op[1])
return sympy.S(res)
@Bitwise.engineFunction(operands=1)
def _not(op):
res = int(op)
res = ~res
return sympy.S(res)
@Bitwise.engineFunction(operands=1)
def _2cmp(op):
res = int(op)
res = ~res + 1
return sympy.S(res)
@Bitwise.engineFunction(operands=1)
def u8bit(op):
res = int(op) & 0xff
return sympy.S(res)
@Bitwise.engineFunction(operands=1)
def u16bit(op):
res = int(op) & 0xffff
return sympy.S(res)
*/