Merge remote-tracking branch 'origin/new-math-rules' into new-math-rules

This commit is contained in:
Andrea Cavalli 2018-03-22 17:38:07 +01:00
commit 3a4964f24b
55 changed files with 206 additions and 2967 deletions

View File

@ -6,7 +6,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>

View File

@ -1,3 +1,6 @@
eclipse.preferences.version=1 eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter

BIN
functions_Division.ser Normal file

Binary file not shown.

48
pom.xml
View File

@ -18,13 +18,37 @@
</snapshots> </snapshots>
</repository> </repository>
</repositories> </repositories>
<!--
<profiles>
<profile>
<id>windows-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
<file>
<exists>${JAVA_HOME}/lib/tools.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${JAVA_HOME}/lib/tools.jar</toolsjar>
</properties>
</profile>
<profile>
<id>mac-profile</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
</properties>
</profile>
</profiles>
-->
<dependencies> <dependencies>
<dependency> <!--<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<groupId>junit</groupId> <version>4.12</version> <scope>test</scope> </dependency> -->
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>it.unimi.dsi</groupId> <groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId> <artifactId>fastutil</artifactId>
@ -55,6 +79,18 @@
<artifactId>pngj</artifactId> <artifactId>pngj</artifactId>
<version>2.1.0</version> <version>2.1.0</version>
</dependency> </dependency>
<dependency>
<groupId>net.openhft</groupId>
<artifactId>compiler</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>jdk1.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/tools.jar</systemPath>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>

View File

@ -36,6 +36,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.nevec.rjm.BigDecimalMath; import org.nevec.rjm.BigDecimalMath;
import org.nevec.rjm.Rational; import org.nevec.rjm.Rational;
@ -841,6 +842,14 @@ public class Utils {
} }
} }
public static InputStream getResourceStreamSafe(String string) throws IOException, URISyntaxException {
try {
return getResourceStream(string);
} catch (Exception ex) {
return null;
}
}
public static InputStream getResourceStream(String string) throws IOException, URISyntaxException { public static InputStream getResourceStream(String string) throws IOException, URISyntaxException {
URL res = Main.instance.getClass().getResource(string); URL res = Main.instance.getClass().getResource(string);
boolean isResource = res != null; boolean isResource = res != null;
@ -865,4 +874,10 @@ public class Utils {
return Files.newInputStream(Paths.get(string.substring(1))); return Files.newInputStream(Paths.get(string.substring(1)));
} }
} }
public static String read(InputStream input) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
}
} }

View File

@ -1,15 +1,10 @@
package org.warp.picalculator.math.functions; package org.warp.picalculator.math.functions;
import java.util.List;
import org.nevec.rjm.BigDecimalMath;
import org.warp.picalculator.Error; import org.warp.picalculator.Error;
import org.warp.picalculator.gui.expression.blocks.Block; import org.warp.picalculator.gui.expression.blocks.Block;
import org.warp.picalculator.gui.expression.blocks.BlockChar; import org.warp.picalculator.gui.expression.blocks.BlockChar;
import org.warp.picalculator.math.Function; import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext; import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.Utils;
import org.warp.picalculator.math.MathematicalSymbols;
import org.warp.picalculator.math.rules.Rule; import org.warp.picalculator.math.rules.Rule;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;

View File

@ -1,5 +1,7 @@
package org.warp.picalculator.math.rules; package org.warp.picalculator.math.rules;
import java.io.Serializable;
import org.warp.picalculator.math.Function; import org.warp.picalculator.math.Function;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
@ -10,7 +12,7 @@ import jdk.nashorn.internal.objects.annotations.SpecializedFunction;
* @author Andrea Cavalli * @author Andrea Cavalli
* *
*/ */
public interface Rule { public interface Rule extends Serializable {
/** /**
* Get rule name * Get rule name
* @return * @return

View File

@ -2,10 +2,14 @@ package org.warp.picalculator.math.rules;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.net.URI; import java.net.URI;
@ -14,11 +18,15 @@ import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.script.Bindings; import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext; import javax.script.ScriptContext;
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager; import javax.script.ScriptEngineManager;
@ -38,6 +46,7 @@ import org.warp.picalculator.math.functions.Variable.V_TYPE;
import org.warp.picalculator.math.solver.MathSolver; import org.warp.picalculator.math.solver.MathSolver;
import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.openhft.compiler.CompilerUtils;
public class RulesManager { public class RulesManager {
@ -53,9 +62,9 @@ public class RulesManager {
rules[val.ordinal()] = new ObjectArrayList<Rule>(); rules[val.ordinal()] = new ObjectArrayList<Rule>();
} }
try { try {
final Path rulesPath = Utils.getResource("/rules.csv"); final Path rulesPath = Utils.getResource("/math-rules.csv");
if (!Files.exists(rulesPath)) { if (!Files.exists(rulesPath)) {
throw new FileNotFoundException("rules.csv not found!"); throw new FileNotFoundException("math-rules.csv not found!");
} }
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
List<String> ruleLines = Files.readAllLines(rulesPath); List<String> ruleLines = Files.readAllLines(rulesPath);
@ -64,23 +73,81 @@ public class RulesManager {
if (rulesLine.length() > 0) { if (rulesLine.length() > 0) {
String[] ruleDetails = rulesLine.split(",", 1); String[] ruleDetails = rulesLine.split(",", 1);
String ruleName = ruleDetails[0]; String ruleName = ruleDetails[0];
Utils.out.println("Evaluating /rules/" + ruleName + ".js"); String ruleNameEscaped = ruleName.replace(".", "_");
InputStream resourcePath = Utils.getResourceStream("/rules/" + ruleName.replace(".", "_") + ".js"); Utils.out.println("Evaluating /rules/" + ruleNameEscaped + ".java");
String pathWithoutExtension = "/rules/" + ruleNameEscaped;
String compiledFile = ruleNameEscaped + ".ser";
InputStream compiledResourcePath = Utils.getResourceStreamSafe(compiledFile);
String scriptFile = pathWithoutExtension + ".java";
InputStream resourcePath = Utils.getResourceStream(scriptFile);
if (resourcePath == null) { if (resourcePath == null) {
System.err.println(new FileNotFoundException("/rules/" + ruleName + ".js not found!")); System.err.println(new FileNotFoundException("/rules/" + ruleName + ".java not found!"));
} else { } else {
engine.eval(new InputStreamReader(resourcePath)); boolean loadedFromCache = false;
if (compiledResourcePath != null) {
if (Files.readAttributes(Paths.get(compiledFile), BasicFileAttributes.class).creationTime().compareTo(Files.readAttributes(Paths.get(scriptFile), BasicFileAttributes.class).creationTime()) < 0) {
//The cached file is older than the source file. Deleting it.
try {
Files.deleteIfExists(Paths.get(compiledFile));
} catch (Exception ex2) {
ex2.printStackTrace();
}
} else {
try {
//Trying to read the compiled script
ObjectInputStream ois = new ObjectInputStream(compiledResourcePath);
RulesManager.addRule((Rule) ois.readObject());
loadedFromCache = true;
} catch (Exception ex) {
ex.printStackTrace();
try {
Files.deleteIfExists(Paths.get(compiledFile));
} catch (Exception ex2) {
ex2.printStackTrace();
}
}
}
}
if (!loadedFromCache) {
Rule r = null;
try {
r = compileJavaRule(scriptFile);
RulesManager.addRule(r);
Path p = Paths.get(compiledFile.replace("/", "_")).toAbsolutePath();
System.out.println(p);
p.toFile().createNewFile();
OutputStream fout = Files.newOutputStream(p, StandardOpenOption.CREATE);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(r);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
} }
} }
} catch (URISyntaxException | IOException e) { } catch (URISyntaxException | IOException e) {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} catch (ScriptException e) {
e.printStackTrace();
} }
} }
public static Rule compileJavaRule(String scriptFile) throws IOException, URISyntaxException, InstantiationException, IllegalAccessException, ClassNotFoundException {
// dynamically you can call
InputStream resource = Utils.getResourceStream(scriptFile);
String text = Utils.read(resource);
String[] textArray = text.split("\\n", 2);
String javaClassName = textArray[0].substring(2, textArray[0].length() - 2);
String javaCode = textArray[1];
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(javaClassName, javaCode);
Rule rule = (Rule) aClass.newInstance();
return rule;
}
public static void warmUp() { public static void warmUp() {
ObjectArrayList<Function> uselessResult = null; ObjectArrayList<Function> uselessResult = null;
boolean uselessVariable = false; boolean uselessVariable = false;

View File

@ -0,0 +1,2 @@
Rule file
FractionsRule2
1 Rule file
2 FractionsRule2

View File

@ -1,44 +0,0 @@
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
1 Rule file
2 functions/Division
3 functions/EmptyNumber
4 functions/Expression
5 functions/Joke
6 functions/Multiplication
7 functions/Negative
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
18 ExponentRule1
19 ExponentRule2
20 ExponentRule3
21 ExponentRule4
22 ExponentRule9
23 ExponentRule15
24 ExponentRule16
25 ExponentRule17
26 FractionsRule1
27 FractionsRule2
28 FractionsRule3
29 FractionsRule4
30 FractionsRule5
31 FractionsRule11
32 FractionsRule12
33 FractionsRule14
34 NumberRule1
35 NumberRule2
36 NumberRule3
37 NumberRule4
38 NumberRule5
39 NumberRule7
40 UndefinedRule1
41 UndefinedRule2
42 VariableRule1
43 VariableRule2
44 VariableRule3

View File

@ -1,128 +0,0 @@
// 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;
/**
* Expand rule
* -(+a+b) = -a-b
* -(+a-b) = -a+b
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExpandRule1";
},
// 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, Multiplication.class)) {
var fnc = f;
if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) {
var expr = fnc.getParameter2();
if (ScriptUtils.instanceOf(expr, Sum.class)) {
isExecutable = true;
} else if (ScriptUtils.instanceOf(expr, Subtraction.class)) {
isExecutable = true;
} else if (ScriptUtils.instanceOf(expr, SumSubtraction.class)) {
isExecutable = true;
}
}
} else if (ScriptUtils.instanceOf(f,Subtraction.class) || ScriptUtils.instanceOf(f,SumSubtraction.class)) {
var fnc = f;
var expr = fnc.getParameter2();
if (ScriptUtils.instanceOf(expr,Sum.class)) {
isExecutable = true;
} else if (ScriptUtils.instanceOf(expr,Subtraction.class)) {
isExecutable = true;
} else if (ScriptUtils.instanceOf(expr,SumSubtraction.class)) {
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
var root = f.getMathContext();
var expr = null;
var fromSubtraction = 0;
var subtraction = null;
if (ScriptUtils.instanceOf(f,Multiplication.class)) {
expr = f.getParameter2();
} else if (ScriptUtils.instanceOf(f, Subtraction.class) || f instanceof SumSubtraction) {
expr = f.getParameter2();
if (ScriptUtils.instanceOf(f, Subtraction.class)) {
fromSubtraction = 1;
} else {
fromSubtraction = 2;
}
}
if (ScriptUtils.instanceOf(f, SumSubtraction.class)) {
}
var fnc = expr;
if (ScriptUtils.instanceOf(fnc, Sum.class)) {
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 (ScriptUtils.instanceOf(fnc, Subtraction.class)) {
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 (ScriptUtils.instanceOf(fnc, SumSubtraction.class)) {
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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,127 +0,0 @@
// 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;
/**
* Expand rule
* -(-a) = a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExpandRule2";
},
// 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, Multiplication.class)) {
var 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 (ScriptUtils.instanceOf(f,Subtraction.class) || ScriptUtils.instanceOf(f,SumSubtraction.class)) {
var fnc = f;
var expr = fnc.getParameter2();
if (ScriptUtils.instanceOf(expr,Sum.class)) {
isExecutable = true;
} else if (ScriptUtils.instanceOf(expr,Subtraction.class)) {
isExecutable = true;
} else if (ScriptUtils.instanceOf(expr,SumSubtraction.class)) {
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
var root = f.getMathContext();
var expr = null;
var fromSubtraction = 0;
var subtraction = null;
if (ScriptUtils.instanceOf(f,Multiplication.class)) {
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) {
}
var 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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,60 +0,0 @@
// Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var Expression = org.warp.picalculator.math.functions.Expression;
var Negative = org.warp.picalculator.math.functions.Negative;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Expand rule
* -(-a) = a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExpandRule5";
},
// 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, Negative.class)) {
isExecutable = ScriptUtils.instanceOf(f.getParameter(), Negative.class);
} else if (ScriptUtils.instanceOf(f, Multiplication.class)) {
if (f.getParameter1().equals(new Number(f.getMathContext(), -1)) && ScriptUtils.instanceOf(f.getParameter2(), Multiplication.class)) {
isExecutable = f.getParameter2().getParameter1().equals(f.getParameter1());
}
}
if (isExecutable) {
var result = new ObjectArrayList();
if (ScriptUtils.instanceOf(f, Negative.class)) {
var fnc = f;
result.add(((fnc.getParameter()).getParameter()).getParameter());
} else if (ScriptUtils.instanceOf(f, Multiplication.class)) {
var fnc = f;
result.add(fnc.getParameter2().getParameter2());
}
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,55 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
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");
/**
* Exponent rule
* 1^a=1
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule1";
},
// 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, Power.class)) {
if (f.getParameter1().equals(new Number(root, 1))) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
result.add(new Number(root, 1));
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,60 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
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");
/**
* Exponent rule
* a*a=a^2
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule15";
},
// 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, Multiplication.class)) {
var fnc = f;
if (fnc.getParameter1().equals(fnc.getParameter2())) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var 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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,68 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var Sum = org.warp.picalculator.math.functions.Sum;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* (a ^ b) * (a ^ c) = a ^ (b + c)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule16";
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
},
/* 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, Multiplication.class)) {
var fnc = f;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
isExecutable = (fnc.getParameter1()).getParameter1().equals((fnc.getParameter2()).getParameter1());
} else if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) {
isExecutable = (fnc.getParameter1()).getParameter1().equals(fnc.getParameter2());
} else if (ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
isExecutable = (fnc.getParameter2()).getParameter1().equals(fnc.getParameter1());
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var fnc = f;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
result.add(new Power(root, (fnc.getParameter1()).getParameter1(), new Sum(root, fnc.getParameter1().getParameter2(), fnc.getParameter2().getParameter2())));
} else if (ScriptUtils.instanceOf(fnc.getParameter1(), Power.class)) {
result.add(new Power(root, fnc.getParameter2(), new Sum(root, fnc.getParameter1().getParameter2(), new Number(root, 1))));
} else if (ScriptUtils.instanceOf(fnc.getParameter2(), Power.class)) {
result.add(new Power(root, fnc.getParameter1(), new Sum(root, new Number(root, 1), fnc.getParameter2().getParameter2())));
}
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,61 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var Root = org.warp.picalculator.math.functions.Root;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* ax=x^1/a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule17";
},
// 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, Root.class)) {
var fnc = f;
if (fnc.getParameter1().equals(fnc.getParameter2())) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var 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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,53 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
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");
/**
* Exponent rule
* a^1=a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule2";
},
// 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, Power.class)) {
var fnc = f;
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 1))) {
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
result.add((f).getParameter1());
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,53 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
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");
/**
* Exponent rule
* a^0=1
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule3";
},
// 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, Power.class)) {
var fnc = f;
if (fnc.getParameter2().equals(new Number(f.getMathContext(), 0))) {
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

@ -1,65 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Expression = org.warp.picalculator.math.functions.Expression;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
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");
/**
* Exponent rule
* (a*b)^n=a^n*b^n
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule4";
},
// 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 (fnc.getParameter1() instanceof Multiplication && ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var 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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,58 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Power = org.warp.picalculator.math.functions.Power;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Exponent rule
* (a ^ b) ^ c = a ^ (b * c)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "ExponentRule9";
},
// 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(), Power.class)) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var 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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,65 +0,0 @@
//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

@ -1,71 +0,0 @@
//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

@ -1,68 +0,0 @@
//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
* (b / c) / a = b / (a * c)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "FractionsRule12";
},
// 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;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) {
var div2 = fnc.getParameter1();
a = fnc.getParameter1();
c = div2.getParameter2();
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
var fnc = f;
var a;
var b;
var c;
var div2 = fnc.getParameter1();
a = fnc.getParameter2();
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

@ -1,104 +0,0 @@
//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 / d) = (a * c) / (b * d)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "FractionsRule14";
},
// 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, Multiplication.class)) {
var fnc = f;
var a;
var b;
var c;
var d;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) {
var div1 = fnc.getParameter1();
var div2 = fnc.getParameter2();
a = div1.getParameter1();
b = div1.getParameter2();
c = div2.getParameter1();
d = div2.getParameter2();
isExecutable = true;
} else if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) {
var div1 = fnc.getParameter1();
a = div1.getParameter1();
b = div1.getParameter2();
c = fnc.getParameter2();
isExecutable = true;
} else if (ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) {
var div2 = fnc.getParameter2();
a = fnc.getParameter1();
c = div2.getParameter1();
d = div2.getParameter2();
isExecutable = true;
}
}
if (isExecutable) {
var result = new ObjectArrayList();
var fnc = f;
var a;
var b;
var c;
var d;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class) && ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) {
var div1 = fnc.getParameter1();
var div2 = fnc.getParameter2();
a = div1.getParameter1();
b = div1.getParameter2();
c = div2.getParameter1();
d = div2.getParameter2();
var div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), new Multiplication(fnc.getMathContext(), b, d));
result.add(div);
} else if (ScriptUtils.instanceOf(fnc.getParameter1(), Division.class)) {
var div1 = fnc.getParameter1();
a = div1.getParameter1();
b = div1.getParameter2();
c = fnc.getParameter2();
var div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), b);
result.add(div);
} else if (ScriptUtils.instanceOf(fnc.getParameter2(), Division.class)) {
var div2 = fnc.getParameter2();
a = fnc.getParameter1();
c = div2.getParameter1();
d = div2.getParameter2();
var div = new Division(fnc.getMathContext(), new Multiplication(fnc.getMathContext(), a, c), d);
result.add(div);
}
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,64 @@
/*org.warp.picalculator.math.rules.FractionsRule2*/
package org.warp.picalculator.math.rules;
//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.rules.Rule;
import org.warp.picalculator.math.rules.RuleType;
import org.warp.picalculator.ScriptUtils;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Fractions rule
* 0 / a = 0
*
* @author Andrea Cavalli
*
*/
public class FractionsRule2 implements Rule {
@Override
public String getRuleName() {
return "FractionsRule1";
}
@Override
public RuleType getRuleType() {
return RuleType.CALCULATION;
}
@Override
public ObjectArrayList<Function> execute(Function f) {
boolean isExecutable = false;
MathContext root = f.getMathContext();
if (ScriptUtils.instanceOf(f, Division.class)) {
Division fnc = (Division) f;
if (ScriptUtils.instanceOf(fnc.getParameter1(), Number.class)) {
Number numb1 = (Number) fnc.getParameter1();
if (numb1.equals(new Number(root, 0))) {
if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) {
Number numb2 = (Number) fnc.getParameter2();
if (numb2.equals(new Number(root, 0)) == false) {
isExecutable = true;
}
} else {
isExecutable = true;
}
}
}
}
if (isExecutable) {
ObjectArrayList<Function> result = new ObjectArrayList<>();
result.add(new Number(f.getMathContext(), 0));
return result;
} else {
return null;
}
}
}

View File

@ -1,57 +0,0 @@
//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

@ -1,53 +0,0 @@
//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

@ -1,61 +0,0 @@
//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

@ -1,76 +0,0 @@
//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));

View File

@ -1,64 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Number rule
* a * 0 = 0
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "NumberRule1";
},
// 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, Multiplication.class)) {
var root = f.getMathContext();
var mult = f;
if (ScriptUtils.instanceOf(mult.getParameter1(), Number.class)) {
var numb = mult.getParameter1();
if (numb.equals(new Number(root, 0))) {
isExecutable = true;
}
}
if (ScriptUtils.instanceOf(mult.getParameter2(), Number.class)) {
var numb = mult.getParameter2();
if (numb.equals(new Number(root, 0))) {
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

@ -1,83 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Number rule
* a * 1 = a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "NumberRule2";
},
// 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, Multiplication.class)) {
var root = f.getMathContext();
var mult = f;
if (ScriptUtils.instanceOf(mult.getParameter1(), Number.class)) {
var numb = mult.getParameter1();
if (numb.equals(new Number(root, 1))) {
isExecutable = true;
}
}
if (ScriptUtils.instanceOf(mult.getParameter2(), Number.class)) {
var numb = mult.getParameter2();
if (numb.equals(new Number(root, 1))) {
isExecutable = true;
}
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var a = null;
var aFound = false;
var mult = f;
if (aFound == false & ScriptUtils.instanceOf(mult.getParameter1(), Number.class)) {
var numb = mult.getParameter1();
if (numb.equals(new Number(root, 1))) {
a = mult.getParameter2();
aFound = true;
}
}
if (aFound == false && ScriptUtils.instanceOf(mult.getParameter2(), Number.class)) {
var numb = mult.getParameter2();
if (numb.equals(new Number(root, 1))) {
a = mult.getParameter1();
aFound = true;
}
}
result.add(a);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,80 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Negative = org.warp.picalculator.math.functions.Negative;
var Number = org.warp.picalculator.math.functions.Number;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var Sum = org.warp.picalculator.math.functions.Sum;
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Number rule
* a - a = 0
* -a + a = 0
* a ± a = {0, 2a}
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "NumberRule3";
},
// 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, Subtraction.class)) {
var sub = f;
if (sub.getParameter1().equals(sub.getParameter2())) {
isExecutable = true;
}
} else if (ScriptUtils.instanceOf(f, Sum.class)) {
var sub = f;
if (ScriptUtils.instanceOf(sub.getParameter1(), Multiplication.class)) {
if (ScriptUtils.instanceOf(sub.getParameter1().getParameter1(), Number.class) && sub.getParameter1().getParameter1().equals(new Number(f.getMathContext(), -1))) {
var neg = sub.getParameter1().getParameter2();
if (neg.equals(sub.getParameter2())) {
isExecutable = true;
}
}
}
} else if (ScriptUtils.instanceOf(f, SumSubtraction.class)) {
var sub = f;
if (sub.getParameter1().equals(sub.getParameter2())) {
isExecutable = true;
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
if (ScriptUtils.instanceOf(f, SumSubtraction.class)) {
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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,55 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var Sum = org.warp.picalculator.math.functions.Sum;
var SumSubtraction = org.warp.picalculator.math.functions.SumSubtraction;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Number rule
* a ± b = {a+b, a-b}
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "NumberRule4";
},
// 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, SumSubtraction.class)) {
isExecutable = true;
}
if (isExecutable) {
var root = f.getMathContext();
var 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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,71 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var FunctionOperator = org.warp.picalculator.math.FunctionOperator;
var MathContext = org.warp.picalculator.math.MathContext;
var Number = org.warp.picalculator.math.functions.Number;
var ObjectArrayList = Java.type("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
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "NumberRule5";
},
// 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, Sum.class) || ScriptUtils.instanceOf(f, Subtraction.class) || ScriptUtils.instanceOf(f, SumSubtraction.class)) {
var root = f.getMathContext();
var 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)) && ScriptUtils.instanceOf(f, SumSubtraction.class))) {
isExecutable = true;
}
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var fnc = f;
var a = fnc.getParameter1();
if (a.equals(new Number(root, 0))) {
if (ScriptUtils.instanceOf(f, Subtraction.class)) {
a = new Multiplication(root, new Number(root, -1), fnc.getParameter2());
} else {
a = fnc.getParameter2();
}
}
result.add(a);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,54 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Sum = org.warp.picalculator.math.functions.Sum;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Number rule
* a + a = 2a
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "NumberRule7";
},
// 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, Sum.class)) {
isExecutable = f.getParameter1().equals(f.getParameter2());
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
var mult = new Multiplication(root, new Number(root, 2), f.getParameter1());
result.add(mult);
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,57 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var MathContext = org.warp.picalculator.math.MathContext;
var Number = org.warp.picalculator.math.functions.Number;
var Power = org.warp.picalculator.math.functions.Power;
var Undefined = org.warp.picalculator.math.functions.Undefined;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Undefined rule
* 0^0=undefined
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "UndefinedRule1";
},
// Rule type
getRuleType: function() {
return RuleType.EXISTENCE;
},
/* 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 root = f.getMathContext();
var 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();
var result = new ObjectArrayList();
result.add(new Undefined(root));
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,60 +0,0 @@
//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 Undefined = org.warp.picalculator.math.functions.Undefined;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Undefined rule
* a / 0 = undefined
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "UndefinedRule2";
},
// Rule type
getRuleType: function() {
return RuleType.EXISTENCE;
},
/* 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 root = f.getMathContext();
var fnc = f;
if (ScriptUtils.instanceOf(fnc.getParameter2(), Number.class)) {
var numb = fnc.getParameter2();
if (numb.equals(new Number(root, 0))) {
isExecutable = true;
}
}
}
if (isExecutable) {
var root = f.getMathContext();
var result = new ObjectArrayList();
result.add(new Undefined(root));
return result;
} else {
return null;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,83 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var FunctionOperator = org.warp.picalculator.math.FunctionOperator;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var Sum = org.warp.picalculator.math.functions.Sum;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Variable rule
* ax+bx=(a+b)*x (a,b NUMBER; x VARIABLE|MULTIPLICATION)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "VariableRule1";
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
},
/* 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 fnc = f;
if (ScriptUtils.instanceOf(f, Subtraction.class) || ScriptUtils.instanceOf(f, Sum.class)) {
if (ScriptUtils.instanceOf(fnc.getParameter1(), Multiplication.class) & ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class)) {
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();
var 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 (ScriptUtils.instanceOf(fnc, Sum.class)) {
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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,72 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var FunctionOperator = org.warp.picalculator.math.FunctionOperator;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var Sum = org.warp.picalculator.math.functions.Sum;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Variable rule
* ax+x=(a+1)*x (a,b NUMBER; x VARIABLES)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "VariableRule2";
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
},
/* 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 fnc = f;
if (ScriptUtils.instanceOf(f, Sum.class) || ScriptUtils.instanceOf(f, Subtraction.class)) {
if (ScriptUtils.instanceOf(fnc.getParameter1(), Multiplication.class)) {
var m1 = fnc.getParameter1();
if (m1.getParameter2().equals(fnc.getParameter2())) {
isExecutable = true;
}
}
}
if (isExecutable) {
var root = fnc.getMathContext();
var result = new ObjectArrayList();
var m1 = fnc.getParameter1();
var a = m1.getParameter1();
var x = fnc.getParameter2();
var rets;
if (ScriptUtils.instanceOf(fnc, Sum.class)) {
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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,73 +0,0 @@
//Imports
var Error = org.warp.picalculator.Error;
var Function = org.warp.picalculator.math.Function;
var FunctionOperator = org.warp.picalculator.math.FunctionOperator;
var MathContext = org.warp.picalculator.math.MathContext;
var Multiplication = org.warp.picalculator.math.functions.Multiplication;
var Number = org.warp.picalculator.math.functions.Number;
var Subtraction = org.warp.picalculator.math.functions.Subtraction;
var Sum = org.warp.picalculator.math.functions.Sum;
var ObjectArrayList = Java.type("it.unimi.dsi.fastutil.objects.ObjectArrayList");
/**
* Variable rule
* x+ax=(a+1)*x (a,b NUMBER; x VARIABLES)
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "VariableRule3";
},
// Rule type
getRuleType: function() {
return RuleType.REDUCTION;
},
/* 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 fnc = f;
if (ScriptUtils.instanceOf(f, Sum.class) || ScriptUtils.instanceOf(f, Subtraction.class)) {
if (ScriptUtils.instanceOf(fnc.getParameter2(), Multiplication.class)) {
var m2 = fnc.getParameter2();
if (m2.getParameter2().equals(fnc.getParameter1())) {
isExecutable = true;
}
}
}
if (isExecutable) {
var root = fnc.getMathContext();
var result = new ObjectArrayList();
var m2 = fnc.getParameter2();
var a = m2.getParameter1();
var x = fnc.getParameter1();
var rets;
if (ScriptUtils.instanceOf(fnc, Sum.class)) {
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;
}
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,108 +0,0 @@
// Imports
var Rule = org.warp.picalculator.math.rules.Rule;
var RuleType = org.warp.picalculator.math.rules.RuleType;
var RulesManager = org.warp.picalculator.math.rules.RulesManager;
// Rule class
var rule = {
// Rule name
getRuleName: function() {
return "ExampleRule1";
},
// Rule type
getRuleType: function() {
return RuleType.CALCULATION;
},
/* Rule function
Returns:
- null if it's not executable on the function "func"
- An ObjectArrayList<Function> if it did something
*/
execute: function(func) {
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));
CONVERSIONI:
inserire all'inizio:
//Imports
------------------
^package [^\n]+?;$
------------------
^import ((?:org|com|net)[^\n]+\.([^\n]+?));$
var $2 = $1;
------------------
^import ([^\n]+\.([^\n]+?));$
var $2 = Java.type("$1");
------------------
<br>|<b>|<\/b>
------------------
(static boolean compare\([\s\S]*?)(?:|\r\n)\treturn false;([\t\r\n}]+?public static)
$1$2
VVVV--------------
(static boolean compare\([\s\S]*?)return ([^;]+)([\s\S]+?public static)
$1isExecutable = $2$3
------------------
(?:|public)(?:|private)(?:| final) class ([^\n]+?)[ ]*\{
var rule = {\r\n\t// Rule name\r\n\tgetRuleName: function() {\r\n\t\treturn "$1";\r\n\t},\r\n\t// Rule type\r\n\tgetRuleType: function() {\r\n\t\treturn RuleType./* TODO: RULE TYPE */;\r\n\t},\r\n\t/* Rule function\r\n\t Returns:\r\n\t - null if it's not executable on the function "f"\r\n\t - An ObjectArrayList<Function> if it did something\r\n\t*/
------------------
(?:|\r\n)\tpublic static boolean compare\(Function f\)[ ]*?\{
\texecute: function(f) {\r\n\t\tvar isExecutable;
------------------
(if \(|= |while \(|return |& |\| )([^ \n\r;]+?) instanceof ([^ \n\r;)]+)
$1ScriptUtils.instanceOf($2, $3.class)
------------------
\t}[\r\n]*?\tpublic static ObjectArrayList<Function> execute\(Function f\) throws Error \{
\t\tif (isExecutable) {
------------------
aggiungere tab
------------------
inserire in fondo al codice
} else {
return null;
}
-----------------
\([A-Z][a-z]*?\)( |)
-----------------
([^.])(?:final )
$1
-----------------
[A-Z][A-z<>?]*? ([A-z]+?)(?: |)=(?: |)
var $1 =
-----------------
(new [A-Z][A-z]+?)<[^;()+\-*\/]*?>
$1
-----------------
inserire in fondo:
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));
-----------------

View File

@ -1,76 +0,0 @@
// 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)));
return result;
}
}
} else {
//divide a by b (a/b = c)
result.add(variable1.divide(variable2));
return result;
}
}
}
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,35 +0,0 @@
// 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;
/**
* EmptyNumber
*
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "EmptyNumber";
},
// 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) {
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,46 +0,0 @@
// 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 Expression = org.warp.picalculator.math.functions.Expression;
/**
* Expression
* (x) = x
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Expression";
},
// 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, Expression.class)) {
var result = new ObjectArrayList();
result.add(f.getParameter(0));
return result;
}
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,35 +0,0 @@
// 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;
/**
* Joke
*
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Joke";
},
// 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) {
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

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

View File

@ -1,62 +0,0 @@
// 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 Number = org.warp.picalculator.math.functions.Number;
var Negative = org.warp.picalculator.math.functions.Negative;
var Error = org.warp.picalculator.Error;
var Errors = org.warp.picalculator.Errors;
var NullPointerException = java.lang.NullPointerException;
var NumberFormatException = java.lang.NumberFormatException;
var ArithmeticException = java.lang.ArithmeticException;
/**
* Negative
* -a = b
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Negative";
},
// 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, Negative.class)) {
var result = new ObjectArrayList();
var variable = f.getParameter();
var mathContext = f.getMathContext();
if (ScriptUtils.instanceOf(variable, Number.class)) {
//-a = a*-1 = b
try {
result.add(variable.multiply(new Number(mathContext, -1)));
} catch (ex) {
if (ScriptUtils.instanceOf(ex, NullPointerException.class)) {
throw new Error(Errors.ERROR);
} else if (ScriptUtils.instanceOf(ex, NumberFormatException.class)) {
throw new Error(Errors.SYNTAX_ERROR);
} else if (ScriptUtils.instanceOf(ex, ArithmeticException.class)) {
throw new Error(Errors.NUMBER_TOO_SMALL);
}
}
return result;
}
}
return null;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,49 +0,0 @@
// 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<Function> 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));

View File

@ -1,53 +0,0 @@
// 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<Function> 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));

View File

@ -1,78 +0,0 @@
// 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 Root = org.warp.picalculator.math.functions.Root;
var RootSquare = org.warp.picalculator.math.functions.RootSquare;
var BigDecimal = java.math.BigDecimal;
/**
* Root
* ab = c
*
* @author Andrea Cavalli
*
*/
var rule = {
// Rule name
getRuleName: function() {
return "Root";
},
// 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 isSquare = false;
if ((isSquare = ScriptUtils.instanceOf(f, RootSquare.class)) || ScriptUtils.instanceOf(f, Root.class)) {
var result = new ObjectArrayList();
var mathContext = f.getMathContext();
var variable1 = f.getParameter1();
var variable2 = f.getParameter2();
var isSolvable = false;
var canBePorted = false;
if (ScriptUtils.instanceOf(variable1, Number.class) && ScriptUtils.instanceOf(variable2, Number.class)) {
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 && ScriptUtils.instanceOf(variable1, Number.class) && 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;
}
}
//Add this rule to the list of rules
RulesManager.addRule(engine.getInterface(rule, Rule.class));

View File

@ -1,52 +0,0 @@
// 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

@ -1,52 +0,0 @@
// 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

@ -1,53 +0,0 @@
// 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

@ -1,55 +0,0 @@
// 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));