2018-09-02 13:36:49 +02:00
|
|
|
package it.cavallium.warppi.math.functions;
|
2018-09-02 12:45:51 +02:00
|
|
|
|
2018-09-02 13:36:49 +02:00
|
|
|
import it.cavallium.warppi.gui.expression.blocks.Block;
|
|
|
|
import it.cavallium.warppi.gui.expression.blocks.BlockChar;
|
|
|
|
import it.cavallium.warppi.gui.expression.blocks.BlockParenthesis;
|
|
|
|
import it.cavallium.warppi.math.Function;
|
|
|
|
import it.cavallium.warppi.math.FunctionOperator;
|
|
|
|
import it.cavallium.warppi.math.MathContext;
|
|
|
|
import it.cavallium.warppi.math.MathematicalSymbols;
|
2018-09-12 22:16:33 +02:00
|
|
|
import it.cavallium.warppi.util.Error;
|
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 Multiplication extends FunctionOperator {
|
2016-09-19 18:48:27 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
public Multiplication(final MathContext root, final Function value1, final Function value2) {
|
2017-01-17 22:32:40 +01:00
|
|
|
super(root, value1, value2);
|
2017-10-31 23:11:53 +01:00
|
|
|
/*if (value1 instanceof Variable && value2 instanceof Variable == false) {
|
2017-02-14 20:02:03 +01:00
|
|
|
parameter1 = value2;
|
|
|
|
parameter2 = value1;
|
2017-10-31 23:11:53 +01:00
|
|
|
}*/
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2016-11-25 22:40:43 +01:00
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public boolean equals(final Object o) {
|
2016-11-25 22:40:43 +01:00
|
|
|
if (o instanceof Multiplication) {
|
2017-02-14 20:02:03 +01:00
|
|
|
final FunctionOperator f = (FunctionOperator) o;
|
2018-09-28 11:39:28 +02:00
|
|
|
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
|
2016-11-25 22:40:43 +01:00
|
|
|
return true;
|
2018-09-28 11:39:28 +02:00
|
|
|
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
|
2016-11-25 22:40:43 +01:00
|
|
|
return true;
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2016-11-25 22:40:43 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-14 20:02:03 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Multiplication clone() {
|
|
|
|
return new Multiplication(mathContext, parameter1, parameter2);
|
|
|
|
}
|
2017-04-10 22:50:43 +02:00
|
|
|
|
2017-05-26 22:37:18 +02:00
|
|
|
@Override
|
2018-09-22 11:17:30 +02:00
|
|
|
public ObjectArrayList<Block> toBlock(final MathContext context) throws Error {
|
2018-05-12 21:18:29 +02:00
|
|
|
final ObjectArrayList<Block> result = new ObjectArrayList<>();
|
|
|
|
final Function par1 = getParameter1();
|
|
|
|
final Function par2 = getParameter2();
|
|
|
|
final ObjectArrayList<Block> sub1 = par1.toBlock(context);
|
|
|
|
final ObjectArrayList<Block> sub2 = par2.toBlock(context);
|
|
|
|
final Block nearLeft = sub1.get(sub1.size() - 1);
|
|
|
|
final Block nearRight = sub2.get(0);
|
2017-09-24 18:47:30 +02:00
|
|
|
|
|
|
|
if (par1 instanceof Number && ((Number) par1).equals(new Number(context, -1))) {
|
2017-07-24 23:11:29 +02:00
|
|
|
result.add(new BlockChar(MathematicalSymbols.MINUS));
|
|
|
|
if (new Expression(context, par2).parenthesisNeeded()) {
|
2018-05-12 21:18:29 +02:00
|
|
|
final ObjectArrayList<Block> parBlocks = par2.toBlock(context);
|
|
|
|
final BlockParenthesis par = new BlockParenthesis(parBlocks);
|
2017-07-24 23:11:29 +02:00
|
|
|
result.add(par);
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2017-11-01 22:34:35 +01:00
|
|
|
result.addAll(sub2);
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2017-07-24 23:11:29 +02:00
|
|
|
return result;
|
2017-05-26 22:37:18 +02:00
|
|
|
} else {
|
2017-11-01 22:34:35 +01:00
|
|
|
if (new Expression(context, par1).parenthesisNeeded()) {
|
2018-05-12 21:18:29 +02:00
|
|
|
final ObjectArrayList<Block> parBlocks = par1.toBlock(context);
|
|
|
|
final BlockParenthesis par = new BlockParenthesis(parBlocks);
|
2017-11-01 22:34:35 +01:00
|
|
|
result.add(par);
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2017-11-01 22:34:35 +01:00
|
|
|
result.addAll(sub1);
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2018-09-22 11:17:30 +02:00
|
|
|
if (nearLeft instanceof BlockChar && nearRight instanceof BlockChar && !(par2 instanceof Negative) && !(par1 instanceof Number && par2 instanceof Number)) {
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2017-07-24 23:11:29 +02:00
|
|
|
result.add(new BlockChar(MathematicalSymbols.MULTIPLICATION));
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2017-11-01 22:34:35 +01:00
|
|
|
if (new Expression(context, par2).parenthesisNeeded()) {
|
2018-05-12 21:18:29 +02:00
|
|
|
final ObjectArrayList<Block> parBlocks = par2.toBlock(context);
|
|
|
|
final BlockParenthesis par = new BlockParenthesis(parBlocks);
|
2017-11-01 22:34:35 +01:00
|
|
|
result.add(par);
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2017-11-01 22:34:35 +01:00
|
|
|
result.addAll(sub2);
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2017-07-24 23:11:29 +02:00
|
|
|
return result;
|
2017-05-26 22:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-05-06 18:05:21 +02:00
|
|
|
public boolean isNegative() {
|
|
|
|
return parameter1.equals(new Number(getMathContext(), -1)) || parameter2.equals(new Number(getMathContext(), -1));
|
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-05-06 18:05:21 +02:00
|
|
|
public Function toPositive() {
|
2018-09-28 11:39:28 +02:00
|
|
|
if (parameter1.equals(new Number(getMathContext(), -1))) {
|
2018-05-06 18:05:21 +02:00
|
|
|
return parameter2;
|
2018-09-28 11:39:28 +02:00
|
|
|
} else if (parameter2.equals(new Number(getMathContext(), -1))) {
|
2018-05-06 18:05:21 +02:00
|
|
|
return parameter2;
|
2018-09-28 11:39:28 +02:00
|
|
|
} else {
|
2018-05-06 18:05:21 +02:00
|
|
|
return null;
|
2018-09-28 11:39:28 +02:00
|
|
|
}
|
2018-05-06 18:05:21 +02:00
|
|
|
}
|
2018-05-12 21:18:29 +02:00
|
|
|
|
2018-09-22 11:17:30 +02:00
|
|
|
public static Multiplication newNegative(final MathContext context, final Function value2) {
|
2018-05-06 18:05:21 +02:00
|
|
|
return new Multiplication(context, new Number(context, -1), value2);
|
|
|
|
}
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|