diff --git a/src/main/java/org/warp/picalculator/ScriptUtils.java b/src/main/java/org/warp/picalculator/ScriptUtils.java index b71e6b19..e4fbe35c 100644 --- a/src/main/java/org/warp/picalculator/ScriptUtils.java +++ b/src/main/java/org/warp/picalculator/ScriptUtils.java @@ -1,7 +1,30 @@ package org.warp.picalculator; +import java.math.BigInteger; +import java.util.Iterator; +import java.util.LinkedList; + public class ScriptUtils { public static boolean instanceOf(Object a, Class b) { return b.isInstance(a); } + + public static LinkedList mcm(LinkedList factors1, LinkedList factors2) { + LinkedList mcm = new LinkedList<>(); + Iterator i1 = factors1.iterator(); + while(i1.hasNext()) { + BigInteger int1 = i1.next(); + Iterator i2 = factors2.iterator(); + while(i2.hasNext()) { + BigInteger int2 = i2.next(); + if (int1.equals(int2)) { + i1.remove(); + i2.remove(); + mcm.add(int1); + break; + } + } + } + return mcm; + } } diff --git a/src/main/java/org/warp/picalculator/Utils.java b/src/main/java/org/warp/picalculator/Utils.java index 5bef4fe5..2cb57243 100755 --- a/src/main/java/org/warp/picalculator/Utils.java +++ b/src/main/java/org/warp/picalculator/Utils.java @@ -30,8 +30,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -728,25 +726,6 @@ public class Utils { return (OS.indexOf("win") >= 0); } - public static LinkedList mcm(LinkedList factors1, LinkedList factors2) { - LinkedList mcm = new LinkedList<>(); - Iterator i1 = factors1.iterator(); - while(i1.hasNext()) { - BigInteger int1 = i1.next(); - Iterator i2 = factors2.iterator(); - while(i2.hasNext()) { - BigInteger int2 = i2.next(); - if (int1.equals(int2)) { - i1.remove(); - i2.remove(); - mcm.add(int1); - break; - } - } - } - return mcm; - } - public static void gc() { Object obj = new Object(); final WeakReference ref = new WeakReference<>(obj); diff --git a/src/main/java/org/warp/picalculator/math/rules/Rule.java b/src/main/java/org/warp/picalculator/math/rules/Rule.java index ea1befd3..821bd3b3 100644 --- a/src/main/java/org/warp/picalculator/math/rules/Rule.java +++ b/src/main/java/org/warp/picalculator/math/rules/Rule.java @@ -3,6 +3,7 @@ package org.warp.picalculator.math.rules; import org.warp.picalculator.math.Function; import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import jdk.nashorn.internal.objects.annotations.SpecializedFunction; /** * Rule interface @@ -22,6 +23,7 @@ public interface Rule { * Get rule type * @return */ + @SpecializedFunction public RuleType getRuleType(); /** diff --git a/src/main/resources/draft unknown variable choice.png b/src/main/resources/draft unknown variable choice.png deleted file mode 100755 index 973440c9..00000000 Binary files a/src/main/resources/draft unknown variable choice.png and /dev/null differ diff --git a/src/main/resources/palettetmp.bmp b/src/main/resources/palettetmp.bmp deleted file mode 100755 index db22ad4f..00000000 Binary files a/src/main/resources/palettetmp.bmp and /dev/null differ diff --git a/src/main/resources/rules.csv b/src/main/resources/rules.csv index 850d45d0..dd5be13a 100644 --- a/src/main/resources/rules.csv +++ b/src/main/resources/rules.csv @@ -1,4 +1,5 @@ functions/Expression +functions/Division ExpandRule1 ExpandRule2 ExpandRule5 \ No newline at end of file diff --git a/src/main/resources/rules/functions/Division.js b/src/main/resources/rules/functions/Division.js new file mode 100644 index 00000000..f25e9f36 --- /dev/null +++ b/src/main/resources/rules/functions/Division.js @@ -0,0 +1,74 @@ +// 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; + +/** + * Division + * a/b = c + * + * @author Andrea Cavalli + * + */ +var rule = { + // Rule name + getRuleName: function() { + return "Division"; + }, + // 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, Division.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)) { + 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))); + } + } + } else { + //divide a by b (a/b = c) + result.add(variable1.divide(variable2)); + } + } + } + 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/Expression.js b/src/main/resources/rules/functions/Expression.js index 7c85ffff..62457c39 100644 --- a/src/main/resources/rules/functions/Expression.js +++ b/src/main/resources/rules/functions/Expression.js @@ -12,9 +12,8 @@ var Number = org.warp.picalculator.math.functions.Number; var Expression = org.warp.picalculator.math.functions.Expression; /** - * Expand rule - * -(+a+b) = -a-b - * -(+a-b) = -a+b + * Expression + * (x) = x * * @author Andrea Cavalli * @@ -26,7 +25,7 @@ var rule = { }, // Rule type getRuleType: function() { - return RuleType.EXPANSION; + return RuleType.CALCULATION; }, /* Rule function Returns: