Fix incorrect rules and port them to the DSL

This commit is contained in:
Riccardo Azzolini 2019-02-11 12:21:43 +01:00
parent b9c025d74c
commit 2e36bc83bf
5 changed files with 27 additions and 202 deletions

View File

@ -1,2 +0,0 @@
ExpandRule1
ExponentRule17

View File

@ -1,3 +1,27 @@
expansion ExpandRule1a:
-1 * (a + b) -> -1*a - b
expansion ExpandRule1b:
-1 * (a - b) -> -1*a + b
expansion ExpandRule1c:
-1 * (a +- b) -> [-1*a + b, -1*a - b]
expansion ExpandRule1d:
x - (a + b) -> x + (-1*a - b)
expansion ExpandRule1e:
x +- (a + b) -> [x + (a + b), x + (-1*a - b)]
expansion ExpandRule1f:
x - (a - b) -> x + (-1*a + b)
expansion ExpandRule1g:
x +- (a - b) -> [x + (a - b), x + (-1*a + b)]
expansion ExpandRule1h:
x - (a +- b) -> [x + (-1*a + b), x + (-1*a - b)]
expansion ExpandRule1i:
x +- (a +- b) -> [
x + (a + b),
x + (a - b),
x + (-1*a + b),
x + (-1*a - b),
]
expansion ExpandRule2a:
a * (b + c) -> a*b + a*c
expansion ExpandRule2b:

View File

@ -25,3 +25,6 @@ reduction ExponentRule16b:
a^b * a -> a ^ (b + 1)
reduction ExponentRule16c:
a * a^b -> a ^ (1 + b)
expansion ExponentRule17:
root(a, x) -> x ^ (1 / a)

View File

@ -1,136 +0,0 @@
package rules;
/*
SETTINGS: (please don't move this part)
PATH=ExpandRule1
*/
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Multiplication;
import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.functions.Subtraction;
import it.cavallium.warppi.math.functions.Sum;
import it.cavallium.warppi.math.functions.SumSubtraction;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.math.rules.RuleType;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Expand rule
* -(+a+b) = -a-b
* -(+a-b) = -a+b
*
* @author Andrea Cavalli
*
*/
public class ExpandRule1 implements Rule {
// Rule name
@Override
public String getRuleName() {
return "ExpandRule1";
}
// 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(final Function f) {
boolean isExecutable = false;
if (f instanceof Multiplication) {
final Multiplication fnc = (Multiplication) f;
if (fnc.getParameter1().equals(new Number(fnc.getMathContext(), -1))) {
final Function expr = fnc.getParameter2();
if (expr instanceof Sum)
isExecutable = true; // -1 * (a + b)
else if (expr instanceof Subtraction)
isExecutable = true; // -1 * (a - b)
else if (expr instanceof SumSubtraction)
isExecutable = true; // -1 * (a +- b)
}
} else if (f instanceof Subtraction || f instanceof SumSubtraction) {
final FunctionOperator fnc = (FunctionOperator) f;
final Function expr = fnc.getParameter2();
if (expr instanceof Sum)
isExecutable = true; // x [- +-] (a + b)
else if (expr instanceof Subtraction)
isExecutable = true; // x [- +-] (a - b)
else if (expr instanceof SumSubtraction)
isExecutable = true; // x [- +-] (a +- b)
}
if (isExecutable) {
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final MathContext root = f.getMathContext();
Function expr = null; // f.getParameter1() [* - +-] expr
int fromSubtraction = 0; // 0: (*), 1: (-), 2: (+-)
FunctionOperator subtraction = null;
if (f instanceof Multiplication)
expr = ((Multiplication) f).getParameter2();
else if (f instanceof Subtraction || f instanceof SumSubtraction) {
expr = ((FunctionOperator) f).getParameter2();
if (f instanceof Subtraction)
fromSubtraction = 1;
else
fromSubtraction = 2;
}
if (f instanceof SumSubtraction) {
}
final Function fnc = expr;
if (fnc instanceof Sum) {
final Function a = ((Sum) fnc).getParameter1();
final Function b = ((Sum) fnc).getParameter2();
final Function fnc2 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b);
if (fromSubtraction > 0) {
// FIXME SumSubtraction treated as just Subtraction
subtraction = new Sum(root, ((FunctionOperator) f).getParameter1(), fnc2);
result.add(subtraction); // x [- +-] (a + b) -> x + (-1*a - b)
} else
result.add(fnc2); // -1 * (a + b) -> -1*a - b
} else if (fnc instanceof Subtraction) {
final Function a = ((Subtraction) fnc).getParameter1();
final Function b = ((Subtraction) fnc).getParameter2();
final Function fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b);
if (fromSubtraction > 0) {
// FIXME SumSubtraction treated as just Subtraction
subtraction = new Sum(root, ((FunctionOperator) f).getParameter1(), fnc2);
result.add(subtraction); // x [- +-] (a - b) -> x + (-1*a + b)
} else
result.add(fnc2); // -1 * (a - b) -> -1*a + b
} else if (fnc instanceof SumSubtraction) { // FIXME Subtraction and SumSubtraction confusion
final Function a = ((SumSubtraction) fnc).getParameter1();
final Function b = ((SumSubtraction) fnc).getParameter2();
final Function fnc2 = new Sum(root, new Multiplication(root, new Number(root, -1), a), b); // -1*a + b
final Function fnc3 = new Subtraction(root, new Multiplication(root, new Number(root, -1), a), b); // -1*a - b
if (fromSubtraction > 0) {
// x [- +-] (a +- b) -> [x +- (-1*a + b), x +- (-1*a - b), x +- (-1*a - b)]
subtraction = new SumSubtraction(root, ((FunctionOperator) f).getParameter1(), fnc2);
result.add(subtraction);
subtraction = new SumSubtraction(root, ((FunctionOperator) f).getParameter1(), fnc3);
// FIXME same result twice
result.add(subtraction);
result.add(subtraction);
} else {
// -1 * (a +- b) -> [-1*a + b, -1*a + b]
// FIXME same result twice
result.add(fnc2);
result.add(fnc2);
}
}
return result;
} else
return null;
}
}

View File

@ -1,64 +0,0 @@
package rules;
/*
SETTINGS: (please don't move this part)
PATH=ExponentRule17
*/
import it.cavallium.warppi.math.Function;
import it.cavallium.warppi.math.FunctionOperator;
import it.cavallium.warppi.math.MathContext;
import it.cavallium.warppi.math.functions.Number;
import it.cavallium.warppi.math.functions.Power;
import it.cavallium.warppi.math.functions.Root;
import it.cavallium.warppi.math.rules.Rule;
import it.cavallium.warppi.math.rules.RuleType;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/**
* Exponent rule
* ax=x^1/a
*
* @author Andrea Cavalli
*
*/
public class ExponentRule17 implements Rule {
// Rule name
@Override
public String getRuleName() {
return "ExponentRule17";
}
// 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(final Function f) { // FIXME incorrect rule
boolean isExecutable = false;
if (f instanceof Root) {
final FunctionOperator fnc = (FunctionOperator) f;
if (fnc.getParameter1().equals(fnc.getParameter2()))
isExecutable = true; // root(a, a)
}
if (isExecutable) {
final MathContext root = f.getMathContext();
final ObjectArrayList<Function> result = new ObjectArrayList<>();
final FunctionOperator fnc = (FunctionOperator) f;
final Function a = fnc.getParameter1();
final Function two = new Number(root, 2);
final Function p = new Power(root, a, two);
result.add(p);
return result; // root(a, a) -> a^2
} else
return null;
}
}