Finished adding the rules of the functions

This commit is contained in:
Andrea Cavalli 2017-12-26 15:04:23 +01:00
parent 738a1f3982
commit f952f51ba8
5 changed files with 216 additions and 0 deletions

View File

@ -8,6 +8,10 @@ functions/Negative
functions/Number
functions/Power
functions/Root
functions/Subtraction
functions/Sum
functions/SumSubtraction
functions/Variable
ExpandRule1
ExpandRule2
ExpandRule5
1 Rule file
8 functions/Number
9 functions/Power
10 functions/Root
11 functions/Subtraction
12 functions/Sum
13 functions/SumSubtraction
14 functions/Variable
15 ExpandRule1
16 ExpandRule2
17 ExpandRule5

View File

@ -0,0 +1,52 @@
// 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;
/**
* Subtraction
* a-b = c
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Subtraction";
},
// 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, Subtraction.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 = a+(b*-1) = c
result.add(variable1.add(variable2.multiply(new Number(mathContext, -1))));
return result;
}
}
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,52 @@
// 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;
/**
* Sum
* a+b = c
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Sum";
},
// 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, Sum.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.add(variable2));
return result;
}
}
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 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;
/**
* SumSumbraction
* a±b = c, d
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "SumSubtraction";
},
// 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, SumSubtraction.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, d
result.add(variable1.add(variable2));
result.add(variable1.add(variable2.multiply(new Number(mathContext, -1))));
return result;
}
}
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -0,0 +1,55 @@
// Imports
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
var BigDecimalMath = org.nevec.rjm.BigDecimalMath;
var Utils = org.warp.picalculator.Utils;
var MathematicalSymbols = org.warp.picalculator.math.MathematicalSymbols;
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 Variable = org.warp.picalculator.math.functions.Variable;
var Number = org.warp.picalculator.math.functions.Number;
/**
* Variable
* a = n
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Variable";
},
// 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, Variable.class)) {
var 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;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));