Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
439 lines (402 loc) · 9.3 KB

File metadata and controls

439 lines (402 loc) · 9.3 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
436
437
438
439
package symjava.symbolic;
import java.math.BigInteger;
import java.util.List;
import symjava.logic.And;
import symjava.logic.Not;
import symjava.logic.Or;
import symjava.logic.Xor;
import symjava.relational.Ge;
import symjava.relational.Relation;
import symjava.symbolic.utils.Utils;
abstract public class Expr implements Cloneable {
/**
* Label(or name) of an expression(Symbol, Func,...)
*/
protected String label = null;
/**
* A string used to sort terms in an expression
*/
protected String sortKey = null;
/**
* Number of operations for simplifying an expression
*/
public int simplifyOpNum = 0;
public boolean isSimplified = false;
public abstract Expr simplify();
public abstract boolean symEquals(Expr other);
/**
* Return the arguments of the expr
* @return
*/
public Expr[] args() { return new Expr[0]; }
/**
* Derivative of expr
* @param expr
* @return
*/
public abstract Expr diff(Expr expr);
/**
* Functional derivative of f with respect to df
* @param f
* @param df
* @return
*/
public Expr fdiff(Expr f, Expr df) {
if(!(f instanceof Func) || !(df instanceof Func))
throw new IllegalArgumentException();
Func F = (Func)f;
Symbol alpha = new Symbol("_alpha_");
Expr ff = this.subs(f, f.add(alpha.multiply(df)));
Expr dff = ff.diff(alpha);
if(Symbol.C0.symEquals(dff)) {
Func ret = new Func("0", F.args);
ret.expr = Symbol.C0;
return ret;
}
return dff.subs(alpha, 0).simplify();
}
/**
* Split the terms into a list for Add and Subtract
* @param outList
*/
public void flattenAdd(List<Expr> outList) {
outList.add(this);
};
/**
* Split the terms into a list for Multiply and Divide
* @param outList
*/
public void flattenMultiply(List<Expr> outList) {
outList.add(this);
}
/**
* Return true if the expression is an abstract thing.
* for example, a abstract function
* @return
*/
public boolean isAbstract() {
return false;
}
/**
* Return the string expression
*/
public String toString() {
return label;
}
/**
* Set the label(or name) of the expression
* @param label
* @return
*/
public Expr setLabel(String label) {
this.label = label;
return this;
}
/**
* Return the label(or name) of the expression
* @return
*/
public String getLabel() {
return label;
}
/**
* Set a string key for arranging terms in an expression
* @param sortKey
* @return
*/
public Expr setSortKey(String sortKey) {
this.sortKey = sortKey;
return this;
}
/**
* Get the string key used to arrange terms in an expression
* @param sortKey
* @return
*/
public String getSortKey() {
return sortKey;
}
/**
* Count number of operations for simplification
* @return
*/
public int getSimplifyOps() {
return simplifyOpNum;
}
public Expr setSimplifyOps(int n) {
//Make no sense, call setAsSimplified() explicitly
//if(n > simplifyOpNum)
// isSimplified = true;
simplifyOpNum = n;
return this;
}
public Expr incSimplifyOps(int n) {
simplifyOpNum += n;
isSimplified = true;
return this;
}
public Expr setAsSimplified() {
isSimplified = true;
return this;
}
/**
* Operator overload support:
* Expr a = 5;
*
* @param v
* @return
*/
public static Expr valueOf(int v) {
return new SymInteger(v);
}
public static Expr valueOf(long v) {
return new SymLong(v);
}
public static Expr valueOf(float v) {
return new SymFloat(v);
}
public static Expr valueOf(double v) {
return new SymDouble(v);
}
/**
* Operator overload support:
* a+b
* @param other
* @return
*/
public Expr add(Expr other) {
return Add.simplifiedIns(this, other);
}
public Expr add(int other) {
return Add.simplifiedIns(this, new SymInteger(other));
}
public Expr addRev(int other) {
return Add.simplifiedIns(new SymInteger(other), this);
}
public Expr add(long other) {
return Add.simplifiedIns(this, new SymLong(other));
}
public Expr addRev(long other) {
return Add.simplifiedIns(new SymLong(other), this);
}
public Expr add(float other) {
return Add.simplifiedIns(this, new SymFloat(other));
}
public Expr addRev(float other) {
return Add.simplifiedIns(new SymFloat(other), this);
}
public Expr add(double other) {
return Add.simplifiedIns(this, new SymDouble(other));
}
public Expr addRev(double other) {
return Add.simplifiedIns(new SymDouble(other), this);
}
/**
* Operator overload support:
* a-b
* @param other
* @return
*/
public Expr subtract(Expr other) {
return Subtract.simplifiedIns(this, other);
}
public Expr subtract(int other) {
return Subtract.simplifiedIns(this, new SymInteger(other));
}
public Expr subtractRev(int other) {
return Subtract.simplifiedIns(new SymInteger(other), this);
}
public Expr subtract(long other) {
return Subtract.simplifiedIns(this, new SymLong(other));
}
public Expr subtractRev(long other) {
return Subtract.simplifiedIns(new SymLong(other), this);
}
public Expr subtract(float other) {
return Subtract.simplifiedIns(this, new SymFloat(other));
}
public Expr subtractRev(float other) {
return Subtract.simplifiedIns(new SymFloat(other), this);
}
public Expr subtract(double other) {
return Subtract.simplifiedIns(this, new SymDouble(other));
}
public Expr subtractRev(double other) {
return Subtract.simplifiedIns(new SymDouble(other), this);
}
/**
* Operator overload support:
* a*b
* @param other
* @return
*/
public Expr multiply(Expr other) {
return Multiply.simplifiedIns(this, other);
}
public Expr multiply(int other) {
return Multiply.simplifiedIns(this, new SymInteger(other));
}
public Expr multiplyRev(int other) {
return Multiply.simplifiedIns(new SymInteger(other), this);
}
public Expr multiply(long other) {
return Multiply.simplifiedIns(this, new SymLong(other));
}
public Expr multiplyRev(long other) {
return Multiply.simplifiedIns(new SymLong(other), this);
}
public Expr multiply(float other) {
return Multiply.simplifiedIns(this, new SymFloat(other));
}
public Expr multiplyRev(float other) {
return Multiply.simplifiedIns(new SymFloat(other), this);
}
public Expr multiply(double other) {
return Multiply.simplifiedIns(this, new SymDouble(other));
}
public Expr multiplyRev(double other) {
return Multiply.simplifiedIns(new SymDouble(other), this);
}
/**
* Operator overload support:
* a/b
* @param other
* @return
*/
public Expr divide(Expr other) {
return Divide.simplifiedIns(this, other);
}
public Expr divide(int other) {
return Divide.simplifiedIns(this, new SymInteger(other));
}
public Expr divideRev(int other) {
return Divide.simplifiedIns(new SymInteger(other), this);
}
public Expr divide(long other) {
return Divide.simplifiedIns(this, new SymLong(other));
}
public Expr divideRev(long other) {
return Divide.simplifiedIns(new SymLong(other), this);
}
public Expr divide(float other) {
return Divide.simplifiedIns(this, new SymFloat(other));
}
public Expr divideRev(float other) {
return Divide.simplifiedIns(new SymFloat(other), this);
}
public Expr divide(double other) {
return Divide.simplifiedIns(this, new SymDouble(other));
}
public Expr divideRev(double other) {
return Divide.simplifiedIns(new SymDouble(other), this);
}
/**
* Operator overload support:
* -a
*
*/
public Expr negate() {
if(this instanceof SymReal<?>) {
SymReal<?> dd = (SymReal<?>)this;
double dv = dd.getValue().doubleValue();
if(dv == 0)
return Symbol.C0;
return new SymDouble(-dv);
}
return Negate.simplifiedIns(this);
};
/**
* x%y
* @return
*/
public Expr remainder(Expr other) {
return new Remainder(this, other);
}
/*
* !x
*/
public Expr not() {
return Not.simplifiedIns(this);
}
/**
* x&y
* @param other
* @return
*/
public Expr and(Expr other) {
return And.simplifiedIns(this, other);
}
/**
* x|y
* @param other
* @return
*/
public Expr or(Expr other) {
return Or.simplifiedIns(this, other);
}
/**
* x^y
* @param other
* @return
*/
public Expr xor(Expr other) {
return Xor.simplifiedIns(this, other);
}
// /**
// * TODO We cannot use the comparison operator overload in java-oo for our use case
// * @param other
// * @return
// */
// public int compareTo(Expr other) {
// if(Ge.stackTop == null)
// Ge.stackTop = Ge.apply(this, other); //fix: use push()
// else
// Ge.stackTop = Ge.apply(Ge.stackTop, other);
// return -1;
// }
/**
* Substitution
*
* @param from
* @param to
* @return
*/
public Expr subs(Expr from, Expr to) {
if(Utils.symCompare(this, from)) {
return to;
}
return this;
}
public Expr subs(Expr from, int to) {
return subs(from, new SymInteger(to));
}
public Expr subs(Expr from, long to) {
return subs(from, new SymLong(to));
}
public Expr subs(Expr from, float to) {
return subs(from, new SymFloat(to));
}
public Expr subs(Expr from, double to) {
return subs(from, new SymDouble(to));
}
protected Expr clone() {
try {
return (Expr) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
/**
* If the labels of two expressions are the same they are considered as
* the same symbolic expressions
*/
@Override
public int hashCode() {
return this.label.hashCode();
}
@Override
public boolean equals(Object obj) {
return this.label.equals(((Expr)obj).label);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.