diff --git a/.classpath b/.classpath index f227ca66..e2cbd638 100644 --- a/.classpath +++ b/.classpath @@ -6,7 +6,7 @@ - + diff --git a/Algebra Cheat Sheet.rtf b/Algebra Cheat Sheet.rtf index 2ce8ebc7..c8989cd1 100644 Binary files a/Algebra Cheat Sheet.rtf and b/Algebra Cheat Sheet.rtf differ diff --git a/src/main/java/org/warp/picalculator/gui/DisplayManager.java b/src/main/java/org/warp/picalculator/gui/DisplayManager.java index d82f1b41..53ced776 100755 --- a/src/main/java/org/warp/picalculator/gui/DisplayManager.java +++ b/src/main/java/org/warp/picalculator/gui/DisplayManager.java @@ -85,10 +85,12 @@ public final class DisplayManager implements RenderingLoop { private GraphicEngine chooseGraphicEngine() { GraphicEngine d; - d = new FBEngine(); - if (d.isSupported()) { - Utils.out.println(1, "Using FB Graphic Engine"); - return d; + if (!StaticVars.debugOn) { + d = new FBEngine(); + if (d.isSupported()) { + Utils.out.println(1, "Using FB Graphic Engine"); + return d; + } } d = new GPUEngine(); if (d.isSupported()) { diff --git a/src/main/java/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java b/src/main/java/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java index 608b3b06..c03ccd42 100755 --- a/src/main/java/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java +++ b/src/main/java/org/warp/picalculator/gui/graphicengine/cpu/SwingWindow.java @@ -41,7 +41,7 @@ public class SwingWindow extends JFrame { final BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); if (StaticVars.debugOn) { - if (StaticVars.debugWindow2x) mult = 3; + if (StaticVars.debugWindow2x) mult = 2; if (Utils.debugThirdScreen) { this.setLocation(2880, 900); setResizable(false); diff --git a/src/main/java/org/warp/picalculator/gui/screens/MathInputScreen.java b/src/main/java/org/warp/picalculator/gui/screens/MathInputScreen.java index bc7833eb..95ea8ea4 100755 --- a/src/main/java/org/warp/picalculator/gui/screens/MathInputScreen.java +++ b/src/main/java/org/warp/picalculator/gui/screens/MathInputScreen.java @@ -52,6 +52,7 @@ public class MathInputScreen extends Screen { private double computingElapsedTime = 0; private boolean computingBreakTipVisible = false; boolean mustRefresh = true; + private int currentStep = 0; public MathInputScreen() { super(); @@ -176,47 +177,22 @@ public class MathInputScreen extends Screen { case HISTORY_BACK: if (userInput.isExtraOpened()) { userInput.closeExtra(); + currentStep = 0; mustRefresh = true; return true; } default: if (userInput.isExtraOpened() && userInput.getExtraKeyboardEventListener().keyPressed(k)) { + currentStep = 0; return true; } else { + final boolean step = k == Key.STEP; switch (k) { case STEP: - // if (newExpression.length() > 0) { - // 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; + currentStep++; case SIMPLIFY: + if (!step) currentStep = 0; if (DisplayManager.INSTANCE.error != null) { //TODO: make the error management a global API rather than being relegated to this screen. Utils.out.println(1, "Resetting after error..."); @@ -241,11 +217,12 @@ public class MathInputScreen extends Screen { calc.f2.clear(); } calc.f.add(expr); + Utils.out.println(2, "INPUT: " + expr); int stop = 0; boolean done = false; ObjectArrayList resultExpressions = new ObjectArrayList<>(); resultExpressions.add(expr.getParameter()); - while (!done && stop < 3000) { + while (!done && stop < (step?currentStep:3000)) { if (Thread.interrupted()) throw new InterruptedException(); ObjectArrayList newResultExpressions = new ObjectArrayList<>(); done = true; @@ -272,10 +249,14 @@ public class MathInputScreen extends Screen { newResultExpressions.add(newResult); } } + if (StaticVars.debugOn) { + Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "STEP: "+newResultExpressions); } resultExpressions = newResultExpressions; 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) { Utils.out.println(1, "RESULT: " + rr.toString()); } @@ -416,6 +397,7 @@ public class MathInputScreen extends Screen { return true; case DELETE: userInput.del(); + currentStep = 0; mustRefresh = true; return true; case LEFT: @@ -429,6 +411,7 @@ public class MathInputScreen extends Screen { case RESET: userInput.clear(); result.clear(); + currentStep = 0; if (DisplayManager.INSTANCE.error != null) { Utils.out.println(1, "Resetting after error..."); DisplayManager.INSTANCE.error = null; @@ -437,6 +420,7 @@ public class MathInputScreen extends Screen { case SURD_MODE: calc.exactMode = !calc.exactMode; result.clear(); + currentStep = 0; Keyboard.keyPressed(Key.SIMPLIFY); return true; case debug1: @@ -465,18 +449,21 @@ public class MathInputScreen extends Screen { case debug_DEG: if (calc.angleMode.equals(AngleMode.DEG) == false) { calc.angleMode = AngleMode.DEG; + currentStep = 0; return true; } return false; case debug_RAD: if (calc.angleMode.equals(AngleMode.RAD) == false) { calc.angleMode = AngleMode.RAD; + currentStep = 0; return true; } return false; case debug_GRA: if (calc.angleMode.equals(AngleMode.GRA) == false) { calc.angleMode = AngleMode.GRA; + currentStep = 0; return true; } return false; @@ -488,6 +475,7 @@ public class MathInputScreen extends Screen { } else { calc.angleMode = AngleMode.DEG; } + currentStep = 0; return true; default: return false; diff --git a/src/main/java/org/warp/picalculator/math/functions/Multiplication.java b/src/main/java/org/warp/picalculator/math/functions/Multiplication.java index 91cbd428..b68e1d5f 100755 --- a/src/main/java/org/warp/picalculator/math/functions/Multiplication.java +++ b/src/main/java/org/warp/picalculator/math/functions/Multiplication.java @@ -9,6 +9,7 @@ import org.warp.picalculator.math.FunctionOperator; import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.MathematicalSymbols; 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.ExponentRule16; import org.warp.picalculator.math.rules.FractionsRule14; @@ -44,6 +45,9 @@ public class Multiplication extends FunctionOperator { if (ExpandRule1.compare(this)) { return true; } + if (ExpandRule2.compare(this)) { + return true; + } if (ExponentRule15.compare(this)) { return true; } @@ -68,6 +72,8 @@ public class Multiplication extends FunctionOperator { result = NumberRule2.execute(this); } else if (ExpandRule1.compare(this)) { result = ExpandRule1.execute(this); + } else if (ExpandRule2.compare(this)) { + result = ExpandRule2.execute(this); } else if (ExponentRule15.compare(this)) { result = ExponentRule15.execute(this); } else if (ExponentRule16.compare(this)) { @@ -128,7 +134,10 @@ public class Multiplication extends FunctionOperator { } else { 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 { result.add(new BlockChar(MathematicalSymbols.MULTIPLICATION)); diff --git a/src/main/java/org/warp/picalculator/math/rules/ExpandRule2.java b/src/main/java/org/warp/picalculator/math/rules/ExpandRule2.java new file mode 100644 index 00000000..336268e2 --- /dev/null +++ b/src/main/java/org/warp/picalculator/math/rules/ExpandRule2.java @@ -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
+ * a(b+c) = ab+ac + * + * @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 execute(Function f) throws Error { + final ObjectArrayList 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; + } + +} diff --git a/src/main/java/org/warp/picalculator/math/rules/NumberRule7.java b/src/main/java/org/warp/picalculator/math/rules/NumberRule7.java index ba1a4830..3fc042a5 100755 --- a/src/main/java/org/warp/picalculator/math/rules/NumberRule7.java +++ b/src/main/java/org/warp/picalculator/math/rules/NumberRule7.java @@ -19,7 +19,6 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; public class NumberRule7 { public static boolean compare(Sum f) { - System.out.println(f); return f.getParameter1().equals(f.getParameter2()); } diff --git a/src/main/java/org/warp/picalculator/math/rules/VariableRule1.java b/src/main/java/org/warp/picalculator/math/rules/VariableRule1.java index f640e695..97e13fdf 100755 --- a/src/main/java/org/warp/picalculator/math/rules/VariableRule1.java +++ b/src/main/java/org/warp/picalculator/math/rules/VariableRule1.java @@ -24,7 +24,7 @@ public class VariableRule1 { 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())) { + if (m1.getParameter1().equals(m2.getParameter1()) || m1.getParameter2().equals(m2.getParameter2())) { return true; } } @@ -36,9 +36,18 @@ public class VariableRule1 { final ObjectArrayList result = new ObjectArrayList<>(); 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 Function a; + final Function b; + 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; if (fnc instanceof Sum) { @@ -46,8 +55,7 @@ public class VariableRule1 { } else { rets = new Subtraction(root, a, b); } - final Expression rete = new Expression(root, rets); - final Multiplication retm = new Multiplication(root, rete, x); + final Multiplication retm = new Multiplication(root, rets, x); result.add(retm); return result; } diff --git a/src/main/java/org/warp/picalculator/math/rules/VariableRule2.java b/src/main/java/org/warp/picalculator/math/rules/VariableRule2.java index 19ece286..5409dbb6 100755 --- a/src/main/java/org/warp/picalculator/math/rules/VariableRule2.java +++ b/src/main/java/org/warp/picalculator/math/rules/VariableRule2.java @@ -44,8 +44,7 @@ public class VariableRule2 { } else { rets = new Subtraction(root, a, new Number(root, 1)); } - final Expression rete = new Expression(root, rets); - final Multiplication retm = new Multiplication(root, rete, x); + final Multiplication retm = new Multiplication(root, rets, x); result.add(retm); return result; } diff --git a/src/main/java/org/warp/picalculator/math/rules/VariableRule3.java b/src/main/java/org/warp/picalculator/math/rules/VariableRule3.java index fdd305f0..6d378c3c 100755 --- a/src/main/java/org/warp/picalculator/math/rules/VariableRule3.java +++ b/src/main/java/org/warp/picalculator/math/rules/VariableRule3.java @@ -45,8 +45,7 @@ public class VariableRule3 { rets = new Subtraction(root, new Number(root, 1), a); } - final Expression rete = new Expression(root, rets); - final Multiplication retm = new Multiplication(root, rete, x); + final Multiplication retm = new Multiplication(root, rets, x); result.add(retm); return result; } diff --git a/src/main/java/org/warp/picalculator/math/rules/methods/DivisionRule1.java b/src/main/java/org/warp/picalculator/math/rules/methods/DivisionRule1.java index 2e1f106c..498ce555 100755 --- a/src/main/java/org/warp/picalculator/math/rules/methods/DivisionRule1.java +++ b/src/main/java/org/warp/picalculator/math/rules/methods/DivisionRule1.java @@ -23,7 +23,6 @@ public class DivisionRule1 { } public static ObjectArrayList execute(Division f) throws Error, InterruptedException { - System.out.println(f); final MathContext root = f.getMathContext(); Function result; final ObjectArrayList[] elements = getDivisionElements(f);