Added new rules and introduced the variable 'maxFactor'

This commit is contained in:
Andrea Cavalli 2018-02-05 00:05:31 +01:00
parent 81975bcd52
commit 62277c7b4f
35 changed files with 672 additions and 613 deletions

View File

@ -60,6 +60,7 @@ public class Utils {
public static final int scale = 24;
public static final int displayScale = 8;
public static final BigInteger maxFactor = BigInteger.valueOf(1000000L);
public static final int scaleMode = BigDecimal.ROUND_HALF_UP;
public static final RoundingMode scaleMode2 = RoundingMode.HALF_UP;

View File

@ -1,6 +1,7 @@
package org.warp.picalculator.math;
import org.warp.picalculator.Error;
import org.warp.picalculator.Errors;
import org.warp.picalculator.Utils;
import org.warp.picalculator.math.rules.Rule;
@ -119,7 +120,13 @@ public abstract class FunctionOperator implements Function {
ObjectArrayList<Function> simplifiedParam1 = parameter1.simplify(rule);
ObjectArrayList<Function> simplifiedParam2 = parameter2.simplify(rule);
try {
if (simplifiedParam1 == null & simplifiedParam2 == null) return rule.execute(this);
} catch (Exception e) {
Error err = new Error(Errors.ERROR, "Error while executing rule '" + rule.getRuleName() + "'!\n" + e.getMessage());
err.initCause(e);
throw err;
}
if (Thread.interrupted()) throw new InterruptedException();
ObjectArrayList<Function> result = new ObjectArrayList<>();

View File

@ -8,10 +8,6 @@ import org.warp.picalculator.math.Function;
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;
import org.warp.picalculator.math.rules.NumberRule1;
import org.warp.picalculator.math.rules.NumberRule2;

View File

@ -9,8 +9,6 @@ import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionSingle;
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.ExpandRule5;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

View File

@ -217,7 +217,7 @@ public class Number implements Function {
if (n.compareTo(BigInteger.ONE) > 0) {
BigInteger f = BigInteger.valueOf(3);
while (f.multiply(f).compareTo(n) <= 0) {
while (f.compareTo(Utils.maxFactor) <= 0 && f.multiply(f).compareTo(n) <= 0) {
if (n.mod(f).equals(BigInteger.ZERO)) {
fs.add(f);
n = n.divide(f);

View File

@ -7,14 +7,6 @@ import org.warp.picalculator.gui.expression.blocks.BlockPower;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionOperator;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.rules.ExponentRule1;
import org.warp.picalculator.math.rules.ExponentRule2;
import org.warp.picalculator.math.rules.ExponentRule3;
import org.warp.picalculator.math.rules.ExponentRule4;
import org.warp.picalculator.math.rules.ExponentRule9;
import org.warp.picalculator.math.rules.FractionsRule4;
import org.warp.picalculator.math.rules.FractionsRule5;
import org.warp.picalculator.math.rules.UndefinedRule1;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

View File

@ -1,34 +1,33 @@
package org.warp.picalculator.math.functions;
import java.math.BigInteger;
import org.warp.picalculator.Error;
import org.warp.picalculator.gui.expression.blocks.Block;
import org.warp.picalculator.gui.expression.blocks.BlockContainer;
import org.warp.picalculator.gui.expression.blocks.BlockSquareRoot;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionSingle;
import org.warp.picalculator.math.FunctionOperator;
import org.warp.picalculator.math.MathContext;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class RootSquare extends FunctionSingle {
public class RootSquare extends FunctionOperator {
public RootSquare(MathContext root, Function value) {
super(root, value);
public RootSquare(MathContext root, Function value2) {
super(root, new Number(root, 2), value2);
}
@Override
public boolean equals(Object o) {
if (o instanceof RootSquare) {
return ((RootSquare) o).getParameter().equals(parameter);
if (o instanceof Root) {
final FunctionOperator f = (FunctionOperator) o;
return parameter1.equals(f.getParameter1()) && parameter2.equals(f.getParameter2());
}
return false;
}
@Override
public RootSquare clone() {
return new RootSquare(mathContext, parameter);
return new RootSquare(mathContext, parameter2);
}
@Override
@ -36,7 +35,7 @@ public class RootSquare extends FunctionSingle {
ObjectArrayList<Block> result = new ObjectArrayList<>();
BlockSquareRoot bsqr = new BlockSquareRoot();
BlockContainer bsqrc = bsqr.getNumberContainer();
for (Block b : getParameter().toBlock(context)) {
for (Block b : getParameter2().toBlock(context)) {
bsqrc.appendBlockUnsafe(b);
}
bsqrc.recomputeDimensions();
@ -44,4 +43,5 @@ public class RootSquare extends FunctionSingle {
result.add((bsqr));
return result;
}
}

View File

@ -7,14 +7,6 @@ import org.warp.picalculator.math.Function;
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.ExpandRule5;
import org.warp.picalculator.math.rules.NumberRule3;
import org.warp.picalculator.math.rules.NumberRule5;
import org.warp.picalculator.math.rules.VariableRule1;
import org.warp.picalculator.math.rules.VariableRule2;
import org.warp.picalculator.math.rules.VariableRule3;
import org.warp.picalculator.math.rules.methods.SumMethod1;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

View File

@ -8,10 +8,6 @@ import org.warp.picalculator.math.Function;
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.NumberRule3;
import org.warp.picalculator.math.rules.NumberRule4;
import org.warp.picalculator.math.rules.NumberRule5;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

View File

@ -1,114 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
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) = -a-b</b><br>
* <b>-(+a-b) = -a+b</b>
*
* @author Andrea Cavalli
*
*/
public class ExpandRule1 {
public static boolean compare(Function f) {
if (f instanceof Multiplication) {
final Multiplication fnc = (Multiplication) f;
if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) {
final Function expr = fnc.getParameter2();
if (expr instanceof Sum) {
return true;
} else if (expr instanceof Subtraction) {
return true;
} else if (expr instanceof SumSubtraction) {
return true;
}
}
} else if (f instanceof Subtraction || f instanceof SumSubtraction) {
final FunctionOperator fnc = (FunctionOperator) f;
final Function expr = fnc.getParameter2();
if (expr instanceof Sum) {
return true;
} else if (expr instanceof Subtraction) {
return true;
} else if (expr instanceof SumSubtraction) {
return true;
}
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final MathContext root = f.getMathContext();
Function expr = null;
int fromSubtraction = 0;
FunctionOperator subtraction = null;
if (f instanceof Multiplication) {
expr = ((Multiplication) f).getParameter2();
} else if (f instanceof Subtraction || f instanceof SumSubtraction) {
expr = ((FunctionOperator) f).getParameter2();
if (f instanceof Subtraction) {
fromSubtraction = 1;
} else {
fromSubtraction = 2;
}
}
if (f instanceof SumSubtraction) {
}
final Function fnc = expr;
if (fnc instanceof Sum) {
final Function a = ((Sum) fnc).getParameter1();
final Function b = ((Sum) fnc).getParameter2();
final Subtraction fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b);
if (fromSubtraction > 0) {
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).getParameter1();
final Function b = ((Subtraction) fnc).getParameter2();
final Sum fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b);
if (fromSubtraction > 0) {
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).getParameter1();
final Function b = ((SumSubtraction) fnc).getParameter2();
final Sum fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b);
final Subtraction fnc3 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b);
if (fromSubtraction > 0) {
subtraction = new SumSubtraction(root, ((FunctionOperator) f).getParameter1(), fnc2);
result.add(subtraction);
subtraction = new SumSubtraction(root, ((FunctionOperator) f).getParameter1(), fnc3);
result.add(subtraction);
result.add(subtraction);
} else {
result.add(fnc2);
result.add(fnc2);
}
}
return result;
}
}

View File

@ -1,64 +0,0 @@
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

@ -1,50 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Negative;
import org.warp.picalculator.math.functions.Subtraction;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Expand rule<br>
* <b>-(-a) = a</b>
*
* @author Andrea Cavalli
*
*/
public class ExpandRule5 {
public static boolean compare(Function f) {
if (f instanceof Negative) {
final Negative fnc = (Negative) f;
if (fnc.getParameter() instanceof Expression) {
final Expression e = (Expression) fnc.getParameter();
return e.getParameter() instanceof Negative;
}
} else if (f instanceof Subtraction) {
final Subtraction fnc = (Subtraction) f;
if (fnc.getParameter2() instanceof Expression) {
final Expression e = (Expression) fnc.getParameter2();
return e.getParameter() instanceof Negative;
}
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
if (f instanceof Negative) {
final Negative fnc = (Negative) f;
result.add(((Negative) ((Expression) fnc.getParameter()).getParameter()).getParameter());
} else if (f instanceof Subtraction) {
final Subtraction fnc = (Subtraction) f;
result.add(((Negative) ((Expression) fnc.getParameter2()).getParameter()).getParameter());
}
return result;
}
}

View File

@ -1,36 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>1^a=1</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule1 {
public static boolean compare(Function f) {
final Power fnc = (Power) f;
final MathContext root = f.getMathContext();
if (fnc.getParameter1().equals(new Number(root, 1))) {
return true;
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
result.add(new Number(root, 1));
return result;
}
}

View File

@ -1,42 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>a*a=a^2</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule15 {
public static boolean compare(Function f) {
final Multiplication fnc = (Multiplication) f;
if (fnc.getParameter1().equals(fnc.getParameter2())) {
return true;
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Multiplication fnc = (Multiplication) f;
final Function a = fnc.getParameter1();
final Expression expr = new Expression(root, a);
final Number two = new Number(root, 2);
final Power p = new Power(root, expr, two);
result.add(p);
return result;
}
}

View File

@ -1,49 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import org.warp.picalculator.math.functions.Sum;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>(a ^ b) * (a ^ c) = a ^ (b + c)</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule16 {
public static boolean compare(Function f) {
final Multiplication fnc = (Multiplication) f;
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 ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Multiplication fnc = (Multiplication) f;
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, fnc.getParameter2(), 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, fnc.getParameter1(), new Sum(root, new Number(root, 1), new Expression(root, ((Power) fnc.getParameter2()).getParameter2()))));
}
return result;
}
}

View File

@ -1,45 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import org.warp.picalculator.math.functions.Root;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>ax=x^1/a</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule17 {
public static boolean compare(Function f) {
if (f instanceof Root) {
final Root fnc = (Root) f;
if (fnc.getParameter1().equals(fnc.getParameter2())) {
return true;
}
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Multiplication fnc = (Multiplication) f;
final Function a = fnc.getParameter1();
final Expression expr = new Expression(root, a);
final Number two = new Number(root, 2);
final Power p = new Power(fnc.getMathContext(), expr, two);
result.add(p);
return result;
}
}

View File

@ -1,33 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>a^1=a</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule2 {
public static boolean compare(Function f) {
final Power fnc = (Power) f;
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) {
return true;
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
result.add(((Power) f).getParameter1());
return result;
}
}

View File

@ -1,33 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>a^0=1</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule3 {
public static boolean compare(Function f) {
final Power fnc = (Power) f;
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) {
return true;
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
result.add(new Number(f.getMathContext(), 1));
return result;
}
}

View File

@ -1,48 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Power;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>(a*b)^n=a^n*b^n</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule4 {
public static boolean compare(Function f) {
final Power fnc = (Power) f;
if (fnc.getParameter1() instanceof Expression && ((Expression) fnc.getParameter1()).getParameter() instanceof Multiplication && fnc.getParameter2() instanceof Number) {
return true;
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Power fnc = (Power) f;
final Expression expr = (Expression) fnc.getParameter1();
final Multiplication mult = (Multiplication) expr.getParameter();
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;
}
}

View File

@ -1,39 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Power;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule<br>
* <b>(a ^ b) ^ c = a ^ (b * c)</b>
*
* @author Andrea Cavalli
*
*/
public class ExponentRule9 {
public static boolean compare(Function f) {
final Power fnc = (Power) f;
if (fnc.getParameter1() instanceof Power) {
return true;
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Power powC = (Power) f;
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;
}
}

View File

@ -83,13 +83,22 @@ public class RulesManager {
public static void warmUp() {
ObjectArrayList<Function> uselessResult = null;
boolean uselessVariable;
boolean uselessVariable = false;
for (RuleType val : RuleType.values()) {
final ObjectArrayList<Rule> ruleList = rules[val.ordinal()];
for (final Rule rule : ruleList) {
String ruleName = "<null>";
try {
ruleName = rule.getRuleName();
ObjectArrayList<Function> uselessResult2 = rule.execute(generateUselessExpression());
uselessVariable = (uselessResult == null ? new ObjectArrayList<>() : uselessResult).equals(uselessResult2);
uselessResult = uselessResult2;
} catch (Exception e) {
if (uselessVariable || true) {
System.err.println("Exception thrown by rule '" + ruleName + "'!");
e.printStackTrace();
}
}
}
}
try {

View File

@ -15,3 +15,11 @@ functions/Variable
ExpandRule1
ExpandRule2
ExpandRule5
ExponentRule1
ExponentRule2
ExponentRule3
ExponentRule4
ExponentRule9
ExponentRule15
ExponentRule16
ExponentRule17
1 Rule file
15 ExpandRule1
16 ExpandRule2
17 ExpandRule5
18 ExponentRule1
19 ExponentRule2
20 ExponentRule3
21 ExponentRule4
22 ExponentRule9
23 ExponentRule15
24 ExponentRule16
25 ExponentRule17

View File

@ -25,7 +25,7 @@ var rule = {
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
return RuleType.EXPANSION;
},
/* Rule function
Returns:
@ -38,11 +38,11 @@ var rule = {
var fnc = f;
if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) {
var expr = fnc.getParameter2();
if (expr instanceof Sum) {
if (ScriptUtils.instanceOf(expr, Sum.class)) {
isExecutable = true;
} else if (expr instanceof Subtraction) {
} else if (ScriptUtils.instanceOf(expr, Subtraction.class)) {
isExecutable = true;
} else if (expr instanceof SumSubtraction) {
} else if (ScriptUtils.instanceOf(expr, SumSubtraction.class)) {
isExecutable = true;
}
}
@ -66,21 +66,21 @@ var rule = {
var subtraction = null;
if (ScriptUtils.instanceOf(f,Multiplication.class)) {
expr = f.getParameter2();
} else if (f instanceof Subtraction || f instanceof SumSubtraction) {
} else if (ScriptUtils.instanceOf(f, Subtraction.class) || f instanceof SumSubtraction) {
expr = f.getParameter2();
if (f instanceof Subtraction) {
if (ScriptUtils.instanceOf(f, Subtraction.class)) {
fromSubtraction = 1;
} else {
fromSubtraction = 2;
}
}
if (f instanceof SumSubtraction) {
if (ScriptUtils.instanceOf(f, SumSubtraction.class)) {
}
var fnc = expr;
if (fnc instanceof Sum) {
if (ScriptUtils.instanceOf(fnc, Sum.class)) {
var a = fnc.getParameter1();
var b = fnc.getParameter2();
var fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b);
@ -90,7 +90,7 @@ var rule = {
} else {
result.add(fnc2);
}
} else if (fnc instanceof Subtraction) {
} else if (ScriptUtils.instanceOf(fnc, Subtraction.class)) {
var a = fnc.getParameter1();
var b = fnc.getParameter2();
var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b);
@ -100,7 +100,7 @@ var rule = {
} else {
result.add(fnc2);
}
} else if (fnc instanceof SumSubtraction) {
} else if (ScriptUtils.instanceOf(fnc, SumSubtraction.class)) {
var a = fnc.getParameter1();
var b = fnc.getParameter2();
var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b);

View File

@ -24,7 +24,7 @@ var rule = {
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
return RuleType.EXPANSION;
},
/* Rule function
Returns:

View File

@ -0,0 +1,60 @@
// Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var Expression = org.warp.picalculator.math.functions.Expression;
var Negative = org.warp.picalculator.math.functions.Negative;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Expand rule
* -(-a) = a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExpandRule5";
},
// Rule type
getRuleType: function() {
return RuleType.EXPANSION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Negative.class)) {
isExecutable = ScriptUtils.instanceOf(f.getParameter(), Negative.class);
} else if (ScriptUtils.instanceOf(f, Multiplication.class)) {
if (f.getParameter1().equals(new Number(f.getMathContext(), -1)) && ScriptUtils.instanceOf(f.getParameter2(), Multiplication.class)) {
isExecutable = f.getParameter2().getParameter1().equals(f.getParameter1());
}
}
if (isExecutable) {
var result = new ObjectArrayList();
if (ScriptUtils.instanceOf(f, Negative.class)) {
var fnc = f;
result.add(((fnc.getParameter()).getParameter()).getParameter());
} else if (ScriptUtils.instanceOf(f, Multiplication.class)) {
var fnc = f;
result.add(fnc.getParameter2().getParameter2());
}
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,55 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* 1^a=1
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule1";
},
// Rule type
getRuleType: function() {
return RuleType.CALCULATION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
var root = f.getMathContext();
if (ScriptUtils.instanceOf(f, Power.class)) {
if (f.getParameter1().equals(new Number(root, 1))) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
result.add(new Number(root, 1));
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,60 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* a*a=a^2
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule15";
},
// Rule type
getRuleType: function() {
return RuleType.EXPANSION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Multiplication.class)) {
var fnc = f;
if (fnc.getParameter1().equals(fnc.getParameter2())) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var fnc = f;
var a = fnc.getParameter1();
var two = new Number(root, 2);
var p = new Power(root, a, two);
result.add(p);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,68 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var Sum = org.warp.picalculator.math.functions.Sum;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* (a ^ b) * (a ^ c) = a ^ (b + c)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule16";
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Multiplication.class)) {
var fnc = f;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
isExecutable = (fnc.getParameter1()).getParameter1().equals((fnc.getParameter2()).getParameter1());
} else if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) {
isExecutable = (fnc.getParameter1()).getParameter1().equals(fnc.getParameter2());
} else if (ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
isExecutable = (fnc.getParameter2()).getParameter1().equals(fnc.getParameter1());
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var fnc = f;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
result.add(new Power(root, (fnc.getParameter1()).getParameter1(), new Sum(root, fnc.getParameter1().getParameter2(), fnc.getParameter2().getParameter2())));
} else if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) {
result.add(new Power(root, fnc.getParameter2(), new Sum(root, fnc.getParameter1().getParameter2(), new Number(root, 1))));
} else if (ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
result.add(new Power(root, fnc.getParameter1(), new Sum(root, new Number(root, 1), fnc.getParameter2().getParameter2())));
}
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,61 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var Root = org.warp.picalculator.math.functions.Root;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* ax=x^1/a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule17";
},
// Rule type
getRuleType: function() {
return RuleType.EXPANSION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Root.class)) {
var fnc = f;
if (fnc.getParameter1().equals(fnc.getParameter2())) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var fnc = f;
var a = fnc.getParameter1();
var two = new Number(root, 2);
var p = new Power(fnc.getMathContext(), a, two);
result.add(p);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,53 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* a^1=a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule2";
},
// Rule type
getRuleType: function() {
return RuleType.CALCULATION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Power.class)) {
var fnc = f;
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) {
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
result.add((f).getParameter1());
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,53 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* a^0=1
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule3";
},
// Rule type
getRuleType: function() {
return RuleType.CALCULATION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Power.class)) {
var fnc = f;
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) {
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
result.add(new Number(f.getMathContext(), 1));
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,65 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Expression = org.warp.picalculator.math.functions.Expression;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* (a*b)^n=a^n*b^n
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule4";
},
// Rule type
getRuleType: function() {
return RuleType.EXPANSION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Power.class)) {
var fnc = f;
if (fnc.getParameter1() instanceof Multiplication && ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var fnc = f;
var mult = fnc.getParameter1();
var a = mult.getParameter1();
var b = mult.getParameter2();
var n = fnc.getParameter2();
var p1 = new Power(root, a, n);
var p2 = new Power(root, b, n);
var retMult = new Multiplication(root, p1, p2);
result.add(retMult);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,58 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* (a ^ b) ^ c = a ^ (b * c)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule9";
},
// Rule type
getRuleType: function() {
return RuleType.EXPANSION;
},
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
execute: function(f) {
var isExecutable;
if (ScriptUtils.instanceOf(f, Power.class)) {
var fnc = f;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var powC = f;
var powB = powC.getParameter1();
var p = new Power(root, powB.getParameter1(), new Multiplication(root, powB.getParameter2(), powC.getParameter2()));
result.add(p);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -26,3 +26,83 @@ var rule = {
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));
CONVERSIONI:
inserire all'inizio:
//Imports
------------------
^package [^\n]+?;$
------------------
^import ((?:org|com|net)[^\n]+\.([^\n]+?));$
var $2 = $1;
------------------
^import ([^\n]+\.([^\n]+?));$
var $2 = Java.type("$1");
------------------
<br>|<b>|<\/b>
------------------
(static boolean compare\([\s\S]*?)(?:|\r\n)\treturn false;([\t\r\n}]+?public static)
$1$2
VVVV--------------
(static boolean compare\([\s\S]*?)return ([^;]+)([\s\S]+?public static)
$1isExecutable = $2$3
------------------
(?:|public)(?:|private)(?:| final) class ([^\n]+?)[ ]*\{
var rule = {\r\n\t// Rule name\r\n\tgetRuleName: function() {\r\n\t\treturn "$1";\r\n\t},\r\n\t// Rule type\r\n\tgetRuleType: function() {\r\n\t\treturn RuleType./* TODO: RULE TYPE */;\r\n\t},\r\n\t/* Rule function\r\n\t Returns:\r\n\t - null if it's not executable on the function "f"\r\n\t - An ObjectArrayList<Function> if it did something\r\n\t*/
------------------
(?:|\r\n)\tpublic static boolean compare\(Function f\)[ ]*?\{
\texecute: function(f) {\r\n\t\tvar isExecutable;
------------------
(if \(|= |while \(|return |& |\| )([^ \n\r;]+?) instanceof ([^ \n\r;)]+)
$1ScriptUtils.instanceOf($2, $3.class)
------------------
\t}[\r\n]*?\tpublic static ObjectArrayList<Function> execute\(Function f\) throws Error \{
\t\tif (isExecutable) {
------------------
aggiungere tab
------------------
inserire in fondo al codice
} else {
return null;
}
-----------------
\([A-Z][a-z]*?\)( |)
-----------------
([^.])(?:final )
$1
-----------------
[A-Z][A-z<>?]*? ([A-z]+?)(?: |)=(?: |)
var $1 =
-----------------
(new [A-Z][A-z]+?)<[^;()+\-*\/]*?>
$1
-----------------
inserire in fondo:
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));
-----------------

View File

@ -24,7 +24,7 @@ var BigDecimal = java.math.BigDecimal;
var rule = {
// Rule name
getRuleName: function() {
return "Power";
return "Root";
},
// Rule type
getRuleType: function() {
@ -37,11 +37,11 @@ var rule = {
*/
execute: function(f) {
var isSquare = false;
if (ScriptUtils.instanceOf(f, Root.class) || (isSquare = ScriptUtils.instanceOf(f, RootSquare.class))) {
if ((isSquare = ScriptUtils.instanceOf(f, RootSquare.class)) || ScriptUtils.instanceOf(f, Root.class)) {
var result = new ObjectArrayList();
var mathContext = f.getMathContext();
var variable1 = isSquare?new Number(mathContext, 2):f.getParameter1();
var variable2 = isSquare?f.getParameter():f.getParameter2();
var variable1 = f.getParameter1();
var variable2 = f.getParameter2();
var isSolvable = false;
var canBePorted = false;
if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) {
@ -50,7 +50,7 @@ var rule = {
try {
var resultVar = variable2.pow(new Number(mathContext, BigDecimal.ONE).divide(variable1));
var originalVariable = resultVar.pow(new Number(mathContext, 2));
if (originalVariable.equals(parameter2)) {
if (originalVariable.equals(f.getParameter2())) {
isSolvable = true;
}
} catch (ex) {