Updated algebra rules

This commit is contained in:
Andrea Cavalli 2017-12-20 18:07:11 +01:00
parent 85d1e7d713
commit 209253fcac
12 changed files with 118 additions and 51 deletions

View File

@ -6,7 +6,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-9.0.1">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

Binary file not shown.

View File

@ -85,10 +85,12 @@ public final class DisplayManager implements RenderingLoop {
private GraphicEngine chooseGraphicEngine() { private GraphicEngine chooseGraphicEngine() {
GraphicEngine d; GraphicEngine d;
d = new FBEngine(); if (!StaticVars.debugOn) {
if (d.isSupported()) { d = new FBEngine();
Utils.out.println(1, "Using FB Graphic Engine"); if (d.isSupported()) {
return d; Utils.out.println(1, "Using FB Graphic Engine");
return d;
}
} }
d = new GPUEngine(); d = new GPUEngine();
if (d.isSupported()) { if (d.isSupported()) {

View File

@ -41,7 +41,7 @@ public class SwingWindow extends JFrame {
final BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); final BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
if (StaticVars.debugOn) { if (StaticVars.debugOn) {
if (StaticVars.debugWindow2x) mult = 3; if (StaticVars.debugWindow2x) mult = 2;
if (Utils.debugThirdScreen) { if (Utils.debugThirdScreen) {
this.setLocation(2880, 900); this.setLocation(2880, 900);
setResizable(false); setResizable(false);

View File

@ -52,6 +52,7 @@ public class MathInputScreen extends Screen {
private double computingElapsedTime = 0; private double computingElapsedTime = 0;
private boolean computingBreakTipVisible = false; private boolean computingBreakTipVisible = false;
boolean mustRefresh = true; boolean mustRefresh = true;
private int currentStep = 0;
public MathInputScreen() { public MathInputScreen() {
super(); super();
@ -176,47 +177,22 @@ public class MathInputScreen extends Screen {
case HISTORY_BACK: case HISTORY_BACK:
if (userInput.isExtraOpened()) { if (userInput.isExtraOpened()) {
userInput.closeExtra(); userInput.closeExtra();
currentStep = 0;
mustRefresh = true; mustRefresh = true;
return true; return true;
} }
default: default:
if (userInput.isExtraOpened() && userInput.getExtraKeyboardEventListener().keyPressed(k)) { if (userInput.isExtraOpened() && userInput.getExtraKeyboardEventListener().keyPressed(k)) {
currentStep = 0;
return true; return true;
} else { } else {
final boolean step = k == Key.STEP;
switch (k) { switch (k) {
case STEP: case STEP:
// if (newExpression.length() > 0) { currentStep++;
// if (firstStep) {
// try {
// try {
// interpreta(true);
// showVariablesDialog(() -> {
// currentExpression = newExpression;
// calc.f2 = calc.f;
// firstStep = false;
// step();
// });
// } catch (final Exception ex) {
// if (Utils.debugOn) {
// ex.printStackTrace();
// }
// throw new Error(Errors.SYNTAX_ERROR);
// }
// } catch (final Error e) {
// final StringWriter sw = new StringWriter();
// final PrintWriter pw = new PrintWriter(sw);
// e.printStackTrace(pw);
// d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
// DisplayManager.INSTANCE.error = e.id.toString();
// System.err.println(e.id);
// }
// } else {
// step();
// }
// }
// return true;
case SIMPLIFY: case SIMPLIFY:
if (!step) currentStep = 0;
if (DisplayManager.INSTANCE.error != null) { if (DisplayManager.INSTANCE.error != null) {
//TODO: make the error management a global API rather than being relegated to this screen. //TODO: make the error management a global API rather than being relegated to this screen.
Utils.out.println(1, "Resetting after error..."); Utils.out.println(1, "Resetting after error...");
@ -241,11 +217,12 @@ public class MathInputScreen extends Screen {
calc.f2.clear(); calc.f2.clear();
} }
calc.f.add(expr); calc.f.add(expr);
Utils.out.println(2, "INPUT: " + expr);
int stop = 0; int stop = 0;
boolean done = false; boolean done = false;
ObjectArrayList<Function> resultExpressions = new ObjectArrayList<>(); ObjectArrayList<Function> resultExpressions = new ObjectArrayList<>();
resultExpressions.add(expr.getParameter()); resultExpressions.add(expr.getParameter());
while (!done && stop < 3000) { while (!done && stop < (step?currentStep:3000)) {
if (Thread.interrupted()) throw new InterruptedException(); if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> newResultExpressions = new ObjectArrayList<>(); ObjectArrayList<Function> newResultExpressions = new ObjectArrayList<>();
done = true; done = true;
@ -272,10 +249,14 @@ public class MathInputScreen extends Screen {
newResultExpressions.add(newResult); newResultExpressions.add(newResult);
} }
} }
if (StaticVars.debugOn) {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "STEP: "+newResultExpressions); }
resultExpressions = newResultExpressions; resultExpressions = newResultExpressions;
stop++; stop++;
} }
Utils.out.println(2, "INPUT: " + expr); if (stop >= 3000) {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "Too much steps! Stopped.");
}
for (Function rr : resultExpressions) { for (Function rr : resultExpressions) {
Utils.out.println(1, "RESULT: " + rr.toString()); Utils.out.println(1, "RESULT: " + rr.toString());
} }
@ -416,6 +397,7 @@ public class MathInputScreen extends Screen {
return true; return true;
case DELETE: case DELETE:
userInput.del(); userInput.del();
currentStep = 0;
mustRefresh = true; mustRefresh = true;
return true; return true;
case LEFT: case LEFT:
@ -429,6 +411,7 @@ public class MathInputScreen extends Screen {
case RESET: case RESET:
userInput.clear(); userInput.clear();
result.clear(); result.clear();
currentStep = 0;
if (DisplayManager.INSTANCE.error != null) { if (DisplayManager.INSTANCE.error != null) {
Utils.out.println(1, "Resetting after error..."); Utils.out.println(1, "Resetting after error...");
DisplayManager.INSTANCE.error = null; DisplayManager.INSTANCE.error = null;
@ -437,6 +420,7 @@ public class MathInputScreen extends Screen {
case SURD_MODE: case SURD_MODE:
calc.exactMode = !calc.exactMode; calc.exactMode = !calc.exactMode;
result.clear(); result.clear();
currentStep = 0;
Keyboard.keyPressed(Key.SIMPLIFY); Keyboard.keyPressed(Key.SIMPLIFY);
return true; return true;
case debug1: case debug1:
@ -465,18 +449,21 @@ public class MathInputScreen extends Screen {
case debug_DEG: case debug_DEG:
if (calc.angleMode.equals(AngleMode.DEG) == false) { if (calc.angleMode.equals(AngleMode.DEG) == false) {
calc.angleMode = AngleMode.DEG; calc.angleMode = AngleMode.DEG;
currentStep = 0;
return true; return true;
} }
return false; return false;
case debug_RAD: case debug_RAD:
if (calc.angleMode.equals(AngleMode.RAD) == false) { if (calc.angleMode.equals(AngleMode.RAD) == false) {
calc.angleMode = AngleMode.RAD; calc.angleMode = AngleMode.RAD;
currentStep = 0;
return true; return true;
} }
return false; return false;
case debug_GRA: case debug_GRA:
if (calc.angleMode.equals(AngleMode.GRA) == false) { if (calc.angleMode.equals(AngleMode.GRA) == false) {
calc.angleMode = AngleMode.GRA; calc.angleMode = AngleMode.GRA;
currentStep = 0;
return true; return true;
} }
return false; return false;
@ -488,6 +475,7 @@ public class MathInputScreen extends Screen {
} else { } else {
calc.angleMode = AngleMode.DEG; calc.angleMode = AngleMode.DEG;
} }
currentStep = 0;
return true; return true;
default: default:
return false; return false;

View File

@ -9,6 +9,7 @@ import org.warp.picalculator.math.FunctionOperator;
import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.MathematicalSymbols; import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.rules.ExpandRule1; import org.warp.picalculator.math.rules.ExpandRule1;
import org.warp.picalculator.math.rules.ExpandRule2;
import org.warp.picalculator.math.rules.ExponentRule15; import org.warp.picalculator.math.rules.ExponentRule15;
import org.warp.picalculator.math.rules.ExponentRule16; import org.warp.picalculator.math.rules.ExponentRule16;
import org.warp.picalculator.math.rules.FractionsRule14; import org.warp.picalculator.math.rules.FractionsRule14;
@ -44,6 +45,9 @@ public class Multiplication extends FunctionOperator {
if (ExpandRule1.compare(this)) { if (ExpandRule1.compare(this)) {
return true; return true;
} }
if (ExpandRule2.compare(this)) {
return true;
}
if (ExponentRule15.compare(this)) { if (ExponentRule15.compare(this)) {
return true; return true;
} }
@ -68,6 +72,8 @@ public class Multiplication extends FunctionOperator {
result = NumberRule2.execute(this); result = NumberRule2.execute(this);
} else if (ExpandRule1.compare(this)) { } else if (ExpandRule1.compare(this)) {
result = ExpandRule1.execute(this); result = ExpandRule1.execute(this);
} else if (ExpandRule2.compare(this)) {
result = ExpandRule2.execute(this);
} else if (ExponentRule15.compare(this)) { } else if (ExponentRule15.compare(this)) {
result = ExponentRule15.execute(this); result = ExponentRule15.execute(this);
} else if (ExponentRule16.compare(this)) { } else if (ExponentRule16.compare(this)) {
@ -128,7 +134,10 @@ public class Multiplication extends FunctionOperator {
} else { } else {
result.addAll(sub1); result.addAll(sub1);
} }
if ((nearLeft instanceof BlockChar && nearRight instanceof BlockChar) && !(par2 instanceof Negative)) { if ((nearLeft instanceof BlockChar && nearRight instanceof BlockChar)
&& !(par2 instanceof Negative)
&& !(par1 instanceof Number && par2 instanceof Number)
) {
} else { } else {
result.add(new BlockChar(MathematicalSymbols.MULTIPLICATION)); result.add(new BlockChar(MathematicalSymbols.MULTIPLICATION));

View File

@ -0,0 +1,64 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.Errors;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionOperator;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Subtraction;
import org.warp.picalculator.math.functions.Sum;
import org.warp.picalculator.math.functions.SumSubtraction;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Expand rule<br>
* <b>a(b+c) = ab+ac</b>
*
* @author Andrea Cavalli
*
*/
public class ExpandRule2 {
public static boolean compare(Function f) {
if (f instanceof Multiplication) {
final Multiplication fnc = (Multiplication) f;
if (fnc.getParameter1() instanceof Sum) {
return true;
} else if (fnc.getParameter2() instanceof Sum) {
return true;
} else {
return false;
}
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final MathContext root = f.getMathContext();
final Multiplication fnc = (Multiplication) f;
final Sum sum;
final Function a;
if (fnc.getParameter1() instanceof Sum) {
sum = (Sum) fnc.getParameter1();
a = fnc.getParameter2();
} else if (fnc.getParameter2() instanceof Sum) {
sum = (Sum) fnc.getParameter2();
a = fnc.getParameter1();
} else {
throw new Error(Errors.UNBALANCED_STACK);
}
final Function b = sum.getParameter1();
final Function c = sum.getParameter2();
final Multiplication ab = new Multiplication(root, a, b);
final Multiplication ac = new Multiplication(root, a, c);
result.add(new Sum(root, ab, ac));
return result;
}
}

View File

@ -19,7 +19,6 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class NumberRule7 { public class NumberRule7 {
public static boolean compare(Sum f) { public static boolean compare(Sum f) {
System.out.println(f);
return f.getParameter1().equals(f.getParameter2()); return f.getParameter1().equals(f.getParameter2());
} }

View File

@ -24,7 +24,7 @@ public class VariableRule1 {
if (fnc.getParameter1() instanceof Multiplication & fnc.getParameter2() instanceof Multiplication) { if (fnc.getParameter1() instanceof Multiplication & fnc.getParameter2() instanceof Multiplication) {
final Multiplication m1 = (Multiplication) fnc.getParameter1(); final Multiplication m1 = (Multiplication) fnc.getParameter1();
final Multiplication m2 = (Multiplication) fnc.getParameter2(); final Multiplication m2 = (Multiplication) fnc.getParameter2();
if (m1.getParameter2().equals(m2.getParameter2())) { if (m1.getParameter1().equals(m2.getParameter1()) || m1.getParameter2().equals(m2.getParameter2())) {
return true; return true;
} }
} }
@ -36,9 +36,18 @@ public class VariableRule1 {
final ObjectArrayList<Function> result = new ObjectArrayList<>(); final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Multiplication m1 = (Multiplication) fnc.getParameter1(); final Multiplication m1 = (Multiplication) fnc.getParameter1();
final Multiplication m2 = (Multiplication) fnc.getParameter2(); final Multiplication m2 = (Multiplication) fnc.getParameter2();
final Function a = m1.getParameter1(); final Function a;
final Function b = m2.getParameter1(); final Function b;
final Function x = m1.getParameter2(); final Function x;
if (m1.getParameter2().equals(m2.getParameter2())) {
x = m1.getParameter2();
a = m1.getParameter1();
b = m2.getParameter1();
} else {
x = m1.getParameter1();
a = m1.getParameter2();
b = m2.getParameter2();
}
FunctionOperator rets; FunctionOperator rets;
if (fnc instanceof Sum) { if (fnc instanceof Sum) {
@ -46,8 +55,7 @@ public class VariableRule1 {
} else { } else {
rets = new Subtraction(root, a, b); rets = new Subtraction(root, a, b);
} }
final Expression rete = new Expression(root, rets); final Multiplication retm = new Multiplication(root, rets, x);
final Multiplication retm = new Multiplication(root, rete, x);
result.add(retm); result.add(retm);
return result; return result;
} }

View File

@ -44,8 +44,7 @@ public class VariableRule2 {
} else { } else {
rets = new Subtraction(root, a, new Number(root, 1)); rets = new Subtraction(root, a, new Number(root, 1));
} }
final Expression rete = new Expression(root, rets); final Multiplication retm = new Multiplication(root, rets, x);
final Multiplication retm = new Multiplication(root, rete, x);
result.add(retm); result.add(retm);
return result; return result;
} }

View File

@ -45,8 +45,7 @@ public class VariableRule3 {
rets = new Subtraction(root, new Number(root, 1), a); rets = new Subtraction(root, new Number(root, 1), a);
} }
final Expression rete = new Expression(root, rets); final Multiplication retm = new Multiplication(root, rets, x);
final Multiplication retm = new Multiplication(root, rete, x);
result.add(retm); result.add(retm);
return result; return result;
} }

View File

@ -23,7 +23,6 @@ public class DivisionRule1 {
} }
public static ObjectArrayList<Function> execute(Division f) throws Error, InterruptedException { public static ObjectArrayList<Function> execute(Division f) throws Error, InterruptedException {
System.out.println(f);
final MathContext root = f.getMathContext(); final MathContext root = f.getMathContext();
Function result; Function result;
final ObjectArrayList<Function>[] elements = getDivisionElements(f); final ObjectArrayList<Function>[] elements = getDivisionElements(f);