2016-09-19 18:48:27 +02:00
|
|
|
package org.warp.picalculator.math.functions;
|
2016-09-18 14:33:25 +02:00
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.Error;
|
|
|
|
import org.warp.picalculator.Errors;
|
2017-02-14 20:02:03 +01:00
|
|
|
import org.warp.picalculator.math.Function;
|
|
|
|
import org.warp.picalculator.math.FunctionOperator;
|
2017-05-16 22:02:44 +02:00
|
|
|
import org.warp.picalculator.math.MathContext;
|
2016-11-25 22:40:43 +01:00
|
|
|
import org.warp.picalculator.math.rules.ExpandRule1;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule3;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule4;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule5;
|
2016-09-18 14:33:25 +02:00
|
|
|
|
2017-05-16 22:02:44 +02:00
|
|
|
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
public class SumSubtraction extends FunctionOperator {
|
2016-09-18 14:33:25 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
public SumSubtraction(MathContext root, Function value1, Function value2) {
|
2017-01-17 22:32:40 +01:00
|
|
|
super(root, value1, value2);
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-10-02 16:01:41 +02:00
|
|
|
@Override
|
2016-11-25 22:40:43 +01:00
|
|
|
protected boolean isSolvable() {
|
2017-02-14 20:02:03 +01:00
|
|
|
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
2016-10-02 16:01:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
if (NumberRule3.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ExpandRule1.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule4.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule5.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-02 16:01:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
@Override
|
2017-03-03 23:06:43 +01:00
|
|
|
public ObjectArrayList<Function> solve() throws Error {
|
2017-02-14 20:02:03 +01:00
|
|
|
if (parameter1 == null || parameter2 == null) {
|
2016-09-18 14:33:25 +02:00
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
2017-03-03 23:06:43 +01:00
|
|
|
ObjectArrayList<Function> result = new ObjectArrayList<>();
|
2016-11-25 22:40:43 +01:00
|
|
|
if (NumberRule3.compare(this)) {
|
|
|
|
result = NumberRule3.execute(this);
|
|
|
|
} else if (ExpandRule1.compare(this)) {
|
|
|
|
result = ExpandRule1.execute(this);
|
|
|
|
} else if (NumberRule4.compare(this)) {
|
|
|
|
result = NumberRule4.execute(this);
|
|
|
|
} else if (NumberRule5.compare(this)) {
|
|
|
|
result = NumberRule5.execute(this);
|
2017-02-14 20:02:03 +01:00
|
|
|
} else if (parameter1.isSimplified() & parameter2.isSimplified()) {
|
|
|
|
result.add(((Number) parameter1).add((Number) parameter2));
|
|
|
|
result.add(((Number) parameter1).add(((Number) parameter2).multiply(new Number(mathContext, "-1"))));
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-25 22:40:43 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o instanceof SumSubtraction) {
|
2017-02-14 20:02:03 +01:00
|
|
|
final FunctionOperator f = (FunctionOperator) o;
|
2017-03-03 23:06:43 +01:00
|
|
|
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
|
2016-11-25 22:40:43 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-03 23:06:43 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public SumSubtraction clone() {
|
|
|
|
return new SumSubtraction(mathContext, parameter1, parameter2);
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|