Skip to content

Commit 1122954

Browse files
committed
Implemented Operable::complexity(); now the determination of whether an expression is simplified is based on its complexity, not the number of nodes. This fixes the issue where x*a*x is not simplified to x^2*a
1 parent cba44ae commit 1122954

File tree

8 files changed

+44
-16
lines changed

8 files changed

+44
-16
lines changed

src/jmc/cas/BinaryOperation.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ private void crossSimplify(ArrayList<Operable> pool) {
602602
Operable other = pool.get(k);
603603
String operation = getPriority() == 2 ? "*" : "+";
604604
BinaryOperation binOp = new BinaryOperation(operable, operation, other);
605-
int n1 = binOp.numNodes();
605+
int n1 = binOp.complexity();
606606
Operable op = binOp.simplify(); //be careful, avoid stack overflow
607-
if (op.numNodes() < n1) { //simplifiable
607+
if (op.complexity() < n1) { //simplifiable
608608
pool.remove(i);
609609
pool.remove(k - 1);
610610
pool.add(op);
@@ -899,6 +899,10 @@ public int levelOf(Operable o) {
899899
return left > right ? right + 1 : left + 1;
900900
}
901901

902+
public int complexity() {
903+
return getLeftHand().complexity() + getRightHand().complexity() + 1;
904+
}
905+
902906
public Operable replace(Operable o, Operable r) {
903907
if (this.equals(o)) return r;
904908
BinaryOperation clone = this.copy();

src/jmc/cas/Constants.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public String toString() {
7979
return getName();
8080
}
8181

82+
public int complexity() {
83+
return 2;
84+
}
85+
8286
public double eval(double x) {
8387
return computedConst.compute();
8488
}
@@ -95,7 +99,7 @@ public Constant copy() {
9599

96100
public boolean equals(Operable other) {
97101
return other instanceof Constant && (((Constant) other).getName().equals(getName())
98-
|| other.val() == this.val());
102+
|| other.val() == this.val());
99103
}
100104

101105
public Operable plugIn(Variable var, Operable nested) {
@@ -117,6 +121,6 @@ public static Constant getConstant(String name) {
117121
if (constant.getName().equals(name))
118122
return constant;
119123
}
120-
throw new JMCException("constant + \""+name+"\" does not exist");
124+
throw new JMCException("constant + \"" + name + "\" does not exist");
121125
}
122126
}

src/jmc/cas/LeafNode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public int numNodes() {
2121
return 1;
2222
}
2323

24+
public abstract int complexity();
25+
2426
public Operable beautify() {
2527
return this;
2628
}

src/jmc/cas/Operable.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public interface Operable extends Evaluable {
3838
*/
3939
int levelOf(Operable o);
4040

41+
/**
42+
* @return level of complexity of the expression represented by an integer with 1 being
43+
* the most simplistic
44+
*/
45+
int complexity();
46+
4147
/**
4248
* plugs in the operable nested for all variables in the expression
4349
* NOTE: the method returns the operable with the desired nested operable plugged in, but

src/jmc/cas/RawValue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public boolean isPositive() {
114114
return doubleValue() > 0;
115115
}
116116

117+
public int complexity() {
118+
return 1;
119+
}
120+
117121
/**
118122
* Since plugIn only applies to variable, a RawValue type should only return itself.
119123
*

src/jmc/cas/UnaryOperation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public double eval(double x) {
4242
return operation.eval(getLeftHand().eval(x));
4343
}
4444

45+
public int complexity() {
46+
return getLeftHand().complexity() + 1;
47+
}
48+
4549

4650
public static void define(String name, String expression) {
4751
UnaryOperation.define(name, Expression.interpret(expression));

src/jmc/cas/Variable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public String toString() {
2828
return name;
2929
}
3030

31+
public int complexity() {
32+
return 3;
33+
}
34+
3135
public Operable simplify() {
3236
return this;
3337
}

src/tests/BinaryOperationTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@
1111
*/
1212
public class BinaryOperationTest {
1313
public static void main(String args[]) {
14-
// BinaryOperation binOp = new BinaryOperation(RawValue.ZERO, "*", RawValue.ONE);
15-
// l(binOp.getRightHand(), binOp.getLeftHand());
16-
// binOp.setRightHand(RawValue.ONE);
17-
// BinaryOperation.define("&", 3, (a, b) -> a + b);
18-
// l(BinaryOperation.binaryOperations(), BinaryOperation.binaryOperations(3));
19-
// l(BinaryOperation.getPriority("&"), BinaryOperation.getPriority("+"));
20-
// l(new BinaryOperation(RawValue.ZERO, "*", RawValue.ONE).getPriority());
21-
// l(new BinaryOperation(RawValue.ZERO, "*", RawValue.ONE).flattened());
22-
// l(binOp.is("*"));
23-
// l(Operation.mult(3, 5));
24-
// l(Operation.exp(Math.random(), new RawValue(3)));
25-
// l(Operation.exp(new RawValue(3), Math.random()));
14+
BinaryOperation binOp = new BinaryOperation(RawValue.ZERO, "*", RawValue.ONE);
15+
l(binOp.getRightHand(), binOp.getLeftHand());
16+
binOp.setRightHand(RawValue.ONE);
17+
BinaryOperation.define("&", 3, (a, b) -> a + b);
18+
l(BinaryOperation.binaryOperations(), BinaryOperation.binaryOperations(3));
19+
l(BinaryOperation.getPriority("&"), BinaryOperation.getPriority("+"));
20+
l(new BinaryOperation(RawValue.ZERO, "*", RawValue.ONE).getPriority());
21+
l(new BinaryOperation(RawValue.ZERO, "*", RawValue.ONE).flattened());
22+
l(binOp.is("*"));
23+
l(Operation.mult(3, 5));
24+
l(Operation.exp(Math.random(), new RawValue(3)));
25+
l(Operation.exp(new RawValue(3), Math.random()));
2626
l(Expression.interpret("x+x*a").simplify());
2727
}
2828

0 commit comments

Comments
 (0)