forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrad.java
More file actions
146 lines (129 loc) · 2.82 KB
/
Grad.java
File metadata and controls
146 lines (129 loc) · 2.82 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
package symjava.math;
import java.util.List;
import symjava.matrix.SymVector;
import symjava.symbolic.Derivative;
import symjava.symbolic.Expr;
import symjava.symbolic.Func;
import symjava.symbolic.utils.Utils;
/**
* Gradient of a function or expression
*
*/
public class Grad extends SymVector {
public Expr[] args = null;
protected Func func = null;
/**
* Construct an instance directly from data and args
*
* @param data
* @param args
*/
public Grad(SymVector data, Expr... args) {
for(Expr e : data)
this.data.add(e);
this.args = args;
}
public Grad(Expr f) {
if(f instanceof Func) {
if(f.isAbstract()) {
this.func = (Func)f;
for(Expr x : this.func.args) {
data.add(f.diff(x));
}
} else {
for(Expr x : ((Func)f).args) {
data.add(f.diff(x));
}
}
} else {
List<Expr> args = Utils.extractSymbols(f);
for(Expr x : args) {
data.add(f.diff(x));
}
}
}
public Grad(Expr f, Expr[] args) {
if(f instanceof Func) {
if(f.isAbstract()) {
this.func = (Func)f;
for(Expr x : this.func.args) {
data.add(f.diff(x));
}
} else {
for(Expr x : ((Func)f).args) {
data.add(f.diff(x));
}
}
} else {
for(Expr x : args) {
data.add(f.diff(x));
}
}
}
/**
* Functional Gradient
*
* @param F
* @param fs
* @param dfs
*/
public Grad(Expr F, Expr[] fs, Expr[] dfs) {
if(fs.length != dfs.length)
throw new IllegalArgumentException();
if(F instanceof Func) {
this.func = (Func)F;
}
for(int i=0; i<fs.length; i++) {
if(fs[i] instanceof Func && dfs[i] instanceof Func) {
data.add(F.fdiff(fs[i], dfs[i]));
} else {
throw new IllegalArgumentException();
}
}
}
public static SymVector apply(Expr f) {
return new Grad(f);
}
public static SymVector apply(Expr f, Expr[] args) {
return new Grad(f, args);
}
public static SymVector apply(Expr F, Expr[] fs, Expr[] dfs) {
return new Grad(F, fs, dfs);
}
public Func getFunc() {
return func;
}
public boolean isAbstract() {
if(func != null)
return func.isAbstract();
return false;
}
public SymVector subs(Expr from, Expr to) {
//if(Utils.symCompare(this, from)) {
// return to;
//}
if(this.func == null || this.func.isAbstract()) {
return new Grad(super.subs(from, to), this.args);
}
Expr funcSub = this.func.subs(from, to);
if(this.func != funcSub) {
return new Grad(funcSub, this.func.args);
}
return this;
}
public SymVector diff(Expr expr) {
return new Grad(super.diff(expr), this.args);
}
@Override
public String toString() {
if(this.func == null) {
for(Expr e : this.data) {
if(!(e instanceof Derivative)) {
return super.toString();
}
}
return "\\nabla{"+((Derivative)this.data.get(0)).getFunc().toString()+"}";
}
return "\\nabla{"+this.func.getLabel()+"}";
}
}