Added division rule

removed also some old and unused resources
This commit is contained in:
Andrea Cavalli 2017-12-23 14:09:42 +01:00
parent 8329b09601
commit c18596661f
8 changed files with 103 additions and 25 deletions

View File

@ -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<BigInteger> mcm(LinkedList<BigInteger> factors1, LinkedList<BigInteger> factors2) {
LinkedList<BigInteger> mcm = new LinkedList<>();
Iterator<BigInteger> i1 = factors1.iterator();
while(i1.hasNext()) {
BigInteger int1 = i1.next();
Iterator<BigInteger> i2 = factors2.iterator();
while(i2.hasNext()) {
BigInteger int2 = i2.next();
if (int1.equals(int2)) {
i1.remove();
i2.remove();
mcm.add(int1);
break;
}
}
}
return mcm;
}
}

View File

@ -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<BigInteger> mcm(LinkedList<BigInteger> factors1, LinkedList<BigInteger> factors2) {
LinkedList<BigInteger> mcm = new LinkedList<>();
Iterator<BigInteger> i1 = factors1.iterator();
while(i1.hasNext()) {
BigInteger int1 = i1.next();
Iterator<BigInteger> 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<Object> ref = new WeakReference<>(obj);

View File

@ -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();
/**

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

View File

@ -1,4 +1,5 @@
functions/Expression
functions/Division
ExpandRule1
ExpandRule2
ExpandRule5
1 functions/Expression
2 functions/Division
3 ExpandRule1
4 ExpandRule2
5 ExpandRule5

View File

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

View File

@ -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: