From 01925526a11df63e280e52e1169dfee02b77c836 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sat, 24 Mar 2018 23:33:04 +0100 Subject: [PATCH] Added all the rules, converted automatically, to fix. --- .../picalculator/math/rules/RulesManager.java | 7 +- src/main/resources/rules.csv | 41 +++++ src/main/resources/rules/ExpandRule2.java | 139 +++++++++++++++++ src/main/resources/rules/ExpandRule5.java | 72 +++++++++ src/main/resources/rules/ExponentRule1.java | 67 +++++++++ src/main/resources/rules/ExponentRule15.java | 72 +++++++++ src/main/resources/rules/ExponentRule16.java | 80 ++++++++++ src/main/resources/rules/ExponentRule17.java | 73 +++++++++ src/main/resources/rules/ExponentRule2.java | 65 ++++++++ src/main/resources/rules/ExponentRule3.java | 65 ++++++++ src/main/resources/rules/ExponentRule4.java | 77 ++++++++++ src/main/resources/rules/ExponentRule9.java | 70 +++++++++ src/main/resources/rules/FractionsRule1.java | 141 ++++++++++++++++++ src/main/resources/rules/FractionsRule3.java | 65 ++++++++ src/main/resources/rules/FractionsRule4.java | 73 +++++++++ src/main/resources/rules/FractionsRule5.java | 88 +++++++++++ src/main/resources/rules/NumberRule1.java | 76 ++++++++++ src/main/resources/rules/NumberRule2.java | 95 ++++++++++++ src/main/resources/rules/NumberRule3.java | 92 ++++++++++++ src/main/resources/rules/NumberRule4.java | 67 +++++++++ src/main/resources/rules/NumberRule5.java | 83 +++++++++++ src/main/resources/rules/NumberRule7.java | 66 ++++++++ src/main/resources/rules/UndefinedRule1.java | 69 +++++++++ src/main/resources/rules/UndefinedRule2.java | 72 +++++++++ src/main/resources/rules/VariableRule1.java | 95 ++++++++++++ src/main/resources/rules/VariableRule2.java | 84 +++++++++++ src/main/resources/rules/VariableRule3.java | 85 +++++++++++ .../resources/rules/functions/Division.java | 88 +++++++++++ .../rules/functions/EmptyNumber.java | 47 ++++++ .../resources/rules/functions/Expression.java | 58 +++++++ src/main/resources/rules/functions/Joke.java | 47 ++++++ .../rules/functions/Multiplication.java | 64 ++++++++ .../resources/rules/functions/Negative.java | 74 +++++++++ .../resources/rules/functions/Number.java | 49 ++++++ src/main/resources/rules/functions/Power.java | 53 +++++++ src/main/resources/rules/functions/Root.java | 90 +++++++++++ .../rules/functions/Subtraction.java | 64 ++++++++ src/main/resources/rules/functions/Sum.java | 64 ++++++++ .../rules/functions/SumSubtraction.java | 65 ++++++++ .../resources/rules/functions/Variable.java | 67 +++++++++ 40 files changed, 2907 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/rules/ExpandRule2.java create mode 100644 src/main/resources/rules/ExpandRule5.java create mode 100644 src/main/resources/rules/ExponentRule1.java create mode 100644 src/main/resources/rules/ExponentRule15.java create mode 100644 src/main/resources/rules/ExponentRule16.java create mode 100644 src/main/resources/rules/ExponentRule17.java create mode 100644 src/main/resources/rules/ExponentRule2.java create mode 100644 src/main/resources/rules/ExponentRule3.java create mode 100644 src/main/resources/rules/ExponentRule4.java create mode 100644 src/main/resources/rules/ExponentRule9.java create mode 100644 src/main/resources/rules/FractionsRule1.java create mode 100644 src/main/resources/rules/FractionsRule3.java create mode 100644 src/main/resources/rules/FractionsRule4.java create mode 100644 src/main/resources/rules/FractionsRule5.java create mode 100644 src/main/resources/rules/NumberRule1.java create mode 100644 src/main/resources/rules/NumberRule2.java create mode 100644 src/main/resources/rules/NumberRule3.java create mode 100644 src/main/resources/rules/NumberRule4.java create mode 100644 src/main/resources/rules/NumberRule5.java create mode 100644 src/main/resources/rules/NumberRule7.java create mode 100644 src/main/resources/rules/UndefinedRule1.java create mode 100644 src/main/resources/rules/UndefinedRule2.java create mode 100644 src/main/resources/rules/VariableRule1.java create mode 100644 src/main/resources/rules/VariableRule2.java create mode 100644 src/main/resources/rules/VariableRule3.java create mode 100644 src/main/resources/rules/functions/Division.java create mode 100644 src/main/resources/rules/functions/EmptyNumber.java create mode 100644 src/main/resources/rules/functions/Expression.java create mode 100644 src/main/resources/rules/functions/Joke.java create mode 100644 src/main/resources/rules/functions/Multiplication.java create mode 100644 src/main/resources/rules/functions/Negative.java create mode 100644 src/main/resources/rules/functions/Number.java create mode 100644 src/main/resources/rules/functions/Power.java create mode 100644 src/main/resources/rules/functions/Root.java create mode 100644 src/main/resources/rules/functions/Subtraction.java create mode 100644 src/main/resources/rules/functions/Sum.java create mode 100644 src/main/resources/rules/functions/SumSubtraction.java create mode 100644 src/main/resources/rules/functions/Variable.java diff --git a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java index dededefd..dcb5a58e 100644 --- a/src/main/java/org/warp/picalculator/math/rules/RulesManager.java +++ b/src/main/java/org/warp/picalculator/math/rules/RulesManager.java @@ -120,12 +120,14 @@ public class RulesManager { Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "RulesManager", ruleName, "This rule is not cached. Compiling"); try { r = compileJavaRule(scriptFile, tDir); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | IOException e) { e.printStackTrace(); } } - RulesManager.addRule(r); + if (r != null) { + RulesManager.addRule(r); + } } } } @@ -142,6 +144,7 @@ public class RulesManager { InputStream resource = Utils.getResourceStream(scriptFile); String text = Utils.read(resource); String[] textArray = text.split("\\n", 5); + System.err.println(text); String javaClassName = textArray[2].substring(6); String javaClassNameAndPath = new StringBuilder("org.warp.picalculator.math.rules.").append(javaClassName).toString(); final int extIndex = javaClassNameAndPath.lastIndexOf('.'); diff --git a/src/main/resources/rules.csv b/src/main/resources/rules.csv index 694a46ae..b87c3d4f 100644 --- a/src/main/resources/rules.csv +++ b/src/main/resources/rules.csv @@ -1,3 +1,44 @@ Rule file +functions/Division +functions/EmptyNumber +functions/Expression +functions/Joke +functions/Multiplication +functions/Negative +functions/Number +functions/Power +functions/Root +functions/Subtraction +functions/Sum +functions/SumSubtraction +functions/Variable ExpandRule1 +ExpandRule2 +ExpandRule5 +ExponentRule1 +ExponentRule2 +ExponentRule3 +ExponentRule4 +ExponentRule9 +ExponentRule15 +ExponentRule16 +ExponentRule17 +FractionsRule1 FractionsRule2 +FractionsRule3 +FractionsRule4 +FractionsRule5 +FractionsRule11 +FractionsRule12 +FractionsRule14 +NumberRule1 +NumberRule2 +NumberRule3 +NumberRule4 +NumberRule5 +NumberRule7 +UndefinedRule1 +UndefinedRule2 +VariableRule1 +VariableRule2 +VariableRule3 \ No newline at end of file diff --git a/src/main/resources/rules/ExpandRule2.java b/src/main/resources/rules/ExpandRule2.java new file mode 100644 index 00000000..f7cece30 --- /dev/null +++ b/src/main/resources/rules/ExpandRule2.java @@ -0,0 +1,139 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; + +/** + * Expand rule + * -(-a) = a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExpandRule2"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + Function fnc = f; + if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) { + var expr = fnc.getParameter2(); + if (expr instanceof Sum) { + isExecutable = true; + } else if (expr instanceof Subtraction) { + isExecutable = true; + } else if (expr instanceof SumSubtraction) { + isExecutable = true; + } + } + } else if (f instanceof Subtraction || f instanceof SumSubtraction) { + Function fnc = f; + var expr = fnc.getParameter2(); + if (expr instanceof Sum) { + isExecutable = true; + } else if (expr instanceof Subtraction) { + isExecutable = true; + } else if (expr instanceof SumSubtraction) { + isExecutable = true; + } + } + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + var root = f.getMathContext(); + + var expr = null; + var fromSubtraction = 0; + var subtraction = null; + if (f instanceof Multiplication) { + expr = f.getParameter2(); + } else if (f instanceof Subtraction || f instanceof SumSubtraction) { + expr = f.getParameter2(); + if (f instanceof Subtraction) { + fromSubtraction = 1; + } else { + fromSubtraction = 2; + } + } + + if (f instanceof SumSubtraction) { + + } + + Function fnc = expr; + if (fnc instanceof Sum) { + var a = fnc.getParameter1(); + var b = fnc.getParameter2(); + var fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); + if (fromSubtraction > 0) { + subtraction = new Subtraction(root, f.getParameter1(), fnc2); + result.add(subtraction); + } else { + result.add(fnc2); + } + } else if (fnc instanceof Subtraction) { + var a = fnc.getParameter1(); + var b = fnc.getParameter2(); + var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); + if (fromSubtraction > 0) { + subtraction = new Subtraction(root, f.getParameter1(), fnc2); + result.add(subtraction); + } else { + result.add(fnc2); + } + } else if (fnc instanceof SumSubtraction) { + var a = fnc.getParameter1(); + var b = fnc.getParameter2(); + var fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); + var fnc3 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); + if (fromSubtraction > 0) { + subtraction = new SumSubtraction(root, f.getParameter1(), fnc2); + result.add(subtraction); + subtraction = new SumSubtraction(root, f.getParameter1(), fnc3); + result.add(subtraction); + result.add(subtraction); + } else { + result.add(fnc2); + result.add(fnc2); + } + } + return result; + } else { + return null; + } + + } +} diff --git a/src/main/resources/rules/ExpandRule5.java b/src/main/resources/rules/ExpandRule5.java new file mode 100644 index 00000000..fde3f8fd --- /dev/null +++ b/src/main/resources/rules/ExpandRule5.java @@ -0,0 +1,72 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +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 + * -(-a) = a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExpandRule5"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Negative) { + isExecutable = f.getParameter() instanceof Negative; + } else if (f instanceof Multiplication) { + if (f.getParameter1().equals(new Number(f.getMathContext(), -1)) && f.getParameter2() instanceof Multiplication) { + isExecutable = f.getParameter2().getParameter1().equals(f.getParameter1()); + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + + if (f instanceof Negative) { + Function fnc = f; + result.add(((fnc.getParameter()).getParameter()).getParameter()); + } else if (f instanceof Multiplication) { + Function fnc = f; + result.add(fnc.getParameter2().getParameter2()); + } + return result; + } else { + return null; + } + } + +} diff --git a/src/main/resources/rules/ExponentRule1.java b/src/main/resources/rules/ExponentRule1.java new file mode 100644 index 00000000..a04a434d --- /dev/null +++ b/src/main/resources/rules/ExponentRule1.java @@ -0,0 +1,67 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 + * 1^a=1 + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule1"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + var root = f.getMathContext(); + if (f instanceof Power) { + if (f.getParameter1().equals(new Number(root, 1))) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(root, 1)); + return result; + } else { + return null; + } + } + +} diff --git a/src/main/resources/rules/ExponentRule15.java b/src/main/resources/rules/ExponentRule15.java new file mode 100644 index 00000000..358be0d6 --- /dev/null +++ b/src/main/resources/rules/ExponentRule15.java @@ -0,0 +1,72 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +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.Power; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Exponent rule + * a*a=a^2 + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule15"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + Function fnc = f; + if (fnc.getParameter1().equals(fnc.getParameter2())) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + Function 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; + } + } +} diff --git a/src/main/resources/rules/ExponentRule16.java b/src/main/resources/rules/ExponentRule16.java new file mode 100644 index 00000000..af12fc6d --- /dev/null +++ b/src/main/resources/rules/ExponentRule16.java @@ -0,0 +1,80 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +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.Power; +import org.warp.picalculator.math.functions.Sum; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Exponent rule + * (a ^ b) * (a ^ c) = a ^ (b + c) + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule16"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.REDUCTION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + Function fnc = f; + if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) { + isExecutable = (fnc.getParameter1()).getParameter1().equals((fnc.getParameter2()).getParameter1()); + } else if (fnc.getParameter1() instanceof Power) { + isExecutable = (fnc.getParameter1()).getParameter1().equals(fnc.getParameter2()); + } else if (fnc.getParameter2() instanceof Power) { + isExecutable = (fnc.getParameter2()).getParameter1().equals(fnc.getParameter1()); + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + Function fnc = f; + if (fnc.getParameter1() instanceof Power && fnc.getParameter2() instanceof Power) { + result.add(new Power(root, (fnc.getParameter1()).getParameter1(), new Sum(root, fnc.getParameter1().getParameter2(), fnc.getParameter2().getParameter2()))); + } else if (fnc.getParameter1() instanceof Power) { + result.add(new Power(root, fnc.getParameter2(), new Sum(root, 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), fnc.getParameter2().getParameter2()))); + } + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/ExponentRule17.java b/src/main/resources/rules/ExponentRule17.java new file mode 100644 index 00000000..0f74387a --- /dev/null +++ b/src/main/resources/rules/ExponentRule17.java @@ -0,0 +1,73 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +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.Power; +import org.warp.picalculator.math.functions.Root; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Exponent rule + * a√x=x^1/a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule17"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Root) { + Function fnc = f; + if (fnc.getParameter1().equals(fnc.getParameter2())) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + Function 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; + } + } +} diff --git a/src/main/resources/rules/ExponentRule2.java b/src/main/resources/rules/ExponentRule2.java new file mode 100644 index 00000000..319ba4e2 --- /dev/null +++ b/src/main/resources/rules/ExponentRule2.java @@ -0,0 +1,65 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 + * a^1=a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule2"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + Function fnc = f; + if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) { + isExecutable = true; + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add((f).getParameter1()); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/ExponentRule3.java b/src/main/resources/rules/ExponentRule3.java new file mode 100644 index 00000000..32182019 --- /dev/null +++ b/src/main/resources/rules/ExponentRule3.java @@ -0,0 +1,65 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 + * a^0=1 + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule3"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + Function fnc = f; + if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) { + isExecutable = true; + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(f.getMathContext(), 1)); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/ExponentRule4.java b/src/main/resources/rules/ExponentRule4.java new file mode 100644 index 00000000..528756fd --- /dev/null +++ b/src/main/resources/rules/ExponentRule4.java @@ -0,0 +1,77 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 + * (a*b)^n=a^n*b^n + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule4"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + Function fnc = f; + if (fnc.getParameter1() instanceof Multiplication && fnc.getParameter2() instanceof Number) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + Function 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; + } + } +} diff --git a/src/main/resources/rules/ExponentRule9.java b/src/main/resources/rules/ExponentRule9.java new file mode 100644 index 00000000..a70167b5 --- /dev/null +++ b/src/main/resources/rules/ExponentRule9.java @@ -0,0 +1,70 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Power; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Exponent rule + * (a ^ b) ^ c = a ^ (b * c) + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "ExponentRule9"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + Function fnc = f; + if (fnc.getParameter1() instanceof Power) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList 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; + } + } +} diff --git a/src/main/resources/rules/FractionsRule1.java b/src/main/resources/rules/FractionsRule1.java new file mode 100644 index 00000000..2a647787 --- /dev/null +++ b/src/main/resources/rules/FractionsRule1.java @@ -0,0 +1,141 @@ +/* +SETTINGS: (please don't move this part) + PATH=FractionsRule2 +*/ + +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * a / 1 = a/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * 0 / a = 0 + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule1"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + var root = f.getMathContext(); + if (f instanceof Division) { + Function fnc = f; + if (fnc.getParameter1() instanceof Number) { + var numb1 = fnc.getParameter1(); + if (numb1.equals(new Number(root, 0))) { + if (fnc.getParameter2() instanceof Number) { + var numb2 = fnc.getParameter2(); + if (numb2.equals(new Number(root, 0)) == false) { + isExecutable = true; + } + } else { + isExecutable = true; + } + } + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(f.getMathContext(), 0)); + return result; + } else { + return null; + } + } +} + + * + * @author Andrea Cavalli + * + */ +public class FractionsRule2 implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule2"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Division) { + Division fnc = (Division) f; + if (fnc.getParameter2() instanceof Number) { + Number numb = (Number) fnc.getParameter2(); + if (numb.equals(new Number(f.getMathContext(), 1))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + Division fnc = (Division) f; + result.add(fnc.getParameter1()); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/FractionsRule3.java b/src/main/resources/rules/FractionsRule3.java new file mode 100644 index 00000000..320231e8 --- /dev/null +++ b/src/main/resources/rules/FractionsRule3.java @@ -0,0 +1,65 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * a / a = 1 + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule3"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Division) { + Function fnc = f; + if (fnc.getParameter1().equals(fnc.getParameter2())) { + isExecutable = true; + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(f.getMathContext(), 1)); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/FractionsRule4.java b/src/main/resources/rules/FractionsRule4.java new file mode 100644 index 00000000..7a567a35 --- /dev/null +++ b/src/main/resources/rules/FractionsRule4.java @@ -0,0 +1,73 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Power; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * (a / b) ^ -1 = b / a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule4"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + Function fnc = f; + if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) { + var n2 = fnc.getParameter2(); + if (n2.equals(new Number(f.getMathContext(), -1))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + Function fnc = f; + var a = (fnc.getParameter1()).getParameter1(); + var b = (fnc.getParameter1()).getParameter2(); + var res = new Division(f.getMathContext(), b, a); + result.add(res); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/FractionsRule5.java b/src/main/resources/rules/FractionsRule5.java new file mode 100644 index 00000000..a10e648f --- /dev/null +++ b/src/main/resources/rules/FractionsRule5.java @@ -0,0 +1,88 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import java.math.BigDecimal; + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Power; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Fractions rule + * (a / b) ^ -c = (b / a) ^ c + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "FractionsRule5"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + Function fnc = f; + if (fnc.getParameter1() instanceof Division) { + if (fnc.getParameter2() instanceof Multiplication && fnc.getParameter2().getParameter1().equals(new Number(f.getMathContext(), -1))) { + isExecutable = true; + } else if (fnc.getParameter2() instanceof Number) { + var n2 = fnc.getParameter2(); + if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) { + isExecutable = true; + } + } + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + Function fnc = f; + var a = (fnc.getParameter1()).getParameter1(); + var b = (fnc.getParameter1()).getParameter2(); + var c; + if (fnc.getParameter2() instanceof Multiplication) { + c = fnc.getParameter2().getParameter2(); + } else { + c = fnc.getParameter2().multiply(new Number(root, "-1")); + } + var dv = new Division(root, b, a); + var pow = new Power(root, dv, c); + result.add(pow); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/NumberRule1.java b/src/main/resources/rules/NumberRule1.java new file mode 100644 index 00000000..7401fc89 --- /dev/null +++ b/src/main/resources/rules/NumberRule1.java @@ -0,0 +1,76 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Number rule + * a * 0 = 0 + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "NumberRule1"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + var root = f.getMathContext(); + var mult = f; + if (mult.getParameter1() instanceof Number) { + var numb = mult.getParameter1(); + if (numb.equals(new Number(root, 0))) { + isExecutable = true; + } + } + if (mult.getParameter2() instanceof Number) { + var numb = mult.getParameter2(); + if (numb.equals(new Number(root, 0))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Number(f.getMathContext(), "0")); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/NumberRule2.java b/src/main/resources/rules/NumberRule2.java new file mode 100644 index 00000000..e41ca65a --- /dev/null +++ b/src/main/resources/rules/NumberRule2.java @@ -0,0 +1,95 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Number; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Number rule + * a * 1 = a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "NumberRule2"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Multiplication) { + var root = f.getMathContext(); + var mult = f; + if (mult.getParameter1() instanceof Number) { + var numb = mult.getParameter1(); + if (numb.equals(new Number(root, 1))) { + isExecutable = true; + } + } + if (mult.getParameter2() instanceof Number) { + var numb = mult.getParameter2(); + if (numb.equals(new Number(root, 1))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + var a = null; + var aFound = false; + var mult = f; + if (aFound == false & mult.getParameter1() instanceof Number) { + var numb = mult.getParameter1(); + if (numb.equals(new Number(root, 1))) { + a = mult.getParameter2(); + aFound = true; + } + } + if (aFound == false && mult.getParameter2() instanceof Number) { + var numb = mult.getParameter2(); + if (numb.equals(new Number(root, 1))) { + a = mult.getParameter1(); + aFound = true; + } + } + + result.add(a); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/NumberRule3.java b/src/main/resources/rules/NumberRule3.java new file mode 100644 index 00000000..0bf05e78 --- /dev/null +++ b/src/main/resources/rules/NumberRule3.java @@ -0,0 +1,92 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Negative; +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; + +/** + * Number rule + * a - a = 0 + * -a + a = 0 + * a ± a = {0, 2a} + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "NumberRule3"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Subtraction) { + var sub = f; + if (sub.getParameter1().equals(sub.getParameter2())) { + isExecutable = true; + } + } else if (f instanceof Sum) { + var sub = f; + if (sub.getParameter1() instanceof Multiplication) { + if (sub.getParameter1().getParameter1() instanceof Number && sub.getParameter1().getParameter1().equals(new Number(f.getMathContext(), -1))) { + var neg = sub.getParameter1().getParameter2(); + if (neg.equals(sub.getParameter2())) { + isExecutable = true; + } + } + } + } else if (f instanceof SumSubtraction) { + var sub = f; + if (sub.getParameter1().equals(sub.getParameter2())) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + if (f instanceof SumSubtraction) { + var mul = new Multiplication(root, new Number(root, 2), f.getParameter1()); + result.add(mul); + } + result.add(new Number(root, 0)); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/NumberRule4.java b/src/main/resources/rules/NumberRule4.java new file mode 100644 index 00000000..cad25021 --- /dev/null +++ b/src/main/resources/rules/NumberRule4.java @@ -0,0 +1,67 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +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; + +/** + * Number rule + * a ± b = {a+b, a-b} + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "NumberRule4"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof SumSubtraction) { + isExecutable = true; + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + var ss = f; + result.add(new Sum(root, ss.getParameter1(), ss.getParameter2())); + result.add(new Subtraction(root, ss.getParameter1(), ss.getParameter2())); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/NumberRule5.java b/src/main/resources/rules/NumberRule5.java new file mode 100644 index 00000000..eec39bd0 --- /dev/null +++ b/src/main/resources/rules/NumberRule5.java @@ -0,0 +1,83 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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.Number; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Number rule + * a + 0 = a + * 0 + a = a + * a - 0 = a + * 0 - a = -a + * a ± 0 = a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "NumberRule5"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Sum || f instanceof Subtraction || f instanceof SumSubtraction) { + var root = f.getMathContext(); + Function fnc = f; + if (fnc.getParameter1().equals(new Number(root, 0)) || fnc.getParameter2().equals(new Number(root, 0))) { + if (!(fnc.getParameter1().equals(new Number(root, 0)) && f instanceof SumSubtraction)) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + Function fnc = f; + var a = fnc.getParameter1(); + if (a.equals(new Number(root, 0))) { + if (f instanceof Subtraction) { + a = new Multiplication(root, new Number(root, -1), fnc.getParameter2()); + } else { + a = fnc.getParameter2(); + } + } + result.add(a); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/NumberRule7.java b/src/main/resources/rules/NumberRule7.java new file mode 100644 index 00000000..8f4acecb --- /dev/null +++ b/src/main/resources/rules/NumberRule7.java @@ -0,0 +1,66 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +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.Sum; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Number rule + * a + a = 2a + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "NumberRule7"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXPANSION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Sum) { + isExecutable = f.getParameter1().equals(f.getParameter2()); + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + var mult = new Multiplication(root, new Number(root, 2), f.getParameter1()); + result.add(mult); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/UndefinedRule1.java b/src/main/resources/rules/UndefinedRule1.java new file mode 100644 index 00000000..9144788a --- /dev/null +++ b/src/main/resources/rules/UndefinedRule1.java @@ -0,0 +1,69 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 org.warp.picalculator.math.functions.Undefined; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Undefined rule + * 0^0=undefined + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "UndefinedRule1"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXISTENCE; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Power) { + var root = f.getMathContext(); + Function fnc = f; + if (fnc.getParameter1().equals(new Number(root, 0)) && fnc.getParameter2().equals(new Number(root, 0))) { + isExecutable = true; + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Undefined(root)); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/UndefinedRule2.java b/src/main/resources/rules/UndefinedRule2.java new file mode 100644 index 00000000..57e3ee57 --- /dev/null +++ b/src/main/resources/rules/UndefinedRule2.java @@ -0,0 +1,72 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +import org.warp.picalculator.Error; +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.MathContext; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Undefined; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Undefined rule + * a / 0 = undefined + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "UndefinedRule2"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.EXISTENCE; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + if (f instanceof Division) { + var root = f.getMathContext(); + Function fnc = f; + if (fnc.getParameter2() instanceof Number) { + var numb = fnc.getParameter2(); + if (numb.equals(new Number(root, 0))) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var root = f.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + result.add(new Undefined(root)); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/VariableRule1.java b/src/main/resources/rules/VariableRule1.java new file mode 100644 index 00000000..065473f8 --- /dev/null +++ b/src/main/resources/rules/VariableRule1.java @@ -0,0 +1,95 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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.Subtraction; +import org.warp.picalculator.math.functions.Sum; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Variable rule + * ax+bx=(a+b)*x (a,b NUMBER; x VARIABLE|MULTIPLICATION) + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "VariableRule1"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.REDUCTION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + Function fnc = f; + if (f instanceof Subtraction || f instanceof Sum) { + if (fnc.getParameter1() instanceof Multiplication & fnc.getParameter2() instanceof Multiplication) { + var m1 = fnc.getParameter1(); + var m2 = fnc.getParameter2(); + if (m1.getParameter1().equals(m2.getParameter1()) || m1.getParameter2().equals(m2.getParameter2())) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var root = fnc.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + var m1 = fnc.getParameter1(); + var m2 = fnc.getParameter2(); + var a; + var b; + var 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(); + } + + var rets; + if (fnc instanceof Sum) { + rets = new Sum(root, a, b); + } else { + rets = new Subtraction(root, a, b); + } + var retm = new Multiplication(root, rets, x); + result.add(retm); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/VariableRule2.java b/src/main/resources/rules/VariableRule2.java new file mode 100644 index 00000000..106d6f04 --- /dev/null +++ b/src/main/resources/rules/VariableRule2.java @@ -0,0 +1,84 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Variable rule + * ax+x=(a+1)*x (a,b NUMBER; x VARIABLES) + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "VariableRule2"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.REDUCTION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + Function fnc = f; + if (f instanceof Sum || f instanceof Subtraction) { + if (fnc.getParameter1() instanceof Multiplication) { + var m1 = fnc.getParameter1(); + if (m1.getParameter2().equals(fnc.getParameter2())) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var root = fnc.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + var m1 = fnc.getParameter1(); + var a = m1.getParameter1(); + var x = fnc.getParameter2(); + + var rets; + if (fnc instanceof Sum) { + rets = new Sum(root, a, new Number(root, 1)); + } else { + rets = new Subtraction(root, a, new Number(root, 1)); + } + var retm = new Multiplication(root, rets, x); + result.add(retm); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/VariableRule3.java b/src/main/resources/rules/VariableRule3.java new file mode 100644 index 00000000..8ca88e1c --- /dev/null +++ b/src/main/resources/rules/VariableRule3.java @@ -0,0 +1,85 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; +//Imports + + +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 it.unimi.dsi.fastutil.objects.ObjectArrayList; + +/** + * Variable rule + * x+ax=(a+1)*x (a,b NUMBER; x VARIABLES) + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "VariableRule3"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.REDUCTION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + + @Override + public ObjectArrayList execute(Function f) { + boolean isExecutable = false; + Function fnc = f; + if (f instanceof Sum || f instanceof Subtraction) { + if (fnc.getParameter2() instanceof Multiplication) { + var m2 = fnc.getParameter2(); + if (m2.getParameter2().equals(fnc.getParameter1())) { + isExecutable = true; + } + } + } + + if (isExecutable) { + var root = fnc.getMathContext(); + ObjectArrayList result = new ObjectArrayList<>(); + var m2 = fnc.getParameter2(); + var a = m2.getParameter1(); + var x = fnc.getParameter1(); + + var rets; + if (fnc instanceof Sum) { + rets = new Sum(root, new Number(root, 1), a); + } else { + rets = new Subtraction(root, new Number(root, 1), a); + } + + var retm = new Multiplication(root, rets, x); + result.add(retm); + return result; + } else { + return null; + } + } +} diff --git a/src/main/resources/rules/functions/Division.java b/src/main/resources/rules/functions/Division.java new file mode 100644 index 00000000..820a6363 --- /dev/null +++ b/src/main/resources/rules/functions/Division.java @@ -0,0 +1,88 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; + +/** + * Division + * a/b = c + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Division"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Division) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var mathContext = f.getMathContext(); + if (variable1 instanceof Number && variable2 instanceof Number) { + if (mathContext.exactMode) { + if (variable1.isInteger() && variable2.isInteger()) { + var factors1, factors2, mcm; + try { + factors1 = variable1.getFactors(); + factors2 = variable2.getFactors(); + mcm = ScriptUtils.mcm(factors1, factors2); + } catch (error) { + return null; + } + if (mcm.size() > 0) { //true if there is at least one common factor + //divide by the common factor (ab/cb = a/c) + var nmb1 = variable1.term.toBigIntegerExact(); + var nmb2 = variable2.term.toBigIntegerExact(); + mcm.forEach(function(integerNumber) { + nmb1 = nmb1.divide(integerNumber); + nmb2 = nmb2.divide(integerNumber); + }); + result.add(new Division(mathContext, new Number(mathContext, nmb1), new Number(mathContext, nmb2))); + return result; + } + } + } else { + //divide a by b (a/b = c) + result.add(variable1.divide(variable2)); + return result; + } + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/EmptyNumber.java b/src/main/resources/rules/functions/EmptyNumber.java new file mode 100644 index 00000000..78660d4f --- /dev/null +++ b/src/main/resources/rules/functions/EmptyNumber.java @@ -0,0 +1,47 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; + +/** + * EmptyNumber + * + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "EmptyNumber"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + return null; + } +} diff --git a/src/main/resources/rules/functions/Expression.java b/src/main/resources/rules/functions/Expression.java new file mode 100644 index 00000000..f2959bfb --- /dev/null +++ b/src/main/resources/rules/functions/Expression.java @@ -0,0 +1,58 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Expression; + +/** + * Expression + * (x) = x + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Expression"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Expression) { + ObjectArrayList result = new ObjectArrayList<>(); + result.add(f.getParameter(0)); + return result; + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Joke.java b/src/main/resources/rules/functions/Joke.java new file mode 100644 index 00000000..d3523fef --- /dev/null +++ b/src/main/resources/rules/functions/Joke.java @@ -0,0 +1,47 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; + +/** + * Joke + * + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Joke"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + return null; + } +} diff --git a/src/main/resources/rules/functions/Multiplication.java b/src/main/resources/rules/functions/Multiplication.java new file mode 100644 index 00000000..72520608 --- /dev/null +++ b/src/main/resources/rules/functions/Multiplication.java @@ -0,0 +1,64 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; + +/** + * Multiplication + * a*b = c + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Multiplication"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Multiplication) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var mathContext = f.getMathContext(); + if (variable1 instanceof Number && variable2 instanceof Number) { + //multiply a by b (a*b = c) + result.add(variable1.multiply(variable2)); + return result; + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Negative.java b/src/main/resources/rules/functions/Negative.java new file mode 100644 index 00000000..b923a718 --- /dev/null +++ b/src/main/resources/rules/functions/Negative.java @@ -0,0 +1,74 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Negative; +import org.warp.picalculator.Error; +import org.warp.picalculator.Errors; +import java.lang.NullPointerException; +import java.lang.NumberFormatException; +import java.lang.ArithmeticException; + +/** + * Negative + * -a = b + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Negative"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Negative) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable = f.getParameter(); + var mathContext = f.getMathContext(); + if (variable instanceof Number) { + //-a = a*-1 = b + try { + result.add(variable.multiply(new Number(mathContext, -1))); + } catch (ex) { + if (ex instanceof NullPointerException) { + throw new Error(Errors.ERROR); + } else if (ex instanceof NumberFormatException) { + throw new Error(Errors.SYNTAX_ERROR); + } else if (ex instanceof ArithmeticException) { + throw new Error(Errors.NUMBER_TOO_SMALL); + } + } + return result; + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Number.java b/src/main/resources/rules/functions/Number.java new file mode 100644 index 00000000..b2c2bdfe --- /dev/null +++ b/src/main/resources/rules/functions/Number.java @@ -0,0 +1,49 @@ +// Imports +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); +var ScriptUtils = org.warp.picalculator.ScriptUtils; +var Rule = org.warp.picalculator.math.rules.Rule; +var RuleType = org.warp.picalculator.math.rules.RuleType; +var RulesManager = org.warp.picalculator.math.rules.RulesManager; +var BigInteger = java.math.BigInteger; + +/** + * Number + * + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "Number"; + }, + // Rule type + getRuleType: function() { + return RuleType.CALCULATION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + execute: function(f) { + if (ScriptUtils.instanceOf(f, Number.class)) { + var result = new ObjectArrayList(); + var mathContext = f.getMathContext(); + if (mathContext.exactMode) { + if (f.isInteger() == false) { + var divisor = new Number(mathContext, BigInteger.TEN.pow(f.getNumberOfDecimalPlaces())); + var number = new Number(mathContext, f.getTerm().multiply(divisor.term)); + var div = new Division(mathContext, number, divisor); + result.add(div); + return result; + } + } + } + return null; + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/Power.java b/src/main/resources/rules/functions/Power.java new file mode 100644 index 00000000..c7feccef --- /dev/null +++ b/src/main/resources/rules/functions/Power.java @@ -0,0 +1,53 @@ +// Imports +var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList"); +var ScriptUtils = org.warp.picalculator.ScriptUtils; +var Rule = org.warp.picalculator.math.rules.Rule; +var RuleType = org.warp.picalculator.math.rules.RuleType; +var RulesManager = org.warp.picalculator.math.rules.RulesManager; +var Multiplication = org.warp.picalculator.math.functions.Multiplication; +var Sum = org.warp.picalculator.math.functions.Sum; +var Subtraction = org.warp.picalculator.math.functions.Subtraction; +var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction; +var Number = org.warp.picalculator.math.functions.Number; +var Division = org.warp.picalculator.math.functions.Division; +var Power = org.warp.picalculator.math.functions.Power; + +/** + * Power + * a^b = c + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "Power"; + }, + // Rule type + getRuleType: function() { + return RuleType.CALCULATION; + }, + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + execute: function(f) { + if (ScriptUtils.instanceOf(f, Power.class)) { + var result = new ObjectArrayList(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var mathContext = f.getMathContext(); + if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) { + //a^b = c + result.add(variable1.pow(variable2)); + return result; + } + } + return null; + } +} + +//Add this rule to the list of rules +RulesManager.addRule(engine.getInterface(rule, Rule.class)); \ No newline at end of file diff --git a/src/main/resources/rules/functions/Root.java b/src/main/resources/rules/functions/Root.java new file mode 100644 index 00000000..3455b931 --- /dev/null +++ b/src/main/resources/rules/functions/Root.java @@ -0,0 +1,90 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; +import org.warp.picalculator.math.functions.Root; +import org.warp.picalculator.math.functions.RootSquare; +import java.math.BigDecimal; + +/** + * Root + * a√b = c + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Root"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + var isSquare = false; + if ((isSquare = f instanceof RootSquare) || f instanceof Root) { + ObjectArrayList result = new ObjectArrayList<>(); + var mathContext = f.getMathContext(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var isSolvable = false; + var canBePorted = false; + if (variable1 instanceof Number && variable2 instanceof Number) { + isSolvable = isSolvable|!mathContext.exactMode; + if (!isSolvable) { + try { + var resultVar = variable2.pow(new Number(mathContext, BigDecimal.ONE).divide(variable1)); + var originalVariable = resultVar.pow(new Number(mathContext, 2)); + if (originalVariable.equals(f.getParameter2())) { + isSolvable = true; + } + } catch (ex) { + ex.printStackTrace(); + } + } + } + if (!isSquare && !isSolvable && variable1 instanceof Number && variable1.equals(new Number(mathContext, 2))) { + canBePorted = true; + } + + if (isSolvable) { + result.add(variable2.pow(new Number(mathContext, BigInteger.ONE).divide(variable1))); + return result; + } + if (canBePorted) { + result.add(new RootSquare(mathContext, variable2)); + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Subtraction.java b/src/main/resources/rules/functions/Subtraction.java new file mode 100644 index 00000000..e72462e0 --- /dev/null +++ b/src/main/resources/rules/functions/Subtraction.java @@ -0,0 +1,64 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; + +/** + * Subtraction + * a-b = c + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Subtraction"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Subtraction) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var mathContext = f.getMathContext(); + if (variable1 instanceof Number && variable2 instanceof Number) { + //a-b = a+(b*-1) = c + result.add(variable1.add(variable2.multiply(new Number(mathContext, -1)))); + return result; + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Sum.java b/src/main/resources/rules/functions/Sum.java new file mode 100644 index 00000000..af68a09d --- /dev/null +++ b/src/main/resources/rules/functions/Sum.java @@ -0,0 +1,64 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; + +/** + * Sum + * a+b = c + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Sum"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Sum) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var mathContext = f.getMathContext(); + if (variable1 instanceof Number && variable2 instanceof Number) { + //a+b = c + result.add(variable1.add(variable2)); + return result; + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/SumSubtraction.java b/src/main/resources/rules/functions/SumSubtraction.java new file mode 100644 index 00000000..4deaed00 --- /dev/null +++ b/src/main/resources/rules/functions/SumSubtraction.java @@ -0,0 +1,65 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.SumSubtraction; +import org.warp.picalculator.math.functions.Number; +import org.warp.picalculator.math.functions.Division; + +/** + * SumSumbraction + * a±b = c, d + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "SumSubtraction"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof SumSubtraction) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable1 = f.getParameter1(); + var variable2 = f.getParameter2(); + var mathContext = f.getMathContext(); + if (variable1 instanceof Number && variable2 instanceof Number) { + //a±b = c, d + result.add(variable1.add(variable2)); + result.add(variable1.add(variable2.multiply(new Number(mathContext, -1)))); + return result; + } + } + return null; + } +} diff --git a/src/main/resources/rules/functions/Variable.java b/src/main/resources/rules/functions/Variable.java new file mode 100644 index 00000000..202b8a12 --- /dev/null +++ b/src/main/resources/rules/functions/Variable.java @@ -0,0 +1,67 @@ +/* +SETTINGS: (please don't move this part) + PATH=__INSERT_PACKAGE_WITH_CLASS_NAME__ +*/ + +import org.warp.picalculator.math.Function; +import org.warp.picalculator.math.FunctionOperator; +import org.warp.picalculator.math.FunctionDynamic; +import org.warp.picalculator.math.FunctionSingle; +import org.warp.picalculator.math.MathContext; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import org.nevec.rjm.BigDecimalMath; +import org.warp.picalculator.Utils; +import org.warp.picalculator.math.MathematicalSymbols; +import org.warp.picalculator.ScriptUtils; +import org.warp.picalculator.math.rules.Rule; +import org.warp.picalculator.math.rules.RuleType; +import org.warp.picalculator.math.rules.RulesManager; +import org.warp.picalculator.math.functions.Multiplication; +import org.warp.picalculator.math.functions.Sum; +import org.warp.picalculator.math.functions.Subtraction; +import org.warp.picalculator.math.functions.Variable; +import org.warp.picalculator.math.functions.Number; + +/** + * Variable + * a = n + * + * @author Andrea Cavalli + * + */ +public class __INSERT_CLASS_NAME__ implements Rule { + // Rule name + @Override + public String getRuleName() { + return "Variable"; + } + + // Rule type + @Override + public RuleType getRuleType() { + return RuleType.CALCULATION; + } + + /* Rule function + Returns: + - null if it's not executable on the function "f" + - An ObjectArrayList if it did something + */ + @Override + public ObjectArrayList execute(Function f) { + if (f instanceof Variable) { + ObjectArrayList result = new ObjectArrayList<>(); + var variable = f.getChar(); + var mathContext = f.getMathContext(); + if (mathContext.exactMode == false) { + if (variable.equals(MathematicalSymbols.PI)) { + //a = n + result.add(new Number(mathContext, BigDecimalMath.pi(new java.math.MathContext(Utils.scale, Utils.scaleMode2)))); + return result; + } + } + } + return null; + } +}