Converted more rules

This commit is contained in:
Andrea Cavalli 2018-02-06 22:46:28 +01:00
parent 62277c7b4f
commit ef1430a9e4
13 changed files with 390 additions and 255 deletions

View File

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

View File

@ -1,52 +0,0 @@
package org.warp.picalculator.math.rules;
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.functions.Division;
import org.warp.picalculator.math.functions.Expression;
import org.warp.picalculator.math.functions.Multiplication;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Fractions rule<br>
* <b>a / (b / c) = (a * c) / b</b>
*
* @author Andrea Cavalli
*
*/
public class FractionsRule11 {
public static boolean compare(Function f) throws InterruptedException {
final Division fnc = (Division) f;
Function a;
Function c;
Division div2;
if (fnc.getParameter2() instanceof Division) {
div2 = (Division) fnc.getParameter2();
} else {
return false;
}
a = fnc.getParameter1();
c = div2.getParameter2();
return new Multiplication(fnc.getMathContext(), a, c).simplify(this) != null;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Division fnc = (Division) f;
Function a;
Function b;
Function c;
final Division div2 = (Division) fnc.getParameter2();
a = fnc.getParameter1();
b = div2.getParameter1();
c = div2.getParameter2();
result.add(new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), new Expression(fnc.getMathContext(), a), new Expression(fnc.getMathContext(), c)), b));
return result;
}
}

View File

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

View File

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

View File

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

View File

@ -1,47 +0,0 @@
package org.warp.picalculator.math.rules;
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<br>
* <b>(a / b) ^ -c = (b / a) ^ c</b>
*
* @author Andrea Cavalli
*
*/
public class FractionsRule5 {
public static boolean compare(Function f) {
final Power fnc = (Power) f;
if (fnc.getParameter1() instanceof Division && fnc.getParameter2() instanceof Number) {
final Number n2 = (Number) fnc.getParameter2();
if (n2.getTerm().compareTo(BigDecimal.ZERO) < 0) {
return true;
}
}
return false;
}
public static ObjectArrayList<Function> execute(Function f) throws Error {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final Power fnc = (Power) f;
final Function a = ((Division) fnc.getParameter1()).getParameter1();
final Function b = ((Division) fnc.getParameter1()).getParameter2();
final Function c = ((Number) fnc.getParameter2()).multiply(new Number(root, "-1"));
final Division dv = new Division(root, b, a);
final Power pow = new Power(root, dv, c);
result.add(pow);
return result;
}
}

View File

@ -22,4 +22,10 @@ ExponentRule4
ExponentRule9
ExponentRule15
ExponentRule16
ExponentRule17
ExponentRule17
FractionsRule1
FractionsRule2
FractionsRule3
FractionsRule4
FractionsRule5
FractionsRule11

1 Rule file
22 ExponentRule9
23 ExponentRule15
24 ExponentRule16
25 ExponentRule17
26 FractionsRule1
27 FractionsRule2
28 FractionsRule3
29 FractionsRule4
30 FractionsRule5
31 FractionsRule11

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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