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"/>
</attributes>
</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>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>

Binary file not shown.

View File

@ -85,11 +85,13 @@ public final class DisplayManager implements RenderingLoop {
private GraphicEngine chooseGraphicEngine() {
GraphicEngine 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()) {
Utils.out.println(1, "Using GPU Graphic Engine");

View File

@ -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);

View File

@ -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<Function> resultExpressions = new ObjectArrayList<>();
resultExpressions.add(expr.getParameter());
while (!done && stop < 3000) {
while (!done && stop < (step?currentStep:3000)) {
if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> 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;

View File

@ -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));

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 static boolean compare(Sum f) {
System.out.println(f);
return f.getParameter1().equals(f.getParameter2());
}

View File

@ -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<Function> 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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

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