forked from yuemingl/SymJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunc.java
More file actions
209 lines (184 loc) · 4.54 KB
/
Func.java
File metadata and controls
209 lines (184 loc) · 4.54 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
package symjava.symbolic;
import java.util.List;
import com.sun.org.apache.bcel.internal.generic.ClassGen;
import symjava.bytecode.BConstant;
import symjava.bytecode.BytecodeFunc;
import symjava.symbolic.arity.NaryOp;
import symjava.symbolic.utils.BytecodeUtils;
import symjava.symbolic.utils.FuncClassLoader;
import symjava.symbolic.utils.Utils;
public class Func extends NaryOp {
protected Expr expr;
/**
* Construct an abstract function
* Note: For an abstract function with only one argument, define it by
* passing an array for the second parameter
*
* Func f = new Func("F", new Expr[]{Symbol.x}); // F(x)
*
* @param name
* @param args
*/
public Func(String name, Expr ...args) {
super(args);
this.expr = null;
this.label = name;
this.sortKey = label;
}
/**
* Construct a function with expression expr
* For example:
* Func f = new Func("F", Symbol.x); // F(x)=x
* @param name
* @param expr
*/
public Func(String name, Expr expr) {
super(new Expr[] {expr});
//Extract free variables from expr
this.expr = expr;
this.args = Utils.extractSymbols(expr).toArray(new Expr[0]);
this.label = name;
this.sortKey = label;
}
public Func(String name, Expr expr, Expr[] args) {
super(args);
//Extract free variables from expr
this.expr = expr;
this.label = name;
this.sortKey = label;
}
public String getName() {
return label;
}
public Expr getExpr() {
return this.expr;
}
@Override
public boolean isAbstract() {
return expr == null;
}
public BytecodeFunc toBytecodeFunc() {
return toBytecodeFunc(false, false);
}
public BytecodeFunc toBytecodeFunc(boolean isWriteFile, boolean staticMethod) {
try {
if(!isWriteFile) {
if(this.expr instanceof SymReal<?>) {
SymReal<?> r = (SymReal<?>)this.expr;
return new BConstant(r.getValue().doubleValue());
}
}
/**
* Return an instance of BytecodeFunc generated by this Func without writing a class file to disk.
*/
FuncClassLoader<BytecodeFunc> fcl = new FuncClassLoader<BytecodeFunc>();
ClassGen genClass = BytecodeUtils.genClassBytecodeFunc(this, isWriteFile, staticMethod);
return fcl.newInstance(genClass);
//return (BytecodeFunc)Class.forName("symjava.bytecode."+this.label).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String toString() {
if(expr != null)
return expr.toString();
else
return getLabel();
}
public String getLabel() {
if(args == null || args.length == 0)
return label;
else
return label+"("+Utils.joinLabels(args, ",")+")";
}
@Override
public Expr diff(Expr expr) {
if(Utils.symCompare(this, expr)) {
return Symbol.C1;
} else if(this.containsArg(expr)) {
if(this.expr != null)
return this.expr.diff(expr);
return new Derivative(this, expr);
} else {
return Symbol.C0;
}
}
public boolean containsArg(Expr arg) {
if(arg != null) {
for(Expr e : args) {
boolean b = Utils.symCompare(e, arg);
if(b) return true;
}
}
return false;
}
@Override
public Expr subs(Expr from, Expr to) {
if(Utils.symCompare(this, from))
return to;
else if(this.expr != null)
return new Func(this.label, this.expr.subs(from, to));
else
return this;
}
@Override
public Expr simplify() {
if(this.isSimplified)
return this;
if(expr != null) {
Func f = new Func(label, expr.simplify());
f.args = this.args;
f.isSimplified = true;
return f;
}
this.isSimplified = true;
return this;
}
@Override
public boolean symEquals(Expr other) {
if(other instanceof Func) {
Func o = (Func)other;
//TODO support map(expr, [e1,e2,e3])
if(this.label.equals("_") && other.label.equals("_"))
return true;
if(this.label.equals("__") && other.label.equals("__"))
return true;
Boolean rlt = Utils.symCompareNull(this.expr, o.expr);
if(rlt != null && rlt == false)
return false;
if(!this.label.equals(o.label))
return false;
if(args.length != o.args.length)
return false;
for(int i=0; i<args.length; i++) {
if(!args[i].symEquals(o.args[i]))
return false;
}
return true;
}
return false;
}
@Override
public void flattenAdd(List<Expr> outList) {
if(expr != null)
expr.flattenAdd(outList);
else
outList.add(this);
}
@Override
public void flattenMultiply(List<Expr> outList) {
if(expr != null)
expr.flattenMultiply(outList);
else
outList.add(this);
}
@Override
public TypeInfo getTypeInfo() {
return expr.getTypeInfo();
}
@Override
public void updateLabel() {
// TODO Auto-generated method stub
}
}