-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.java
More file actions
217 lines (175 loc) · 5.15 KB
/
Expression.java
File metadata and controls
217 lines (175 loc) · 5.15 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
210
211
212
213
214
215
216
217
package gradDescent;
import java.lang.Math;
// expressions which depend on x_0, x_1, etc.
public class Expression {
// abstract class defining evaluation and derivative
protected static abstract class Expr {
protected int nVars; // number of variables in the expression
// getter for nVars
public int nVars() {
return nVars;
}
// convert the expression to a string
public abstract String toString();
// evaluate the expression at x
public abstract double evaluate(double[] x);
// return an expression for the derivative
public abstract Expr derivative(int var);
// simplify the expression
public abstract Expr simplify();
// return an array of expressions for the gradient (which is a vector)
public Expr[] gradient() {
Expr[] grad = new Expr[nVars];
for (int i = 0; i < nVars; i++) {
grad[i] = derivative(i);
}
return grad;
}
}
// constant expression
public static class Const extends Expr {
private double val;
public double getVal() {
return val;
}
public Const(double new_val) {
nVars = 0;
val = new_val;
}
public String toString() {
return String.valueOf(val);
}
public double evaluate(double[] x) {
return val;
}
public Expr derivative(int x) {
return (new Const(0));
}
public Expr simplify() {
return this;
}
}
// identity expression for x_n
public static class Id extends Expr {
private int n; // variable number
public Id(int new_n) {
nVars = new_n + 1;
n = new_n;
}
public int varNum() {
return n;
}
public String toString() {
return String.format("x_%d", n);
}
public double evaluate(double[] x) {
return x[n];
}
public Expr derivative(int x) {
if (x == n) return (new Const(1));
else return (new Const(0));
}
public Expr simplify() {
return this;
}
}
// addition
public static class Add extends Expr {
private Expr a;
private Expr b;
public String toString() {
return String.format("%s + %s", a.toString(), b.toString());
}
public Add(Expr new_a, Expr new_b) {
nVars = Math.max(new_a.nVars, new_b.nVars);
a = new_a;
b = new_b;
}
public double evaluate(double[] x) {
return (a.evaluate(x) + b.evaluate(x));
}
public Expr derivative(int n) {
return (new Add(a.derivative(n), b.derivative(n))).simplify();
}
public Expr simplify() {
if (a.getClass() == Const.class && ((Const) a).getVal() == 0) return b;
if (b.getClass() == Const.class && ((Const) b).getVal() == 0) return a;
if (a.getClass() == Const.class && a.getClass() == Const.class) {
double aVal = ((Const) a).getVal();
double bVal = ((Const) b).getVal();
return new Const(aVal + bVal);
}
return this;
}
}
// multiplication
public static class Mul extends Expr {
private Expr a;
private Expr b;
public Mul(Expr new_a, Expr new_b) {
nVars = Math.max(new_a.nVars, new_b.nVars);
a = new_a;
b = new_b;
}
public String toString() {
String astr = a.toString();
String bstr = b.toString();
if (a.getClass() == Add.class) astr = String.format("(%s)", astr);
if (b.getClass() == Add.class) bstr = String.format("(%s)", bstr);
return String.format("%s*%s", astr, bstr);
}
public double evaluate(double[] x) {
return (a.evaluate(x) * b.evaluate(x));
}
// product rule for derivative
public Expr derivative(int n) {
return (new Add(
(new Mul (a.derivative(n), b)).simplify(),
(new Mul (a, b.derivative(n))).simplify()
)).simplify();
}
public Expr simplify() {
if (a.getClass() == Const.class && ((Const) a).getVal() == 0) return (new Const(0));
if (b.getClass() == Const.class && ((Const) b).getVal() == 0) return (new Const(0));
if (a.getClass() == Const.class && ((Const) a).getVal() == 1) return b;
if (b.getClass() == Const.class && ((Const) b).getVal() == 1) return a;
if (a.getClass() == Const.class && b.getClass() == Const.class) {
double aVal = ((Const) a).getVal();
double bVal = ((Const) b).getVal();
return new Const(aVal * bVal);
}
return this;
}
}
// polynomial term b^p, constant power
public static class Pow extends Expr {
private Expr b;
private double p;
public Pow(Expr new_b, double new_p) {
nVars = new_b.nVars;
b = new_b;
p = new_p;
}
public String toString() {
String bstr = b.toString();
if (b.getClass() == Add.class || b.getClass() == Mul.class) bstr = String.format("(%s)", bstr);
return String.format("%s^%f", bstr, p);
}
public double evaluate(double[] x) {
return Math.pow(b.evaluate(x), p);
}
// chain rule
public Expr derivative(int n) {
return new Mul (
new Mul(new Const(p), new Pow(b, p-1).simplify()).simplify(),
b.derivative(n)
).simplify();
}
public Expr simplify() {
if (p == 0) return (new Const(1));
if (p == 1) return b;
if (b.getClass() == Const.class) return new Const(Math.pow(((Const) b).getVal(), p));
return this;
}
}
}