Skip to content

Commit 977c046

Browse files
committed
Refactor solver loops: index loops, remove itors, remove Map usage
Speedup reasons
1 parent 1fec4f7 commit 977c046

9 files changed

Lines changed: 224 additions & 152 deletions

File tree

src/main/java/com/babai/ssplot/cli/SSPlotCLI.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.IOException;
2626
import java.nio.file.Path;
2727
import java.nio.file.Paths;
28-
import java.util.Map;
2928
import java.util.Scanner;
3029

3130
import javax.imageio.ImageIO;
@@ -73,7 +72,7 @@ private static void startREPL() {
7372
if (line.equalsIgnoreCase("exit")) {
7473
break;
7574
} else {
76-
System.out.println(parser.evaluate(line, Map.of()));
75+
System.out.println(parser.evaluate(line));
7776
}
7877
}
7978
} catch (Exception e) {

src/main/java/com/babai/ssplot/math/system/core/EquationSystem.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package com.babai.ssplot.math.system.core;
2525

2626
import java.util.Arrays;
27-
import java.util.Iterator;
2827

2928
/**
3029
* Class that holds data for system of equations.
@@ -43,9 +42,18 @@ public record EquationSystem (
4342
public static final double DEFAULT_H = 0.05; /* Default stepsize for RK4 */
4443
public static final Range DEFAULT_RANGE =
4544
new Range(-10, 10, 0.1); /* Default range for the variables x, y, z */
45+
4646
public int numberOfEqns() {
4747
return (int) Arrays.stream(eqns).filter(eqn -> !eqn.isEmpty() && !eqn.isBlank()).count();
4848
}
49+
50+
public String eqn(int i) {
51+
return eqns[i];
52+
}
53+
54+
public Range range(int i) {
55+
return ranges[i];
56+
}
4957

5058
@Override
5159
public String toString() {
@@ -154,7 +162,7 @@ public EquationSystem build() {
154162
}
155163

156164
/** Small record for storing range info for each independent variable */
157-
public record Range(double start, double end, double step) implements Iterable<Double> {
165+
public record Range(double start, double end, double step) {
158166

159167
public Range(double start, double end) {
160168
this(start, end, start < end ? 1 : -1);
@@ -163,42 +171,20 @@ public Range(double start, double end) {
163171
public Range(double end) {
164172
this(0, end, end > 0 ? 1 : -1);
165173
}
166-
167-
@Override
168-
public Iterator<Double> iterator() {
169-
return new Iterator<Double>() {
170-
Double current = start;
171-
172-
@Override
173-
public boolean hasNext() {
174-
if (step == 0) throw new IllegalArgumentException("Step cannot be zero!");
175-
return step > 0 ? current < end : current > end;
176-
}
177-
178-
@Override
179-
public Double next() {
180-
current += step;
181-
return current;
182-
}
183-
184-
};
174+
175+
public double at(int i) {
176+
return start + step * i;
185177
}
186178

187179
/** NOTE: does not include endpoint */
188180
public int count() {
189-
return (int) Math.floor((end - start) / step);
181+
if (step == 0) throw new IllegalArgumentException("Step cannot be zero!");
182+
return (int) Math.round((end - start) / step);
190183
}
191184

192185
public double[] toArray() {
193186
return new double[] { start(), end(), step() };
194187
}
195-
196-
197-
public void printAllPoints() {
198-
for (double d : this) {
199-
System.out.println(d);
200-
}
201-
}
202188
}
203189

204190
}

src/main/java/com/babai/ssplot/math/system/parser/Parser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
package com.babai.ssplot.math.system.parser;
2525

26-
import java.util.Map;
27-
2826
public interface Parser {
29-
// Evaluate an expression using this parser
30-
double evaluate(String expression, Map<String, Double> variables);
27+
public void setVariables(String... variables);
28+
29+
/** Evaluate an expression using this parser */
30+
public double evaluate(String expression, double... values);
3131

3232
public String getName();
3333
}

src/main/java/com/babai/ssplot/math/system/parser/ScriptParser.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
package com.babai.ssplot.math.system.parser;
2525

26-
import java.util.Map;
27-
2826
import javax.script.ScriptEngine;
2927
import javax.script.ScriptException;
3028

@@ -33,19 +31,28 @@
3331
public class ScriptParser implements Parser {
3432
private String name;
3533
private ScriptEngine engine;
34+
private String[] varNames;
35+
36+
@Override
37+
public void setVariables(String... varNames) {
38+
this.varNames = varNames;
39+
}
3640

3741
public ScriptParser(String name, ScriptEngine engine) {
3842
this.name = name;
3943
this.engine = engine;
4044
}
4145

4246
@Override
43-
public double evaluate(String expression, Map<String, Double> variables) {
47+
public double evaluate(String expression, double... variables) {
4448
if (expression.isEmpty()) {
4549
return 0;
4650
}
4751

48-
variables.forEach((var, val) -> engine.put(var, val));
52+
for (int i = 0; varNames != null && i < varNames.length; i++) {
53+
engine.put(varNames[i], variables[i]);
54+
}
55+
4956
try {
5057
engine.eval("answer = " + expression);
5158
return (double) engine.get("answer");

src/main/java/com/babai/ssplot/math/system/parser/internal/tree/TreeNode.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@
2323

2424
package com.babai.ssplot.math.system.parser.internal.tree;
2525

26-
import java.util.Map;
27-
import java.util.Vector;
26+
import java.util.ArrayList;
2827

2928
public class TreeNode {
3029
private TreeOperator nodeOp = null;
31-
private Vector<TreeNode> nodes = new Vector<TreeNode>();
30+
private ArrayList<TreeNode> nodes = new ArrayList<>();
31+
private String[] varNames = new String[0];
3232

3333
public TreeNode(TreeOperator op) {
3434
this.nodeOp = op;
3535
}
36+
37+
public void setVarNames(String... varNames) {
38+
this.varNames = varNames;
39+
}
3640

3741
/**
3842
* Calculates the value of this node by
@@ -48,8 +52,11 @@ public double getValue() {
4852
return res;
4953
}
5054

51-
public double evalAt(Map<String, Double> variables) {
52-
variables.forEach((var, val) -> scanVariables(var, val));
55+
public double evalAt(double... values) {
56+
// TODO check values len >= varName length
57+
for (int varIdx = 0; varNames != null && varIdx < varNames.length; varIdx++) {
58+
scanVariables(varNames[varIdx], values[varIdx]);
59+
}
5360
return getValue();
5461
}
5562

@@ -84,10 +91,10 @@ public void addChild(TreeNode node) {
8491
}
8592

8693
public void removeChilds() {
87-
nodes.removeAllElements();
94+
nodes.clear();
8895
}
8996

90-
public Vector<TreeNode> getChilds() {
97+
public ArrayList<TreeNode> getChilds() {
9198
return nodes;
9299
}
93100

src/main/java/com/babai/ssplot/math/system/parser/internal/tree/TreeParser.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
public class TreeParser implements Parser {
3131
private static final String NAME = "Internal";
32+
private String[] varNames;
33+
3234
private static final Map<String, TreeNode> exprCache = new LinkedHashMap<>(16, 0.75f, true) {
3335
private static final int MAX_CACHE_ENTRIES = 100;
3436
@Override
@@ -37,30 +39,38 @@ protected boolean removeEldestEntry(Map.Entry<String, TreeNode> eldest) {
3739
}
3840
};
3941

42+
public static String internalParserName() {
43+
return NAME;
44+
}
45+
4046
@Override
41-
public double evaluate(String expression, Map<String, Double> variables) {
47+
public double evaluate(String expression, double... vals) {
4248
// get the cached TreeNode or parse it if missing
4349
TreeNode tree = exprCache.computeIfAbsent(expression, this::parse);
4450

4551
// evaluate the cached AST with the provided variables
46-
return tree.evalAt(variables);
52+
return tree.evalAt(vals);
4753
}
4854

4955
@Override
5056
public String getName() {
5157
return internalParserName();
5258
}
53-
54-
public static String internalParserName() {
55-
return NAME;
56-
}
5759

5860
public TreeNode parse(String token) {
61+
TreeNode exprNode;
5962
try {
60-
return SSMathParser.parseString(token);
63+
exprNode = SSMathParser.parseString(token);
6164
} catch(Exception e) {
62-
return new TreeNode(new Constant(0.0));
65+
exprNode = new TreeNode(new Constant(0.0));
6366
}
67+
exprNode.setVarNames(varNames);
68+
return exprNode;
69+
}
70+
71+
@Override
72+
public void setVariables(String... varNames) {
73+
this.varNames = varNames;
6474
}
6575
}
6676

0 commit comments

Comments
 (0)