Finished rewriting calculus engine.
Finished rewriting calculus engine and affected classes: functions and rules.
This commit is contained in:
parent
36de6ab02a
commit
50e6837eda
28
src/org/warp/picalculator/TestCalcBenchmark.java
Normal file
28
src/org/warp/picalculator/TestCalcBenchmark.java
Normal file
@ -0,0 +1,28 @@
|
||||
package org.warp.picalculator;
|
||||
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
|
||||
public class TestCalcBenchmark {
|
||||
|
||||
public static void main(String[] args) throws Error {
|
||||
Utils.debugOn = true;
|
||||
int times = 1;
|
||||
|
||||
MathContext mc = new MathContext();
|
||||
long time1 = System.nanoTime();
|
||||
mc.parseInputString("5WABCDEFGHIWABCDEFGHIWABCDEFGHIWABCDEFGHIWABCDEFGHIWABCDEFGHI");
|
||||
long time2 = System.nanoTime();
|
||||
for (int i = 0; i < times; i++) {
|
||||
if (i == 1) {
|
||||
Utils.debugOn = false;
|
||||
}
|
||||
mc.f2 = mc.solveExpression(mc.f);
|
||||
}
|
||||
long time3 = System.nanoTime();
|
||||
// System.out.println(mc.f2.get(0).toString());
|
||||
System.out.println("PARSING\t"+((time2-time1) / 1000000d / ((double) times)) + " milliseconds");
|
||||
System.out.println("WORK\t"+((time3-time2) / 1000000d / ((double) times)) + " milliseconds");
|
||||
System.out.println("TOTAL\t"+((time3-time1) / 1000000d / ((double) times)) + " milliseconds");
|
||||
}
|
||||
}
|
@ -22,11 +22,11 @@ import org.nevec.rjm.BigDecimalMath;
|
||||
import org.nevec.rjm.Rational;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.BinaryFont;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Negative;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
@ -127,12 +127,12 @@ public class Utils {
|
||||
public static boolean areThereOnlySettedUpFunctionsSumsEquationsAndSystems(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
|
||||
if (fl.get(i) instanceof AnteriorFunction) {
|
||||
if (((AnteriorFunction) fl.get(i)).getVariable() == null) {
|
||||
if (fl.get(i) instanceof FunctionSingle) {
|
||||
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
|
||||
return false;
|
||||
}
|
||||
} else if (fl.get(i) instanceof FunctionTwoValues) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null || ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
} else if (fl.get(i) instanceof FunctionOperator) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -146,12 +146,12 @@ public class Utils {
|
||||
public static boolean areThereOnlySettedUpFunctionsSumsMultiplicationsEquationsAndSystems(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
|
||||
if (fl.get(i) instanceof AnteriorFunction) {
|
||||
if (((AnteriorFunction) fl.get(i)).getVariable() == null) {
|
||||
if (fl.get(i) instanceof FunctionSingle) {
|
||||
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
|
||||
return false;
|
||||
}
|
||||
} else if (fl.get(i) instanceof FunctionTwoValues) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null || ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
} else if (fl.get(i) instanceof FunctionOperator) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -165,12 +165,12 @@ public class Utils {
|
||||
public static boolean areThereOnlySettedUpFunctionsEquationsAndSystems(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
|
||||
if (fl.get(i) instanceof AnteriorFunction) {
|
||||
if (((AnteriorFunction) fl.get(i)).getVariable() == null) {
|
||||
if (fl.get(i) instanceof FunctionSingle) {
|
||||
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
|
||||
return false;
|
||||
}
|
||||
} else if (fl.get(i) instanceof FunctionTwoValues) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null || ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
} else if (fl.get(i) instanceof FunctionOperator) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -184,12 +184,12 @@ public class Utils {
|
||||
public static boolean areThereOnlySettedUpFunctionsAndSystems(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Equation || fl.get(i) instanceof EquationsSystemPart || fl.get(i) instanceof Expression)) {
|
||||
if (fl.get(i) instanceof AnteriorFunction) {
|
||||
if (((AnteriorFunction) fl.get(i)).getVariable() == null) {
|
||||
if (fl.get(i) instanceof FunctionSingle) {
|
||||
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
|
||||
return false;
|
||||
}
|
||||
} else if (fl.get(i) instanceof FunctionTwoValues) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null || ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
} else if (fl.get(i) instanceof FunctionOperator) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -202,8 +202,8 @@ public class Utils {
|
||||
|
||||
public static boolean areThereOnlyEmptySNFunctions(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (fl.get(i) instanceof AnteriorFunction) {
|
||||
if (((AnteriorFunction) fl.get(i)).getVariable() == null) {
|
||||
if (fl.get(i) instanceof FunctionSingle) {
|
||||
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -213,8 +213,8 @@ public class Utils {
|
||||
|
||||
public static boolean areThereOnlyEmptyNSNFunctions(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (fl.get(i) instanceof FunctionTwoValues && !(fl.get(i) instanceof Sum) && !(fl.get(i) instanceof SumSubtraction) && !(fl.get(i) instanceof Subtraction) && !(fl.get(i) instanceof Multiplication) && !(fl.get(i) instanceof Division)) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null && ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
if (fl.get(i) instanceof FunctionOperator && !(fl.get(i) instanceof Sum) && !(fl.get(i) instanceof SumSubtraction) && !(fl.get(i) instanceof Subtraction) && !(fl.get(i) instanceof Multiplication) && !(fl.get(i) instanceof Division)) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -225,7 +225,7 @@ public class Utils {
|
||||
public static boolean areThereEmptyMultiplications(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null && ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -236,7 +236,7 @@ public class Utils {
|
||||
public static boolean areThereEmptySums(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Subtraction) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null && ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null && ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -247,7 +247,7 @@ public class Utils {
|
||||
public static boolean areThereEmptySystems(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (fl.get(i) instanceof EquationsSystemPart) {
|
||||
if (((EquationsSystemPart) fl.get(i)).getVariable() == null) {
|
||||
if (((EquationsSystemPart) fl.get(i)).getParameter() == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -257,13 +257,13 @@ public class Utils {
|
||||
|
||||
public static boolean areThereOtherSettedUpFunctions(ArrayList<Function> fl) {
|
||||
for (int i = 0; i < fl.size(); i++) {
|
||||
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Expression || fl.get(i) instanceof AnteriorFunction || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division)) {
|
||||
if (fl.get(i) instanceof AnteriorFunction) {
|
||||
if (((AnteriorFunction) fl.get(i)).getVariable() == null) {
|
||||
if (!(fl.get(i) instanceof Number || fl.get(i) instanceof Variable || fl.get(i) instanceof Sum || fl.get(i) instanceof SumSubtraction || fl.get(i) instanceof Expression || fl.get(i) instanceof FunctionSingle || fl.get(i) instanceof Multiplication || fl.get(i) instanceof Division)) {
|
||||
if (fl.get(i) instanceof FunctionSingle) {
|
||||
if (((FunctionSingle) fl.get(i)).getParameter() == null) {
|
||||
return true;
|
||||
}
|
||||
} else if (fl.get(i) instanceof FunctionTwoValues) {
|
||||
if (((FunctionTwoValues) fl.get(i)).getVariable1() == null || ((FunctionTwoValues) fl.get(i)).getVariable2() == null) {
|
||||
} else if (fl.get(i) instanceof FunctionOperator) {
|
||||
if (((FunctionOperator) fl.get(i)).getParameter1() == null || ((FunctionOperator) fl.get(i)).getParameter2() == null) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
@ -358,7 +358,7 @@ public class Utils {
|
||||
final int wsegno = 5;
|
||||
final int hsegno = h1 + 2;
|
||||
|
||||
var.draw(x + wsegno, y + (hsegno - h1));
|
||||
var.draw(x + wsegno, y + (hsegno - h1), null, null);
|
||||
|
||||
DisplayManager.renderer.glDrawLine(x + 1, y + hsegno - 3, x + 1, y + hsegno - 3);
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + hsegno - 2, x + 2, y + hsegno - 2);
|
||||
@ -449,7 +449,7 @@ public class Utils {
|
||||
|
||||
public static boolean allSolved(List<Function> expressions) throws Error {
|
||||
for (final Function itm : expressions) {
|
||||
if (itm.isSolved() == false) {
|
||||
if (itm.isSimplified() == false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -461,7 +461,7 @@ public class Utils {
|
||||
final int size2 = l2.size();
|
||||
int cur1 = 0;
|
||||
int cur2 = 0;
|
||||
final int total = l1.size() * l2.size();
|
||||
final int total = size1 * size2;
|
||||
final Function[][] results = new Function[total][2];
|
||||
for (int i = 0; i < total; i++) {
|
||||
results[i] = new Function[] { l1.get(cur1), l2.get(cur2) };
|
||||
@ -480,6 +480,45 @@ public class Utils {
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
public static Function[][] joinFunctionsResults(ArrayList<ArrayList<Function>> ln) {
|
||||
final int[] sizes = new int[ln.size()];
|
||||
for (int i = 0; i < ln.size(); i++) {
|
||||
sizes[i] = ln.get(i).size();
|
||||
}
|
||||
int[] curs = new int[sizes.length];
|
||||
int total = 0;
|
||||
for (int i = 0; i < ln.size(); i++) {
|
||||
if (i == 0) {
|
||||
total = sizes[i];
|
||||
} else {
|
||||
total *= sizes[i];
|
||||
}
|
||||
}
|
||||
final Function[][] results = new Function[total][sizes.length];
|
||||
for (int i = 0; i < total; i++) {
|
||||
results[i] = new Function[sizes.length];
|
||||
for (int j = 0; j < sizes.length; j++) {
|
||||
results[i][j] = ln.get(j).get(curs[j]);
|
||||
}
|
||||
for (int k = 0; k < sizes.length; k++) {
|
||||
if (i % sizes[k] == 0) {
|
||||
for (int l = 0; l < sizes.length; l++) {
|
||||
if (l != k) {
|
||||
curs[l] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < sizes.length; k++) {
|
||||
if (curs[k] >= sizes[k]) {
|
||||
curs[k] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static boolean isNegative(Function b) {
|
||||
if (b instanceof Negative) {
|
||||
|
11
src/org/warp/picalculator/gui/math/Block.java
Normal file
11
src/org/warp/picalculator/gui/math/Block.java
Normal file
@ -0,0 +1,11 @@
|
||||
package org.warp.picalculator.gui.math;
|
||||
|
||||
public abstract class Block {
|
||||
/**
|
||||
*
|
||||
* @param x Position relative to the window.
|
||||
* @param y Position relative to the window.
|
||||
* @param c Parent container.
|
||||
*/
|
||||
public abstract void draw(int x, int y, Container c);
|
||||
}
|
112
src/org/warp/picalculator/gui/math/Container.java
Normal file
112
src/org/warp/picalculator/gui/math/Container.java
Normal file
@ -0,0 +1,112 @@
|
||||
package org.warp.picalculator.gui.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.device.graphicengine.Display;
|
||||
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
|
||||
import org.warp.picalculator.gui.graphicengine.Renderer;
|
||||
|
||||
public class Container implements GraphicalElement {
|
||||
|
||||
private final int minWidth;
|
||||
private final int minHeight;
|
||||
private final ArrayList<Block> content;
|
||||
private int width;
|
||||
private int height;
|
||||
private int line;
|
||||
|
||||
public Container(int minWidth, int minHeight) {
|
||||
this.minWidth = minWidth;
|
||||
this.minHeight = minHeight;
|
||||
this.content = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Container(int minWidth, int minHeight, ArrayList<Block> content) {
|
||||
this.minWidth = minWidth;
|
||||
this.minHeight = minHeight;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public void addBlock(Block b) {
|
||||
content.add(b);
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public void removeBlock(Block b) {
|
||||
content.remove(b);
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public void removeAt(int i) {
|
||||
content.remove(i);
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public Block getBlockAt(int i) {
|
||||
return content.get(i);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
content.clear();
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param x Position relative to the window.
|
||||
* @param y Position relative to the window.
|
||||
* @param small size of the element.
|
||||
* @param caretPos remaining positions of the caret.
|
||||
* @return <code>caretPos - currentElementLength</code>
|
||||
*/
|
||||
public int draw(GraphicEngine g, int x, int y, boolean small, int caretPos) {
|
||||
Renderer r = g.getRenderer();
|
||||
return caretPos;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recomputeDimensions() {
|
||||
int w = 0;
|
||||
int h = 0;
|
||||
int l = 0;
|
||||
|
||||
for (Block b : content) {
|
||||
w += b.getWidth();
|
||||
}
|
||||
|
||||
if (w > minWidth) {
|
||||
width = w;
|
||||
} else {
|
||||
width = minWidth;
|
||||
}
|
||||
if (h > minHeight) {
|
||||
height = h;
|
||||
} else {
|
||||
height = minHeight;
|
||||
}
|
||||
line = l;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
33
src/org/warp/picalculator/gui/math/GraphicalElement.java
Normal file
33
src/org/warp/picalculator/gui/math/GraphicalElement.java
Normal file
@ -0,0 +1,33 @@
|
||||
package org.warp.picalculator.gui.math;
|
||||
|
||||
public interface GraphicalElement {
|
||||
|
||||
/**
|
||||
* Recompute element's dimension parameters, like <strong>width</strong>, <strong>height</strong>, <strong>line</strong> or <strong>length</strong>.
|
||||
*/
|
||||
public void recomputeDimensions();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Width of the element.
|
||||
*/
|
||||
public int getWidth();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Height of the element.
|
||||
*/
|
||||
public int getHeight();
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Position of the vertical alignment line of the element, relative to itself.
|
||||
*/
|
||||
public int getLine();
|
||||
|
||||
/**
|
||||
* Used to compute the position of the caret.
|
||||
* @return Length (in characters) of the element.
|
||||
*/
|
||||
public int getLength();
|
||||
}
|
@ -4,7 +4,7 @@ import org.warp.picalculator.Main;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.device.Keyboard.Key;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Variable.VariableValue;
|
||||
|
||||
public class ChooseVariableValueScreen extends Screen {
|
||||
|
@ -19,12 +19,12 @@ import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.BinaryFont;
|
||||
import org.warp.picalculator.gui.graphicengine.Renderer;
|
||||
import org.warp.picalculator.math.AngleMode;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionDynamic;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionMultipleValues;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Variable;
|
||||
import org.warp.picalculator.math.functions.Variable.VariableValue;
|
||||
import org.warp.picalculator.math.functions.equations.Equation;
|
||||
@ -37,7 +37,7 @@ public class MathInputScreen extends Screen {
|
||||
public volatile int caretPos = 0;
|
||||
public volatile boolean showCaret = true;
|
||||
public volatile float showCaretDelta = 0f;
|
||||
public Calculator calc;
|
||||
public MathContext calc;
|
||||
public boolean autoscroll;
|
||||
public int scrollX = 0;
|
||||
public int errorLevel = 0; // 0 = nessuno, 1 = risultato, 2 = tutto
|
||||
@ -48,7 +48,7 @@ public class MathInputScreen extends Screen {
|
||||
super();
|
||||
canBeInHistory = true;
|
||||
|
||||
calc = new Calculator();
|
||||
calc = new MathContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -204,7 +204,7 @@ public class MathInputScreen extends Screen {
|
||||
}
|
||||
}
|
||||
final int y = padding + 20 + padding + fontBig.getCharacterHeight() + 1 + topSpacing;
|
||||
fnc.draw(padding + scrollA, y);
|
||||
fnc.draw(padding + scrollA, y, null, null);
|
||||
if (tooLong) {
|
||||
DisplayManager.renderer.glColor(DisplayManager.renderer.glGetClearColor());
|
||||
DisplayManager.renderer.glFillColor(Main.screenSize[0] - 16 - 2, y, fnc.getHeight(), Main.screenSize[0]);
|
||||
@ -221,7 +221,7 @@ public class MathInputScreen extends Screen {
|
||||
int bottomSpacing = 0;
|
||||
for (final Function f : calc.f2) {
|
||||
bottomSpacing += f.getHeight() + 2;
|
||||
f.draw(DisplayManager.engine.getWidth() - 2 - f.getWidth(), DisplayManager.engine.getHeight() - bottomSpacing);
|
||||
f.draw(DisplayManager.engine.getWidth() - 2 - f.getWidth(), DisplayManager.engine.getHeight() - bottomSpacing, null, null);
|
||||
}
|
||||
if (calc.resultsCount > 1 && calc.resultsCount != calc.f2.size()) {
|
||||
final String resultsCountText = calc.resultsCount + " total results".toUpperCase();
|
||||
@ -558,8 +558,8 @@ public class MathInputScreen extends Screen {
|
||||
} else {
|
||||
results.add(f);
|
||||
for (final Function itm : results) {
|
||||
if (itm.isSolved() == false) {
|
||||
final List<Function> dt = itm.solveOneStep();
|
||||
if (itm.isSimplified() == false) {
|
||||
final List<Function> dt = itm.simplify();
|
||||
partialResults.addAll(dt);
|
||||
} else {
|
||||
partialResults.add(itm);
|
||||
@ -582,7 +582,7 @@ public class MathInputScreen extends Screen {
|
||||
results.addAll(hs);
|
||||
calc.f2 = results;
|
||||
for (final Function rf : calc.f2) {
|
||||
rf.generateGraphics();
|
||||
rf.recomputeDimensions();
|
||||
}
|
||||
}
|
||||
Utils.debug.println(calc.f2.toString());
|
||||
@ -625,7 +625,7 @@ public class MathInputScreen extends Screen {
|
||||
results.addAll(hs);
|
||||
calc.f2 = results;
|
||||
for (final Function rf : calc.f2) {
|
||||
rf.generateGraphics();
|
||||
rf.recomputeDimensions();
|
||||
}
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
@ -730,12 +730,12 @@ public class MathInputScreen extends Screen {
|
||||
private ArrayList<Function> getKnownVariables(Function[] fncs) {
|
||||
final ArrayList<Function> res = new ArrayList<>();
|
||||
for (final Function f : fncs) {
|
||||
if (f instanceof FunctionTwoValues) {
|
||||
res.addAll(getKnownVariables(new Function[] { ((FunctionTwoValues) f).getVariable1(), ((FunctionTwoValues) f).getVariable2() }));
|
||||
} else if (f instanceof FunctionMultipleValues) {
|
||||
res.addAll(getKnownVariables(((FunctionMultipleValues) f).getVariables()));
|
||||
} else if (f instanceof AnteriorFunction) {
|
||||
res.addAll(getKnownVariables(new Function[] { ((AnteriorFunction) f).getVariable() }));
|
||||
if (f instanceof FunctionOperator) {
|
||||
res.addAll(getKnownVariables(new Function[] { ((FunctionOperator) f).getParameter1(), ((FunctionOperator) f).getParameter2() }));
|
||||
} else if (f instanceof FunctionDynamic) {
|
||||
res.addAll(getKnownVariables(((FunctionDynamic) f).getParameters()));
|
||||
} else if (f instanceof FunctionSingle) {
|
||||
res.addAll(getKnownVariables(new Function[] { ((FunctionSingle) f).getParameter() }));
|
||||
} else if (f instanceof Variable) {
|
||||
if (((Variable)f).getType() == Variable.V_TYPE.KNOWN) {
|
||||
if (!res.contains(f)) {
|
||||
|
@ -4,7 +4,7 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Main;
|
||||
import org.warp.picalculator.device.Keyboard.Key;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
|
||||
public class SolveEquationScreen extends Screen {
|
||||
|
||||
|
56
src/org/warp/picalculator/math/Function.java
Normal file
56
src/org/warp/picalculator/math/Function.java
Normal file
@ -0,0 +1,56 @@
|
||||
package org.warp.picalculator.math;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
|
||||
public interface Function {
|
||||
|
||||
/**
|
||||
* Returns this function and its children in a string form.
|
||||
* @return This function and its children in a string form.
|
||||
*/
|
||||
@Override
|
||||
public String toString();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o);
|
||||
|
||||
/**
|
||||
* Deep clone this function.
|
||||
* @return A clone of this function.
|
||||
*/
|
||||
public Function clone();
|
||||
|
||||
/**
|
||||
* Generic method to change a parameter in a known position.
|
||||
* @param index parameter index.
|
||||
* @param var parameter.
|
||||
* @return A new instance of this function.
|
||||
*/
|
||||
public Function setParameter(int index, Function var) throws IndexOutOfBoundsException;
|
||||
|
||||
/**
|
||||
* Generic method to retrieve a parameter in a known position.
|
||||
* @param index parameter index.
|
||||
* @return The requested parameter.
|
||||
*/
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException;
|
||||
|
||||
/**
|
||||
* Retrieve the current Math Context used by this function
|
||||
* @return Calculator mathContext
|
||||
*/
|
||||
public MathContext getMathContext();
|
||||
|
||||
/**
|
||||
* Simplify the current function or it's children
|
||||
*/
|
||||
public List<Function> simplify() throws Error;
|
||||
|
||||
/**
|
||||
* The current simplification status of this function and it's childrens
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isSimplified();
|
||||
}
|
165
src/org/warp/picalculator/math/FunctionDynamic.java
Normal file
165
src/org/warp/picalculator/math/FunctionDynamic.java
Normal file
@ -0,0 +1,165 @@
|
||||
package org.warp.picalculator.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public abstract class FunctionDynamic implements Function {
|
||||
public FunctionDynamic(MathContext root) {
|
||||
this.root = root;
|
||||
functions = new Function[] {};
|
||||
}
|
||||
|
||||
public FunctionDynamic(Function[] values) {
|
||||
if (values.length > 0) {
|
||||
root = values[0].getMathContext();
|
||||
} else {
|
||||
throw new NullPointerException("Nessun elemento nell'array. Impossibile ricavare il nodo root");
|
||||
}
|
||||
functions = values;
|
||||
}
|
||||
|
||||
public FunctionDynamic(MathContext root, Function[] values) {
|
||||
this.root = root;
|
||||
functions = values;
|
||||
}
|
||||
|
||||
protected final MathContext root;
|
||||
protected Function[] functions;
|
||||
|
||||
public Function[] getParameters() {
|
||||
return Arrays.copyOf(functions, functions.length);
|
||||
}
|
||||
|
||||
public FunctionDynamic setParameters(final List<Function> value) {
|
||||
FunctionDynamic f = this.clone();
|
||||
final int vsize = value.size();
|
||||
final Function[] tmp = new Function[vsize];
|
||||
for (int i = 0; i < vsize; i++) {
|
||||
tmp[i] = value.get(i);
|
||||
}
|
||||
f.functions = tmp;
|
||||
return f;
|
||||
}
|
||||
|
||||
public FunctionDynamic setParameters(final Function[] value) {
|
||||
FunctionDynamic f = this.clone();
|
||||
f.functions = value;
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) {
|
||||
return functions[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionDynamic setParameter(int index, Function value) {
|
||||
FunctionDynamic f = this.clone();
|
||||
f.functions[index] = value;
|
||||
return f;
|
||||
}
|
||||
|
||||
public FunctionDynamic appendParameter(Function value) {
|
||||
FunctionDynamic f = this.clone();
|
||||
f.functions = Arrays.copyOf(f.functions, f.functions.length + 1);
|
||||
f.functions[f.functions.length - 1] = value;
|
||||
return f;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the current number of parameters.
|
||||
* @return The number of parameters.
|
||||
*/
|
||||
public int getParametersLength() {
|
||||
return functions.length;
|
||||
}
|
||||
|
||||
public FunctionDynamic setParametersLength(int length) {
|
||||
FunctionDynamic f = this.clone();
|
||||
f.functions = Arrays.copyOf(functions, length);
|
||||
return f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimplified() {
|
||||
for (final Function variable : functions) {
|
||||
if (variable.isSimplified() == false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return !isSolvable();
|
||||
}
|
||||
|
||||
/**
|
||||
* The current simplification status of this function, assuming that its children are already simplified.
|
||||
* @return <strong>true</strong> if this function can be solved, otherwise <strong>false</strong>.
|
||||
*/
|
||||
protected abstract boolean isSolvable();
|
||||
|
||||
@Override
|
||||
public final ArrayList<Function> simplify() throws Error {
|
||||
boolean solved = true;
|
||||
Function[] fncs = getParameters();
|
||||
for (Function f : fncs) {
|
||||
if (f.isSimplified() == false) {
|
||||
solved = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ArrayList<Function> result = solved ? solve() : null;
|
||||
|
||||
if (result == null || result.isEmpty()) {
|
||||
result = new ArrayList<>();
|
||||
|
||||
final ArrayList<ArrayList<Function>> ln = new ArrayList<>();
|
||||
for (int i = 0; i < fncs.length; i++) {
|
||||
ArrayList<Function> l = new ArrayList<>();
|
||||
if (fncs[i].isSimplified()) {
|
||||
l.add(fncs[i]);
|
||||
} else {
|
||||
l.addAll(fncs[i].simplify());
|
||||
}
|
||||
ln.add(l);
|
||||
}
|
||||
|
||||
final Function[][] results = Utils.joinFunctionsResults(ln);
|
||||
|
||||
for (final Function[] f : results) {
|
||||
result.add(this.setParameters(f));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Solves only this function, assuming that its children are already simplified and it can be solved.
|
||||
* @return The solved function.
|
||||
* @throws Error Errors during computation, like a/0 or similar.
|
||||
*/
|
||||
protected abstract ArrayList<Function> solve() throws Error;
|
||||
|
||||
@Override
|
||||
public MathContext getMathContext() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract FunctionDynamic clone();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return functions.hashCode() + 883 * super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return false;
|
||||
}
|
||||
}
|
164
src/org/warp/picalculator/math/FunctionOperator.java
Normal file
164
src/org/warp/picalculator/math/FunctionOperator.java
Normal file
@ -0,0 +1,164 @@
|
||||
package org.warp.picalculator.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
|
||||
public abstract class FunctionOperator implements Function {
|
||||
|
||||
/**
|
||||
* Create a new instance of FunctionOperator. The Math Context will be the same of <strong>value1</strong>'s.
|
||||
* @throws NullPointerException when value1 is null.
|
||||
* @param value1 The parameter of this function.
|
||||
* @param value2 The parameter of this function.
|
||||
*/
|
||||
public FunctionOperator(Function value1, Function value2) throws NullPointerException {
|
||||
this.mathContext = value1.getMathContext();
|
||||
parameter1 = value1;
|
||||
parameter2 = value2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of FunctionOperator.
|
||||
* @param value1 The parameter of this function.
|
||||
* @param value2 The parameter of this function.
|
||||
*/
|
||||
public FunctionOperator(MathContext mc, Function value1, Function value2) {
|
||||
this.mathContext = mc;
|
||||
parameter1 = value1;
|
||||
parameter2 = value2;
|
||||
}
|
||||
|
||||
protected final MathContext mathContext;
|
||||
|
||||
protected Function parameter1 = null;
|
||||
protected Function parameter2 = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return First parameter.
|
||||
*/
|
||||
public Function getParameter1() {
|
||||
return parameter1;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Second parameter.
|
||||
*/
|
||||
public Function getParameter2() {
|
||||
return parameter2;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param var First parameter.
|
||||
* @return A new instance of this function.
|
||||
*/
|
||||
public FunctionOperator setParameter1(Function var) {
|
||||
FunctionOperator s = this.clone();
|
||||
s.parameter1 = var;
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param var Second parameter.
|
||||
* @return A new instance of this function.
|
||||
*/
|
||||
public FunctionOperator setParameter2(Function var) {
|
||||
FunctionOperator s = this.clone();
|
||||
s.parameter2 = var;
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionOperator setParameter(int index, Function var) throws IndexOutOfBoundsException {
|
||||
switch(index) {
|
||||
case 0:
|
||||
return this.setParameter1(var);
|
||||
case 1:
|
||||
return this.setParameter2(var);
|
||||
default:
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException {
|
||||
switch(index) {
|
||||
case 0:
|
||||
return this.getParameter1();
|
||||
case 1:
|
||||
return this.getParameter2();
|
||||
default:
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MathContext getMathContext() {
|
||||
return mathContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimplified() {
|
||||
return (parameter1.isSimplified() & parameter2.isSimplified()) ? !isSolvable() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The current simplification status of this function, assuming that its children are already simplified.
|
||||
* @return <strong>true</strong> if this function can be solved, otherwise <strong>false</strong>.
|
||||
*/
|
||||
protected abstract boolean isSolvable();
|
||||
|
||||
@Override
|
||||
public final ArrayList<Function> simplify() throws Error {
|
||||
final boolean solved = parameter1.isSimplified() & parameter2.isSimplified();
|
||||
ArrayList<Function> result = solved ? solve() : null;;
|
||||
|
||||
if (result == null || result.isEmpty()) {
|
||||
result = new ArrayList<>();
|
||||
|
||||
final ArrayList<Function> l1 = new ArrayList<>();
|
||||
final ArrayList<Function> l2 = new ArrayList<>();
|
||||
if (parameter1.isSimplified()) {
|
||||
l1.add(parameter1);
|
||||
} else {
|
||||
l1.addAll(parameter1.simplify());
|
||||
}
|
||||
if (parameter2.isSimplified()) {
|
||||
l2.add(parameter2);
|
||||
} else {
|
||||
l2.addAll(parameter2.simplify());
|
||||
}
|
||||
|
||||
final Function[][] results = Utils.joinFunctionsResults(l1, l2);
|
||||
|
||||
for (final Function[] f : results) {
|
||||
result.add(this.setParameter1(f[0]).setParameter2(f[1]));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Solves only this function, assuming that its children are already simplified and it can be solved.
|
||||
* @return The solved function.
|
||||
* @throws Error Errors during computation, like a/0 or similar.
|
||||
*/
|
||||
protected abstract ArrayList<Function> solve() throws Error;
|
||||
|
||||
@Override
|
||||
public abstract FunctionOperator clone();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return parameter1.hashCode() + 7 * parameter2.hashCode() + 883 * super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object o);
|
||||
}
|
130
src/org/warp/picalculator/math/FunctionSingle.java
Normal file
130
src/org/warp/picalculator/math/FunctionSingle.java
Normal file
@ -0,0 +1,130 @@
|
||||
package org.warp.picalculator.math;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
|
||||
public abstract class FunctionSingle implements Function {
|
||||
|
||||
/**
|
||||
* Create a new instance of FunctionSingle. The Math Context will be the same of <strong>value</strong>'s.
|
||||
* @throws NullPointerException when value is null.
|
||||
* @param value The parameter of this function.
|
||||
*/
|
||||
public FunctionSingle(Function value) throws NullPointerException {
|
||||
mathContext = value.getMathContext();
|
||||
parameter = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of FunctionSingle.
|
||||
* @param mathContext Math Context
|
||||
* @param value The parameter of this function.
|
||||
*/
|
||||
public FunctionSingle(MathContext mathContext, Function value) {
|
||||
this.mathContext = mathContext;
|
||||
parameter = value;
|
||||
}
|
||||
|
||||
private final MathContext mathContext;
|
||||
|
||||
/**
|
||||
* Function parameter.<br>
|
||||
* <u>MUST NOT BE MODIFIED IF ALREADY SET UP.</u>
|
||||
*/
|
||||
protected Function parameter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Parameter.
|
||||
*/
|
||||
public Function getParameter() {
|
||||
return parameter;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param var Parameter.
|
||||
* @return A new instance of this function.
|
||||
*/
|
||||
public FunctionSingle setParameter(Function value) {
|
||||
FunctionSingle s = this.clone();
|
||||
s.parameter = value;
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionSingle setParameter(int index, Function var) throws IndexOutOfBoundsException {
|
||||
if (index == 0) {
|
||||
return this.setParameter(var);
|
||||
} else {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException {
|
||||
if (index == 0) {
|
||||
return this.getParameter();
|
||||
} else {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public MathContext getMathContext() {
|
||||
return mathContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ArrayList<Function> simplify() throws Error {
|
||||
final boolean simplified = parameter.isSimplified();
|
||||
ArrayList<Function> result = simplified ? solve() : null;
|
||||
|
||||
if (result == null || result.isEmpty()) {
|
||||
result = new ArrayList<>();
|
||||
|
||||
final ArrayList<Function> l1 = new ArrayList<>();
|
||||
if (parameter.isSimplified()) {
|
||||
l1.add(parameter);
|
||||
} else {
|
||||
l1.addAll(parameter.simplify());
|
||||
}
|
||||
|
||||
for (final Function f : l1) {
|
||||
result.add(this.setParameter(f));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Solves only this function, assuming that its children are already simplified and it can be solved.
|
||||
* @return The solved function.
|
||||
* @throws Error Errors during computation, like a/0 or similar.
|
||||
*/
|
||||
protected abstract ArrayList<Function> solve() throws Error;
|
||||
|
||||
@Override
|
||||
public boolean isSimplified() {
|
||||
return parameter.isSimplified() ? !isSolvable() : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The current simplification status of this function, assuming that its children are already simplified.
|
||||
* @return <strong>true</strong> if this function can be solved, otherwise <strong>false</strong>.
|
||||
*/
|
||||
protected abstract boolean isSolvable();
|
||||
|
||||
@Override
|
||||
public abstract FunctionSingle clone();
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return parameter.hashCode() + 883 * super.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object o);
|
||||
}
|
@ -7,13 +7,12 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Errors;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Variable.VariableValue;
|
||||
import org.warp.picalculator.math.functions.equations.Equation;
|
||||
import org.warp.picalculator.math.functions.equations.EquationsSystem;
|
||||
|
||||
public class Calculator {
|
||||
public class MathContext {
|
||||
|
||||
public AngleMode angleMode = AngleMode.DEG;
|
||||
public boolean exactMode = false;
|
||||
@ -22,7 +21,7 @@ public class Calculator {
|
||||
public ArrayList<VariableValue> variablesValues;
|
||||
public int resultsCount;
|
||||
|
||||
public Calculator() {
|
||||
public MathContext() {
|
||||
f = new ArrayList<>();
|
||||
f2 = new ArrayList<>();
|
||||
variablesValues = new ArrayList<>();
|
||||
@ -37,7 +36,7 @@ public class Calculator {
|
||||
final String[] parts = string.substring(1).split("\\{");
|
||||
final EquationsSystem s = new EquationsSystem(this);
|
||||
for (final String part : parts) {
|
||||
s.addFunctionToEnd(parseEquationString(part));
|
||||
s.appendParameter(parseEquationString(part));
|
||||
}
|
||||
return s;
|
||||
} else if (string.contains("=")) {
|
||||
@ -50,15 +49,9 @@ public class Calculator {
|
||||
public Function parseEquationString(String string) throws Error {
|
||||
final String[] parts = string.split("=");
|
||||
if (parts.length == 1) {
|
||||
final Equation e = new Equation(this, null, null);
|
||||
e.setVariable1(new Expression(this, parts[0]));
|
||||
e.setVariable2(new Number(this, BigInteger.ZERO));
|
||||
return e;
|
||||
return new Equation(this, new Expression(this, parts[0]), new Number(this, BigInteger.ZERO));
|
||||
} else if (parts.length == 2) {
|
||||
final Equation e = new Equation(this, null, null);
|
||||
e.setVariable1(new Expression(this, parts[0]));
|
||||
e.setVariable2(new Expression(this, parts[1]));
|
||||
return e;
|
||||
return new Equation(this, new Expression(this, parts[0]), new Expression(this, parts[1]));
|
||||
} else {
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
@ -74,11 +67,11 @@ public class Calculator {
|
||||
results.add(f);
|
||||
while (Utils.allSolved(results) == false) {
|
||||
for (final Function itm : results) {
|
||||
if (itm.isSolved() == false) {
|
||||
if (itm.isSimplified() == false) {
|
||||
final long t1 = System.currentTimeMillis();
|
||||
final List<Function> dt = itm.solveOneStep();
|
||||
final List<Function> dt = itm.simplify();
|
||||
final long t2 = System.currentTimeMillis();
|
||||
if (t2 - t1 >= 3000) {
|
||||
if (!Utils.debugOn & (t2 - t1 >= 3000)) {
|
||||
throw new Error(Errors.TIMEOUT);
|
||||
}
|
||||
partialResults.addAll(dt);
|
||||
@ -117,13 +110,6 @@ public class Calculator {
|
||||
}
|
||||
}
|
||||
f = fncs;
|
||||
for (final Function f : f) {
|
||||
try {
|
||||
f.generateGraphics();
|
||||
} catch (final NullPointerException ex) {
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*public void solve(EquationScreen equationScreen, char letter) throws Error {
|
@ -1,150 +0,0 @@
|
||||
package org.warp.picalculator.math.functions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public abstract class AnteriorFunction implements Function {
|
||||
public AnteriorFunction(Function value) {
|
||||
root = value.getRoot();
|
||||
variable = value;
|
||||
}
|
||||
|
||||
public AnteriorFunction(Calculator root, Function value) {
|
||||
this.root = root;
|
||||
variable = value;
|
||||
}
|
||||
|
||||
protected abstract Function NewInstance(Calculator root, Function value);
|
||||
|
||||
protected final Calculator root;
|
||||
protected Function variable;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int line;
|
||||
protected boolean small;
|
||||
|
||||
public Function getVariable() {
|
||||
return variable;
|
||||
}
|
||||
|
||||
public void setVariable(Function value) {
|
||||
variable = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String getSymbol();
|
||||
|
||||
@Override
|
||||
public final ArrayList<Function> solveOneStep() throws Error {
|
||||
final boolean solved = variable.isSolved();
|
||||
ArrayList<Function> result = solved ? solve() : null;
|
||||
|
||||
if (result == null || result.isEmpty()) {
|
||||
result = new ArrayList<>();
|
||||
|
||||
final ArrayList<Function> l1 = new ArrayList<>();
|
||||
if (variable.isSolved()) {
|
||||
l1.add(variable);
|
||||
} else {
|
||||
l1.addAll(variable.solveOneStep());
|
||||
}
|
||||
|
||||
for (final Function f : l1) {
|
||||
result.add(NewInstance(root, f));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected abstract ArrayList<Function> solve() throws Error;
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
return variable.isSolved() ? !isSolvable() : false;
|
||||
}
|
||||
|
||||
protected abstract boolean isSolvable();
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable.setSmall(small);
|
||||
variable.generateGraphics();
|
||||
|
||||
width = Utils.getFont(small).getStringWidth(getSymbol()) + 1 + getVariable().getWidth();
|
||||
height = variable.getHeight();
|
||||
line = variable.getLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
final float h1 = getVariable().getHeight();
|
||||
final int wsegno = Utils.getFont(small).getStringWidth(getSymbol());
|
||||
final float hsegno = Utils.getFontHeight(small);
|
||||
final float maxh = getHeight();
|
||||
Utils.getFont(small).use(DisplayManager.engine);
|
||||
|
||||
DisplayManager.renderer.glDrawStringLeft(x, (int) Math.floor(y + (maxh - hsegno) / 2), getSymbol());
|
||||
getVariable().draw(x + wsegno + 1, (int) Math.floor(y + (maxh - h1) / 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// try {
|
||||
// return solve().toString();
|
||||
String val1 = "null";
|
||||
if (variable != null) {
|
||||
val1 = variable.toString();
|
||||
}
|
||||
return getSymbol() + val1;
|
||||
// } catch (Error e) {
|
||||
// return e.id.toString();
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnteriorFunction clone() {
|
||||
final Cloner cloner = new Cloner();
|
||||
return cloner.deepClone(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return variable.hashCode() + 883 * getSymbol().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object o);
|
||||
}
|
@ -6,7 +6,10 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.gui.math.GraphicalElement;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.rules.FractionsRule1;
|
||||
import org.warp.picalculator.math.rules.FractionsRule11;
|
||||
@ -15,24 +18,16 @@ import org.warp.picalculator.math.rules.FractionsRule2;
|
||||
import org.warp.picalculator.math.rules.FractionsRule3;
|
||||
import org.warp.picalculator.math.rules.UndefinedRule2;
|
||||
|
||||
public class Division extends FunctionTwoValues {
|
||||
public class Division extends FunctionOperator {
|
||||
|
||||
public Division(Calculator root, Function value1, Function value2) {
|
||||
public Division(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
return new Division(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return MathematicalSymbols.DIVISION;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
Function variable1 = getParameter1();
|
||||
Function variable2 = getParameter2();
|
||||
if (FractionsRule1.compare(this)) {
|
||||
return true;
|
||||
}
|
||||
@ -52,7 +47,7 @@ public class Division extends FunctionTwoValues {
|
||||
return true;
|
||||
}
|
||||
if (variable1 instanceof Number && variable2 instanceof Number) {
|
||||
if (root.exactMode) {
|
||||
if (getMathContext().exactMode) {
|
||||
try {
|
||||
return ((Number)variable1).divide((Number)variable2).isInteger();
|
||||
} catch (Error e) {
|
||||
@ -67,6 +62,8 @@ public class Division extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
Function variable1 = getParameter1();
|
||||
Function variable2 = getParameter2();
|
||||
ArrayList<Function> result = new ArrayList<>();
|
||||
if (FractionsRule1.compare(this)) {
|
||||
result = FractionsRule1.execute(this);
|
||||
@ -85,148 +82,18 @@ public class Division extends FunctionTwoValues {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean hasMinus() {
|
||||
final String numerator = variable1.toString();
|
||||
if (numerator.startsWith("-")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void draw(int x, int y, boolean small, boolean drawMinus) {
|
||||
final boolean beforedrawminus = this.drawMinus;
|
||||
this.drawMinus = drawMinus;
|
||||
draw(x, y);
|
||||
this.drawMinus = beforedrawminus;
|
||||
}
|
||||
|
||||
private boolean drawMinus = true;
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable1.generateGraphics();
|
||||
|
||||
variable2.generateGraphics();
|
||||
|
||||
width = calcWidth();
|
||||
height = calcHeight();
|
||||
line = variable1.getHeight() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
// glColor3f(255, 127-50+new Random().nextInt(50), 0);
|
||||
// glFillRect(x,y,width,height);
|
||||
// glColor3f(0, 0, 0);
|
||||
|
||||
final Object var1 = variable1;
|
||||
final Object var2 = variable2;
|
||||
boolean minus = false;
|
||||
int minusw = 0;
|
||||
int minush = 0;
|
||||
String numerator = ((Function) var1).toString();
|
||||
if (numerator.startsWith("-") && ((Function) var1) instanceof Number) {
|
||||
minus = true;
|
||||
numerator = numerator.substring(1);
|
||||
}
|
||||
int w1 = 0;
|
||||
int h1 = 0;
|
||||
Utils.getFont(small).use(DisplayManager.engine);
|
||||
if (minus) {
|
||||
w1 = Utils.getFont(small).getStringWidth(numerator);
|
||||
h1 = Utils.getFont(small).getCharacterHeight();
|
||||
} else {
|
||||
w1 = ((Function) var1).getWidth();
|
||||
h1 = ((Function) var1).getHeight();
|
||||
}
|
||||
final int w2 = ((Function) var2).getWidth();
|
||||
int maxw;
|
||||
if (w1 > w2) {
|
||||
maxw = 1 + w1;
|
||||
} else {
|
||||
maxw = 1 + w2;
|
||||
}
|
||||
if (minus && drawMinus) {
|
||||
minusw = Utils.getFont(small).getCharacterWidth() /* Width of minus */ + 1;
|
||||
minush = Utils.getFont(small).getCharacterHeight();
|
||||
DisplayManager.renderer.glDrawStringLeft(x + 1, y + h1 + 1 + 1 - (minush / 2), "-");
|
||||
DisplayManager.renderer.glDrawStringLeft((int) (x + 1 + minusw + 1 + (maxw - w1) / 2d), y, numerator);
|
||||
} else {
|
||||
((Function) var1).draw((int) (x + 1 + minusw + (maxw - w1) / 2d), y);
|
||||
}
|
||||
((Function) var2).draw((int) (x + 1 + minusw + (maxw - w2) / 2d), y + h1 + 1 + 1 + 1);
|
||||
DisplayManager.renderer.glFillColor(x + 1 + minusw, y + h1 + 1, maxw, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int calcHeight() {
|
||||
|
||||
boolean minus = false;
|
||||
String numerator = variable1.toString();
|
||||
if (numerator.startsWith("-") && variable1 instanceof Number) {
|
||||
minus = true;
|
||||
numerator = numerator.substring(1);
|
||||
}
|
||||
int h1 = 0;
|
||||
if (minus) {
|
||||
h1 = Utils.getFontHeight(small);
|
||||
} else {
|
||||
h1 = variable1.getHeight();
|
||||
}
|
||||
final int h2 = variable2.getHeight();
|
||||
return h1 + 3 + h2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int calcWidth() {
|
||||
boolean minus = false;
|
||||
String numerator = variable1.toString();
|
||||
if (numerator.startsWith("-") && variable1 instanceof Number) {
|
||||
minus = true;
|
||||
numerator = numerator.substring(1);
|
||||
}
|
||||
int w1 = 0;
|
||||
if (minus) {
|
||||
w1 = Utils.getFont(small).getStringWidth(numerator);
|
||||
} else {
|
||||
w1 = variable1.getWidth();
|
||||
}
|
||||
final int w2 = variable2.getWidth();
|
||||
int maxw = 0;
|
||||
if (w1 > w2) {
|
||||
maxw = w1 + 1;
|
||||
} else {
|
||||
maxw = w2 + 1;
|
||||
}
|
||||
if (minus && drawMinus) {
|
||||
return 1 + Utils.getFont(small).getCharacterWidth() /* Width of minus */ + 1 + maxw;
|
||||
} else {
|
||||
return 1 + maxw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Division) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
return variable1.equals(f.variable1) && variable2.equals(f.variable2);
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
return getParameter1().equals(f.getParameter1()) && getParameter2().equals(f.getParameter2());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionOperator clone() {
|
||||
return new Division(this.getMathContext(), this.getParameter1(), this.getParameter2());
|
||||
}
|
||||
}
|
@ -1,76 +1,58 @@
|
||||
package org.warp.picalculator.math.functions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
|
||||
public class EmptyNumber implements Function {
|
||||
|
||||
public EmptyNumber(Calculator root) {
|
||||
public EmptyNumber(MathContext root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
private final Calculator root;
|
||||
private final MathContext root;
|
||||
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return " ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Function> solveOneStep() throws Error {
|
||||
public ArrayList<Function> simplify() throws Error {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
public boolean isSimplified() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
DisplayManager.renderer.glDrawStringLeft(x, y, "␀");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return Utils.getFont(small).getStringWidth("␀");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return Utils.getFont(small).getCharacterHeight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return Utils.getFont(small).getCharacterHeight() / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
public MathContext getMathContext() {
|
||||
return root;
|
||||
}
|
||||
|
||||
private boolean small = false;
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof EmptyNumber;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function clone() {
|
||||
return new EmptyNumber(root);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function setParameter(int index, Function var) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,7 +13,11 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Errors;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionDynamic;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.trigonometry.ArcCosine;
|
||||
import org.warp.picalculator.math.functions.trigonometry.ArcSine;
|
||||
@ -22,27 +26,27 @@ import org.warp.picalculator.math.functions.trigonometry.Cosine;
|
||||
import org.warp.picalculator.math.functions.trigonometry.Sine;
|
||||
import org.warp.picalculator.math.functions.trigonometry.Tangent;
|
||||
|
||||
public class Expression extends FunctionMultipleValues {
|
||||
public class Expression extends FunctionDynamic {
|
||||
|
||||
public Expression(Calculator root) {
|
||||
public Expression(MathContext root) {
|
||||
super(root);
|
||||
}
|
||||
|
||||
public Expression(Calculator root, Function[] values) {
|
||||
public Expression(MathContext root, Function[] values) {
|
||||
super(root, values);
|
||||
}
|
||||
|
||||
public Expression(Calculator root, Function value) {
|
||||
public Expression(MathContext root, Function value) {
|
||||
super(root, new Function[]{value});
|
||||
}
|
||||
|
||||
private boolean initialParenthesis = false;
|
||||
|
||||
public Expression(Calculator root, String string) throws Error {
|
||||
public Expression(MathContext root, String string) throws Error {
|
||||
this(root, string, "", true);
|
||||
}
|
||||
|
||||
public Expression(Calculator root, String string, String debugSpaces, boolean initialParenthesis) throws Error {
|
||||
public Expression(MathContext root, String string, String debugSpaces, boolean initialParenthesis) throws Error {
|
||||
super(root);
|
||||
this.initialParenthesis = initialParenthesis;
|
||||
boolean isNumber = false;
|
||||
@ -65,7 +69,7 @@ public class Expression extends FunctionMultipleValues {
|
||||
// If the expression is already a number:
|
||||
// Se l'espressione è già un numero:
|
||||
final Number t = new Number(root, string);
|
||||
setVariables(new Function[] { t });
|
||||
functions = new Function[] { t };
|
||||
Utils.debug.println(debugSpaces + "•Result:" + t.toString());
|
||||
} else {
|
||||
// Else prepare the expression:
|
||||
@ -215,8 +219,8 @@ public class Expression extends FunctionMultipleValues {
|
||||
debugSpaces += " ";
|
||||
|
||||
// Convert the expression to a list of objects
|
||||
final Expression imputRawParenthesis = new Expression(root);
|
||||
imputRawParenthesis.setVariables(new Function[] {});
|
||||
Expression imputRawParenthesis = new Expression(root);
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.setParameters(new Function[] {});
|
||||
String tmp = "";
|
||||
final String[] functions = concat(concat(concat(concat(MathematicalSymbols.functions(), MathematicalSymbols.parentheses()), MathematicalSymbols.signums(true)), MathematicalSymbols.variables()), MathematicalSymbols.genericSyntax());
|
||||
for (int i = 0; i < processExpression.length(); i++) {
|
||||
@ -322,30 +326,30 @@ public class Expression extends FunctionMultipleValues {
|
||||
if (f instanceof Expression) {
|
||||
tmp = "";
|
||||
} else if (f instanceof Variable) {
|
||||
if (imputRawParenthesis.getVariablesLength() == 0) {
|
||||
if (imputRawParenthesis.getParametersLength() == 0) {
|
||||
if (tmp.length() > 0) {
|
||||
imputRawParenthesis.addFunctionToEnd(new Number(root, tmp));
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Number(root, tmp));
|
||||
Utils.debug.println(debugSpaces + "•Added number to expression:" + tmp);
|
||||
imputRawParenthesis.addFunctionToEnd(new Multiplication(root, null, null));
|
||||
Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getSymbol());
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Multiplication(root, null, null));
|
||||
Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getClass().getSimpleName());
|
||||
}
|
||||
} else {
|
||||
final Function precedentFunction = imputRawParenthesis.getVariable(imputRawParenthesis.getVariablesLength() - 1);
|
||||
final Function precedentFunction = imputRawParenthesis.getParameter(imputRawParenthesis.getParametersLength() - 1);
|
||||
if (tmp.length() > 0) {
|
||||
if (precedentFunction instanceof Number || precedentFunction instanceof Variable) {
|
||||
imputRawParenthesis.addFunctionToEnd(new Multiplication(root, null, null));
|
||||
Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getSymbol());
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Multiplication(root, null, null));
|
||||
Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getClass().getSimpleName());
|
||||
}
|
||||
if (tmp.equals("-")) {
|
||||
imputRawParenthesis.addFunctionToEnd(new Subtraction(root, null, null));
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Subtraction(root, null, null));
|
||||
} else {
|
||||
imputRawParenthesis.addFunctionToEnd(new Number(root, tmp));
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Number(root, tmp));
|
||||
Utils.debug.println(debugSpaces + "•Added number to expression:" + tmp);
|
||||
}
|
||||
}
|
||||
if (tmp.length() > 0 || (precedentFunction instanceof Number || precedentFunction instanceof Variable)) {
|
||||
imputRawParenthesis.addFunctionToEnd(new Multiplication(root, null, null));
|
||||
Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getSymbol());
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Multiplication(root, null, null));
|
||||
Utils.debug.println(debugSpaces + "•Added multiplication to expression:" + new Multiplication(root, null, null).getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@ -355,12 +359,12 @@ public class Expression extends FunctionMultipleValues {
|
||||
tmp = "-1";
|
||||
}
|
||||
}
|
||||
imputRawParenthesis.addFunctionToEnd(new Number(root, tmp));
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Number(root, tmp));
|
||||
Utils.debug.println(debugSpaces + "•Added number to expression:" + tmp);
|
||||
}
|
||||
}
|
||||
imputRawParenthesis.addFunctionToEnd(f);
|
||||
Utils.debug.println(debugSpaces + "•Added variable to expression:" + f.getSymbol() + (f instanceof Number ? " (number)" : " (variable)"));
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(f);
|
||||
Utils.debug.println(debugSpaces + "•Added variable to expression:" + f.getClass().getSimpleName() + (f instanceof Number ? " (number)" : " (variable)"));
|
||||
tmp = "";
|
||||
} else {
|
||||
try {
|
||||
@ -378,7 +382,7 @@ public class Expression extends FunctionMultipleValues {
|
||||
if (tmp.length() > 0) {
|
||||
Utils.debug.println(debugSpaces + "•Added variable to expression:" + tmp);
|
||||
try {
|
||||
imputRawParenthesis.addFunctionToEnd(new Number(root, tmp));
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.appendParameter(new Number(root, tmp));
|
||||
} catch (final NumberFormatException ex) {
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
@ -394,13 +398,13 @@ public class Expression extends FunctionMultipleValues {
|
||||
// Fine suddivisione di insieme
|
||||
|
||||
Utils.debug.println(debugSpaces + "•Removing useless parentheses");
|
||||
for (int i = 0; i < imputRawParenthesis.functions.length; i++) {
|
||||
if (imputRawParenthesis.functions[i] instanceof Expression) {
|
||||
final Expression par = (Expression) imputRawParenthesis.functions[i];
|
||||
if (par.functions.length == 1) {
|
||||
final Function subFunz = par.functions[0];
|
||||
for (int i = 0; i < imputRawParenthesis.getParametersLength(); i++) {
|
||||
if (imputRawParenthesis.getParameter(i) instanceof Expression) {
|
||||
final Expression par = (Expression) imputRawParenthesis.getParameter(i);
|
||||
if (par.getParametersLength() == 1) {
|
||||
final Function subFunz = par.getParameter(0);
|
||||
if (subFunz instanceof Expression || subFunz instanceof Number || subFunz instanceof Variable) {
|
||||
imputRawParenthesis.functions[i] = subFunz;
|
||||
imputRawParenthesis = (Expression) imputRawParenthesis.setParameter(i, subFunz);
|
||||
Utils.debug.println(debugSpaces + " •Useless parentheses removed");
|
||||
}
|
||||
}
|
||||
@ -410,7 +414,7 @@ public class Expression extends FunctionMultipleValues {
|
||||
// Inizia l'affinazione dell'espressione
|
||||
Utils.debug.println(debugSpaces + "•Pushing classes...");
|
||||
|
||||
final Function[] oldFunctionsArray = imputRawParenthesis.getVariables();
|
||||
final Function[] oldFunctionsArray = imputRawParenthesis.getParameters();
|
||||
final ArrayList<Function> oldFunctionsList = new ArrayList<>();
|
||||
for (int i = 0; i < oldFunctionsArray.length; i++) {
|
||||
Function funzione = oldFunctionsArray[i];
|
||||
@ -458,15 +462,15 @@ public class Expression extends FunctionMultipleValues {
|
||||
}
|
||||
Utils.debug.println(debugSpaces + " •Phase: " + step);
|
||||
while (i < oldFunctionsList.size() && change == false && oldFunctionsList.size() > 1) {
|
||||
final Function funzioneTMP = oldFunctionsList.get(i);
|
||||
if (funzioneTMP instanceof FunctionTwoValues) {
|
||||
Function funzioneTMP = oldFunctionsList.get(i);
|
||||
if (funzioneTMP instanceof FunctionOperator) {
|
||||
if (step != "SN Functions") {
|
||||
if ((step == "sums" && (funzioneTMP instanceof Sum || funzioneTMP instanceof SumSubtraction || funzioneTMP instanceof Subtraction) == true && ((funzioneTMP instanceof AnteriorFunction && ((AnteriorFunction) funzioneTMP).variable == null) || (funzioneTMP instanceof FunctionTwoValues && ((FunctionTwoValues) funzioneTMP).variable1 == null && ((FunctionTwoValues) funzioneTMP).variable2 == null) || (!(funzioneTMP instanceof AnteriorFunction) && !(funzioneTMP instanceof FunctionTwoValues)))) || (step.equals("multiplications") && ((funzioneTMP instanceof Multiplication) || (funzioneTMP instanceof Division)) && ((FunctionTwoValues) funzioneTMP).variable1 == null && ((FunctionTwoValues) funzioneTMP).variable2 == null) || (step == "NSN Functions" && (funzioneTMP instanceof Sum) == false && (funzioneTMP instanceof SumSubtraction) == false && (funzioneTMP instanceof Subtraction) == false && (funzioneTMP instanceof Multiplication) == false && (funzioneTMP instanceof Division) == false && ((funzioneTMP instanceof AnteriorFunction && ((AnteriorFunction) funzioneTMP).variable == null) || (funzioneTMP instanceof FunctionTwoValues && ((FunctionTwoValues) funzioneTMP).variable1 == null && ((FunctionTwoValues) funzioneTMP).variable2 == null) || (!(funzioneTMP instanceof AnteriorFunction) && !(funzioneTMP instanceof FunctionTwoValues))))) {
|
||||
if ((step == "sums" && (funzioneTMP instanceof Sum || funzioneTMP instanceof SumSubtraction || funzioneTMP instanceof Subtraction) == true && ((funzioneTMP instanceof FunctionSingle && ((FunctionSingle) funzioneTMP).getParameter() == null) || (funzioneTMP instanceof FunctionOperator && ((FunctionOperator) funzioneTMP).getParameter1() == null && ((FunctionOperator) funzioneTMP).getParameter2() == null) || (!(funzioneTMP instanceof FunctionSingle) && !(funzioneTMP instanceof FunctionOperator)))) || (step.equals("multiplications") && ((funzioneTMP instanceof Multiplication) || (funzioneTMP instanceof Division)) && ((FunctionOperator) funzioneTMP).getParameter1() == null && ((FunctionOperator) funzioneTMP).getParameter2() == null) || (step == "NSN Functions" && (funzioneTMP instanceof Sum) == false && (funzioneTMP instanceof SumSubtraction) == false && (funzioneTMP instanceof Subtraction) == false && (funzioneTMP instanceof Multiplication) == false && (funzioneTMP instanceof Division) == false && ((funzioneTMP instanceof FunctionSingle && ((FunctionSingle) funzioneTMP).getParameter() == null) || (funzioneTMP instanceof FunctionOperator && ((FunctionOperator) funzioneTMP).getParameter1() == null && ((FunctionOperator) funzioneTMP).getParameter2() == null) || (!(funzioneTMP instanceof FunctionSingle) && !(funzioneTMP instanceof FunctionOperator))))) {
|
||||
change = true;
|
||||
|
||||
if (i + 1 < oldFunctionsList.size() && i - 1 >= 0) {
|
||||
((FunctionTwoValues) funzioneTMP).setVariable1(oldFunctionsList.get(i - 1));
|
||||
((FunctionTwoValues) funzioneTMP).setVariable2(oldFunctionsList.get(i + 1));
|
||||
funzioneTMP = ((FunctionOperator) funzioneTMP).setParameter1(oldFunctionsList.get(i - 1));
|
||||
funzioneTMP = ((FunctionOperator) funzioneTMP).setParameter2(oldFunctionsList.get(i + 1));
|
||||
oldFunctionsList.set(i, funzioneTMP);
|
||||
|
||||
// è importante togliere prima gli elementi
|
||||
@ -475,15 +479,15 @@ public class Expression extends FunctionMultipleValues {
|
||||
oldFunctionsList.remove(i + 1);
|
||||
oldFunctionsList.remove(i - 1);
|
||||
|
||||
Utils.debug.println(debugSpaces + " •Set variable to expression:" + funzioneTMP.getSymbol());
|
||||
Utils.debug.println(debugSpaces + " •Set variable to expression:" + funzioneTMP.getClass().getSimpleName());
|
||||
try {
|
||||
Utils.debug.println(debugSpaces + " " + "var1=" + ((FunctionTwoValues) funzioneTMP).getVariable1().toString());
|
||||
Utils.debug.println(debugSpaces + " " + "var1=" + ((FunctionOperator) funzioneTMP).getParameter1().toString());
|
||||
} catch (final NullPointerException ex2) {}
|
||||
try {
|
||||
Utils.debug.println(debugSpaces + " " + "var2=" + ((FunctionTwoValues) funzioneTMP).getVariable2().toString());
|
||||
Utils.debug.println(debugSpaces + " " + "var2=" + ((FunctionOperator) funzioneTMP).getParameter2().toString());
|
||||
} catch (final NullPointerException ex2) {}
|
||||
try {
|
||||
Utils.debug.println(debugSpaces + " " + "(result)=" + ((FunctionTwoValues) funzioneTMP).toString());
|
||||
Utils.debug.println(debugSpaces + " " + "(result)=" + ((FunctionOperator) funzioneTMP).toString());
|
||||
} catch (final NullPointerException ex2) {}
|
||||
|
||||
} else {
|
||||
@ -491,15 +495,15 @@ public class Expression extends FunctionMultipleValues {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (funzioneTMP instanceof AnteriorFunction) {
|
||||
if ((step == "SN Functions" && ((AnteriorFunction) funzioneTMP).variable == null)) {
|
||||
} else if (funzioneTMP instanceof FunctionSingle) {
|
||||
if ((step == "SN Functions" && ((FunctionSingle) funzioneTMP).getParameter() == null)) {
|
||||
if (i + 1 < oldFunctionsList.size()) {
|
||||
final Function nextFunc = oldFunctionsList.get(i + 1);
|
||||
if (nextFunc instanceof AnteriorFunction && ((AnteriorFunction) nextFunc).variable == null) {
|
||||
if (nextFunc instanceof FunctionSingle && ((FunctionSingle) nextFunc).getParameter() == null) {
|
||||
|
||||
} else {
|
||||
change = true;
|
||||
((AnteriorFunction) funzioneTMP).setVariable(nextFunc);
|
||||
funzioneTMP = ((FunctionSingle) funzioneTMP).setParameter(nextFunc);
|
||||
oldFunctionsList.set(i, funzioneTMP);
|
||||
|
||||
// è importante togliere prima gli elementi in
|
||||
@ -507,8 +511,8 @@ public class Expression extends FunctionMultipleValues {
|
||||
// scalano da destra a sinistra.
|
||||
oldFunctionsList.remove(i + 1);
|
||||
|
||||
Utils.debug.println(debugSpaces + " •Set variable to expression:" + funzioneTMP.getSymbol());
|
||||
final Function var = ((AnteriorFunction) funzioneTMP).getVariable();
|
||||
Utils.debug.println(debugSpaces + " •Set variable to expression:" + funzioneTMP.getClass().getSimpleName());
|
||||
final Function var = ((FunctionSingle) funzioneTMP).getParameter();
|
||||
if (var == null) {
|
||||
Utils.debug.println(debugSpaces + " " + "var=null");
|
||||
} else {
|
||||
@ -533,9 +537,9 @@ public class Expression extends FunctionMultipleValues {
|
||||
} while (((oldFunctionsList.size() != before || step != "sums") && oldFunctionsList.size() > 1));
|
||||
}
|
||||
if (oldFunctionsList.isEmpty()) {
|
||||
setVariables(new Function[] { new Number(root, 0) });
|
||||
super.functions = new Function[] { new Number(root, 0) };
|
||||
} else {
|
||||
setVariables(oldFunctionsList);
|
||||
super.functions = oldFunctionsList.toArray(new Function[oldFunctionsList.size()]);
|
||||
}
|
||||
|
||||
dsl = debugSpaces.length();
|
||||
@ -550,18 +554,13 @@ public class Expression extends FunctionMultipleValues {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return "Parentesi";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (functions.length > 1) {
|
||||
if (getParametersLength() > 1) {
|
||||
return true;
|
||||
} else if (functions.length == 1) {
|
||||
final Function f = functions[0];
|
||||
if (f.isSolved() == false) {
|
||||
} else if (getParametersLength() == 1) {
|
||||
final Function f = getParameter(0);
|
||||
if (f.isSimplified() == false) {
|
||||
return true;
|
||||
} else {
|
||||
return !parenthesisNeeded();
|
||||
@ -571,14 +570,14 @@ public class Expression extends FunctionMultipleValues {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Function> solveOneStep() throws Error {
|
||||
final List<Function> ret = new ArrayList<>();
|
||||
if (functions.length == 1) {
|
||||
if (functions[0].isSolved() || !parenthesisNeeded()) {
|
||||
ret.add(functions[0]);
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
final ArrayList<Function> ret = new ArrayList<>();
|
||||
if (getParametersLength() == 1) {
|
||||
if (getParameter(0).isSimplified() || !parenthesisNeeded()) {
|
||||
ret.add(getParameter(0));
|
||||
return ret;
|
||||
} else {
|
||||
final List<Function> l = functions[0].solveOneStep();
|
||||
final List<Function> l = getParameter(0).simplify();
|
||||
for (final Function f : l) {
|
||||
if (f instanceof Number || f instanceof Variable) {
|
||||
ret.add(f);
|
||||
@ -589,9 +588,9 @@ public class Expression extends FunctionMultipleValues {
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
for (final Function f : functions) {
|
||||
if (f.isSolved() == false) {
|
||||
final List<Function> partial = f.solveOneStep();
|
||||
for (final Function f : getParameters()) {
|
||||
if (f.isSimplified() == false) {
|
||||
final List<Function> partial = f.simplify();
|
||||
for (final Function fnc : partial) {
|
||||
ret.add(new Expression(root, new Function[] { fnc }));
|
||||
}
|
||||
@ -600,35 +599,23 @@ public class Expression extends FunctionMultipleValues {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
for (final Function var : functions) {
|
||||
var.setSmall(small);
|
||||
var.generateGraphics();
|
||||
}
|
||||
|
||||
width = calcWidth();
|
||||
height = calcHeight();
|
||||
line = calcLine();
|
||||
}
|
||||
|
||||
|
||||
public boolean parenthesisNeeded() {
|
||||
boolean parenthesisneeded = true;
|
||||
if (initialParenthesis) {
|
||||
parenthesisneeded = false;
|
||||
} else {
|
||||
if (functions.length == 1) {
|
||||
final Function f = functions[0];
|
||||
if (getParametersLength() == 1) {
|
||||
final Function f = getParameter(0);
|
||||
if (f instanceof Number || f instanceof Variable || f instanceof Expression || f instanceof Division || f instanceof Joke || f instanceof Undefined || f instanceof Power || f instanceof Sine || f instanceof Cosine || f instanceof Tangent || f instanceof ArcSine || f instanceof ArcCosine || f instanceof ArcTangent || f instanceof RootSquare) {
|
||||
parenthesisneeded = false;
|
||||
}
|
||||
if (f instanceof Multiplication) {
|
||||
if (((Multiplication) f).getVariable1() instanceof Number) {
|
||||
parenthesisneeded = !(((Multiplication) f).getVariable2() instanceof Variable);
|
||||
} else if (((Multiplication) f).getVariable2() instanceof Number) {
|
||||
parenthesisneeded = !(((Multiplication) f).getVariable1() instanceof Variable);
|
||||
} else if (((Multiplication) f).getVariable1() instanceof Variable || ((Multiplication) f).getVariable2() instanceof Variable) {
|
||||
if (((Multiplication) f).getParameter1() instanceof Number) {
|
||||
parenthesisneeded = !(((Multiplication) f).getParameter2() instanceof Variable);
|
||||
} else if (((Multiplication) f).getParameter2() instanceof Number) {
|
||||
parenthesisneeded = !(((Multiplication) f).getParameter1() instanceof Variable);
|
||||
} else if (((Multiplication) f).getParameter1() instanceof Variable || ((Multiplication) f).getParameter2() instanceof Variable) {
|
||||
parenthesisneeded = false;
|
||||
}
|
||||
}
|
||||
@ -636,137 +623,26 @@ public class Expression extends FunctionMultipleValues {
|
||||
}
|
||||
return parenthesisneeded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
if (parenthesisNeeded() == false) {
|
||||
functions[0].draw(x, y);
|
||||
} else {
|
||||
final float miny = y;
|
||||
final float maxy = y + getHeight();
|
||||
final int h = getHeight();
|
||||
x += 1;
|
||||
DisplayManager.renderer.glDrawLine(x, y + 2, x, y + 2);
|
||||
DisplayManager.renderer.glDrawLine( x + 1, y + 1, x + 1, y + 1);
|
||||
DisplayManager.renderer.glDrawLine( x + 2, y, x + 2, y);
|
||||
|
||||
DisplayManager.renderer.glDrawLine(x, y + 2, x, y + h - 3);
|
||||
|
||||
DisplayManager.renderer.glDrawLine(x, y + h - 3, x, y + h - 3);
|
||||
DisplayManager.renderer.glDrawLine(x + 1, y + h - 2, x + 1, y + h - 2);
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + h - 1, x + 2, y + h - 1);
|
||||
x += 4;
|
||||
for (final Function f : functions) {
|
||||
final float fheight = f.getHeight();
|
||||
final float y2 = miny + ((maxy - miny) / 2 - fheight / 2);
|
||||
f.draw(x, (int) y2);
|
||||
x += f.getWidth();
|
||||
}
|
||||
x += 2;
|
||||
DisplayManager.renderer.glDrawLine(x, y, x, y);
|
||||
DisplayManager.renderer.glDrawLine(x + 1, y + 1, x + 1, y + 1);
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + 2, x + 2, y + 2);
|
||||
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + 2, x + 2, y + h - 3);
|
||||
|
||||
DisplayManager.renderer.glDrawLine(x, y + h - 1, x, y + h - 1);
|
||||
DisplayManager.renderer.glDrawLine(x + 1, y + h - 2, x + 1, y + h - 2);
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + h - 3, x + 2, y + h - 3);
|
||||
x += 4;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
private int calcWidth() {
|
||||
if (parenthesisNeeded() == false) {
|
||||
return functions[0].getWidth();
|
||||
} else {
|
||||
int w = 0;
|
||||
for (final Function f : functions) {
|
||||
w += f.getWidth();
|
||||
}
|
||||
return 1 + 4 + w + 2 + 4;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
private int calcHeight() {
|
||||
if (initialParenthesis || functions.length == 1) {
|
||||
return functions[0].getHeight();
|
||||
} else {
|
||||
Function tmin = null;
|
||||
Function tmax = null;
|
||||
for (final Function t : functions) {
|
||||
if (tmin == null || t.getLine() >= tmin.getLine()) {
|
||||
tmin = t;
|
||||
}
|
||||
if (tmax == null || t.getHeight() - t.getLine() >= tmax.getHeight() - tmax.getLine()) {
|
||||
tmax = t;
|
||||
}
|
||||
}
|
||||
if (tmin == null) {
|
||||
return Utils.getFontHeight(small);
|
||||
}
|
||||
return tmin.getLine() + tmax.getHeight() - tmax.getLine();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
private int calcLine() {
|
||||
if (initialParenthesis || functions.length == 1) {
|
||||
return functions[0].getLine();
|
||||
} else {
|
||||
Function tl = null;
|
||||
for (final Function t : functions) {
|
||||
if (tl == null || t.getLine() >= tl.getLine()) {
|
||||
tl = t;
|
||||
}
|
||||
}
|
||||
if (tl == null) {
|
||||
return Utils.getFontHeight(small) / 2;
|
||||
}
|
||||
return tl.getLine();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String vars = "null";
|
||||
if (functions != null && functions.length > 0) {
|
||||
if (functions.length == 1) {
|
||||
if (functions[0] != null) {
|
||||
vars = functions[0].toString();
|
||||
}
|
||||
} else {
|
||||
for (final Function variable : functions) {
|
||||
if (variable != null) {
|
||||
vars += ", " + variable.toString();
|
||||
}
|
||||
}
|
||||
vars = vars.substring(2);
|
||||
String s = "(";
|
||||
if (functions.length > 0) {
|
||||
for (Function f : functions) {
|
||||
s+=f.toString()+",";
|
||||
}
|
||||
s = s.substring(0, s.length()-1);
|
||||
}
|
||||
return "(" + vars + ")";
|
||||
s+=")";
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Expression) {
|
||||
final Expression f = (Expression) o;
|
||||
final Function[] exprFuncs1 = functions;
|
||||
final Function[] exprFuncs2 = f.functions;
|
||||
final Function[] exprFuncs1 = getParameters();
|
||||
final Function[] exprFuncs2 = f.getParameters();
|
||||
if (exprFuncs1.length == exprFuncs2.length) {
|
||||
for (int i = 0; i < exprFuncs1.length; i++) {
|
||||
if (exprFuncs1[i].equals(exprFuncs2[i]) == false) {
|
||||
@ -775,13 +651,18 @@ public class Expression extends FunctionMultipleValues {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else if (o != null & getVariablesLength() == 1) {
|
||||
} else if (o != null & getParametersLength() == 1) {
|
||||
final Function f = (Function) o;
|
||||
return (functions[0].equals(f));
|
||||
} else if (o == null & getVariablesLength() == 0) {
|
||||
return (getParameter(0).equals(f));
|
||||
} else if (o == null & getParametersLength() == 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Expression clone() {
|
||||
return new Expression(root, functions);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,34 +0,0 @@
|
||||
package org.warp.picalculator.math.functions;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
|
||||
public interface Function {
|
||||
public String getSymbol();
|
||||
|
||||
public List<Function> solveOneStep() throws Error;
|
||||
|
||||
public boolean isSolved();
|
||||
|
||||
public void generateGraphics() throws NullPointerException;
|
||||
|
||||
public void draw(int x, int y);
|
||||
|
||||
public int getWidth();
|
||||
|
||||
public int getHeight();
|
||||
|
||||
public int getLine();
|
||||
|
||||
public Calculator getRoot();
|
||||
|
||||
public void setSmall(boolean small);
|
||||
|
||||
@Override
|
||||
public int hashCode();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o);
|
||||
}
|
@ -1,129 +0,0 @@
|
||||
package org.warp.picalculator.math.functions;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public abstract class FunctionMultipleValues implements Function {
|
||||
public FunctionMultipleValues(Calculator root) {
|
||||
this.root = root;
|
||||
functions = new Function[] {};
|
||||
}
|
||||
|
||||
public FunctionMultipleValues(Function[] values) {
|
||||
if (values.length > 0) {
|
||||
root = values[0].getRoot();
|
||||
} else {
|
||||
throw new NullPointerException("Nessun elemento nell'array. Impossibile ricavare il nodo root");
|
||||
}
|
||||
functions = values;
|
||||
}
|
||||
|
||||
public FunctionMultipleValues(Calculator root, Function[] values) {
|
||||
this.root = root;
|
||||
functions = values;
|
||||
}
|
||||
|
||||
protected final Calculator root;
|
||||
protected Function[] functions;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int line;
|
||||
protected boolean small;
|
||||
|
||||
public Function[] getVariables() {
|
||||
return functions;
|
||||
}
|
||||
|
||||
public void setVariables(final List<Function> value) {
|
||||
final int vsize = value.size();
|
||||
final Function[] tmp = new Function[vsize];
|
||||
for (int i = 0; i < vsize; i++) {
|
||||
tmp[i] = value.get(i);
|
||||
}
|
||||
functions = tmp;
|
||||
}
|
||||
|
||||
public void setVariables(final Function[] value) {
|
||||
functions = value;
|
||||
}
|
||||
|
||||
public Function getVariable(int index) {
|
||||
return functions[index];
|
||||
}
|
||||
|
||||
public void setVariable(int index, Function value) {
|
||||
functions[index] = value;
|
||||
}
|
||||
|
||||
public void addFunctionToEnd(Function value) {
|
||||
final int index = functions.length;
|
||||
setVariablesLength(index + 1);
|
||||
functions[index] = value;
|
||||
}
|
||||
|
||||
public int getVariablesLength() {
|
||||
return functions.length;
|
||||
}
|
||||
|
||||
public void setVariablesLength(int length) {
|
||||
functions = Arrays.copyOf(functions, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String getSymbol();
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
for (final Function variable : functions) {
|
||||
if (!variable.isSolved()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return !isSolvable();
|
||||
}
|
||||
|
||||
protected abstract boolean isSolvable();
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract void generateGraphics();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
// try {
|
||||
// return solve().toString();
|
||||
return "TODO: fare una nuova alternativa a solve().toString()";
|
||||
// } catch (Error e) {
|
||||
// return e.id.toString();
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function clone() {
|
||||
final Cloner cloner = new Cloner();
|
||||
return cloner.deepClone(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return functions.hashCode() + 883 * getSymbol().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,197 +0,0 @@
|
||||
package org.warp.picalculator.math.functions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public abstract class FunctionTwoValues implements Function {
|
||||
public FunctionTwoValues(Calculator root, Function value1, Function value2) {
|
||||
this.root = root;
|
||||
variable1 = value1;
|
||||
variable2 = value2;
|
||||
}
|
||||
|
||||
protected abstract Function NewInstance(Calculator root, Function value1, Function value2);
|
||||
|
||||
protected final Calculator root;
|
||||
|
||||
protected Function variable1 = null;
|
||||
protected Function variable2 = null;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int line;
|
||||
protected boolean small;
|
||||
|
||||
public Function getVariable1() {
|
||||
return variable1;
|
||||
}
|
||||
|
||||
public void setVariable1(Function value) {
|
||||
variable1 = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
public Function getVariable2() {
|
||||
return variable2;
|
||||
}
|
||||
|
||||
public void setVariable2(Function value) {
|
||||
variable2 = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract String getSymbol();
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
return (variable1.isSolved() & variable2.isSolved()) ? !isSolvable() : false;
|
||||
}
|
||||
|
||||
protected abstract boolean isSolvable();
|
||||
|
||||
@Override
|
||||
public final ArrayList<Function> solveOneStep() throws Error {
|
||||
final boolean solved = variable1.isSolved() & variable2.isSolved();
|
||||
ArrayList<Function> result = solved ? solve() : null;;
|
||||
|
||||
if (result == null || result.isEmpty()) {
|
||||
result = new ArrayList<>();
|
||||
|
||||
final ArrayList<Function> l1 = new ArrayList<>();
|
||||
final ArrayList<Function> l2 = new ArrayList<>();
|
||||
if (variable1.isSolved()) {
|
||||
l1.add(variable1);
|
||||
} else {
|
||||
l1.addAll(variable1.solveOneStep());
|
||||
}
|
||||
if (variable2.isSolved()) {
|
||||
l2.add(variable2);
|
||||
} else {
|
||||
l2.addAll(variable2.solveOneStep());
|
||||
}
|
||||
|
||||
final Function[][] results = Utils.joinFunctionsResults(l1, l2);
|
||||
|
||||
for (final Function[] f : results) {
|
||||
result.add(NewInstance(root, f[0], f[1]));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected abstract ArrayList<Function> solve() throws Error;
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable1.setSmall(small);
|
||||
variable1.generateGraphics();
|
||||
|
||||
variable2.setSmall(small);
|
||||
variable2.generateGraphics();
|
||||
|
||||
width = calcWidth();
|
||||
height = calcHeight();
|
||||
line = calcLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
final int ln = getLine();
|
||||
int dx = 0;
|
||||
variable1.draw(dx + x, ln - variable1.getLine() + y);
|
||||
dx += 1 + variable1.getWidth();
|
||||
if (drawSignum()) {
|
||||
Utils.getFont(small).use(DisplayManager.engine);
|
||||
DisplayManager.renderer.glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol());
|
||||
dx += Utils.getFont(small).getStringWidth(getSymbol());
|
||||
}
|
||||
variable2.draw(dx + x, ln - variable2.getLine() + y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String val1 = "null";
|
||||
String val2 = "null";
|
||||
if (variable1 != null) {
|
||||
val1 = variable1.toString();
|
||||
}
|
||||
if (variable2 != null) {
|
||||
val2 = variable2.toString();
|
||||
}
|
||||
return val1 + getSymbol() + val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionTwoValues clone() {
|
||||
final Cloner cloner = new Cloner();
|
||||
return cloner.deepClone(this);
|
||||
}
|
||||
|
||||
public boolean drawSignum() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
}
|
||||
|
||||
protected int calcWidth() {
|
||||
return variable1.getWidth() + 1 + (drawSignum() ? Utils.getFont(small).getStringWidth(getSymbol()) : 0) + variable2.getWidth();
|
||||
}
|
||||
|
||||
protected int calcHeight() {
|
||||
|
||||
Function tmin = variable1;
|
||||
Function tmax = variable1;
|
||||
if (tmin == null || variable2.getLine() >= tmin.getLine()) {
|
||||
tmin = variable2;
|
||||
}
|
||||
if (tmax == null || variable2.getHeight() - variable2.getLine() >= tmax.getHeight() - tmax.getLine()) {
|
||||
tmax = variable2;
|
||||
}
|
||||
return tmin.getLine() + tmax.getHeight() - tmax.getLine();
|
||||
}
|
||||
|
||||
protected int calcLine() {
|
||||
Function tl = variable1;
|
||||
if (tl == null || variable2.getLine() >= tl.getLine()) {
|
||||
tl = variable2;
|
||||
}
|
||||
return tl.getLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return variable1.hashCode() + 7 * variable2.hashCode() + 883 * getSymbol().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object o);
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package org.warp.picalculator.math.functions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
@ -7,7 +8,8 @@ import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.BinaryFont;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
|
||||
public class Joke implements Function {
|
||||
|
||||
@ -17,78 +19,41 @@ public class Joke implements Function {
|
||||
private static final String[] jokes = new String[] { "♓", "TORNADO", "SHARKNADO" };
|
||||
private static final int[] jokesFont = new int[] { 4, -1, -1 };
|
||||
private final byte joke;
|
||||
private final Calculator root;
|
||||
private final MathContext root;
|
||||
|
||||
public Joke(Calculator root, byte joke) {
|
||||
public Joke(MathContext root, byte joke) {
|
||||
this.root = root;
|
||||
this.joke = joke;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return "joke";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Function> solveOneStep() throws Error {
|
||||
public ArrayList<Function> simplify() throws Error {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
public boolean isSimplified() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
final BinaryFont rf = DisplayManager.renderer.getCurrentFont();
|
||||
if (jokesFont[joke] >= 0) {
|
||||
DisplayManager.fonts[jokesFont[joke]].use(DisplayManager.engine);
|
||||
}
|
||||
DisplayManager.renderer.glDrawStringLeft(x, y, jokes[joke]);
|
||||
if (jokesFont[joke] >= 0) {
|
||||
rf.use(DisplayManager.engine);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
if (jokesFont[joke] >= 0) {
|
||||
return DisplayManager.fonts[jokesFont[joke]].getStringWidth(jokes[joke]);
|
||||
} else {
|
||||
return Utils.getFont(small).getStringWidth(jokes[joke]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
if (jokesFont[joke] >= 0) {
|
||||
return DisplayManager.fonts[jokesFont[joke]].getCharacterHeight();
|
||||
} else {
|
||||
return Utils.getFont(small).getCharacterHeight();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return getHeight() / 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
public MathContext getMathContext() {
|
||||
return root;
|
||||
}
|
||||
|
||||
private boolean small = false;
|
||||
@Override
|
||||
public Function clone() {
|
||||
return new Joke(root, joke);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
public Function setParameter(int index, Function var) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,7 +3,10 @@ package org.warp.picalculator.math.functions;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.rules.ExponentRule15;
|
||||
import org.warp.picalculator.math.rules.ExponentRule16;
|
||||
@ -14,28 +17,20 @@ import org.warp.picalculator.math.rules.NumberRule6;
|
||||
import org.warp.picalculator.math.rules.SyntaxRule1;
|
||||
import org.warp.picalculator.math.rules.methods.MultiplicationMethod1;
|
||||
|
||||
public class Multiplication extends FunctionTwoValues {
|
||||
public class Multiplication extends FunctionOperator {
|
||||
|
||||
public Multiplication(Calculator root, Function value1, Function value2) {
|
||||
public Multiplication(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
if (value1 instanceof Variable && value2 instanceof Variable == false) {
|
||||
variable1 = value2;
|
||||
variable2 = value1;
|
||||
parameter1 = value2;
|
||||
parameter2 = value1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
return new Multiplication(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return MathematicalSymbols.MULTIPLICATION;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
Function variable1 = getParameter1();
|
||||
Function variable2 = getParameter2();
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
@ -85,92 +80,36 @@ public class Multiplication extends FunctionTwoValues {
|
||||
result = FractionsRule14.execute(this);
|
||||
} else if (MultiplicationMethod1.compare(this)) {
|
||||
result = MultiplicationMethod1.execute(this);
|
||||
} else if (variable1.isSolved() & variable2.isSolved()) {
|
||||
result.add(((Number) variable1).multiply((Number) variable2));
|
||||
} else if (parameter1.isSimplified() & parameter2.isSimplified()) {
|
||||
result.add(((Number) parameter1).multiply((Number) parameter2));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawSignum() {
|
||||
final Function[] tmpVar = new Function[] { variable1, variable2 };
|
||||
final boolean[] ok = new boolean[] { false, false };
|
||||
for (int val = 0; val < 2; val++) {
|
||||
while (!ok[val]) {
|
||||
if (tmpVar[val] instanceof Division) {
|
||||
ok[0] = true;
|
||||
ok[1] = true;
|
||||
} else if (tmpVar[val] instanceof Variable) {
|
||||
ok[val] = true;
|
||||
} else if (tmpVar[val] instanceof Number) {
|
||||
if (val == 0) {
|
||||
ok[val] = true;
|
||||
} else {
|
||||
if (!(tmpVar[0] instanceof Number)) {
|
||||
ok[val] = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (tmpVar[val] instanceof Power) {
|
||||
tmpVar[val] = ((Power) tmpVar[val]).variable1;
|
||||
} else if (tmpVar[val] instanceof Root) {
|
||||
if (val == 0) {
|
||||
break;
|
||||
}
|
||||
ok[val] = true;
|
||||
} else if (tmpVar[val] instanceof RootSquare) {
|
||||
if (val == 0) {
|
||||
break;
|
||||
}
|
||||
ok[val] = true;
|
||||
} else if (tmpVar[val] instanceof Undefined) {
|
||||
break;
|
||||
} else if (tmpVar[val] instanceof Joke) {
|
||||
break;
|
||||
} else if (tmpVar[val] instanceof Negative) {
|
||||
if (val == 1) {
|
||||
break;
|
||||
}
|
||||
ok[val] = true;
|
||||
} else if (tmpVar[val] instanceof Expression) {
|
||||
if (((Expression)tmpVar[val]).parenthesisNeeded() == true) {
|
||||
ok[0] = true;
|
||||
ok[1] = true;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (tmpVar[val] instanceof FunctionTwoValues) {
|
||||
if (val == 0) {
|
||||
tmpVar[val] = ((FunctionTwoValues) tmpVar[val]).variable2;
|
||||
} else {
|
||||
tmpVar[val] = ((FunctionTwoValues) tmpVar[val]).variable1;
|
||||
}
|
||||
} else if (tmpVar[val] instanceof AnteriorFunction) {
|
||||
tmpVar[val] = ((AnteriorFunction) tmpVar[val]).variable;
|
||||
} else {
|
||||
ok[val] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ok[0] == true && ok[1] == true) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Multiplication) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
if (variable1.equals(f.variable1) && variable2.equals(f.variable2)) {
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
|
||||
return true;
|
||||
} else if (variable1.equals(f.variable2) && variable2.equals(f.variable1)) {
|
||||
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Multiplication clone() {
|
||||
return new Multiplication(mathContext, parameter1, parameter2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (parameter1 != null && parameter2 != null) {
|
||||
return parameter1.toString()+"*"+parameter2.toString();
|
||||
} else {
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
}
|
@ -7,19 +7,21 @@ import org.warp.picalculator.Errors;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.rules.ExpandRule1;
|
||||
import org.warp.picalculator.math.rules.ExpandRule5;
|
||||
|
||||
public class Negative extends AnteriorFunction {
|
||||
public class Negative extends FunctionSingle {
|
||||
|
||||
public Negative(Calculator root, Function value) {
|
||||
public Negative(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new Negative(root, value);
|
||||
}
|
||||
|
||||
@ -29,13 +31,13 @@ public class Negative extends AnteriorFunction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
public void recomputeDimensions() {
|
||||
variable.setSmall(small);
|
||||
variable.generateGraphics();
|
||||
variable.recomputeDimensions();
|
||||
|
||||
height = getVariable().getHeight();
|
||||
width = Utils.getFont(small).getCharacterWidth() /* Width of - */ + getVariable().getWidth();
|
||||
line = getVariable().getLine();
|
||||
height = getParameter().getHeight();
|
||||
width = Utils.getFont(small).getCharacterWidth() /* Width of - */ + getParameter().getWidth();
|
||||
line = getParameter().getLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,10 +64,10 @@ public class Negative extends AnteriorFunction {
|
||||
result = ExpandRule1.execute(this);
|
||||
} else if (ExpandRule5.compare(this)) {
|
||||
result = ExpandRule5.execute(this);
|
||||
} else if (variable.isSolved()) {
|
||||
} else if (variable.isSimplified()) {
|
||||
try {
|
||||
final Number var = (Number) getVariable();
|
||||
result.add(var.multiply(new Number(root, "-1")));
|
||||
final Number var = (Number) getParameter();
|
||||
result.add(var.multiply(new Number(mathContext, "-1")));
|
||||
} catch (final NullPointerException ex) {
|
||||
throw new Error(Errors.ERROR);
|
||||
} catch (final NumberFormatException ex) {
|
||||
@ -75,14 +77,14 @@ public class Negative extends AnteriorFunction {
|
||||
}
|
||||
} else {
|
||||
final ArrayList<Function> l1 = new ArrayList<>();
|
||||
if (variable.isSolved()) {
|
||||
if (variable.isSimplified()) {
|
||||
l1.add(variable);
|
||||
} else {
|
||||
l1.addAll(variable.solveOneStep());
|
||||
l1.addAll(variable.simplify());
|
||||
}
|
||||
|
||||
for (final Function f : l1) {
|
||||
result.add(new Negative(root, f));
|
||||
result.add(new Negative(mathContext, f));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -12,42 +12,39 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.BinaryFont;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public class Number implements Function {
|
||||
|
||||
private final Calculator root;
|
||||
private final MathContext root;
|
||||
protected BigDecimal term;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int line;
|
||||
protected boolean small;
|
||||
|
||||
public Number(Calculator root, BigInteger val) {
|
||||
public Number(MathContext root, BigInteger val) {
|
||||
this.root = root;
|
||||
term = new BigDecimal(val).setScale(Utils.scale, Utils.scaleMode2);
|
||||
}
|
||||
|
||||
public Number(Calculator root, BigDecimal val) {
|
||||
public Number(MathContext root, BigDecimal val) {
|
||||
this.root = root;
|
||||
term = val.setScale(Utils.scale, Utils.scaleMode2);
|
||||
}
|
||||
|
||||
public Number(Calculator root, String s) throws Error {
|
||||
public Number(MathContext root, String s) throws Error {
|
||||
this(root, new BigDecimal(s).setScale(Utils.scale, Utils.scaleMode2));
|
||||
}
|
||||
|
||||
public Number(Calculator root, int s) {
|
||||
public Number(MathContext root, int s) {
|
||||
this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2));
|
||||
}
|
||||
|
||||
public Number(Calculator root, float s) {
|
||||
public Number(MathContext root, float s) {
|
||||
this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2));
|
||||
}
|
||||
|
||||
public Number(Calculator root, double s) {
|
||||
public Number(MathContext root, double s) {
|
||||
this(root, BigDecimal.valueOf(s).setScale(Utils.scale, Utils.scaleMode2));
|
||||
}
|
||||
|
||||
@ -59,18 +56,6 @@ public class Number implements Function {
|
||||
term = val.setScale(Utils.scale, Utils.scaleMode2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
line = calcLine(); //TODO pp
|
||||
height = calcHeight();
|
||||
width = calcWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
public Number add(Number f) {
|
||||
final Number ret = new Number(root, getTerm().add(f.getTerm()));
|
||||
return ret;
|
||||
@ -120,115 +105,13 @@ public class Number implements Function {
|
||||
return s;
|
||||
}
|
||||
|
||||
// public void draw(int x, int y, PIDisplay g, boolean small, boolean drawMinus) {
|
||||
// boolean beforedrawminus = this.drawMinus;
|
||||
// this.drawMinus = drawMinus;
|
||||
// draw(x, y, small);
|
||||
// this.drawMinus = beforedrawminus;
|
||||
// }
|
||||
|
||||
private boolean drawMinus = true;
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
Utils.getFont(small).use(DisplayManager.engine);
|
||||
String t = toString();
|
||||
|
||||
if (t.startsWith("-")) {
|
||||
if (drawMinus) {
|
||||
|
||||
} else {
|
||||
t = t.substring(1);
|
||||
}
|
||||
}
|
||||
if (t.contains("ℯ℮")) {
|
||||
final BinaryFont defaultf = Utils.getFont(small);
|
||||
final BinaryFont smallf = Utils.getFont(true);
|
||||
final String s = t.substring(0, t.indexOf("ℯ℮") + 2);
|
||||
final int sw = defaultf.getStringWidth(s);
|
||||
DisplayManager.renderer.glDrawStringLeft(x + 1, y + smallf.getCharacterHeight() - 2, s);
|
||||
smallf.use(DisplayManager.engine);
|
||||
DisplayManager.renderer.glDrawStringLeft(x + 1 + sw - 3, y, t.substring(t.indexOf("ℯ℮") + 2));
|
||||
} else {
|
||||
DisplayManager.renderer.glDrawStringLeft(x + 1, y, t);
|
||||
}
|
||||
}
|
||||
|
||||
public int getHeight(boolean drawMinus) {
|
||||
final boolean beforedrawminus = this.drawMinus;
|
||||
this.drawMinus = drawMinus;
|
||||
final int h = getHeight();
|
||||
this.drawMinus = beforedrawminus;
|
||||
return h;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
private int calcHeight() {
|
||||
final String t = toString();
|
||||
if (t.contains("ℯ℮")) {
|
||||
return Utils.getFontHeight(small) - 2 + Utils.getFontHeight(true);
|
||||
} else {
|
||||
final int h1 = Utils.getFontHeight(small);
|
||||
return h1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int calcWidth() {
|
||||
String t = toString();
|
||||
if (t.startsWith("-")) {
|
||||
if (drawMinus) {
|
||||
|
||||
} else {
|
||||
t = t.substring(1);
|
||||
}
|
||||
}
|
||||
if (t.contains("ℯ℮")) {
|
||||
final BinaryFont defaultf = Utils.getFont(small);
|
||||
final BinaryFont smallf = Utils.getFont(true);
|
||||
final String s = t.substring(0, t.indexOf("ℯ℮") + 2);
|
||||
final int sw = defaultf.getStringWidth(s);
|
||||
return 1 + sw - 3 + smallf.getStringWidth(t.substring(t.indexOf("ℯ℮") + 2));
|
||||
} else {
|
||||
return Utils.getFont(small).getStringWidth(t) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
private int calcLine() {
|
||||
final String t = toString();
|
||||
if (t.contains("ℯ℮")) {
|
||||
return (Utils.getFontHeight(small) / 2) - 2 + Utils.getFontHeight(true);
|
||||
} else {
|
||||
return Utils.getFontHeight(small) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number clone() {
|
||||
final Cloner cloner = new Cloner();
|
||||
return cloner.deepClone(this);
|
||||
return new Number(root, term);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
public boolean isSimplified() {
|
||||
if (root.exactMode) {
|
||||
return isInteger();
|
||||
} else {
|
||||
@ -237,7 +120,7 @@ public class Number implements Function {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Function> solveOneStep() throws Error {
|
||||
public List<Function> simplify() throws Error {
|
||||
final List<Function> result = new ArrayList<>();
|
||||
if (root.exactMode) {
|
||||
Number divisor = new Number(root, BigInteger.TEN.pow(getNumberOfDecimalPlaces()));
|
||||
@ -284,7 +167,7 @@ public class Number implements Function {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
public MathContext getMathContext() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@ -340,4 +223,14 @@ public class Number implements Function {
|
||||
|
||||
return fs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function setParameter(int index, Function var) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ package org.warp.picalculator.math.functions;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.rules.ExponentRule1;
|
||||
import org.warp.picalculator.math.rules.ExponentRule2;
|
||||
@ -14,25 +16,15 @@ import org.warp.picalculator.math.rules.FractionsRule4;
|
||||
import org.warp.picalculator.math.rules.FractionsRule5;
|
||||
import org.warp.picalculator.math.rules.UndefinedRule1;
|
||||
|
||||
public class Power extends FunctionTwoValues {
|
||||
public class Power extends FunctionOperator {
|
||||
|
||||
public Power(Calculator root, Function value1, Function value2) {
|
||||
public Power(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
return new Power(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return MathematicalSymbols.POWER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
if (UndefinedRule1.compare(this)) {
|
||||
@ -62,19 +54,6 @@ public class Power extends FunctionTwoValues {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable1.setSmall(small);
|
||||
variable1.generateGraphics();
|
||||
|
||||
variable2.setSmall(true);
|
||||
variable2.generateGraphics();
|
||||
|
||||
height = variable1.getHeight() + variable2.getHeight() - 4;
|
||||
line = variable2.getHeight() - 4 + variable1.getLine();
|
||||
width = getVariable1().getWidth() + getVariable2().getWidth() + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
@ -94,45 +73,23 @@ public class Power extends FunctionTwoValues {
|
||||
result.addAll(FractionsRule4.execute(this));
|
||||
} else if (FractionsRule5.compare(this)) {
|
||||
result.addAll(FractionsRule5.execute(this));
|
||||
} else if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
result.add(((Number) variable1).pow((Number) variable2));
|
||||
} else if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
result.add(((Number) parameter1).pow((Number) parameter2));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
// glColor3f(0, 127-50+new Random().nextInt(50), 0);
|
||||
// glFillRect(x,y,width,height);
|
||||
// glColor3f(0, 0, 0);
|
||||
|
||||
int dx = 0;
|
||||
variable1.draw(dx + x, getHeight() - variable1.getHeight() + y);
|
||||
dx += variable1.getWidth();
|
||||
variable2.draw(dx + x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Power) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
return variable1.equals(f.variable1) && variable2.equals(f.variable2);
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Power clone() {
|
||||
return new Power(mathContext, parameter1, parameter2);
|
||||
}
|
||||
}
|
||||
|
@ -6,17 +6,19 @@ import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
|
||||
public class Root extends FunctionTwoValues {
|
||||
public class Root extends FunctionOperator {
|
||||
|
||||
public Root(Calculator root, Function value1, Function value2) {
|
||||
public Root(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
protected Function NewInstance(MathContext root, Function value1, Function value2) {
|
||||
return new Root(root, value1, value2);
|
||||
}
|
||||
|
||||
@ -27,36 +29,36 @@ public class Root extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable1.setSmall(true);
|
||||
variable1.generateGraphics();
|
||||
parameter1.setSmall(true);
|
||||
parameter1.recomputeDimensions();
|
||||
|
||||
variable2.setSmall(small);
|
||||
variable2.generateGraphics();
|
||||
parameter2.setSmall(small);
|
||||
parameter2.recomputeDimensions();
|
||||
|
||||
width = 1 + variable1.getWidth() + 2 + variable2.getWidth() + 2;
|
||||
height = variable1.getHeight() + variable2.getHeight() - 2;
|
||||
line = variable1.getHeight() + variable2.getLine() - 2;
|
||||
width = 1 + parameter1.getWidth() + 2 + parameter2.getWidth() + 2;
|
||||
height = parameter1.getHeight() + parameter2.getHeight() - 2;
|
||||
line = parameter1.getHeight() + parameter2.getLine() - 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
if (root.exactMode == false) {
|
||||
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
if (mathContext.exactMode == false) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
Number exponent = new Number(root, BigDecimal.ONE);
|
||||
exponent = exponent.divide((Number) variable1);
|
||||
final Number resultVal = ((Number) variable2).pow(exponent);
|
||||
final Number originalVariable = resultVal.pow(new Number(root, 2));
|
||||
if (originalVariable.equals(variable2)) {
|
||||
Number exponent = new Number(mathContext, BigDecimal.ONE);
|
||||
exponent = exponent.divide((Number) parameter1);
|
||||
final Number resultVal = ((Number) parameter2).pow(exponent);
|
||||
final Number originalVariable = resultVal.pow(new Number(mathContext, 2));
|
||||
if (originalVariable.equals(parameter2)) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception | Error ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (variable1 instanceof Number && ((Number) variable1).equals(new Number(root, 2))) {
|
||||
if (parameter1 instanceof Number && ((Number) parameter1).equals(new Number(mathContext, 2))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -65,38 +67,38 @@ public class Root extends FunctionTwoValues {
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
if (root.exactMode) {
|
||||
if (variable1 instanceof Number && ((Number) variable1).equals(new Number(root, 2))) {
|
||||
result.add(new RootSquare(root, variable2));
|
||||
if (mathContext.exactMode) {
|
||||
if (parameter1 instanceof Number && ((Number) parameter1).equals(new Number(mathContext, 2))) {
|
||||
result.add(new RootSquare(mathContext, parameter2));
|
||||
} else {
|
||||
Number exponent = new Number(root, BigInteger.ONE);
|
||||
exponent = exponent.divide((Number) variable1);
|
||||
result.add(((Number) variable2).pow(exponent));
|
||||
Number exponent = new Number(mathContext, BigInteger.ONE);
|
||||
exponent = exponent.divide((Number) parameter1);
|
||||
result.add(((Number) parameter2).pow(exponent));
|
||||
}
|
||||
} else {
|
||||
final Number exp = (Number) variable1;
|
||||
final Number numb = (Number) variable2;
|
||||
final Number exp = (Number) parameter1;
|
||||
final Number numb = (Number) parameter2;
|
||||
|
||||
result.add(numb.pow(new Number(root, 1).divide(exp)));
|
||||
result.add(numb.pow(new Number(mathContext, 1).divide(exp)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
public void draw(int x, int y, boolean small, int caretPos) {
|
||||
// glColor3f(0, 255, 0);
|
||||
// glFillRect(x,y,width,height);
|
||||
// glColor3f(0, 0, 0);
|
||||
|
||||
final int w1 = getVariable2().getWidth();
|
||||
final int h1 = getVariable2().getHeight();
|
||||
final int w2 = getVariable1().getWidth();
|
||||
final int h2 = getVariable1().getHeight();
|
||||
final int w2 = getParameter1().getWidth();
|
||||
final int h2 = getParameter1().getHeight();
|
||||
final int height = getHeight();
|
||||
final int hh = (int) Math.ceil((double) h1 / 2);
|
||||
|
||||
getVariable1().draw(x + 1, y);
|
||||
getVariable2().draw(x + 1 + w2 + 2, y + h2 - 2);
|
||||
getParameter1().draw(x + 1, y, null, null);
|
||||
getVariable2().draw(x + 1 + w2 + 2, y + h2 - 2, null, null);
|
||||
|
||||
DisplayManager.renderer.glDrawLine(x + 1 + w2 - 2, y + height - 2, x + 1 + w2 - 2, y + height - 2);
|
||||
DisplayManager.renderer.glDrawLine(x + 1 + w2 - 1, y + height - 1, x + 1 + w2 - 1, y + height - 1);
|
||||
@ -124,8 +126,8 @@ public class Root extends FunctionTwoValues {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Root) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
return variable1.equals(f.variable1) && variable2.equals(f.variable2);
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
return parameter1.equals(f.parameter1) && parameter2.equals(f.parameter2);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -5,17 +5,19 @@ import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
|
||||
public class RootSquare extends AnteriorFunction {
|
||||
public class RootSquare extends FunctionSingle {
|
||||
|
||||
public RootSquare(Calculator root, Function value) {
|
||||
public RootSquare(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new RootSquare(root, value);
|
||||
}
|
||||
|
||||
@ -25,26 +27,26 @@ public class RootSquare extends AnteriorFunction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
public void recomputeDimensions() {
|
||||
variable.setSmall(small);
|
||||
variable.generateGraphics();
|
||||
variable.recomputeDimensions();
|
||||
|
||||
height = getVariable().getHeight() + 2;
|
||||
width = 1 + 4 + getVariable().getWidth() + 1;
|
||||
line = getVariable().getLine() + 2;
|
||||
height = getParameter().getHeight() + 2;
|
||||
width = 1 + 4 + getParameter().getWidth() + 1;
|
||||
line = getParameter().getLine() + 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable instanceof Number) {
|
||||
if (root.exactMode == false) {
|
||||
if (mathContext.exactMode == false) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
Number exponent = new Number(root, BigInteger.ONE);
|
||||
exponent = exponent.divide(new Number(root, 2));
|
||||
Number exponent = new Number(mathContext, BigInteger.ONE);
|
||||
exponent = exponent.divide(new Number(mathContext, 2));
|
||||
final Number resultVal = ((Number) variable).pow(exponent);
|
||||
final Number originalVariable = resultVal.pow(new Number(root, 2));
|
||||
final Number originalVariable = resultVal.pow(new Number(mathContext, 2));
|
||||
if (originalVariable.equals(variable)) {
|
||||
return true;
|
||||
}
|
||||
@ -58,26 +60,26 @@ public class RootSquare extends AnteriorFunction {
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
if (root.exactMode) {
|
||||
Number exponent = new Number(root, BigInteger.ONE);
|
||||
exponent = exponent.divide(new Number(root, 2));
|
||||
if (mathContext.exactMode) {
|
||||
Number exponent = new Number(mathContext, BigInteger.ONE);
|
||||
exponent = exponent.divide(new Number(mathContext, 2));
|
||||
result.add(((Number) variable).pow(exponent));
|
||||
} else {
|
||||
final Number exp = new Number(root, 2);
|
||||
final Number exp = new Number(mathContext, 2);
|
||||
final Number numb = (Number) variable;
|
||||
|
||||
result.add(numb.pow(new Number(root, 1).divide(exp)));
|
||||
result.add(numb.pow(new Number(mathContext, 1).divide(exp)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
public int draw(int x, int y, boolean small, int caretPos) {
|
||||
// glColor3f(0, 255, 0);
|
||||
// glFillRect(x,y,width,height);
|
||||
// glColor3f(0, 0, 0);
|
||||
|
||||
Utils.writeSquareRoot(getVariable(), x, y, small);
|
||||
Utils.writeSquareRoot(getParameter(), x, y, small);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -3,7 +3,9 @@ package org.warp.picalculator.math.functions;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.rules.ExpandRule1;
|
||||
import org.warp.picalculator.math.rules.ExpandRule5;
|
||||
@ -14,14 +16,14 @@ import org.warp.picalculator.math.rules.VariableRule2;
|
||||
import org.warp.picalculator.math.rules.VariableRule3;
|
||||
import org.warp.picalculator.math.rules.methods.SumMethod1;
|
||||
|
||||
public class Subtraction extends FunctionTwoValues {
|
||||
public class Subtraction extends FunctionOperator {
|
||||
|
||||
public Subtraction(Calculator root, Function value1, Function value2) {
|
||||
public Subtraction(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
protected Function NewInstance(MathContext root, Function value1, Function value2) {
|
||||
return new Subtraction(root, value1, value2);
|
||||
}
|
||||
|
||||
@ -32,7 +34,7 @@ public class Subtraction extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
if (VariableRule1.compare(this)) {
|
||||
@ -81,8 +83,8 @@ public class Subtraction extends FunctionTwoValues {
|
||||
result = NumberRule5.execute(this);
|
||||
} else if (SumMethod1.compare(this)) {
|
||||
result = SumMethod1.execute(this);
|
||||
} else if (variable1.isSolved() & variable2.isSolved()) {
|
||||
result.add(((Number) variable1).add(((Number) variable2).multiply(new Number(root, "-1"))));
|
||||
} else if (parameter1.isSimplified() & parameter2.isSimplified()) {
|
||||
result.add(((Number) parameter1).add(((Number) parameter2).multiply(new Number(mathContext, "-1"))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -90,8 +92,8 @@ public class Subtraction extends FunctionTwoValues {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Subtraction) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
return variable1.equals(f.variable1) && variable2.equals(f.variable2);
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
return parameter1.equals(f.parameter1) && parameter2.equals(f.parameter2);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -7,7 +7,9 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Errors;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.rules.NumberRule3;
|
||||
import org.warp.picalculator.math.rules.NumberRule5;
|
||||
@ -18,25 +20,15 @@ import org.warp.picalculator.math.rules.VariableRule2;
|
||||
import org.warp.picalculator.math.rules.VariableRule3;
|
||||
import org.warp.picalculator.math.rules.methods.SumMethod1;
|
||||
|
||||
public class Sum extends FunctionTwoValues {
|
||||
public class Sum extends FunctionOperator {
|
||||
|
||||
public Sum(Calculator root, Function value1, Function value2) {
|
||||
public Sum(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
return new Sum(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return MathematicalSymbols.SUM;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
if (SyntaxRule2.compare(this)) {
|
||||
@ -68,7 +60,7 @@ public class Sum extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
if (variable1 == null || variable2 == null) {
|
||||
if (parameter1 == null || parameter2 == null) {
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
ArrayList<Function> result = new ArrayList<>();
|
||||
@ -88,61 +80,40 @@ public class Sum extends FunctionTwoValues {
|
||||
result = NumberRule7.execute(this);
|
||||
} else if (SumMethod1.compare(this)) {
|
||||
result = SumMethod1.execute(this);
|
||||
} else if (variable1.isSolved() & variable2.isSolved()) {
|
||||
if ((root.getChild().equals(this))) {
|
||||
if (((Number) variable1).term.compareTo(new BigDecimal(2)) == 0 && ((Number) variable2).term.compareTo(new BigDecimal(2)) == 0) {
|
||||
result.add(new Joke(root, Joke.FISH));
|
||||
} else if (parameter1.isSimplified() & parameter2.isSimplified()) {
|
||||
if ((mathContext.getChild().equals(this))) {
|
||||
if (((Number) parameter1).term.compareTo(new BigDecimal(2)) == 0 && ((Number) parameter2).term.compareTo(new BigDecimal(2)) == 0) {
|
||||
result.add(new Joke(mathContext, Joke.FISH));
|
||||
return result;
|
||||
} else if (((Number) variable1).term.compareTo(new BigDecimal(20)) == 0 && ((Number) variable2).term.compareTo(new BigDecimal(20)) == 0) {
|
||||
result.add(new Joke(root, Joke.TORNADO));
|
||||
} else if (((Number) parameter1).term.compareTo(new BigDecimal(20)) == 0 && ((Number) parameter2).term.compareTo(new BigDecimal(20)) == 0) {
|
||||
result.add(new Joke(mathContext, Joke.TORNADO));
|
||||
return result;
|
||||
} else if (((Number) variable1).term.compareTo(new BigDecimal(29)) == 0 && ((Number) variable2).term.compareTo(new BigDecimal(29)) == 0) {
|
||||
result.add(new Joke(root, Joke.SHARKNADO));
|
||||
} else if (((Number) parameter1).term.compareTo(new BigDecimal(29)) == 0 && ((Number) parameter2).term.compareTo(new BigDecimal(29)) == 0) {
|
||||
result.add(new Joke(mathContext, Joke.SHARKNADO));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
result.add(((Number) variable1).add((Number) variable2));
|
||||
result.add(((Number) parameter1).add((Number) parameter2));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable1.setSmall(small);
|
||||
variable1.generateGraphics();
|
||||
|
||||
variable2.setSmall(small);
|
||||
variable2.generateGraphics();
|
||||
|
||||
width = calcWidth();
|
||||
height = calcHeight();
|
||||
line = calcLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int calcWidth() {
|
||||
int dx = 0;
|
||||
dx += variable1.getWidth();
|
||||
dx += 1;
|
||||
dx += Utils.getFont(small).getStringWidth(getSymbol());
|
||||
return dx += variable2.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Sum) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
if (variable1.equals(f.variable1) && variable2.equals(f.variable2)) {
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
if (parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2())) {
|
||||
return true;
|
||||
} else if (variable1.equals(f.variable2) && variable2.equals(f.variable1)) {
|
||||
} else if (parameter1.equals(f.getParameter2()) && parameter2.equals(f.getParameter1())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Sum clone() {
|
||||
return new Sum(mathContext, parameter1, parameter2);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,21 +7,23 @@ import org.warp.picalculator.Errors;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
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;
|
||||
|
||||
public class SumSubtraction extends FunctionTwoValues {
|
||||
public class SumSubtraction extends FunctionOperator {
|
||||
|
||||
public SumSubtraction(Calculator root, Function value1, Function value2) {
|
||||
public SumSubtraction(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
protected Function NewInstance(MathContext root, Function value1, Function value2) {
|
||||
return new SumSubtraction(root, value1, value2);
|
||||
}
|
||||
|
||||
@ -32,7 +34,7 @@ public class SumSubtraction extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
if (NumberRule3.compare(this)) {
|
||||
@ -52,7 +54,7 @@ public class SumSubtraction extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
if (variable1 == null || variable2 == null) {
|
||||
if (parameter1 == null || parameter2 == null) {
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
ArrayList<Function> result = new ArrayList<>();
|
||||
@ -64,20 +66,20 @@ public class SumSubtraction extends FunctionTwoValues {
|
||||
result = NumberRule4.execute(this);
|
||||
} else if (NumberRule5.compare(this)) {
|
||||
result = NumberRule5.execute(this);
|
||||
} else if (variable1.isSolved() & variable2.isSolved()) {
|
||||
result.add(((Number) variable1).add((Number) variable2));
|
||||
result.add(((Number) variable1).add(((Number) variable2).multiply(new Number(root, "-1"))));
|
||||
} 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"))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
variable1.setSmall(small);
|
||||
variable1.generateGraphics();
|
||||
parameter1.setSmall(small);
|
||||
parameter1.recomputeDimensions();
|
||||
|
||||
variable2.setSmall(small);
|
||||
variable2.generateGraphics();
|
||||
parameter2.setSmall(small);
|
||||
parameter2.recomputeDimensions();
|
||||
|
||||
width = calcWidth();
|
||||
height = calcHeight();
|
||||
@ -85,20 +87,20 @@ public class SumSubtraction extends FunctionTwoValues {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
public void draw(int x, int y, boolean small, int caretPos) {
|
||||
// glColor3f(127, 127-50+new Random().nextInt(50), 255);
|
||||
// glFillRect(x,y,width,height);
|
||||
// glColor3f(0, 0, 0);
|
||||
|
||||
final int ln = getLine();
|
||||
int dx = 0;
|
||||
variable1.draw(dx + x, ln - variable1.getLine() + y);
|
||||
dx += variable1.getWidth();
|
||||
parameter1.draw(dx + x, ln - parameter1.getLine() + y, null, null);
|
||||
dx += parameter1.getWidth();
|
||||
Utils.getFont(small).use(DisplayManager.engine);
|
||||
dx += 1;
|
||||
DisplayManager.renderer.glDrawStringLeft(dx + x, ln - Utils.getFontHeight(small) / 2 + y, getSymbol());
|
||||
dx += Utils.getFont(small).getStringWidth(getSymbol());
|
||||
variable2.draw(dx + x, ln - variable2.getLine() + y);
|
||||
parameter2.draw(dx + x, ln - parameter2.getLine() + y, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -109,17 +111,17 @@ public class SumSubtraction extends FunctionTwoValues {
|
||||
@Override
|
||||
protected int calcWidth() {
|
||||
int dx = 0;
|
||||
dx += variable1.getWidth();
|
||||
dx += parameter1.getWidth();
|
||||
dx += 1;
|
||||
dx += Utils.getFont(small).getStringWidth(getSymbol());
|
||||
return dx += variable2.getWidth();
|
||||
return dx += parameter2.getWidth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof SumSubtraction) {
|
||||
final FunctionTwoValues f = (FunctionTwoValues) o;
|
||||
return variable1.equals(f.variable1) && variable2.equals(f.variable2);
|
||||
final FunctionOperator f = (FunctionOperator) o;
|
||||
return parameter1.equals(f.parameter1) && parameter2.equals(f.parameter2);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -6,13 +6,14 @@ import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.gui.graphicengine.cpu.CPUEngine;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
|
||||
public class Undefined implements Function {
|
||||
|
||||
protected final Calculator root;
|
||||
protected final MathContext root;
|
||||
|
||||
public Undefined(Calculator root) {
|
||||
public Undefined(MathContext root) {
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
@ -63,7 +64,7 @@ public class Undefined implements Function {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
public MathContext getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
|
@ -6,28 +6,24 @@ import java.util.List;
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public class Variable implements Function {
|
||||
|
||||
protected char var;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected int line;
|
||||
protected int[] varColor;
|
||||
protected boolean small;
|
||||
protected final Calculator root;
|
||||
protected final MathContext root;
|
||||
protected V_TYPE type = V_TYPE.KNOWN;
|
||||
|
||||
public Variable(Calculator root, char val, V_TYPE type) {
|
||||
public Variable(MathContext root, char val, V_TYPE type) {
|
||||
this.root = root;
|
||||
var = val;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Variable(Calculator root, String s, V_TYPE type) throws Error {
|
||||
public Variable(MathContext root, String s, V_TYPE type) throws Error {
|
||||
this(root, s.charAt(0), type);
|
||||
}
|
||||
|
||||
@ -47,84 +43,11 @@ public class Variable implements Function {
|
||||
return new Variable(root, var, typ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
line = calcLine();
|
||||
height = calcHeight();
|
||||
width = calcWidth();
|
||||
varColor = new int[3];
|
||||
switch (type) {
|
||||
case KNOWN:
|
||||
varColor[0] = 0;
|
||||
varColor[1] = 200;
|
||||
varColor[2] = 0;
|
||||
break;
|
||||
case UNKNOWN:
|
||||
varColor[0] = 200;
|
||||
varColor[1] = 0;
|
||||
varColor[2] = 0;
|
||||
break;
|
||||
case SOLUTION:
|
||||
varColor[0] = 0;
|
||||
varColor[1] = 0;
|
||||
varColor[2] = 200;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSymbol() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "" + getChar();
|
||||
}
|
||||
|
||||
// public void draw(int x, int y, PIDisplay g, boolean small, boolean drawMinus) {
|
||||
// boolean beforedrawminus = this.drawMinus;
|
||||
// this.drawMinus = drawMinus;
|
||||
// draw(x, y, small);
|
||||
// this.drawMinus = beforedrawminus;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
Utils.getFont(small).use(DisplayManager.engine);
|
||||
DisplayManager.renderer.glColor3i(varColor[0], varColor[1], varColor[2]);
|
||||
DisplayManager.renderer.glDrawStringLeft(x + 1, y, toString());
|
||||
DisplayManager.renderer.glColor3i(0, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
private int calcHeight() {
|
||||
final int h1 = Utils.getFontHeight(small);
|
||||
return h1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public int calcWidth() {
|
||||
return Utils.getFont(small).getStringWidth(toString()) + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLine() {
|
||||
return line;
|
||||
}
|
||||
|
||||
private int calcLine() {
|
||||
return Utils.getFontHeight(small) / 2;
|
||||
}
|
||||
|
||||
|
||||
public static class VariableValue {
|
||||
public final Variable v;
|
||||
public final Number n;
|
||||
@ -136,23 +59,12 @@ public class Variable implements Function {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Variable clone() {
|
||||
final Cloner cloner = new Cloner();
|
||||
return cloner.deepClone(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSmall(boolean small) {
|
||||
this.small = small;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSolved() {
|
||||
public boolean isSimplified() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Function> solveOneStep() throws Error {
|
||||
public List<Function> simplify() throws Error {
|
||||
final List<Function> result = new ArrayList<>();
|
||||
result.add(this);
|
||||
return result;
|
||||
@ -172,13 +84,28 @@ public class Variable implements Function {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Calculator getRoot() {
|
||||
public MathContext getMathContext() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Variable clone() {
|
||||
return new Variable(root, var, type);
|
||||
}
|
||||
|
||||
public static enum V_TYPE {
|
||||
KNOWN,
|
||||
UNKNOWN,
|
||||
SOLUTION
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function setParameter(int index, Function var) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function getParameter(int index) throws IndexOutOfBoundsException {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
}
|
||||
|
@ -8,24 +8,24 @@ import java.util.Set;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.Errors;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.SolveMethod;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
|
||||
import com.rits.cloning.Cloner;
|
||||
|
||||
public class Equation extends FunctionTwoValues {
|
||||
public class Equation extends FunctionOperator {
|
||||
|
||||
public Equation(Calculator root, Function value1, Function value2) {
|
||||
public Equation(MathContext root, Function value1, Function value2) {
|
||||
super(root, value1, value2);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value1, Function value2) {
|
||||
protected Function NewInstance(MathContext root, Function value1, Function value2) {
|
||||
return new Equation(root, value1, value2);
|
||||
}
|
||||
|
||||
@ -36,7 +36,7 @@ public class Equation extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable1 instanceof Number & variable2 instanceof Number) {
|
||||
if (parameter1 instanceof Number & parameter2 instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -44,17 +44,17 @@ public class Equation extends FunctionTwoValues {
|
||||
|
||||
@Override
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
if (variable1 == null || variable2 == null) {
|
||||
if (parameter1 == null || parameter2 == null) {
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
if (variable1.isSolved() & variable2.isSolved()) {
|
||||
if (((Number) variable2).getTerm().compareTo(new BigDecimal(0)) == 0) {
|
||||
if (parameter1.isSimplified() & parameter2.isSimplified()) {
|
||||
if (((Number) parameter2).getTerm().compareTo(new BigDecimal(0)) == 0) {
|
||||
result.add(this);
|
||||
} else {
|
||||
final Equation e = new Equation(root, null, null);
|
||||
e.setVariable1(new Subtraction(root, variable1, variable2));
|
||||
e.setVariable2(new Number(root, "0"));
|
||||
final Equation e = new Equation(mathContext, null, null);
|
||||
e.setParameter1(new Subtraction(mathContext, parameter1, parameter2));
|
||||
e.setParameter2(new Number(mathContext, "0"));
|
||||
result.add(e);
|
||||
}
|
||||
}
|
||||
|
@ -5,24 +5,24 @@ import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionDynamic;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionMultipleValues;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
public class EquationsSystem extends FunctionMultipleValues {
|
||||
public class EquationsSystem extends FunctionDynamic {
|
||||
static final int spacing = 2;
|
||||
|
||||
public EquationsSystem(Calculator root) {
|
||||
public EquationsSystem(MathContext root) {
|
||||
super(root);
|
||||
}
|
||||
|
||||
public EquationsSystem(Calculator root, Function value) {
|
||||
public EquationsSystem(MathContext root, Function value) {
|
||||
super(root, new Function[] { value });
|
||||
}
|
||||
|
||||
public EquationsSystem(Calculator root, Function[] value) {
|
||||
public EquationsSystem(MathContext root, Function[] value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@ -43,11 +43,11 @@ public class EquationsSystem extends FunctionMultipleValues {
|
||||
public List<Function> solveOneStep() throws Error {
|
||||
final List<Function> ret = new ArrayList<>();
|
||||
if (functions.length == 1) {
|
||||
if (functions[0].isSolved()) {
|
||||
if (functions[0].isSimplified()) {
|
||||
ret.add(functions[0]);
|
||||
return ret;
|
||||
} else {
|
||||
final List<Function> l = functions[0].solveOneStep();
|
||||
final List<Function> l = functions[0].simplify();
|
||||
for (final Function f : l) {
|
||||
if (f instanceof Number) {
|
||||
ret.add(f);
|
||||
@ -59,8 +59,8 @@ public class EquationsSystem extends FunctionMultipleValues {
|
||||
}
|
||||
} else {
|
||||
for (final Function f : functions) {
|
||||
if (f.isSolved() == false) {
|
||||
final List<Function> partial = f.solveOneStep();
|
||||
if (f.isSimplified() == false) {
|
||||
final List<Function> partial = f.simplify();
|
||||
for (final Function fnc : partial) {
|
||||
ret.add(new Expression(root, new Function[] { fnc }));
|
||||
}
|
||||
@ -74,7 +74,7 @@ public class EquationsSystem extends FunctionMultipleValues {
|
||||
public void generateGraphics() {
|
||||
for (final Function f : functions) {
|
||||
f.setSmall(false);
|
||||
f.generateGraphics();
|
||||
f.recomputeDimensions();
|
||||
}
|
||||
|
||||
width = 0;
|
||||
@ -103,7 +103,7 @@ public class EquationsSystem extends FunctionMultipleValues {
|
||||
final int spazioSopra = h - marginBottom;
|
||||
int dy = marginTop;
|
||||
for (final Function f : functions) {
|
||||
f.draw(x + 5, y + dy);
|
||||
f.draw(x + 5, y + dy, null, null);
|
||||
dy += f.getHeight() + spacing;
|
||||
}
|
||||
|
||||
|
@ -5,19 +5,19 @@ import java.util.List;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.gui.DisplayManager;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
|
||||
public class EquationsSystemPart extends AnteriorFunction {
|
||||
public class EquationsSystemPart extends FunctionSingle {
|
||||
|
||||
public EquationsSystemPart(Calculator root, Equation equazione) {
|
||||
public EquationsSystemPart(MathContext root, Equation equazione) {
|
||||
super(root, equazione);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function NewInstance(Calculator root, Function value) {
|
||||
protected Function NewInstance(MathContext root, Function value) {
|
||||
return new EquationsSystemPart(root, (Equation) value);
|
||||
}
|
||||
|
||||
@ -27,22 +27,22 @@ public class EquationsSystemPart extends AnteriorFunction {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateGraphics() {
|
||||
public void recomputeDimensions() {
|
||||
variable.setSmall(false);
|
||||
variable.generateGraphics();
|
||||
variable.recomputeDimensions();
|
||||
|
||||
width = 5 + getVariable().getWidth();
|
||||
height = 3 + getVariable().getHeight() + 2;
|
||||
line = 3 + getVariable().getLine();
|
||||
width = 5 + getParameter().getWidth();
|
||||
height = 3 + getParameter().getHeight() + 2;
|
||||
line = 3 + getParameter().getLine();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(int x, int y) {
|
||||
public int draw(int x, int y, boolean small, int caretPos) {
|
||||
final int h = getHeight() - 1;
|
||||
final int paddingTop = 3;
|
||||
final int spazioSotto = (h - 3 - 2) / 2 + paddingTop;
|
||||
final int spazioSopra = h - spazioSotto;
|
||||
variable.draw(x + 5, y + paddingTop);
|
||||
variable.draw(x + 5, y + paddingTop, null, null);
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + 0, x + 3, y + 0);
|
||||
DisplayManager.renderer.glDrawLine(x + 1, y + 1, x + 1, y + spazioSotto / 2);
|
||||
DisplayManager.renderer.glDrawLine(x + 2, y + spazioSotto / 2 + 1, x + 2, y + spazioSotto - 1);
|
||||
|
@ -3,19 +3,19 @@ package org.warp.picalculator.math.functions.trigonometry;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
|
||||
public class ArcCosine extends AnteriorFunction {
|
||||
public class ArcCosine extends FunctionSingle {
|
||||
|
||||
public ArcCosine(Calculator root, Function value) {
|
||||
public ArcCosine(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new ArcCosine(root, value);
|
||||
}
|
||||
|
||||
|
@ -3,19 +3,19 @@ package org.warp.picalculator.math.functions.trigonometry;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
|
||||
public class ArcSine extends AnteriorFunction {
|
||||
public class ArcSine extends FunctionSingle {
|
||||
|
||||
public ArcSine(Calculator root, Function value) {
|
||||
public ArcSine(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new ArcSine(root, value);
|
||||
}
|
||||
|
||||
|
@ -3,19 +3,19 @@ package org.warp.picalculator.math.functions.trigonometry;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
|
||||
public class ArcTangent extends AnteriorFunction {
|
||||
public class ArcTangent extends FunctionSingle {
|
||||
|
||||
public ArcTangent(Calculator root, Function value) {
|
||||
public ArcTangent(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new ArcTangent(root, value);
|
||||
}
|
||||
|
||||
|
@ -3,19 +3,19 @@ package org.warp.picalculator.math.functions.trigonometry;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
|
||||
public class Cosine extends AnteriorFunction {
|
||||
public class Cosine extends FunctionSingle {
|
||||
|
||||
public Cosine(Calculator root, Function value) {
|
||||
public Cosine(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new Cosine(root, value);
|
||||
}
|
||||
|
||||
|
@ -5,20 +5,20 @@ import java.util.ArrayList;
|
||||
import org.nevec.rjm.BigDecimalMath;
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.AngleMode;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
public class Sine extends AnteriorFunction {
|
||||
public class Sine extends FunctionSingle {
|
||||
|
||||
public Sine(Calculator root, Function value) {
|
||||
public Sine(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new Sine(root, value);
|
||||
}
|
||||
|
||||
@ -30,12 +30,12 @@ public class Sine extends AnteriorFunction {
|
||||
@Override
|
||||
protected boolean isSolvable() {
|
||||
if (variable instanceof Number) {
|
||||
if (root.exactMode == false) {
|
||||
if (mathContext.exactMode == false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (root.angleMode == AngleMode.DEG) {
|
||||
final Function[] solvableValues = new Function[] { new Number(root, 0), new Number(root, 30), new Number(root, 90), };
|
||||
if (mathContext.angleMode == AngleMode.DEG) {
|
||||
final Function[] solvableValues = new Function[] { new Number(mathContext, 0), new Number(mathContext, 30), new Number(mathContext, 90), };
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -44,8 +44,8 @@ public class Sine extends AnteriorFunction {
|
||||
public ArrayList<Function> solve() throws Error {
|
||||
final ArrayList<Function> results = new ArrayList<>();
|
||||
if (variable instanceof Number) {
|
||||
if (root.exactMode == false) {
|
||||
results.add(new Number(root, BigDecimalMath.sin(((Number) variable).getTerm())));
|
||||
if (mathContext.exactMode == false) {
|
||||
results.add(new Number(mathContext, BigDecimalMath.sin(((Number) variable).getTerm())));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
@ -54,8 +54,8 @@ public class Sine extends AnteriorFunction {
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Sine) {
|
||||
final AnteriorFunction f = (AnteriorFunction) o;
|
||||
if (variable.equals(f.getVariable())) {
|
||||
final FunctionSingle f = (FunctionSingle) o;
|
||||
if (variable.equals(f.getParameter())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -3,19 +3,19 @@ package org.warp.picalculator.math.functions.trigonometry;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionSingle;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.functions.AnteriorFunction;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
|
||||
public class Tangent extends AnteriorFunction {
|
||||
public class Tangent extends FunctionSingle {
|
||||
|
||||
public Tangent(Calculator root, Function value) {
|
||||
public Tangent(MathContext root, Function value) {
|
||||
super(root, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function NewInstance(Calculator root, Function value) {
|
||||
public Function NewInstance(MathContext root, Function value) {
|
||||
return new Tangent(root, value);
|
||||
}
|
||||
|
||||
|
@ -3,10 +3,10 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Negative;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
import org.warp.picalculator.math.functions.Sum;
|
||||
@ -25,28 +25,28 @@ public class ExpandRule1 {
|
||||
public static boolean compare(Function f) {
|
||||
if (f instanceof Negative) {
|
||||
final Negative fnc = (Negative) f;
|
||||
if (fnc.getVariable() instanceof Expression) {
|
||||
final Expression expr = (Expression) fnc.getVariable();
|
||||
if (expr.getVariablesLength() == 1) {
|
||||
if (expr.getVariable(0) instanceof Sum) {
|
||||
if (fnc.getParameter() instanceof Expression) {
|
||||
final Expression expr = (Expression) fnc.getParameter();
|
||||
if (expr.getParametersLength() == 1) {
|
||||
if (expr.getParameter(0) instanceof Sum) {
|
||||
return true;
|
||||
} else if (expr.getVariable(0) instanceof Subtraction) {
|
||||
} else if (expr.getParameter(0) instanceof Subtraction) {
|
||||
return true;
|
||||
} else if (expr.getVariable(0) instanceof SumSubtraction) {
|
||||
} else if (expr.getParameter(0) instanceof SumSubtraction) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (f instanceof Subtraction || f instanceof SumSubtraction) {
|
||||
final FunctionTwoValues fnc = (FunctionTwoValues) f;
|
||||
if (fnc.getVariable2() instanceof Expression) {
|
||||
final Expression expr = (Expression) fnc.getVariable2();
|
||||
if (expr.getVariablesLength() == 1) {
|
||||
if (expr.getVariable(0) instanceof Sum) {
|
||||
final FunctionOperator fnc = (FunctionOperator) f;
|
||||
if (fnc.getParameter2() instanceof Expression) {
|
||||
final Expression expr = (Expression) fnc.getParameter2();
|
||||
if (expr.getParametersLength() == 1) {
|
||||
if (expr.getParameter(0) instanceof Sum) {
|
||||
return true;
|
||||
} else if (expr.getVariable(0) instanceof Subtraction) {
|
||||
} else if (expr.getParameter(0) instanceof Subtraction) {
|
||||
return true;
|
||||
} else if (expr.getVariable(0) instanceof SumSubtraction) {
|
||||
} else if (expr.getParameter(0) instanceof SumSubtraction) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -57,15 +57,15 @@ public class ExpandRule1 {
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
|
||||
Expression expr = null;
|
||||
int fromSubtraction = 0;
|
||||
FunctionTwoValues subtraction = null;
|
||||
FunctionOperator subtraction = null;
|
||||
if (f instanceof Negative) {
|
||||
expr = ((Expression) ((Negative) f).getVariable());
|
||||
expr = ((Expression) ((Negative) f).getParameter());
|
||||
} else if (f instanceof Subtraction || f instanceof SumSubtraction) {
|
||||
expr = ((Expression) ((FunctionTwoValues) f).getVariable2());
|
||||
expr = ((Expression) ((FunctionOperator) f).getParameter2());
|
||||
if (f instanceof Subtraction) {
|
||||
fromSubtraction = 1;
|
||||
} else {
|
||||
@ -77,48 +77,36 @@ public class ExpandRule1 {
|
||||
|
||||
}
|
||||
|
||||
final Function fnc = expr.getVariable(0);
|
||||
final Function fnc = expr.getParameter(0);
|
||||
if (fnc instanceof Sum) {
|
||||
final Function a = ((Sum) fnc).getVariable1();
|
||||
final Function b = ((Sum) fnc).getVariable2();
|
||||
final Subtraction fnc2 = new Subtraction(root, null, b);
|
||||
fnc2.setVariable1(new Negative(root, a));
|
||||
final Function a = ((Sum) fnc).getParameter1();
|
||||
final Function b = ((Sum) fnc).getParameter2();
|
||||
final Subtraction fnc2 = new Subtraction(root, new Negative(root, a), b);
|
||||
if (fromSubtraction > 0) {
|
||||
subtraction = new Subtraction(root, null, null);
|
||||
subtraction.setVariable1(((FunctionTwoValues) f).getVariable1());
|
||||
subtraction.setVariable2(fnc2);
|
||||
subtraction = new Subtraction(root, ((FunctionOperator) f).getParameter1(), fnc2);
|
||||
result.add(subtraction);
|
||||
} else {
|
||||
result.add(fnc2);
|
||||
}
|
||||
} else if (fnc instanceof Subtraction) {
|
||||
final Function a = ((Subtraction) fnc).getVariable1();
|
||||
final Function b = ((Subtraction) fnc).getVariable2();
|
||||
final Sum fnc2 = new Sum(root, null, b);
|
||||
fnc2.setVariable1(new Negative(root, a));
|
||||
final Function a = ((Subtraction) fnc).getParameter1();
|
||||
final Function b = ((Subtraction) fnc).getParameter2();
|
||||
final Sum fnc2 = new Sum(root, new Negative(root, a), b);
|
||||
if (fromSubtraction > 0) {
|
||||
subtraction = new Subtraction(root, null, null);
|
||||
subtraction.setVariable1(((FunctionTwoValues) f).getVariable1());
|
||||
subtraction.setVariable2(fnc2);
|
||||
subtraction = new Subtraction(root, ((FunctionOperator) f).getParameter1(), fnc2);
|
||||
result.add(subtraction);
|
||||
} else {
|
||||
result.add(fnc2);
|
||||
}
|
||||
} else if (fnc instanceof SumSubtraction) {
|
||||
final Function a = ((SumSubtraction) fnc).getVariable1();
|
||||
final Function b = ((SumSubtraction) fnc).getVariable2();
|
||||
final Sum fnc2 = new Sum(root, null, b);
|
||||
fnc2.setVariable1(new Negative(root, a));
|
||||
final Subtraction fnc3 = new Subtraction(root, null, b);
|
||||
fnc3.setVariable1(new Negative(root, a));
|
||||
final Function a = ((SumSubtraction) fnc).getParameter1();
|
||||
final Function b = ((SumSubtraction) fnc).getParameter2();
|
||||
final Sum fnc2 = new Sum(root, new Negative(root, a), b);
|
||||
final Subtraction fnc3 = new Subtraction(root, new Negative(root, a), b);
|
||||
if (fromSubtraction > 0) {
|
||||
subtraction = new SumSubtraction(root, null, null);
|
||||
subtraction.setVariable1(((FunctionTwoValues) f).getVariable1());
|
||||
subtraction.setVariable2(fnc2);
|
||||
subtraction = new SumSubtraction(root, ((FunctionOperator) f).getParameter1(), fnc2);
|
||||
result.add(subtraction);
|
||||
subtraction = new SumSubtraction(root, null, null);
|
||||
subtraction.setVariable1(((FunctionTwoValues) f).getVariable1());
|
||||
subtraction.setVariable2(fnc3);
|
||||
subtraction = new SumSubtraction(root, ((FunctionOperator) f).getParameter1(), fnc3);
|
||||
result.add(subtraction);
|
||||
result.add(subtraction);
|
||||
} else {
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Negative;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
|
||||
@ -20,15 +20,15 @@ public class ExpandRule5 {
|
||||
public static boolean compare(Function f) {
|
||||
if (f instanceof Negative) {
|
||||
final Negative fnc = (Negative) f;
|
||||
if (fnc.getVariable() instanceof Expression) {
|
||||
final Expression e = (Expression) fnc.getVariable();
|
||||
return e.getVariablesLength() == 1 && e.getVariable(0) instanceof Negative;
|
||||
if (fnc.getParameter() instanceof Expression) {
|
||||
final Expression e = (Expression) fnc.getParameter();
|
||||
return e.getParametersLength() == 1 && e.getParameter(0) instanceof Negative;
|
||||
}
|
||||
} else if (f instanceof Subtraction) {
|
||||
final Subtraction fnc = (Subtraction) f;
|
||||
if (fnc.getVariable2() instanceof Expression) {
|
||||
final Expression e = (Expression) fnc.getVariable2();
|
||||
return e.getVariablesLength() == 1 && e.getVariable(0) instanceof Negative;
|
||||
if (fnc.getParameter2() instanceof Expression) {
|
||||
final Expression e = (Expression) fnc.getParameter2();
|
||||
return e.getParametersLength() == 1 && e.getParameter(0) instanceof Negative;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@ -40,10 +40,10 @@ public class ExpandRule5 {
|
||||
|
||||
if (f instanceof Negative) {
|
||||
final Negative fnc = (Negative) f;
|
||||
result.add(((Negative) ((Expression) fnc.getVariable()).getVariable(0)).getVariable());
|
||||
result.add(((Negative) ((Expression) fnc.getParameter()).getParameter(0)).getParameter());
|
||||
} else if (f instanceof Subtraction) {
|
||||
final Subtraction fnc = (Subtraction) f;
|
||||
result.add(((Negative) ((Expression) fnc.getVariable2()).getVariable(0)).getVariable());
|
||||
result.add(((Negative) ((Expression) fnc.getParameter2()).getParameter(0)).getParameter());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
|
||||
@ -19,15 +19,15 @@ public class ExponentRule1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
final Calculator root = f.getRoot();
|
||||
if (fnc.getVariable1().equals(new Number(root, 1))) {
|
||||
final MathContext root = f.getMathContext();
|
||||
if (fnc.getParameter1().equals(new Number(root, 1))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Number(root, 1));
|
||||
return result;
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
@ -21,23 +21,20 @@ public class ExponentRule15 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Multiplication fnc = (Multiplication) f;
|
||||
if (fnc.getVariable1().equals(fnc.getVariable2())) {
|
||||
if (fnc.getParameter1().equals(fnc.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication fnc = (Multiplication) f;
|
||||
final Power p = new Power(root, null, null);
|
||||
final Expression expr = new Expression(root);
|
||||
final Function a = fnc.getVariable1();
|
||||
expr.addFunctionToEnd(a);
|
||||
final Function a = fnc.getParameter1();
|
||||
final Expression expr = new Expression(root, a);
|
||||
final Number two = new Number(root, 2);
|
||||
p.setVariable1(expr);
|
||||
p.setVariable2(two);
|
||||
final Power p = new Power(root, expr, two);
|
||||
result.add(p);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
@ -22,26 +22,26 @@ public class ExponentRule16 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Multiplication fnc = (Multiplication) f;
|
||||
if (fnc.getVariable1() instanceof Power && fnc.getVariable2() instanceof Power) {
|
||||
return ((Power)fnc.getVariable1()).getVariable1().equals(((Power)fnc.getVariable2()).getVariable1());
|
||||
} else if (fnc.getVariable1() instanceof Power) {
|
||||
return ((Power)fnc.getVariable1()).getVariable1().equals(fnc.getVariable2());
|
||||
} else if (fnc.getVariable2() instanceof Power) {
|
||||
return ((Power)fnc.getVariable2()).getVariable1().equals(fnc.getVariable1());
|
||||
if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) {
|
||||
return ((Power)fnc.getParameter1()).getParameter1().equals(((Power)fnc.getParameter2()).getParameter1());
|
||||
} else if (fnc.getParameter1() instanceof Power) {
|
||||
return ((Power)fnc.getParameter1()).getParameter1().equals(fnc.getParameter2());
|
||||
} else if (fnc.getParameter2() instanceof Power) {
|
||||
return ((Power)fnc.getParameter2()).getParameter1().equals(fnc.getParameter1());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication fnc = (Multiplication) f;
|
||||
if (fnc.getVariable1() instanceof Power && fnc.getVariable2() instanceof Power) {
|
||||
result.add(new Power(root, ((Power)fnc.getVariable1()).getVariable1(), new Sum(root, new Expression(root, ((Power)fnc.getVariable1()).getVariable2()), new Expression(root, ((Power)fnc.getVariable2()).getVariable2()))));
|
||||
} else if (fnc.getVariable1() instanceof Power) {
|
||||
result.add(new Power(root, ((Power)fnc.getVariable1()).getVariable1(), new Sum(root, new Expression(root, ((Power)fnc.getVariable1()).getVariable2()), new Number(root, 1))));
|
||||
} else if (fnc.getVariable2() instanceof Power) {
|
||||
result.add(new Power(root, ((Power)fnc.getVariable1()).getVariable1(), new Sum(root, new Number(root, 1), new Expression(root, ((Power)fnc.getVariable2()).getVariable2()))));
|
||||
if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) {
|
||||
result.add(new Power(root, ((Power)fnc.getParameter1()).getParameter1(), new Sum(root, new Expression(root, ((Power)fnc.getParameter1()).getParameter2()), new Expression(root, ((Power)fnc.getParameter2()).getParameter2()))));
|
||||
} else if (fnc.getParameter1() instanceof Power) {
|
||||
result.add(new Power(root, ((Power)fnc.getParameter1()).getParameter1(), new Sum(root, new Expression(root, ((Power)fnc.getParameter1()).getParameter2()), new Number(root, 1))));
|
||||
} else if (fnc.getParameter2() instanceof Power) {
|
||||
result.add(new Power(root, ((Power)fnc.getParameter1()).getParameter1(), new Sum(root, new Number(root, 1), new Expression(root, ((Power)fnc.getParameter2()).getParameter2()))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
@ -23,7 +23,7 @@ public class ExponentRule17 {
|
||||
public static boolean compare(Function f) {
|
||||
if (f instanceof Root) {
|
||||
final Root fnc = (Root) f;
|
||||
if (fnc.getVariable1().equals(fnc.getVariable2())) {
|
||||
if (fnc.getParameter1().equals(fnc.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -31,16 +31,13 @@ public class ExponentRule17 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication fnc = (Multiplication) f;
|
||||
final Power p = new Power(fnc.getRoot(), null, null);
|
||||
final Expression expr = new Expression(root);
|
||||
final Function a = fnc.getVariable1();
|
||||
expr.addFunctionToEnd(a);
|
||||
final Function a = fnc.getParameter1();
|
||||
final Expression expr = new Expression(root, a);
|
||||
final Number two = new Number(root, 2);
|
||||
p.setVariable1(expr);
|
||||
p.setVariable2(two);
|
||||
final Power p = new Power(fnc.getMathContext(), expr, two);
|
||||
result.add(p);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
|
||||
@ -18,7 +18,7 @@ public class ExponentRule2 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable2().equals(new Number(f.getRoot(), 1))) {
|
||||
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -26,7 +26,7 @@ public class ExponentRule2 {
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(((Power) f).getVariable1());
|
||||
result.add(((Power) f).getParameter1());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
|
||||
@ -18,7 +18,7 @@ public class ExponentRule3 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable2().equals(new Number(f.getRoot(), 0))) {
|
||||
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -26,7 +26,7 @@ public class ExponentRule3 {
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Number(f.getRoot(), 1));
|
||||
result.add(new Number(f.getMathContext(), 1));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
@ -21,34 +21,26 @@ public class ExponentRule4 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable1() instanceof Expression && ((Expression) fnc.getVariable1()).getVariablesLength() == 1 && ((Expression) fnc.getVariable1()).getVariable(0) instanceof Multiplication && fnc.getVariable2() instanceof Number) {
|
||||
if (fnc.getParameter1() instanceof Expression && ((Expression) fnc.getParameter1()).getParametersLength() == 1 && ((Expression) fnc.getParameter1()).getParameter(0) instanceof Multiplication && fnc.getParameter2() instanceof Number) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Power fnc = (Power) f;
|
||||
final Expression expr = (Expression) fnc.getVariable1();
|
||||
final Multiplication mult = (Multiplication) expr.getVariable(0);
|
||||
final Function a = mult.getVariable1();
|
||||
final Function b = mult.getVariable2();
|
||||
final Number n = (Number) fnc.getVariable2();
|
||||
final Multiplication retMult = new Multiplication(root, null, null);
|
||||
final Power p1 = new Power(root, null, null);
|
||||
final Expression e1 = new Expression(root);
|
||||
e1.addFunctionToEnd(a);
|
||||
p1.setVariable1(e1);
|
||||
p1.setVariable2(n);
|
||||
final Power p2 = new Power(root, null, null);
|
||||
final Expression e2 = new Expression(root);
|
||||
e2.addFunctionToEnd(b);
|
||||
p2.setVariable1(e2);
|
||||
p2.setVariable2(n);
|
||||
retMult.setVariable1(p1);
|
||||
retMult.setVariable2(p2);
|
||||
final Expression expr = (Expression) fnc.getParameter1();
|
||||
final Multiplication mult = (Multiplication) expr.getParameter(0);
|
||||
final Function a = mult.getParameter1();
|
||||
final Function b = mult.getParameter2();
|
||||
final Number n = (Number) fnc.getParameter2();
|
||||
final Expression e1 = new Expression(root, a);
|
||||
final Power p1 = new Power(root, e1, n);
|
||||
final Expression e2 = new Expression(root, b);
|
||||
final Power p2 = new Power(root, e2, n);
|
||||
final Multiplication retMult = new Multiplication(root, p1, p2);
|
||||
result.add(retMult);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
@ -21,18 +21,18 @@ public class ExponentRule9 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable1() instanceof Power) {
|
||||
if (fnc.getParameter1() instanceof Power) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Power powC = (Power) f;
|
||||
final Power powB = (Power) powC.getVariable1();
|
||||
final Power p = new Power(root, powB.getVariable1(), new Multiplication(root, new Expression(root, powB.getVariable2()), new Expression(root, powC.getVariable2())));
|
||||
final Power powB = (Power) powC.getParameter1();
|
||||
final Power p = new Power(root, powB.getParameter1(), new Multiplication(root, new Expression(root, powB.getParameter2()), new Expression(root, powC.getParameter2())));
|
||||
result.add(p);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
/**
|
||||
@ -18,13 +18,13 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class FractionsRule1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final Division fnc = (Division) f;
|
||||
if (fnc.getVariable1() instanceof Number) {
|
||||
final Number numb1 = (Number) fnc.getVariable1();
|
||||
if (fnc.getParameter1() instanceof Number) {
|
||||
final Number numb1 = (Number) fnc.getParameter1();
|
||||
if (numb1.equals(new Number(root, 0))) {
|
||||
if (fnc.getVariable2() instanceof Number) {
|
||||
final Number numb2 = (Number) fnc.getVariable2();
|
||||
if (fnc.getParameter2() instanceof Number) {
|
||||
final Number numb2 = (Number) fnc.getParameter2();
|
||||
if (numb2.equals(new Number(root, 0))) {
|
||||
return false;
|
||||
}
|
||||
@ -37,7 +37,7 @@ public class FractionsRule1 {
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Number(f.getRoot(), 0));
|
||||
result.add(new Number(f.getMathContext(), 0));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
|
||||
/**
|
||||
@ -22,14 +22,14 @@ public class FractionsRule11 {
|
||||
Function a;
|
||||
Function c;
|
||||
Division div2;
|
||||
if (fnc.getVariable2() instanceof Division) {
|
||||
div2 = (Division) fnc.getVariable2();
|
||||
if (fnc.getParameter2() instanceof Division) {
|
||||
div2 = (Division) fnc.getParameter2();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
a = fnc.getVariable1();
|
||||
c = div2.getVariable2();
|
||||
return new Multiplication(fnc.getRoot(), a, c).isSolved() == false;
|
||||
a = fnc.getParameter1();
|
||||
c = div2.getParameter2();
|
||||
return new Multiplication(fnc.getMathContext(), a, c).isSimplified() == false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
@ -39,12 +39,12 @@ public class FractionsRule11 {
|
||||
Function b;
|
||||
Function c;
|
||||
|
||||
Division div2 = (Division) fnc.getVariable2();
|
||||
Division div2 = (Division) fnc.getParameter2();
|
||||
|
||||
a = fnc.getVariable1();
|
||||
b = div2.getVariable1();
|
||||
c = div2.getVariable2();
|
||||
result.add(new Division(fnc.getRoot(), new Multiplication(fnc.getRoot(), new Expression(fnc.getRoot(), a), new Expression(fnc.getRoot(), c)), b));
|
||||
a = fnc.getParameter1();
|
||||
b = div2.getParameter1();
|
||||
c = div2.getParameter2();
|
||||
result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), b));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
|
||||
/**
|
||||
@ -21,11 +21,11 @@ public class FractionsRule12 {
|
||||
final Division fnc = (Division) f;
|
||||
Function a;
|
||||
Function c;
|
||||
if (fnc.getVariable1() instanceof Division) {
|
||||
final Division div2 = (Division) fnc.getVariable1();
|
||||
a = fnc.getVariable1();
|
||||
c = div2.getVariable2();
|
||||
return new Multiplication(fnc.getRoot(), a, c).isSolved() == false;
|
||||
if (fnc.getParameter1() instanceof Division) {
|
||||
final Division div2 = (Division) fnc.getParameter1();
|
||||
a = fnc.getParameter1();
|
||||
c = div2.getParameter2();
|
||||
return new Multiplication(fnc.getMathContext(), a, c).isSimplified() == false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -37,11 +37,11 @@ public class FractionsRule12 {
|
||||
Function b;
|
||||
Function c;
|
||||
|
||||
final Division div2 = (Division) fnc.getVariable1();
|
||||
a = fnc.getVariable2();
|
||||
b = div2.getVariable1();
|
||||
c = div2.getVariable2();
|
||||
result.add(new Division(fnc.getRoot(), new Multiplication(fnc.getRoot(), new Expression(fnc.getRoot(), a), new Expression(fnc.getRoot(), c)), b));
|
||||
final Division div2 = (Division) fnc.getParameter1();
|
||||
a = fnc.getParameter2();
|
||||
b = div2.getParameter1();
|
||||
c = div2.getParameter2();
|
||||
result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), b));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
|
||||
/**
|
||||
@ -23,26 +23,26 @@ public class FractionsRule14 {
|
||||
Function b;
|
||||
Function c;
|
||||
Function d;
|
||||
if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getVariable1();
|
||||
final Division div2 = (Division) fnc.getVariable2();
|
||||
a = div1.getVariable1();
|
||||
b = div1.getVariable2();
|
||||
c = div2.getVariable1();
|
||||
d = div2.getVariable2();
|
||||
return new Multiplication(f.getRoot(), a, c).isSolved() == false || new Multiplication(f.getRoot(), b, d).isSolved() == false;
|
||||
} else if (fnc.getVariable1() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getVariable1();
|
||||
a = div1.getVariable1();
|
||||
b = div1.getVariable2();
|
||||
c = fnc.getVariable2();
|
||||
return new Multiplication(f.getRoot(), a, c).isSolved() == false;
|
||||
} else if (fnc.getVariable2() instanceof Division) {
|
||||
final Division div2 = (Division) fnc.getVariable2();
|
||||
a = fnc.getVariable1();
|
||||
c = div2.getVariable1();
|
||||
d = div2.getVariable2();
|
||||
return new Multiplication(f.getRoot(), a, c).isSolved() == false;
|
||||
if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getParameter1();
|
||||
final Division div2 = (Division) fnc.getParameter2();
|
||||
a = div1.getParameter1();
|
||||
b = div1.getParameter2();
|
||||
c = div2.getParameter1();
|
||||
d = div2.getParameter2();
|
||||
return new Multiplication(f.getMathContext(), a, c).isSimplified() == false || new Multiplication(f.getMathContext(), b, d).isSimplified() == false;
|
||||
} else if (fnc.getParameter1() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getParameter1();
|
||||
a = div1.getParameter1();
|
||||
b = div1.getParameter2();
|
||||
c = fnc.getParameter2();
|
||||
return new Multiplication(f.getMathContext(), a, c).isSimplified() == false;
|
||||
} else if (fnc.getParameter2() instanceof Division) {
|
||||
final Division div2 = (Division) fnc.getParameter2();
|
||||
a = fnc.getParameter1();
|
||||
c = div2.getParameter1();
|
||||
d = div2.getParameter2();
|
||||
return new Multiplication(f.getMathContext(), a, c).isSimplified() == false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -55,28 +55,28 @@ public class FractionsRule14 {
|
||||
Function c;
|
||||
Function d;
|
||||
|
||||
if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getVariable1();
|
||||
final Division div2 = (Division) fnc.getVariable2();
|
||||
a = div1.getVariable1();
|
||||
b = div1.getVariable2();
|
||||
c = div2.getVariable1();
|
||||
d = div2.getVariable2();
|
||||
final Division div = new Division(fnc.getRoot(), new Multiplication(fnc.getRoot(), new Expression(fnc.getRoot(), a), new Expression(fnc.getRoot(), c)), new Multiplication(fnc.getRoot(), new Expression(fnc.getRoot(), b), new Expression(fnc.getRoot(), d)));
|
||||
if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getParameter1();
|
||||
final Division div2 = (Division) fnc.getParameter2();
|
||||
a = div1.getParameter1();
|
||||
b = div1.getParameter2();
|
||||
c = div2.getParameter1();
|
||||
d = div2.getParameter2();
|
||||
final Division div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), b), new Expression(fnc.getMathContext(), d)));
|
||||
result.add(div);
|
||||
} else if (fnc.getVariable1() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getVariable1();
|
||||
a = div1.getVariable1();
|
||||
b = div1.getVariable2();
|
||||
c = fnc.getVariable2();
|
||||
final Division div = new Division(fnc.getRoot(), new Multiplication(fnc.getRoot(), new Expression(fnc.getRoot(), a), new Expression(fnc.getRoot(), c)), b);
|
||||
} else if (fnc.getParameter1() instanceof Division) {
|
||||
final Division div1 = (Division) fnc.getParameter1();
|
||||
a = div1.getParameter1();
|
||||
b = div1.getParameter2();
|
||||
c = fnc.getParameter2();
|
||||
final Division div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), b);
|
||||
result.add(div);
|
||||
} else if (fnc.getVariable2() instanceof Division) {
|
||||
final Division div2 = (Division) fnc.getVariable2();
|
||||
a = fnc.getVariable1();
|
||||
c = div2.getVariable1();
|
||||
d = div2.getVariable2();
|
||||
final Division div = new Division(fnc.getRoot(), new Multiplication(fnc.getRoot(), new Expression(fnc.getRoot(), a), new Expression(fnc.getRoot(), c)), d);
|
||||
} else if (fnc.getParameter2() instanceof Division) {
|
||||
final Division div2 = (Division) fnc.getParameter2();
|
||||
a = fnc.getParameter1();
|
||||
c = div2.getParameter1();
|
||||
d = div2.getParameter2();
|
||||
final Division div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), d);
|
||||
result.add(div);
|
||||
}
|
||||
return result;
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
/**
|
||||
@ -18,9 +18,9 @@ public class FractionsRule2 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Division fnc = (Division) f;
|
||||
if (fnc.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) fnc.getVariable2();
|
||||
if (numb.equals(new Number(f.getRoot(), 1))) {
|
||||
if (fnc.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) fnc.getParameter2();
|
||||
if (numb.equals(new Number(f.getMathContext(), 1))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -30,7 +30,7 @@ public class FractionsRule2 {
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Division fnc = (Division) f;
|
||||
result.add(fnc.getVariable1());
|
||||
result.add(fnc.getParameter1());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
/**
|
||||
@ -18,7 +18,7 @@ public class FractionsRule3 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Division fnc = (Division) f;
|
||||
if (fnc.getVariable1().equals(fnc.getVariable2())) {
|
||||
if (fnc.getParameter1().equals(fnc.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -26,7 +26,7 @@ public class FractionsRule3 {
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Number(f.getRoot(), 1));
|
||||
result.add(new Number(f.getMathContext(), 1));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
|
||||
@ -19,9 +19,9 @@ public class FractionsRule4 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) {
|
||||
final Number n2 = (Number) fnc.getVariable2();
|
||||
if (n2.equals(new Number(f.getRoot(), -1))) {
|
||||
if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) {
|
||||
final Number n2 = (Number) fnc.getParameter2();
|
||||
if (n2.equals(new Number(f.getMathContext(), -1))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -31,9 +31,9 @@ public class FractionsRule4 {
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Power fnc = (Power) f;
|
||||
final Function a = ((Division) fnc.getVariable1()).getVariable1();
|
||||
final Function b = ((Division) fnc.getVariable1()).getVariable2();
|
||||
final Division res = new Division(f.getRoot(), b, a);
|
||||
final Function a = ((Division) fnc.getParameter1()).getParameter1();
|
||||
final Function b = ((Division) fnc.getParameter1()).getParameter2();
|
||||
final Division res = new Division(f.getMathContext(), b, a);
|
||||
result.add(res);
|
||||
return result;
|
||||
}
|
||||
|
@ -4,9 +4,9 @@ import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
|
||||
@ -21,8 +21,8 @@ public class FractionsRule5 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable1() instanceof Division && fnc.getVariable2() instanceof Number) {
|
||||
final Number n2 = (Number) fnc.getVariable2();
|
||||
if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) {
|
||||
final Number n2 = (Number) fnc.getParameter2();
|
||||
if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) {
|
||||
return true;
|
||||
}
|
||||
@ -31,12 +31,12 @@ public class FractionsRule5 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Power fnc = (Power) f;
|
||||
final Function a = ((Division) fnc.getVariable1()).getVariable1();
|
||||
final Function b = ((Division) fnc.getVariable1()).getVariable2();
|
||||
final Function c = ((Number) fnc.getVariable2()).multiply(new Number(root, "-1"));
|
||||
final Function a = ((Division) fnc.getParameter1()).getParameter1();
|
||||
final Function b = ((Division) fnc.getParameter1()).getParameter2();
|
||||
final Function c = ((Number) fnc.getParameter2()).multiply(new Number(root, "-1"));
|
||||
final Division dv = new Division(root, b, a);
|
||||
final Power pow = new Power(root, dv, c);
|
||||
result.add(pow);
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
@ -18,16 +18,16 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class NumberRule1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final Multiplication mult = (Multiplication) f;
|
||||
if (mult.getVariable1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable1();
|
||||
if (mult.getParameter1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter1();
|
||||
if (numb.equals(new Number(root, 0))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (mult.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable2();
|
||||
if (mult.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter2();
|
||||
if (numb.equals(new Number(root, 0))) {
|
||||
return true;
|
||||
}
|
||||
@ -37,7 +37,7 @@ public class NumberRule1 {
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Number(f.getRoot(), "0"));
|
||||
result.add(new Number(f.getMathContext(), "0"));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
@ -18,16 +18,16 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class NumberRule2 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final Multiplication mult = (Multiplication) f;
|
||||
if (mult.getVariable1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable1();
|
||||
if (mult.getParameter1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter1();
|
||||
if (numb.equals(new Number(root, 1))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (mult.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable2();
|
||||
if (mult.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter2();
|
||||
if (numb.equals(new Number(root, 1))) {
|
||||
return true;
|
||||
}
|
||||
@ -36,22 +36,22 @@ public class NumberRule2 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
Function a = null;
|
||||
boolean aFound = false;
|
||||
final Multiplication mult = (Multiplication) f;
|
||||
if (aFound == false & mult.getVariable1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable1();
|
||||
if (aFound == false & mult.getParameter1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter1();
|
||||
if (numb.equals(new Number(root, 1))) {
|
||||
a = mult.getVariable2();
|
||||
a = mult.getParameter2();
|
||||
aFound = true;
|
||||
}
|
||||
}
|
||||
if (aFound == false && mult.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable2();
|
||||
if (aFound == false && mult.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter2();
|
||||
if (numb.equals(new Number(root, 1))) {
|
||||
a = mult.getVariable1();
|
||||
a = mult.getParameter1();
|
||||
aFound = true;
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Negative;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
@ -26,20 +26,20 @@ public class NumberRule3 {
|
||||
public static boolean compare(Function f) {
|
||||
if (f instanceof Subtraction) {
|
||||
final Subtraction sub = (Subtraction) f;
|
||||
if (sub.getVariable1().equals(sub.getVariable2())) {
|
||||
if (sub.getParameter1().equals(sub.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
} else if (f instanceof Sum) {
|
||||
final Sum sub = (Sum) f;
|
||||
if (sub.getVariable1() instanceof Negative) {
|
||||
final Negative neg = (Negative) sub.getVariable1();
|
||||
if (neg.getVariable().equals(sub.getVariable2())) {
|
||||
if (sub.getParameter1() instanceof Negative) {
|
||||
final Negative neg = (Negative) sub.getParameter1();
|
||||
if (neg.getParameter().equals(sub.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (f instanceof SumSubtraction) {
|
||||
final SumSubtraction sub = (SumSubtraction) f;
|
||||
if (sub.getVariable1().equals(sub.getVariable2())) {
|
||||
if (sub.getParameter1().equals(sub.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -47,12 +47,10 @@ public class NumberRule3 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
if (f instanceof SumSubtraction) {
|
||||
final Multiplication mul = new Multiplication(root, null, null);
|
||||
mul.setVariable1(new Number(root, 2));
|
||||
mul.setVariable2(f);
|
||||
final Multiplication mul = new Multiplication(root, new Number(root, 2), f);
|
||||
result.add(mul);
|
||||
}
|
||||
result.add(new Number(root, 0));
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
import org.warp.picalculator.math.functions.Sum;
|
||||
import org.warp.picalculator.math.functions.SumSubtraction;
|
||||
@ -26,11 +26,11 @@ public class NumberRule4 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final SumSubtraction ss = (SumSubtraction) f;
|
||||
result.add(new Sum(root, ss.getVariable1(), ss.getVariable2()));
|
||||
result.add(new Subtraction(root, ss.getVariable1(), ss.getVariable2()));
|
||||
result.add(new Sum(root, ss.getParameter1(), ss.getParameter2()));
|
||||
result.add(new Subtraction(root, ss.getParameter1(), ss.getParameter2()));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
/**
|
||||
@ -23,21 +23,21 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class NumberRule5 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final FunctionTwoValues fnc = (FunctionTwoValues) f;
|
||||
if (fnc.getVariable1().equals(new Number(root, 0)) || fnc.getVariable2().equals(new Number(root, 0))) {
|
||||
final MathContext root = f.getMathContext();
|
||||
final FunctionOperator fnc = (FunctionOperator) f;
|
||||
if (fnc.getParameter1().equals(new Number(root, 0)) || fnc.getParameter2().equals(new Number(root, 0))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final FunctionTwoValues fnc = (FunctionTwoValues) f;
|
||||
Function a = fnc.getVariable1();
|
||||
final FunctionOperator fnc = (FunctionOperator) f;
|
||||
Function a = fnc.getParameter1();
|
||||
if (a.equals(new Number(root, 0))) {
|
||||
a = fnc.getVariable2();
|
||||
a = fnc.getParameter2();
|
||||
}
|
||||
result.add(a);
|
||||
return result;
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Negative;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
@ -19,16 +19,16 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class NumberRule6 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final Multiplication mult = (Multiplication) f;
|
||||
if (mult.getVariable1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable1();
|
||||
if (mult.getParameter1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter1();
|
||||
if (numb.equals(new Number(root, -1))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (mult.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable2();
|
||||
if (mult.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter2();
|
||||
if (numb.equals(new Number(root, -1))) {
|
||||
return true;
|
||||
}
|
||||
@ -37,28 +37,27 @@ public class NumberRule6 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
Function a = null;
|
||||
boolean aFound = false;
|
||||
final Multiplication mult = (Multiplication) f;
|
||||
if (aFound == false & mult.getVariable1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable1();
|
||||
if (aFound == false & mult.getParameter1() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter1();
|
||||
if (numb.equals(new Number(root, -1))) {
|
||||
a = mult.getVariable2();
|
||||
a = mult.getParameter2();
|
||||
aFound = true;
|
||||
}
|
||||
}
|
||||
if (aFound == false && mult.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getVariable2();
|
||||
if (aFound == false && mult.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) mult.getParameter2();
|
||||
if (numb.equals(new Number(root, -1))) {
|
||||
a = mult.getVariable1();
|
||||
a = mult.getParameter1();
|
||||
aFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
final Negative minus = new Negative(root, null);
|
||||
minus.setVariable(a);
|
||||
final Negative minus = new Negative(root, a);
|
||||
|
||||
result.add(minus);
|
||||
return result;
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Sum;
|
||||
@ -19,15 +19,13 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
public class NumberRule7 {
|
||||
|
||||
public static boolean compare(Sum f) {
|
||||
return f.getVariable1().equals(f.getVariable2());
|
||||
return f.getParameter1().equals(f.getParameter2());
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Sum f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication mult = new Multiplication(root, null, null);
|
||||
mult.setVariable1(new Number(root, 2));
|
||||
mult.setVariable2(f.getVariable1());
|
||||
final Multiplication mult = new Multiplication(root, new Number(root, 2), f.getParameter1());
|
||||
result.add(mult);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Sum;
|
||||
|
||||
@ -19,32 +19,30 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
public class SyntaxRule1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final FunctionTwoValues m = (FunctionTwoValues) f;
|
||||
if (m instanceof Multiplication & m.getVariable1() instanceof Multiplication) {
|
||||
final FunctionOperator m = (FunctionOperator) f;
|
||||
if (m instanceof Multiplication & m.getParameter1() instanceof Multiplication) {
|
||||
return true;
|
||||
} else if (m instanceof Sum & m.getVariable1() instanceof Sum) {
|
||||
} else if (m instanceof Sum & m.getParameter1() instanceof Sum) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final FunctionTwoValues mOut = (FunctionTwoValues) f;
|
||||
final Function a = ((FunctionTwoValues) mOut.getVariable1()).getVariable1();
|
||||
final Function b = ((FunctionTwoValues) mOut.getVariable1()).getVariable2();
|
||||
final Function c = mOut.getVariable2();
|
||||
FunctionTwoValues mIn;
|
||||
FunctionOperator mOut = (FunctionOperator) f;
|
||||
final Function a = ((FunctionOperator) mOut.getParameter1()).getParameter1();
|
||||
final Function b = ((FunctionOperator) mOut.getParameter1()).getParameter2();
|
||||
final Function c = mOut.getParameter2();
|
||||
FunctionOperator mIn;
|
||||
if (f instanceof Multiplication) {
|
||||
mIn = new Multiplication(root, null, null);
|
||||
mIn = new Multiplication(root, b, c);
|
||||
} else {
|
||||
mIn = new Sum(root, null, null);
|
||||
mIn = new Sum(root, b, c);
|
||||
}
|
||||
mOut.setVariable1(a);
|
||||
mIn.setVariable1(b);
|
||||
mIn.setVariable2(c);
|
||||
mOut.setVariable2(mIn);
|
||||
mOut = mOut.setParameter1(a);
|
||||
mOut = mOut.setParameter2(mIn);
|
||||
result.add(mOut);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Sum;
|
||||
|
||||
/**
|
||||
@ -18,12 +18,12 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
public class SyntaxRule2 {
|
||||
|
||||
public static boolean compare(Sum f) {
|
||||
if (f.getVariable2() instanceof Sum) {
|
||||
if (f.getParameter2() instanceof Sum) {
|
||||
return true;
|
||||
}
|
||||
if (f.getVariable2() instanceof Expression) {
|
||||
final Expression e = (Expression) f.getVariable2();
|
||||
if (e.getVariablesLength() == 1 && e.getVariable(0) instanceof Sum) {
|
||||
if (f.getParameter2() instanceof Expression) {
|
||||
final Expression e = (Expression) f.getParameter2();
|
||||
if (e.getParametersLength() == 1 && e.getParameter(0) instanceof Sum) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -31,22 +31,20 @@ public class SyntaxRule2 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Sum f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Function a = f.getVariable1();
|
||||
final Function a = f.getParameter1();
|
||||
Function b, c;
|
||||
if (f.getVariable2() instanceof Sum) {
|
||||
b = ((Sum) f.getVariable2()).getVariable1();
|
||||
c = ((Sum) f.getVariable2()).getVariable2();
|
||||
if (f.getParameter2() instanceof Sum) {
|
||||
b = ((Sum) f.getParameter2()).getParameter1();
|
||||
c = ((Sum) f.getParameter2()).getParameter2();
|
||||
} else {
|
||||
b = ((Sum) ((Expression) f.getVariable2()).getVariable(0)).getVariable1();
|
||||
c = ((Sum) ((Expression) f.getVariable2()).getVariable(0)).getVariable2();
|
||||
b = ((Sum) ((Expression) f.getParameter2()).getParameter(0)).getParameter1();
|
||||
c = ((Sum) ((Expression) f.getParameter2()).getParameter(0)).getParameter2();
|
||||
}
|
||||
final Sum mIn = new Sum(root, null, null);
|
||||
f.setVariable1(mIn);
|
||||
mIn.setVariable1(a);
|
||||
mIn.setVariable2(b);
|
||||
f.setVariable2(c);
|
||||
final Sum mIn = new Sum(root, a, b);
|
||||
f.setParameter1(mIn);
|
||||
f.setParameter2(c);
|
||||
result.add(f);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Power;
|
||||
import org.warp.picalculator.math.functions.Undefined;
|
||||
@ -19,16 +19,16 @@ import org.warp.picalculator.math.functions.Undefined;
|
||||
public class UndefinedRule1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final Power fnc = (Power) f;
|
||||
if (fnc.getVariable1().equals(new Number(root, 0)) && fnc.getVariable2().equals(new Number(root, 0))) {
|
||||
if (fnc.getParameter1().equals(new Number(root, 0)) && fnc.getParameter2().equals(new Number(root, 0))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Undefined(root));
|
||||
return result;
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Undefined;
|
||||
|
||||
@ -19,10 +19,10 @@ import org.warp.picalculator.math.functions.Undefined;
|
||||
public class UndefinedRule2 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final Division fnc = (Division) f;
|
||||
if (fnc.getVariable2() instanceof Number) {
|
||||
final Number numb = (Number) fnc.getVariable2();
|
||||
if (fnc.getParameter2() instanceof Number) {
|
||||
final Number numb = (Number) fnc.getParameter2();
|
||||
if (numb.equals(new Number(root, 0))) {
|
||||
return true;
|
||||
}
|
||||
@ -31,7 +31,7 @@ public class UndefinedRule2 {
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
result.add(new Undefined(root));
|
||||
return result;
|
||||
|
@ -3,10 +3,10 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
import org.warp.picalculator.math.functions.Sum;
|
||||
@ -20,39 +20,34 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
*/
|
||||
public class VariableRule1 {
|
||||
|
||||
public static boolean compare(FunctionTwoValues fnc) {
|
||||
if (fnc.getVariable1() instanceof Multiplication & fnc.getVariable2() instanceof Multiplication) {
|
||||
final Multiplication m1 = (Multiplication) fnc.getVariable1();
|
||||
final Multiplication m2 = (Multiplication) fnc.getVariable2();
|
||||
if (m1.getVariable2().equals(m2.getVariable2())) {
|
||||
public static boolean compare(FunctionOperator fnc) {
|
||||
if (fnc.getParameter1() instanceof Multiplication & fnc.getParameter2() instanceof Multiplication) {
|
||||
final Multiplication m1 = (Multiplication) fnc.getParameter1();
|
||||
final Multiplication m2 = (Multiplication) fnc.getParameter2();
|
||||
if (m1.getParameter2().equals(m2.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(FunctionTwoValues fnc) throws Error {
|
||||
final Calculator root = fnc.getRoot();
|
||||
public static ArrayList<Function> execute(FunctionOperator fnc) throws Error {
|
||||
final MathContext root = fnc.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication m1 = (Multiplication) fnc.getVariable1();
|
||||
final Multiplication m2 = (Multiplication) fnc.getVariable2();
|
||||
final Function a = m1.getVariable1();
|
||||
final Function b = m2.getVariable1();
|
||||
final Function x = m1.getVariable2();
|
||||
final Multiplication m1 = (Multiplication) fnc.getParameter1();
|
||||
final Multiplication m2 = (Multiplication) fnc.getParameter2();
|
||||
final Function a = m1.getParameter1();
|
||||
final Function b = m2.getParameter1();
|
||||
final Function x = m1.getParameter2();
|
||||
|
||||
final Multiplication retm = new Multiplication(root, null, null);
|
||||
final Expression rete = new Expression(root);
|
||||
FunctionTwoValues rets;
|
||||
FunctionOperator rets;
|
||||
if (fnc instanceof Sum) {
|
||||
rets = new Sum(root, null, null);
|
||||
rets = new Sum(root, a, b);
|
||||
} else {
|
||||
rets = new Subtraction(root, null, null);
|
||||
rets = new Subtraction(root, a, b);
|
||||
}
|
||||
rets.setVariable1(a);
|
||||
rets.setVariable2(b);
|
||||
rete.addFunctionToEnd(rets);
|
||||
retm.setVariable1(rete);
|
||||
retm.setVariable2(x);
|
||||
final Expression rete = new Expression(root, rets);
|
||||
final Multiplication retm = new Multiplication(root, rete, x);
|
||||
result.add(retm);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
@ -21,37 +21,32 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
*/
|
||||
public class VariableRule2 {
|
||||
|
||||
public static boolean compare(FunctionTwoValues fnc) {
|
||||
if (fnc.getVariable1() instanceof Multiplication) {
|
||||
final Multiplication m1 = (Multiplication) fnc.getVariable1();
|
||||
if (m1.getVariable2().equals(fnc.getVariable2())) {
|
||||
public static boolean compare(FunctionOperator fnc) {
|
||||
if (fnc.getParameter1() instanceof Multiplication) {
|
||||
final Multiplication m1 = (Multiplication) fnc.getParameter1();
|
||||
if (m1.getParameter2().equals(fnc.getParameter2())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(FunctionTwoValues fnc) throws Error {
|
||||
final Calculator root = fnc.getRoot();
|
||||
public static ArrayList<Function> execute(FunctionOperator fnc) throws Error {
|
||||
final MathContext root = fnc.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication m1 = (Multiplication) fnc.getVariable1();
|
||||
final Function a = m1.getVariable1();
|
||||
final Function x = fnc.getVariable2();
|
||||
final Multiplication m1 = (Multiplication) fnc.getParameter1();
|
||||
final Function a = m1.getParameter1();
|
||||
final Function x = fnc.getParameter2();
|
||||
|
||||
final Multiplication retm = new Multiplication(root, null, null);
|
||||
final Expression rete = new Expression(root);
|
||||
|
||||
FunctionTwoValues rets;
|
||||
FunctionOperator rets;
|
||||
if (fnc instanceof Sum) {
|
||||
rets = new Sum(root, null, null);
|
||||
rets = new Sum(root, a, new Number(root, 1));
|
||||
} else {
|
||||
rets = new Subtraction(root, null, null);
|
||||
rets = new Subtraction(root, a, new Number(root, 1));
|
||||
}
|
||||
rets.setVariable1(a);
|
||||
rets.setVariable2(new Number(root, 1));
|
||||
rete.addFunctionToEnd(rets);
|
||||
retm.setVariable1(rete);
|
||||
retm.setVariable2(x);
|
||||
final Expression rete = new Expression(root, rets);
|
||||
final Multiplication retm = new Multiplication(root, rete, x);
|
||||
result.add(retm);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,10 +3,10 @@ package org.warp.picalculator.math.rules;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Expression;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
@ -21,37 +21,32 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
*/
|
||||
public class VariableRule3 {
|
||||
|
||||
public static boolean compare(FunctionTwoValues fnc) {
|
||||
if (fnc.getVariable2() instanceof Multiplication) {
|
||||
final Multiplication m2 = (Multiplication) fnc.getVariable2();
|
||||
if (m2.getVariable2().equals(fnc.getVariable1())) {
|
||||
public static boolean compare(FunctionOperator fnc) {
|
||||
if (fnc.getParameter2() instanceof Multiplication) {
|
||||
final Multiplication m2 = (Multiplication) fnc.getParameter2();
|
||||
if (m2.getParameter2().equals(fnc.getParameter1())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(FunctionTwoValues fnc) throws Error {
|
||||
final Calculator root = fnc.getRoot();
|
||||
public static ArrayList<Function> execute(FunctionOperator fnc) throws Error {
|
||||
final MathContext root = fnc.getMathContext();
|
||||
final ArrayList<Function> result = new ArrayList<>();
|
||||
final Multiplication m2 = (Multiplication) fnc.getVariable2();
|
||||
final Function a = m2.getVariable1();
|
||||
final Function x = fnc.getVariable1();
|
||||
final Multiplication m2 = (Multiplication) fnc.getParameter2();
|
||||
final Function a = m2.getParameter1();
|
||||
final Function x = fnc.getParameter1();
|
||||
|
||||
final Multiplication retm = new Multiplication(root, null, null);
|
||||
final Expression rete = new Expression(root);
|
||||
FunctionTwoValues rets;
|
||||
FunctionOperator rets;
|
||||
if (fnc instanceof Sum) {
|
||||
rets = new Sum(root, null, null);
|
||||
rets = new Sum(root, new Number(root, 1), a);
|
||||
} else {
|
||||
rets = new Subtraction(root, null, null);
|
||||
rets = new Subtraction(root, new Number(root, 1), a);
|
||||
}
|
||||
|
||||
rets.setVariable1(new Number(root, 1));
|
||||
rets.setVariable2(a);
|
||||
rete.addFunctionToEnd(rets);
|
||||
retm.setVariable1(rete);
|
||||
retm.setVariable2(x);
|
||||
final Expression rete = new Expression(root, rets);
|
||||
final Multiplication retm = new Multiplication(root, rete, x);
|
||||
result.add(retm);
|
||||
return result;
|
||||
}
|
||||
|
@ -3,9 +3,9 @@ package org.warp.picalculator.math.rules.methods;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Division;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
@ -19,11 +19,11 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class DivisionRule1 {
|
||||
|
||||
public static boolean compare(Division f) {
|
||||
return f.getVariable1().isSolved() && f.getVariable2().isSolved() && !(f.getVariable1() instanceof Number && f.getVariable2() instanceof Number) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null;
|
||||
return f.getParameter1().isSimplified() && f.getParameter2().isSimplified() && !(f.getParameter1() instanceof Number && f.getParameter2() instanceof Number) && getFirstWorkingDivisionCouple(getDivisionElements(f)) != null;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Division f) throws Error {
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
Function result;
|
||||
final ArrayList<Function> elements = getDivisionElements(f);
|
||||
final int[] workingElementCouple = getFirstWorkingDivisionCouple(elements);
|
||||
@ -49,18 +49,18 @@ public class DivisionRule1 {
|
||||
|
||||
private static ArrayList<Function> getDivisionElements(Division division) {
|
||||
final ArrayList<Function> elementsNumerator = new ArrayList<>();
|
||||
Function numMult = division.getVariable1();
|
||||
Function numMult = division.getParameter1();
|
||||
while (numMult instanceof Multiplication) {
|
||||
elementsNumerator.add(((Multiplication) numMult).getVariable1());
|
||||
numMult = ((Multiplication) numMult).getVariable2();
|
||||
elementsNumerator.add(((Multiplication) numMult).getParameter1());
|
||||
numMult = ((Multiplication) numMult).getParameter2();
|
||||
}
|
||||
elementsNumerator.add(numMult);
|
||||
|
||||
final ArrayList<Function> elementsDenominator = new ArrayList<>();
|
||||
Function denomMult = division.getVariable1();
|
||||
Function denomMult = division.getParameter1();
|
||||
while (denomMult instanceof Multiplication) {
|
||||
elementsDenominator.add(((Multiplication) denomMult).getVariable1());
|
||||
denomMult = ((Multiplication) denomMult).getVariable2();
|
||||
elementsDenominator.add(((Multiplication) denomMult).getParameter1());
|
||||
denomMult = ((Multiplication) denomMult).getParameter2();
|
||||
}
|
||||
elementsDenominator.add(denomMult);
|
||||
|
||||
@ -81,7 +81,7 @@ public class DivisionRule1 {
|
||||
if (i != j) {
|
||||
Function testFunc;
|
||||
testFunc = new Multiplication(root, a, b);
|
||||
if (!testFunc.isSolved()) {
|
||||
if (!testFunc.isSimplified()) {
|
||||
return new int[] { i, j };
|
||||
}
|
||||
}
|
||||
|
@ -3,8 +3,8 @@ package org.warp.picalculator.math.rules.methods;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.functions.Multiplication;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
|
||||
@ -18,12 +18,12 @@ import org.warp.picalculator.math.functions.Number;
|
||||
public class MultiplicationMethod1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
return ((Multiplication) f).getVariable1().isSolved() && ((Multiplication) f).getVariable2().isSolved() && !(((Multiplication) f).getVariable1() instanceof Number && ((Multiplication) f).getVariable2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null;
|
||||
return ((Multiplication) f).getParameter1().isSimplified() && ((Multiplication) f).getParameter2().isSimplified() && !(((Multiplication) f).getParameter1() instanceof Number && ((Multiplication) f).getParameter2() instanceof Number) && getFirstWorkingMultiplicationCouple(getMultiplicationElements(f)) != null;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
Function result;
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> elements = getMultiplicationElements(f);
|
||||
final int[] workingElementCouple = getFirstWorkingMultiplicationCouple(elements);
|
||||
final Function elem1 = elements.get(workingElementCouple[0]);
|
||||
@ -49,8 +49,8 @@ public class MultiplicationMethod1 {
|
||||
private static ArrayList<Function> getMultiplicationElements(Function mult) {
|
||||
final ArrayList<Function> elements = new ArrayList<>();
|
||||
while (mult instanceof Multiplication) {
|
||||
elements.add(((Multiplication) mult).getVariable1());
|
||||
mult = ((Multiplication) mult).getVariable2();
|
||||
elements.add(((Multiplication) mult).getParameter1());
|
||||
mult = ((Multiplication) mult).getParameter2();
|
||||
}
|
||||
elements.add(mult);
|
||||
return elements;
|
||||
@ -66,7 +66,7 @@ public class MultiplicationMethod1 {
|
||||
if (elements.size() == 2) {
|
||||
return null;
|
||||
}
|
||||
final Calculator root = elements.get(0).getRoot();
|
||||
final MathContext root = elements.get(0).getMathContext();
|
||||
for (int i = 0; i < size; i++) {
|
||||
a = elements.get(i);
|
||||
for (int j = 0; j < size; j++) {
|
||||
@ -74,7 +74,7 @@ public class MultiplicationMethod1 {
|
||||
if (i != j) {
|
||||
Function testFunc;
|
||||
testFunc = new Multiplication(root, a, b);
|
||||
if (!testFunc.isSolved()) {
|
||||
if (!testFunc.isSimplified()) {
|
||||
return new int[] { i, j };
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,9 @@ import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.warp.picalculator.Error;
|
||||
import org.warp.picalculator.math.Calculator;
|
||||
import org.warp.picalculator.math.functions.Function;
|
||||
import org.warp.picalculator.math.functions.FunctionTwoValues;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.Function;
|
||||
import org.warp.picalculator.math.FunctionOperator;
|
||||
import org.warp.picalculator.math.functions.Negative;
|
||||
import org.warp.picalculator.math.functions.Number;
|
||||
import org.warp.picalculator.math.functions.Subtraction;
|
||||
@ -22,13 +22,13 @@ import org.warp.picalculator.math.functions.Sum;
|
||||
public class SumMethod1 {
|
||||
|
||||
public static boolean compare(Function f) {
|
||||
final Calculator root = f.getRoot();
|
||||
return (f instanceof Sum || f instanceof Subtraction) && ((FunctionTwoValues) f).getVariable1().isSolved() && ((FunctionTwoValues) f).getVariable2().isSolved() && !(((FunctionTwoValues) f).getVariable1() instanceof Number && ((FunctionTwoValues) f).getVariable2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null;
|
||||
final MathContext root = f.getMathContext();
|
||||
return (f instanceof Sum || f instanceof Subtraction) && ((FunctionOperator) f).getParameter1().isSimplified() && ((FunctionOperator) f).getParameter2().isSimplified() && !(((FunctionOperator) f).getParameter1() instanceof Number && ((FunctionOperator) f).getParameter2() instanceof Number) && getFirstWorkingSumCouple(root, getSumElements(f)) != null;
|
||||
}
|
||||
|
||||
public static ArrayList<Function> execute(Function f) throws Error {
|
||||
Function result;
|
||||
final Calculator root = f.getRoot();
|
||||
final MathContext root = f.getMathContext();
|
||||
final ArrayList<Function> elements = getSumElements(f);
|
||||
final int[] workingElementCouple = getFirstWorkingSumCouple(root, elements);
|
||||
final Function elem1 = elements.get(workingElementCouple[0]);
|
||||
@ -41,11 +41,11 @@ public class SumMethod1 {
|
||||
final Function a = prec;
|
||||
final Function b = elements.get(i);
|
||||
if (b instanceof Negative) {
|
||||
prec = new Subtraction(root, a, ((Negative) b).getVariable());
|
||||
((FunctionTwoValues) prec).getVariable2();
|
||||
prec = new Subtraction(root, a, ((Negative) b).getParameter());
|
||||
((FunctionOperator) prec).getParameter2();
|
||||
} else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) {
|
||||
prec = new Subtraction(root, a, ((Number) b).multiply(new Number(root, -1)));
|
||||
((FunctionTwoValues) prec).getVariable2();
|
||||
((FunctionOperator) prec).getParameter2();
|
||||
} else {
|
||||
prec = new Sum(root, a, b);
|
||||
}
|
||||
@ -60,21 +60,21 @@ public class SumMethod1 {
|
||||
}
|
||||
|
||||
private static ArrayList<Function> getSumElements(Function sum) {
|
||||
final Calculator root = sum.getRoot();
|
||||
final MathContext root = sum.getMathContext();
|
||||
final ArrayList<Function> elements = new ArrayList<>();
|
||||
while (sum instanceof Sum || sum instanceof Subtraction) {
|
||||
if (sum instanceof Sum) {
|
||||
elements.add(((FunctionTwoValues) sum).getVariable2());
|
||||
elements.add(((FunctionOperator) sum).getParameter2());
|
||||
} else {
|
||||
elements.add(new Negative(root, ((FunctionTwoValues) sum).getVariable2()));
|
||||
elements.add(new Negative(root, ((FunctionOperator) sum).getParameter2()));
|
||||
}
|
||||
sum = ((FunctionTwoValues) sum).getVariable1();
|
||||
sum = ((FunctionOperator) sum).getParameter1();
|
||||
}
|
||||
elements.add(sum);
|
||||
return elements;
|
||||
}
|
||||
|
||||
private static int[] getFirstWorkingSumCouple(Calculator root, ArrayList<Function> elements) {
|
||||
private static int[] getFirstWorkingSumCouple(MathContext root, ArrayList<Function> elements) {
|
||||
final int size = elements.size();
|
||||
Function a;
|
||||
Function b;
|
||||
@ -88,17 +88,17 @@ public class SumMethod1 {
|
||||
if (i != j) {
|
||||
Function testFunc;
|
||||
if (b instanceof Negative) {
|
||||
testFunc = new Subtraction(root, a, ((Negative) b).getVariable());
|
||||
testFunc = new Subtraction(root, a, ((Negative) b).getParameter());
|
||||
} else if (b instanceof Number && ((Number) b).getTerm().compareTo(BigDecimal.ZERO) < 0) {
|
||||
testFunc = new Subtraction(root, a, ((Number) b).multiply(new Number(root, -1)));
|
||||
} else if (a instanceof Negative) {
|
||||
testFunc = new Subtraction(root, b, ((Negative) a).getVariable());
|
||||
testFunc = new Subtraction(root, b, ((Negative) a).getParameter());
|
||||
} else if (a instanceof Number && ((Number) a).getTerm().compareTo(BigDecimal.ZERO) < 0) {
|
||||
testFunc = new Subtraction(root, b, ((Number) a).multiply(new Number(root, -1)));
|
||||
} else {
|
||||
testFunc = new Sum(root, a, b);
|
||||
}
|
||||
if (!testFunc.isSolved()) {
|
||||
if (!testFunc.isSimplified()) {
|
||||
return new int[] { i, j };
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user