forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivide.java
More file actions
105 lines (92 loc) · 3.14 KB
/
Divide.java
File metadata and controls
105 lines (92 loc) · 3.14 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
package symjava.symbolic;
import java.util.ArrayList;
import java.util.List;
import symjava.symbolic.arity.BinaryOp;
import symjava.symbolic.utils.Utils;
/**
*
* @author yuemingl
*
*/
public class Divide extends BinaryOp {
public Divide(Expr numerator, Expr denominator) {
super(numerator, denominator);
label = SymPrinting.addParenthsesIfNeeded(arg1, this)
+ "/" +
SymPrinting.addParenthsesIfNeeded2(arg2, this);
sortKey = arg1.getSortKey()+arg2.getSortKey();
}
public static Expr shallowSimplifiedIns(Expr numerator, Expr denominator) {
int simOps = numerator.getSimplifyOps() + denominator.getSimplifyOps() + 1;
if(Symbol.C0.symEquals(numerator))
return new SymInteger(0).setSimplifyOps(simOps).setAsSimplified();
else if(numerator instanceof SymReal<?> && denominator instanceof SymReal<?>) {
Number t1 = (Number)((SymReal<?>)numerator).getValue();
Number t2 = (Number)((SymReal<?>)denominator).getValue();
return new SymDouble(t1.doubleValue() / t2.doubleValue()).setSimplifyOps(simOps).setAsSimplified();
} else if(denominator.symEquals(Symbol.C0))
throw new IllegalArgumentException("Argument 'divisor' is 0");
else if(Symbol.C1.symEquals(numerator))
return Reciprocal.simplifiedIns(denominator).setSimplifyOps(simOps).setAsSimplified();
else if(Symbol.C1.symEquals(denominator))
return numerator.clone().setSimplifyOps(simOps).setAsSimplified();
return new Divide(numerator, denominator).setAsSimplified();
}
public static Expr simplifiedIns(Expr numerator, Expr denominator) {
//return shallowSimplifiedIns(numerator, denominator);
return Utils.flattenSortAndSimplify(shallowSimplifiedIns(numerator, denominator));
}
@Override
public Expr subs(Expr from, Expr to) {
if(Utils.symCompare(this, from))
return to;
return new Divide(arg1.subs(from, to), arg2.subs(from, to));
}
@Override
public Expr diff(Expr expr) {
//For debug purpose
if(expr instanceof Symbol) {
boolean bl = Utils.containSymbol(arg1, (Symbol)expr);
boolean br = Utils.containSymbol(arg2, (Symbol)expr);
if(!bl && !br) {
return Symbol.C0;
} else if(!bl) {
return arg1.multiply(Reciprocal.simplifiedIns(arg2).diff(expr));
} else if(!br) {
return arg1.diff(expr).multiply(Reciprocal.simplifiedIns(arg2));
}
}
Expr n0 = arg1.diff(expr);
Expr n1 = n0.multiply(arg2);
Expr n2 = arg1.multiply(arg2.diff(expr));
Expr n3 = n1.subtract(n2);
Expr n4 = arg2.multiply(arg2);
Expr n5 = n3.divide(n4);
return n5;
}
@Override
public Expr simplify() {
if(!this.isSimplified) {
return simplifiedIns(arg1, arg2);
}
return this;
}
@Override
public void flattenAdd(List<Expr> outList) {
List<Expr> list1 = new ArrayList<Expr>();
arg1.flattenAdd(list1);
Expr r = Reciprocal.simplifiedIns(arg2);
for(Expr e : list1) {
outList.add( new Multiply(e, r) );
}
}
@Override
public void flattenMultiply(List<Expr> outList) {
arg1.flattenMultiply(outList);
Reciprocal.simplifiedIns(arg2).flattenMultiply(outList);
}
public boolean symEquals(Expr other) {
//return Utils.flattenSortAndCompare(this, other);
return Utils.flattenSortAndCompare(this.simplify(), other.simplify());
}
}