2016-09-19 18:48:27 +02:00
|
|
|
package org.warp.picalculator.math.functions;
|
2016-09-12 16:30:51 +02:00
|
|
|
|
2016-09-19 18:48:27 +02:00
|
|
|
import org.warp.picalculator.Error;
|
2017-05-26 22:37:18 +02:00
|
|
|
import org.warp.picalculator.gui.expression.blocks.Block;
|
|
|
|
import org.warp.picalculator.gui.expression.blocks.BlockChar;
|
2017-07-24 23:11:29 +02:00
|
|
|
import org.warp.picalculator.gui.expression.blocks.BlockParenthesis;
|
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;
|
2017-05-26 22:37:18 +02:00
|
|
|
import org.warp.picalculator.math.MathematicalSymbols;
|
2016-11-25 22:40:43 +01:00
|
|
|
import org.warp.picalculator.math.rules.ExponentRule15;
|
2017-02-10 18:12:17 +01:00
|
|
|
import org.warp.picalculator.math.rules.ExponentRule16;
|
2017-02-02 12:49:31 +01:00
|
|
|
import org.warp.picalculator.math.rules.FractionsRule14;
|
2016-11-02 21:56:40 +01:00
|
|
|
import org.warp.picalculator.math.rules.NumberRule1;
|
|
|
|
import org.warp.picalculator.math.rules.NumberRule2;
|
2016-11-25 22:40:43 +01:00
|
|
|
import org.warp.picalculator.math.rules.NumberRule6;
|
|
|
|
import org.warp.picalculator.math.rules.SyntaxRule1;
|
2016-12-05 22:57:11 +01:00
|
|
|
import org.warp.picalculator.math.rules.methods.MultiplicationMethod1;
|
2016-09-12 16:30:51 +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 Multiplication extends FunctionOperator {
|
2016-09-19 18:48:27 +02:00
|
|
|
|
2017-02-14 20:02:03 +01:00
|
|
|
public Multiplication(MathContext root, Function value1, 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-09-12 16:30:51 +02:00
|
|
|
@Override
|
2016-11-25 22:40:43 +01:00
|
|
|
protected boolean isSolvable() {
|
2017-04-10 22:50:43 +02:00
|
|
|
final Function variable1 = getParameter1();
|
|
|
|
final Function variable2 = getParameter2();
|
2016-10-02 16:01:41 +02:00
|
|
|
if (variable1 instanceof Number & variable2 instanceof Number) {
|
|
|
|
return true;
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
if (SyntaxRule1.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule1.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule2.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (NumberRule6.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ExponentRule15.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-10 18:12:17 +01:00
|
|
|
if (ExponentRule16.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-02 12:49:31 +01:00
|
|
|
if (FractionsRule14.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
if (MultiplicationMethod1.compare(this)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-02 16:01:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-03-03 23:06:43 +01:00
|
|
|
public ObjectArrayList<Function> solve() throws Error {
|
|
|
|
ObjectArrayList<Function> result = new ObjectArrayList<>();
|
2016-11-25 22:40:43 +01:00
|
|
|
if (SyntaxRule1.compare(this)) {
|
|
|
|
result = SyntaxRule1.execute(this);
|
|
|
|
} else if (NumberRule1.compare(this)) {
|
2016-11-02 21:56:40 +01:00
|
|
|
result = NumberRule1.execute(this);
|
|
|
|
} else if (NumberRule2.compare(this)) {
|
|
|
|
result = NumberRule2.execute(this);
|
2016-11-25 22:40:43 +01:00
|
|
|
} else if (NumberRule6.compare(this)) {
|
|
|
|
result = NumberRule6.execute(this);
|
|
|
|
} else if (ExponentRule15.compare(this)) {
|
|
|
|
result = ExponentRule15.execute(this);
|
2017-02-10 18:12:17 +01:00
|
|
|
} else if (ExponentRule16.compare(this)) {
|
|
|
|
result = ExponentRule16.execute(this);
|
2017-02-02 12:49:31 +01:00
|
|
|
} else if (FractionsRule14.compare(this)) {
|
|
|
|
result = FractionsRule14.execute(this);
|
2016-12-05 22:57:11 +01:00
|
|
|
} else if (MultiplicationMethod1.compare(this)) {
|
|
|
|
result = MultiplicationMethod1.execute(this);
|
2017-02-14 20:02:03 +01:00
|
|
|
} else if (parameter1.isSimplified() & parameter2.isSimplified()) {
|
|
|
|
result.add(((Number) parameter1).multiply((Number) parameter2));
|
2016-09-18 14:33:25 +02:00
|
|
|
}
|
|
|
|
return result;
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 22:40:43 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o instanceof Multiplication) {
|
2017-02-14 20:02:03 +01:00
|
|
|
final FunctionOperator f = (FunctionOperator) o;
|
|
|
|
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
|
2016-11-25 22:40:43 +01:00
|
|
|
return true;
|
2017-02-14 20:02:03 +01:00
|
|
|
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
|
2016-11-25 22:40:43 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
public ObjectArrayList<Block> toBlock(MathContext context) throws Error {
|
|
|
|
ObjectArrayList<Block> result = new ObjectArrayList<>();
|
2017-07-24 23:11:29 +02:00
|
|
|
Function par1 = getParameter1();
|
|
|
|
Function par2 = getParameter2();
|
|
|
|
ObjectArrayList<Block> sub1 = par1.toBlock(context);
|
|
|
|
ObjectArrayList<Block> sub2 = par2.toBlock(context);
|
2017-09-24 18:47:30 +02:00
|
|
|
Block nearLeft = sub1.get(sub1.size() - 1);
|
2017-05-26 22:37:18 +02:00
|
|
|
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()) {
|
|
|
|
BlockParenthesis par = new BlockParenthesis();
|
|
|
|
ObjectArrayList<Block> parBlocks = par2.toBlock(context);
|
2017-09-24 18:47:30 +02:00
|
|
|
for (Block b : parBlocks) {
|
2017-07-24 23:11:29 +02:00
|
|
|
par.getNumberContainer().appendBlockUnsafe(b); // Skips recomputeDimension
|
|
|
|
}
|
|
|
|
par.recomputeDimensions(); // Recompute dimensions after appendBlockUnsafe
|
|
|
|
result.add(par);
|
|
|
|
} else {
|
|
|
|
result.addAll(par2.toBlock(context));
|
|
|
|
}
|
|
|
|
return result;
|
2017-05-26 22:37:18 +02:00
|
|
|
} else {
|
2017-07-24 23:11:29 +02:00
|
|
|
result.addAll(sub1);
|
|
|
|
if ((nearLeft instanceof BlockChar && nearRight instanceof BlockChar) && !(par2 instanceof Negative)) {
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-07-24 23:11:29 +02:00
|
|
|
} else {
|
|
|
|
result.add(new BlockChar(MathematicalSymbols.MULTIPLICATION));
|
|
|
|
}
|
|
|
|
result.addAll(sub2);
|
|
|
|
return result;
|
2017-05-26 22:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 16:30:51 +02:00
|
|
|
}
|