-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNode.java
387 lines (361 loc) · 12.2 KB
/
Node.java
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Author: Isaac Newell
* Description: Nodes are the units of mathematical expressions,
* and are expressions in themselves.
*/
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Comparator;
public class Node {
private String op;
private Fraction value;
private Node parent;
private ArrayList<Node> children;
private static String[] opTypes = {"fraction",
"radical",
"power",
"plus",
"times",
"recip",
"parens",
"var",
"i"};
public Node(String op) {
this.op = op;
this.children = new ArrayList<Node>();
}
public Node() {
this("fraction");
}
public Node(int value) {
this();
this.value = new Fraction(value);
}
public Node(Fraction value) {
this();
this.value = new Fraction(value);
}
// Does a deep copy
public Node clone() {
if (op.equals("fraction")) {
if (value == null) {
return new Node();
} else {
return new Node(new Fraction(value));
}
} else {
Node newNode = new Node(op);
for (Node n : children) {
Node cloneN = n.clone();
newNode.children.add(cloneN);
cloneN.parent = newNode;
}
return newNode;
}
}
public void setValue(Fraction value) {
if (op.equals("fraction")) {
this.value = new Fraction(value);
} else {
//System.out.println("You can't assign a value to an operation node");
}
}
public String getOp() {
return op;
}
public Fraction getValue() {
return value;
}
public Node getParent() {
return parent;
}
public ArrayList<Node> getChildren() {
return children;
}
public boolean addChild(Node child) {
// remove child from its previous parent's children if applicable
if (child.parent != null) {
child.parent.children.remove(child);
}
// add child to this node's children
children.add(child);
// set this node as child's parent
child.parent = this;
return true;
}
public boolean remove(Node child) {
child.parent = null;
children.remove(child);
return true;
}
public void setParent(Node par) {
if (par != null)
par.addChild(this);
else
this.parent = null;
}
public void setChildren(ArrayList<Node> ch) {
children = ch;
for (Node c : ch) {
c.parent = this;
}
}
public static Node performOp(ArrayList<Node> nlist, String op, boolean cl) {
Node[] narray = new Node[nlist.size()];
for (int i = 0; i < narray.length; i++) {
narray[i] = nlist.get(i);
}
return performOp(narray, op, cl);
}
// Same as below, but for unary operations, e.g. power_2
public static Node performOp(Node n, String op, boolean cl) {
Node[] nlist = new Node[] {n};
Node ncopy = n.clone();
Node newNode = new Node(op);
newNode.addChild(ncopy);
//System.out.println(newNode);
if (op.indexOf("power") != -1) {
// If the child node is a frac, then apply the power to it.
return Simplify.simplifyPower(newNode);
} else if (op.indexOf("radical") != -1) {
//System.out.println("RADICAL FOUND");
return Simplify.simplifyRadical(newNode);
}
else if (op.equals("recip")) {
return Simplify.simplifyRecip(newNode);
}
return newNode;
}
// Performs the specified operation on the Node s in nlist.
public static Node performOp(Node[] nlist, String op, boolean cl) {
//System.out.println("LTH: " + nlist.length);
ArrayList<Node> nlistcopy = new ArrayList<Node>();
for (Node n : nlist) {
nlistcopy.add(n.clone());
}
Node newNode = new Node(op);
newNode.setChildren(nlistcopy);
if (op.equals("times")) {
return Simplify.combineConstantsTimes(Simplify.collapseTimes(newNode));
} else if (op.equals("plus")) {
return Simplify.combineConstantsPlus(Simplify.collapsePlus(newNode));
}
return newNode;
}
public String toString() {
String s = "";
if (this.op.equals("fraction")) {
s = s + " " + this.value + " ";
return s;
} else {
s = s + " " + this.op + "(";
for (int i = 0; i < this.children.size() - 1; i++) {
s = s + this.children.get(i).toString();
s = s + ",";
}
if (this.children.size() >= 1)
s = s + this.children.get(this.children.size()-1).toString();
s = s + ")";
return s;
}
}
// Expresses the Node as LaTeX code.
public String toLatex() {
String s = "";
if (this.op.equals("fraction")) {
if (this.value.getDenom() == 1) {
s = s + " " + this.value + " ";
} else {
if (this.value.getNum() < 0) {
s = s + "-\\frac{" + (-this.value.getNum()) + "}{" + this.value.getDenom() + "}";
}
else {
s = s + "\\frac{" + this.value.getNum() + "}{" + this.value.getDenom() + "}";
}
}
return s.replaceAll("\\s+", " ");
}
else {
if (this.op.equals("times")) {
// System.out.println("Doing times: " + this.children.size());
// boolean fracFound = false;
// boolean badFound = false;
// boolean fractionize = false;
// Node fracNode = null;
// for (int i = 0; i < this.children.size(); i++) {
// String op1 = this.children.get(i).op;
// if (op1.equals("fraction") && this.children.get(i).getValue().getDenom() != 1) {
// fracNode = this.children.get(i);
// fracFound = true;
// }
// if (!op1.equals("fraction") && !op1.equals("i")) {
// if (op1.equals("radical")) {
// if (!this.children.get(i).children.get(0).op.equals("fraction")) {
// badFound = true;
// System.out.println(this.children.get(i));
// break;
// }
// }
// else {
// badFound = true;
// System.out.println(this.children.get(i));
// break;
// }
// }
// }
// System.out.println("fracFound: " + fracFound);
// System.out.println("badFound: " + badFound);
// System.out.println();
// if (fracFound && !badFound) {
// System.out.println("FRACTIONIZE!!!!!!");
// fractionize = true;
// }
// if (fractionize) {
// s = "\\frac{";
// }
// Comparator<Node> comparator = new OpOrderComparator();
// PriorityQueue<Node> queue = new PriorityQueue<Node>(10, comparator);
// for (int i = 0; i < this.children.size(); i++) {
// //System.out.println("SSSSSSS: " + s);
// queue.add(this.children.get(i));
// }
// while (queue.size() != 0) {
// Node next = queue.remove();
// if (fractionize) {
// if (next == fracNode) {
// int v = fracNode.getValue().getNum();
// String body = v + "";
// if (v < 0) {
// body = "\\left(" + body + "\\right)";
// }
// s = s + body;
// continue;
// }
// }
// if (next.op.equals("plus")) {
// s = s + "\\left(" + next.toLatex() + "\\right)";
// }
// else {
// s = s + " " + next.toLatex() + " ";
// }
// }
for (int i = 0; i < this.children.size(); i++) {
if (this.children.get(i).op.equals("plus") || (this.children.get(i).op.equals("fraction")) && this.children.get(i).value.getNum() < 0) {
s = s + "\\left(" + this.children.get(i).toLatex() + "\\right)";
}
else {
s = s + " " + this.children.get(i).toLatex() + " ";
}
}
// if (fractionize) {
// System.out.println("ENDING FRACTIONIZE");
// s = s + "}{" + fracNode.getValue().getDenom() + "}";
// }
}
else if (this.op.equals("plus")) {
//System.out.println("Doing plus: " + this.children.size());
if (this.children.size() > 0) {
s = s + " " + this.children.get(0).toLatex() + " ";
}
for (int i = 1; i < this.children.size(); i++) {
if (this.children.get(i).op.equals("times")) {
boolean flipsign = false;
// search for negative constants - subtract.
for (Node n : this.children.get(i).children) {
if (n.op.equals("fraction")) {
if (n.value.getNum() < 0) {
flipsign = true;
break;
}
}
}
if (flipsign) {
Node toSubtract = Node.performOp(new Node[] {this.children.get(i), new Node(-1)}, "times", true);
s = s + " - " + toSubtract.toLatex() + " ";
continue;
}
}
s = s + "+" + this.children.get(i).toLatex() + " ";
}
}
else if (this.op.indexOf("power") != -1) {
int power = Integer.parseInt(this.op.split("_")[1]);
//System.out.println("Doing power: " + power);
if (this.children.size() > 0) {
if (this.children.get(0).op.equals("fraction")) {
s = s + "{" + this.children.get(0).toLatex() + "}^{" + power + "}";
}
else {
s = s + "{\\left(" + this.children.get(0).toLatex() + "\\right)}^{" + power + "}";
}
}
}
else if (this.op.indexOf("radical") != -1) {
int power = Integer.parseInt(this.op.split("_")[1]);
//System.out.println("Doing radical: " + power);
if (this.children.size() > 0) {
String index = (power == 2) ? "" : "[" + power + "]";
s = s + "\\sqrt" + index + "{" + this.children.get(0).toLatex() + "}";
}
}
else if (this.op.equals("i")) {
//System.out.println("doing i");
return "i";
}
else if (this.op.equals("recip")) {
s = "\\frac{1}{" + this.children.get(0).toLatex() + "}";
}
else {
s = this.toString();
}
return s.replaceAll("\\s+", " ");
}
}
public Complex evaluate() {
//System.out.println("GET: " + this);
if (this.op.equals("fraction")) {
//System.out.println("RETURN: " + (new Complex((double)(this.getValue().getNum()) / this.getValue().getDenom())));
return new Complex((double)(this.getValue().getNum()) / this.getValue().getDenom());
}
else if (this.op.equals("times")) {
Complex prod = new Complex(1);
for (Node n : this.children) {
prod = prod.multiply(n.evaluate());
}
//System.out.println("RETURN: " +prod);
return prod;
}
else if (this.op.equals("plus")) {
Complex sum = new Complex(0);
for (Node n : this.children) {
sum = sum.add(n.evaluate());
}
//System.out.println("RETURN: " + sum);
return sum;
}
else if (this.op.indexOf("power") != -1) {
int power = Integer.parseInt(this.op.split("_")[1]);
//System.out.println("RETURN: "+this.children.get(0).evaluate().power(power));
return this.children.get(0).evaluate().power(power);
}
else if (this.op.indexOf("radical") != -1) {
int power = Integer.parseInt(this.op.split("_")[1]);
//System.out.println("RETURN: "+this.children.get(0).evaluate().radical(power));
return this.children.get(0).evaluate().radical(power);
}
else if (this.op.equals("i")) {
//System.out.println("RETURN: " + (new Complex(0,1)));
return new Complex(0,1);
}
else if (this.op.equals("recip")) {
Complex recip = this.children.get(0).evaluate();
double a = recip.getRe();
double b = recip.getIm();
//System.out.println("RETURN: "+recip.conjugate().multiply(new Complex(1/(a*a-b*b),0)));
return recip.conjugate().multiply(new Complex(1/(a*a-b*b),0));
}
else {
return new Complex(0);
}
}
}