1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 07:20:09 -08:00

Move some more operators out of StandardPlugin.java to separate classes

This commit is contained in:
Danila Fedorin 2017-09-23 17:11:05 -07:00
parent 579ff78a99
commit f809183126
8 changed files with 179 additions and 100 deletions

View File

@ -6,8 +6,6 @@ import org.nwapw.abacus.number.NaiveNumber;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.number.PreciseNumber; import org.nwapw.abacus.number.PreciseNumber;
import org.nwapw.abacus.plugin.standard.*; import org.nwapw.abacus.plugin.standard.*;
import org.nwapw.abacus.tree.TreeNode;
import org.nwapw.abacus.tree.VariableNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -21,36 +19,11 @@ public class StandardPlugin extends Plugin {
/** /**
* The set operator. * The set operator.
*/ */
public final TreeValueOperator opSet = new TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { public static final TreeValueOperator OP_SET = new OperatorSet();
@Override
public boolean matchesParams(MutableEvaluationContext context, TreeNode[] params) {
return params.length == 2 && params[0] instanceof VariableNode;
}
@Override
public NumberInterface applyInternal(MutableEvaluationContext context, TreeNode[] params) {
String assignTo = ((VariableNode) params[0]).getVariable();
NumberInterface value = params[1].reduce(context.getInheritedReducer());
context.setVariable(assignTo, value);
return value;
}
};
/** /**
* The define operator. * The define operator.
*/ */
public final TreeValueOperator opDefine = new TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { public final TreeValueOperator OP_DEFINE = new OperatorDefine();
@Override
public boolean matchesParams(MutableEvaluationContext context, TreeNode[] params) {
return params.length == 2 && params[0] instanceof VariableNode;
}
@Override
public NumberInterface applyInternal(MutableEvaluationContext context, TreeNode[] params) {
String assignTo = ((VariableNode) params[0]).getVariable();
context.setDefinition(assignTo, params[1]);
return params[1].reduce(context.getInheritedReducer());
}
};
/** /**
* The addition operator, + * The addition operator, +
*/ */
@ -129,49 +102,11 @@ public class StandardPlugin extends Plugin {
/** /**
* The permutation operator. * The permutation operator.
*/ */
public static final NumberOperator OP_NPR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { public static final NumberOperator OP_NPR = new OperatorNpr();
@Override
public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) {
return params.length == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0;
}
@Override
public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) {
NumberImplementation implementation = context.getInheritedNumberImplementation();
if (params[0].compareTo(params[1]) < 0 ||
params[0].signum() < 0 ||
(params[0].signum() == 0 && params[1].signum() != 0)) return implementation.instanceForString("0");
NumberInterface total = implementation.instanceForString("1");
NumberInterface multiplyBy = params[0];
NumberInterface remainingMultiplications = params[1];
NumberInterface halfway = params[0].divide(implementation.instanceForString("2"));
if (remainingMultiplications.compareTo(halfway) > 0) {
remainingMultiplications = params[0].subtract(remainingMultiplications);
}
while (remainingMultiplications.signum() > 0) {
total = total.multiply(multiplyBy);
remainingMultiplications = remainingMultiplications.subtract(implementation.instanceForString("1"));
multiplyBy = multiplyBy.subtract(implementation.instanceForString("1"));
}
return total;
}
};
/** /**
* The combination operator. * The combination operator.
*/ */
public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { public static final NumberOperator OP_NCR = new OperatorNcr();
@Override
public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) {
return params.length == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0;
}
@Override
public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) {
return OP_NPR.apply(context, params).divide(OP_FACTORIAL.apply(context, params[1]));
}
};
/** /**
* The absolute value function, abs(-3) = 3 * The absolute value function, abs(-3) = 3
*/ */
@ -284,34 +219,7 @@ public class StandardPlugin extends Plugin {
/** /**
* The caret / pow operator, ^ * The caret / pow operator, ^
*/ */
public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { public static final NumberOperator OP_CARET = new OperatorCaret();
@Override
public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) {
NumberInterface zero = context.getInheritedNumberImplementation().instanceForString("0");
return params.length == 2
&& !(params[0].compareTo(zero) == 0
&& params[1].compareTo(zero) == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(zero) != 0);
}
@Override
public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) {
NumberImplementation implementation = context.getInheritedNumberImplementation();
NumberInterface zero = implementation.instanceForString("0");
if (params[0].compareTo(zero) == 0)
return zero;
else if (params[1].compareTo(zero) == 0)
return implementation.instanceForString("1");
//Detect integer bases:
if (params[0].fractionalPart().compareTo(implementation.instanceForString("0")) == 0
&& FUNCTION_ABS.apply(context, params[1]).compareTo(implementation.instanceForString(Integer.toString(Integer.MAX_VALUE))) < 0
&& FUNCTION_ABS.apply(context, params[1]).compareTo(implementation.instanceForString("1")) >= 0) {
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(context, newParams));
}
return FUNCTION_EXP.apply(context, FUNCTION_LN.apply(context, FUNCTION_ABS.apply(context, params[0])).multiply(params[1]));
}
};
/** /**
* The square root function. * The square root function.
*/ */
@ -677,8 +585,8 @@ public class StandardPlugin extends Plugin {
registerOperator("^", OP_CARET); registerOperator("^", OP_CARET);
registerOperator("!", OP_FACTORIAL); registerOperator("!", OP_FACTORIAL);
registerTreeValueOperator("=", opSet); registerTreeValueOperator("=", OP_SET);
registerTreeValueOperator(":=", opDefine); registerTreeValueOperator(":=", OP_DEFINE);
registerOperator("nPr", OP_NPR); registerOperator("nPr", OP_NPR);
registerOperator("nCr", OP_NCR); registerOperator("nCr", OP_NCR);

View File

@ -0,0 +1,38 @@
package org.nwapw.abacus.plugin.standard
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.function.NumberOperator
import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.plugin.StandardPlugin.*
/**
* The power operator.
*
* This is a standard operator that brings one number to the power of the other.
*/
class OperatorCaret: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params.size == 2
&& !(params[0].signum() == 0 && params[1].signum() == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().signum() != 0)
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>): NumberInterface {
val implementation = context.inheritedNumberImplementation
if (params[0].signum() == 0)
return implementation.instanceForString("0")
else if (params[1].signum() == 0)
return implementation.instanceForString("1")
//Detect integer bases:
if (params[0].fractionalPart().signum() == 0
&& FUNCTION_ABS.apply(context, params[1]) < implementation.instanceForString(Integer.toString(Integer.MAX_VALUE))
&& FUNCTION_ABS.apply(context, params[1]) >= implementation.instanceForString("1")) {
val newParams = arrayOf(params[0], params[1].fractionalPart())
return params[0].intPow(params[1].floor().intValue()) * applyInternal(context, newParams)
}
return FUNCTION_EXP.apply(context, FUNCTION_LN.apply(context, FUNCTION_ABS.apply(context, params[0])) * params[1])
}
}

View File

@ -0,0 +1,28 @@
package org.nwapw.abacus.plugin.standard
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.function.TreeValueOperator
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.tree.TreeNode
import org.nwapw.abacus.tree.VariableNode
/**
* The definition operator.
*
* This is a standard operator that creates a definition - something that doesn't capture variable values
* when it's created, but rather the variables themselves, and changes when the variables it uses change.
*/
class OperatorDefine: TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out TreeNode>) =
params.size == 2 && params[0] is VariableNode
override fun applyInternal(context: MutableEvaluationContext, params: Array<out TreeNode>): NumberInterface {
val assignTo = (params[0] as VariableNode).variable
context.setDefinition(assignTo, params[1])
return params[1].reduce(context.inheritedReducer)
}
}

View File

@ -6,11 +6,16 @@ import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.number.NumberInterface import org.nwapw.abacus.number.NumberInterface
/**
* The factorial operator.
*
* This is a standard operator that simply evaluates the factorial of a number.
*/
class OperatorFactorial: NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_POSTFIX, 0) { class OperatorFactorial: NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_POSTFIX, 0) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) = override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params.size == 1 params.size == 1
&& params[0].fractionalPart().compareTo(context.inheritedNumberImplementation.instanceForString("0")) == 0 && params[0].fractionalPart().signum() == 0
&& params[0].signum() >= 0 && params[0].signum() >= 0
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>): NumberInterface { override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>): NumberInterface {

View File

@ -0,0 +1,25 @@
package org.nwapw.abacus.plugin.standard
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.function.NumberOperator
import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.plugin.StandardPlugin.*
/**
* The "N choose R" operator.
*
* This is a standard operator that returns the number of possible combinations, regardless of order,
* of a certain size can be taken out of a pool of a bigger size.
*/
class OperatorNcr: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params.size == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
OP_NPR.apply(context, *params) / OP_FACTORIAL.apply(context, params[1])
}

View File

@ -6,6 +6,11 @@ import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.number.NumberInterface import org.nwapw.abacus.number.NumberInterface
/**
* The negation operator.
*
* This is a standard operator that negates a number.
*/
class OperatorNegate: NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) { class OperatorNegate: NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) = override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =

View File

@ -0,0 +1,42 @@
package org.nwapw.abacus.plugin.standard
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.function.NumberOperator
import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.number.NumberInterface
/**
* The "N pick R" operator.
*
* his is a standard operator that returns the number of possible combinations
* of a certain size can be taken out of a pool of a bigger size.
*/
class OperatorNpr: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params.size == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>): NumberInterface {
val implementation = context.inheritedNumberImplementation
if (params[0] < params[1] ||
params[0].signum() < 0 ||
params[0].signum() == 0 && params[1].signum() != 0)
return implementation.instanceForString("0")
var total = implementation.instanceForString("1")
var multiplyBy = params[0]
var remainingMultiplications = params[1]
val halfway = params[0] / implementation.instanceForString("2")
if (remainingMultiplications > halfway) {
remainingMultiplications = params[0] - remainingMultiplications
}
while (remainingMultiplications.signum() > 0) {
total *= multiplyBy
remainingMultiplications -= implementation.instanceForString("1")
multiplyBy -= implementation.instanceForString("1")
}
return total
}
}

View File

@ -0,0 +1,28 @@
package org.nwapw.abacus.plugin.standard
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.function.OperatorAssociativity
import org.nwapw.abacus.function.OperatorType
import org.nwapw.abacus.function.TreeValueOperator
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.tree.TreeNode
import org.nwapw.abacus.tree.VariableNode
/**
* The set operator.
*
* This is a standard operator that assigns a value to a variable name.
*/
class OperatorSet: TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
override fun matchesParams(context: MutableEvaluationContext, params: Array<out TreeNode>) =
params.size == 2 && params[0] is VariableNode
override fun applyInternal(context: MutableEvaluationContext, params: Array<out TreeNode>): NumberInterface {
val assignTo = (params[0] as VariableNode).variable
val value = params[1].reduce(context.inheritedReducer)
context.setVariable(assignTo, value)
return value
}
}