Added ExponentRule8 and fixed results with multiple expressions

This commit is contained in:
Andrea Cavalli 2018-05-26 20:19:53 +02:00
parent 5d585a9ad6
commit 773f2c14bf
6 changed files with 112 additions and 11 deletions

View File

@ -11,7 +11,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**|rules/|rules/" kind="src" output="target/classes" path="src/main/resources">
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

Binary file not shown.

View File

@ -42,11 +42,23 @@ public class MathSolver {
int initStepState = 0, endStepState = 0;
final AtomicInteger stepState = new AtomicInteger(0);
do {
lastFunctions[1] = lastFunctions[0];
lastFunctions[0] = new ObjectArrayList[stepStates.length];
final ObjectArrayList<Function>[] currFncHistory = new ObjectArrayList[stepStates.length];
final String stepName = "Step " + stepNumber;
for (int i = initStepState; i <= endStepState; i++) {
lastFunctions[0][i] = currFnc;
if (initStepState > endStepState) {
for (int i = initStepState; i < stepStates.length; i++) {
currFncHistory[i] = currFnc;
}
for (int i = 0; i <= initStepState; i++) {
currFncHistory[i] = currFnc;
}
} else {
for (int i = initStepState; i <= endStepState; i++) {
currFncHistory[i] = currFnc;
}
}
if (currFnc != null) {
lastFunctions[1] = lastFunctions[0];
lastFunctions[0] = currFncHistory;
}
lastFnc = currFnc;
initStepState = stepState.get();
@ -200,24 +212,41 @@ public class MathSolver {
throws InterruptedException, Error {
final ObjectArrayList<Rule> rules = initialFunction.getMathContext().getAcceptableRules(currentAcceptedRules);
ObjectArrayList<Function> results = null;
Rule appliedRule = null;
ObjectArrayList<Rule> appliedRules = new ObjectArrayList<>();
out: {
for (final Function fnc : fncs) {
boolean didSomething = false;
for (final Rule rule : rules) {
final List<Function> ruleResults = fnc.simplify(rule);
if (ruleResults != null && !ruleResults.isEmpty()) {
List<Function> ruleResults = fnc.simplify(rule);
if ((ruleResults != null && !ruleResults.isEmpty())) {
if (results == null) {
results = new ObjectArrayList<>();
}
results.addAll(ruleResults);
appliedRule = rule;
appliedRules.add(rule);
didSomething = true;
break;
}
}
if (!didSomething && fncs.size() > 1) {
if (results == null) {
results = new ObjectArrayList<>();
}
results.add(fnc);
}
}
}
if (StaticVars.debugOn & results != null & appliedRule != null) {
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", currentAcceptedRules.toString(), "Applied rule " + appliedRule.getRuleName());
if (appliedRules.isEmpty()) results = null;
if (StaticVars.debugOn & results != null && !appliedRules.isEmpty()) {
StringBuilder rulesStr = new StringBuilder();
appliedRules.forEach((r) -> {
rulesStr.append(r.getRuleName());
rulesStr.append(',');
});
if (rulesStr.length() > 0) {
rulesStr.setLength(rulesStr.length() - 1);
}
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_VERBOSE, "Math Solver", currentAcceptedRules.toString(), "Applied rules: " + rulesStr);
}
return results;
}

View File

@ -18,6 +18,7 @@ ExponentRule1
ExponentRule2
ExponentRule3
ExponentRule4
ExponentRule8
ExponentRule9
ExponentRule15
ExponentRule16

View File

@ -0,0 +1,70 @@
/*
SETTINGS: (please don't move this part)
PATH=ExponentRule8
*/
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionOperator;
import org.warp.picalculator.math.FunctionDynamic;
import org.warp.picalculator.math.FunctionSingle;
import org.warp.picalculator.math.MathContext;
//Imports
import org.warp.picalculator.Error;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Power;
import org.warp.picalculator.math.functions.Sum;
import org.warp.picalculator.math.rules.Rule;
import org.warp.picalculator.math.rules.RuleType;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule
* (a) ^ (b+c) = a ^ b + a ^ c
*
* @author Andrea Cavalli
*
*/
public class ExponentRule8 implements Rule {
// Rule name
@Override
public String getRuleName() {
return "ExponentRule8";
}
// Rule type
@Override
public RuleType getRuleType() {
return RuleType.EXPANSION;
}
/* Rule function
Returns:
- null if it's not executable on the function "f"
- An ObjectArrayList<Function> if it did something
*/
@Override
public ObjectArrayList<Function> execute(Function f) {
boolean isExecutable = false;
if (f instanceof Power) {
FunctionOperator fnc = (FunctionOperator) f;
if (fnc.getParameter2() instanceof Sum) {
Sum sum = (Sum) fnc.getParameter2();
MathContext root = f.getMathContext();
ObjectArrayList<Function> result = new ObjectArrayList<>();
Function a = fnc.getParameter1();
Function b = sum.getParameter1();
Function c = sum.getParameter2();
result.add(new Multiplication(root, new Power(root, a, b), new Power(root, a, c)));
return result;
}
}
return null;
}
}

View File

@ -22,6 +22,7 @@ import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Sum;
import org.warp.picalculator.math.functions.Subtraction;
import org.warp.picalculator.math.functions.Variable;
import org.warp.picalculator.math.functions.Variable.V_TYPE;
import org.warp.picalculator.math.functions.Number;
/**