2016-11-02 21:56:40 +01:00
|
|
|
package org.warp.picalculator.math.functions;
|
|
|
|
|
|
|
|
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.FunctionSingle;
|
2017-05-16 22:02:44 +02:00
|
|
|
import org.warp.picalculator.math.MathContext;
|
2016-11-02 21:56:40 +01:00
|
|
|
import org.warp.picalculator.math.rules.ExpandRule1;
|
|
|
|
import org.warp.picalculator.math.rules.ExpandRule5;
|
|
|
|
|
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 Negative extends FunctionSingle {
|
2016-11-02 21:56:40 +01:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
public Negative(MathContext root, Function value) {
|
2017-01-17 22:32:40 +01:00
|
|
|
super(root, value);
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|
2017-01-17 22:32:40 +01:00
|
|
|
|
2016-11-02 21:56:40 +01:00
|
|
|
@Override
|
2016-11-25 22:40:43 +01:00
|
|
|
protected boolean isSolvable() {
|
2017-03-03 23:06:43 +01:00
|
|
|
if (parameter instanceof Number) {
|
2017-01-31 22:29:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ExpandRule1.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ExpandRule5.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-02 21:56:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-17 22:32:40 +01:00
|
|
|
@Override
|
2017-03-03 23:06:43 +01:00
|
|
|
public ObjectArrayList<Function> solve() throws Error {
|
|
|
|
if (parameter == null) {
|
2016-11-02 21:56:40 +01:00
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
|
|
|
}
|
2017-03-03 23:06:43 +01:00
|
|
|
ObjectArrayList<Function> result = new ObjectArrayList<>();
|
2016-11-02 21:56:40 +01:00
|
|
|
if (ExpandRule1.compare(this)) {
|
|
|
|
result = ExpandRule1.execute(this);
|
|
|
|
} else if (ExpandRule5.compare(this)) {
|
|
|
|
result = ExpandRule5.execute(this);
|
2017-03-03 23:06:43 +01:00
|
|
|
} else if (parameter.isSimplified()) {
|
2016-11-02 21:56:40 +01:00
|
|
|
try {
|
2017-02-14 20:02:03 +01:00
|
|
|
final Number var = (Number) getParameter();
|
|
|
|
result.add(var.multiply(new Number(mathContext, "-1")));
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final NullPointerException ex) {
|
2016-11-02 21:56:40 +01:00
|
|
|
throw new Error(Errors.ERROR);
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final NumberFormatException ex) {
|
2016-11-02 21:56:40 +01:00
|
|
|
throw new Error(Errors.SYNTAX_ERROR);
|
2017-01-31 22:29:49 +01:00
|
|
|
} catch (final ArithmeticException ex) {
|
2016-11-02 21:56:40 +01:00
|
|
|
throw new Error(Errors.NUMBER_TOO_SMALL);
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-03 23:06:43 +01:00
|
|
|
final ObjectArrayList<Function> l1 = new ObjectArrayList<>();
|
|
|
|
if (parameter.isSimplified()) {
|
|
|
|
l1.add(parameter);
|
2016-11-02 21:56:40 +01:00
|
|
|
} else {
|
2017-03-03 23:06:43 +01:00
|
|
|
l1.addAll(parameter.simplify());
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
|
|
|
for (final Function f : l1) {
|
2017-02-14 20:02:03 +01:00
|
|
|
result.add(new Negative(mathContext, f));
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-25 22:40:43 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o instanceof Negative) {
|
2017-03-03 23:06:43 +01:00
|
|
|
return ((Negative) o).getParameter().equals(parameter);
|
2016-11-25 22:40:43 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-03 23:06:43 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Negative clone() {
|
|
|
|
return new Negative(mathContext, parameter);
|
|
|
|
}
|
2016-11-02 21:56:40 +01:00
|
|
|
}
|