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
149 lines (132 loc) · 2.87 KB

File metadata and controls

149 lines (132 loc) · 2.87 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
package symjava.math;
import java.util.List;
import symjava.matrix.ExprVector;
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 ExprVector {
public Expr[] args = null;
protected Func func = null;
/**
* Construct an instance directly from data and args
*
* @param data
* @param args
*/
public Grad(ExprVector 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 ExprVector apply(Expr f) {
return new Grad(f);
}
public static ExprVector apply(Expr f, Expr[] args) {
return new Grad(f, args);
}
public static ExprVector 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 ExprVector 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 ExprVector 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();
}
}
if(data.size() > 0)
return "\\nabla{"+((Derivative)this.data.get(0)).getFunc().toString()+"}";
else
return "0";
}
return "\\nabla{"+this.func.getLabel()+"}";
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.