forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpr.java
More file actions
645 lines (589 loc) · 14.7 KB
/
Expr.java
File metadata and controls
645 lines (589 loc) · 14.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
package symjava.symbolic;
import java.util.List;
import java.util.Map;
import lambdacloud.core.lang.LCAssign;
import lambdacloud.core.lang.LCDevice;
import symjava.logic.And;
import symjava.logic.Not;
import symjava.logic.Or;
import symjava.logic.Xor;
import symjava.symbolic.utils.Utils;
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
import com.sun.org.apache.bcel.internal.generic.InstructionFactory;
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
import com.sun.org.apache.bcel.internal.generic.InstructionList;
import com.sun.org.apache.bcel.internal.generic.MethodGen;
abstract public class Expr implements Cloneable {
/**
* Label(or name) of an expression
*/
protected String label = null;
/**
* A string used to sort the terms in an expression
*/
protected String sortKey = null;
/**
* Number of operations for simplifying an expression
*/
public int simplifyOpNum = 0;
/**
* Return true if simplify() is called
*/
public boolean isSimplified = false;
/**
* Simplify the expression
* @return
*/
public abstract Expr simplify();
/**
* Return true if two expressions are equal in the sense of mathematics
* @param other
* @return
*/
public abstract boolean symEquals(Expr other);
/**
* Return the arguments of the expression
* @return
*/
public abstract Expr[] args();// { return new Expr[0]; }
/**
* Derivative of the expression with respect to x
* @param x
* @return
*/
public abstract Expr diff(Expr x);
/**
* 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 representation of the expression
*/
public String toString() {
return label;
}
/**
* Return the LaTex representation of the expression
* @return
*/
public String toLaTex() {
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 sorting the terms in the expression
* @param sortKey
* @return
*/
public Expr setSortKey(String sortKey) {
this.sortKey = sortKey;
return this;
}
/**
* Get the string key used to sort the terms in the 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 overloading 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 overloading 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 for Groovy:
* a+b
* @param other
* @return
*/
public Expr plus(Expr other) {
return Add.simplifiedIns(this, other);
}
public Expr plus(int other) {
return Add.simplifiedIns(this, new SymInteger(other));
}
public Expr plus(long other) {
return Add.simplifiedIns(this, new SymLong(other));
}
public Expr plus(float other) {
return Add.simplifiedIns(this, new SymFloat(other));
}
public Expr plus(double other) {
return Add.simplifiedIns(this, new SymDouble(other));
}
/**
* Operator overloading 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 for Groovy:
* a-b
* @param other
* @return
*/
public Expr minus(Expr other) {
return Subtract.simplifiedIns(this, other);
}
public Expr minus(int other) {
return Subtract.simplifiedIns(this, new SymInteger(other));
}
public Expr minus(long other) {
return Subtract.simplifiedIns(this, new SymLong(other));
}
public Expr minus(float other) {
return Subtract.simplifiedIns(this, new SymFloat(other));
}
public Expr minus(double other) {
return Subtract.simplifiedIns(this, new SymDouble(other));
}
/**
* Operator overloading 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 overloading 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 for Groovy:
* a/b
* @param other
* @return
*/
public Expr div(Expr other) {
return Divide.simplifiedIns(this, other);
}
public Expr divi(int other) {
return Divide.simplifiedIns(this, new SymInteger(other));
}
public Expr div(long other) {
return Divide.simplifiedIns(this, new SymLong(other));
}
public Expr div(float other) {
return Divide.simplifiedIns(this, new SymFloat(other));
}
public Expr div(double other) {
return Divide.simplifiedIns(this, new SymDouble(other));
}
/**
* Operator overloading 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 overloading 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));
}
public 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);
}
////////////////////////////////////////////////////////////////////////////
// /**
// * Assign the result of evaluating an expression to a local variable
// * when compiling. The local variable must be a symbol which is declared
// * as a local variable.
// * <p>
// * The call of this function can be understand as
// * Symbol symLocal; //Declared somewhere
// * symLocal = this;
// *
// * <p>
// * Note: The name of a symbol is a global name. Make sure you don't have
// * two symbols with the same name, otherwise they are treated as the same
// * symbol or local variable in compiled code.
// *
// * @param symLocal A symbol declared as a local variable
// * @return An instance of operator OPAsign
// */
// public Expr assignTo(Symbol symLocal) {
// return new OPAsign(symLocal, this);
// }
public InstructionHandle bytecodeGen(String clsName, MethodGen mg,
ConstantPoolGen cp, InstructionFactory factory,
InstructionList il, Map<String, Integer> argsMap, int argsStartPos,
Map<Expr, Integer> funcRefsMap) {
throw new UnsupportedOperationException();//il.append(InstructionConstants.NOP);
}
/**
* Reset the compile flags for an expression.
*
* For example, a matrix is defined and stored to new a local variable when it is referenced
* at the first time. The following references of the matrix just load the local variable.
* By calling this function, the state is reset to define a new local variable.
*/
public void bytecodeGenReset() {
}
public enum TYPE {INT, LONG, FLOAT, DOUBLE, BOOLEAN, BYTE, CHAR, SHORT, VOID,
MATRIX, VECTOR, TENSOR};
public abstract TypeInfo getTypeInfo();
public TYPE getType() {
return getTypeInfo().type;
}
public Expr assign(Expr expr) {
return new LCAssign(this, expr);
}
public Expr assign(double val) {
return new LCAssign(this, Expr.valueOf(val));
}
public Expr assign(int val) {
return new LCAssign(this, Expr.valueOf(val));
}
/////////////////////////////////////////////////////////////////////////
protected LCDevice device = null;
public Expr runOn(LCDevice dev) {
device = dev;
return this;
}
public LCDevice getDevice() {
return device;
}
/**
* Set the argument of the current expression
* Depends on the specific implementation
*
* @param index The position of the argument
* @param arg The expression of the argument
* @return
*/
public Expr setArg(int index, Expr arg) {
throw new UnsupportedOperationException();
}
abstract public void updateLabel();
/**
* Parent expression, for example: a sub-matrix or sub-vector has a parent
*/
public Expr getParent() {
throw new UnsupportedOperationException();
}
/**
* Return the dimension of a vector
* @return
*/
public int dim() {
throw new UnsupportedOperationException();
}
/**
* return the dimension of a matrix or tensor
* @return
*/
public int[] dims() {
throw new UnsupportedOperationException();
}
/**
* return the element at index
* @param index
* @return
*/
public Expr get(int index) {
throw new UnsupportedOperationException();
}
}