forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrt.java
More file actions
77 lines (69 loc) · 1.95 KB
/
Sqrt.java
File metadata and controls
77 lines (69 loc) · 1.95 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
package symjava.symbolic;
import symjava.symbolic.arity.BinaryOp;
import symjava.symbolic.utils.Utils;
public class Sqrt extends BinaryOp {
public Sqrt(Expr expr) {
super(expr, Expr.valueOf(2));
label = "\\sqrt{" + expr + "}";
sortKey = expr.getSortKey()+"sqrt[2]"+String.valueOf(arg2);
}
public Sqrt(Expr expr, Expr root) {
super(expr, root);
String displayRoot = String.format("%s", this.arg2);
if(root instanceof SymReal<?>) {
SymReal<?> realExp = (SymReal<?>)root;
if(realExp.isInteger()) {
displayRoot = String.format("%d", realExp.getIntValue());
}
}
label = "\\sqrt["+displayRoot+"]{" + expr + "}";
//TODO
sortKey = expr.getSortKey()+"sqrt["+root+"]"+String.valueOf(root);
}
@Override
public Expr diff(Expr expr) {
return Pow.simplifiedIns(arg1, 1.0/arg2).diff(expr);
}
public static Expr simplifiedIns(Expr expr, Expr root) {
if(expr instanceof SymReal<?> && root instanceof SymReal<?>) {
return new SymDouble(Math.pow(
((SymReal<?>)expr).getDoubleValue(),
1.0/((SymReal<?>)root).getDoubleValue())
);
} else if(expr instanceof SymReal<?>) {
SymReal<?> realBase = (SymReal<?>)expr;
if(realBase.isZero())
return Symbol.C0;
else if(realBase.isOne())
return Symbol.C1;
}else if(root instanceof SymReal<?>) {
SymReal<?> realExp = (SymReal<?>)root;
if(realExp.isZero())
return Symbol.C1;
else if(realExp.isOne())
return expr;
else if(realExp.isNegativeOne())
return Reciprocal.simplifiedIns(expr);
}
return new Sqrt(expr, root);
}
@Override
public Expr simplify() {
return this;
}
@Override
public boolean symEquals(Expr other) {
if(other instanceof Sqrt) {
Sqrt o = (Sqrt)other;
if(Utils.symCompare(arg1, o.arg1) && Utils.symCompare(arg2, o.arg2))
return true;
}
return false;
}
@Override
public Expr subs(Expr from, Expr to) {
if(Utils.symCompare(this, from))
return to;
return new Sqrt(arg1.subs(from, to), arg2.subs(from, to));
}
}