diff --git a/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 8f0441c..223d7aa 100644 --- a/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -70,7 +70,7 @@ public abstract class Plugin { * To be used in load(). Registers a tree value function abstract class * with the plugin internally, which makes it accessible to the plugin manager. * - * @param name the name to register by. + * @param name the name to register by. * @param toRegister the tree value function implementation. */ protected final void registerTreeValueFunction(String name, TreeValueFunction toRegister) { @@ -94,10 +94,10 @@ public abstract class Plugin { * with the plugin internally, which makes it accessible * to the plugin manager. * - * @param name the name of the tree value operator. + * @param name the name of the tree value operator. * @param operator the tree value operator to register. */ - protected final void registerTreeValueOperator(String name, TreeValueOperator operator){ + protected final void registerTreeValueOperator(String name, TreeValueOperator operator) { manager.registerTreeValueOperator(name, operator); } @@ -142,7 +142,7 @@ public abstract class Plugin { * @param name the name for which to search. * @return the resulting tree value function, or null if none was found for that name. */ - protected final TreeValueFunction treeValueFunctionFor(String name){ + protected final TreeValueFunction treeValueFunctionFor(String name) { return manager.treeValueFunctionFor(name); } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 90439fa..64b3eb3 100644 --- a/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -100,7 +100,7 @@ public class PluginManager { /** * Registers a tree value function under the given name. * - * @param name the name of the function. + * @param name the name of the function. * @param function the function to register. */ public void registerTreeValueFunction(String name, TreeValueFunction function) { @@ -120,10 +120,10 @@ public class PluginManager { /** * Registers a tree value operator under the given name. * - * @param name the name of the tree value operator. + * @param name the name of the tree value operator. * @param operator the tree value operator to register. */ - public void registerTreeValueOperator(String name, TreeValueOperator operator){ + public void registerTreeValueOperator(String name, TreeValueOperator operator) { registeredTreeValueOperators.put(name, operator); } @@ -163,7 +163,7 @@ public class PluginManager { * @param name the name of the function. * @return the function, or null if it was not found. */ - public TreeValueFunction treeValueFunctionFor(String name){ + public TreeValueFunction treeValueFunctionFor(String name) { return registeredTreeValueFunctions.get(name); } @@ -374,7 +374,7 @@ public class PluginManager { * * @return the set of all tree value operators that were loaded. */ - public Set getAllTreeValueOperators(){ + public Set getAllTreeValueOperators() { return registeredTreeValueOperators.keySet(); } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 1e0ca6f..3ba9d76 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -80,21 +80,6 @@ public class StandardPlugin extends Plugin { return product; } }; - /** - * The combination operator. - */ - public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { - @Override - public boolean matchesParams(NumberInterface[] params) { - return params.length == 2 && params[0].fractionalPart().signum() == 0 - && params[1].fractionalPart().signum() == 0; - } - - @Override - public NumberInterface applyInternal(NumberInterface[] params) { - return OP_NPR.apply(params).divide(OP_FACTORIAL.apply(params[1])); - } - }; /** * The implementation for double-based naive numbers. */ @@ -109,6 +94,20 @@ public class StandardPlugin extends Plugin { return new NaiveNumber(Math.PI); } }; + /** + * The square root function. + */ + public static final NumberFunction FUNCTION_SQRT = new NumberFunction() { + @Override + public boolean matchesParams(NumberInterface[] params) { + return params.length == 1; + } + + @Override + public NumberInterface applyInternal(NumberInterface[] params) { + return OP_CARET.apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); + } + }; /** * The implementation for the infinite-precision BigDecimal. */ @@ -225,6 +224,21 @@ public class StandardPlugin extends Plugin { return total; } }; + /** + * The combination operator. + */ + public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { + @Override + public boolean matchesParams(NumberInterface[] params) { + return params.length == 2 && params[0].fractionalPart().signum() == 0 + && params[1].fractionalPart().signum() == 0; + } + + @Override + public NumberInterface applyInternal(NumberInterface[] params) { + return OP_NPR.apply(params).divide(OP_FACTORIAL.apply(params[1])); + } + }; /** * The absolute value function, abs(-3) = 3 */ @@ -333,50 +347,6 @@ public class StandardPlugin extends Plugin { return fromInt(params[0].getClass(), (int) Math.round(Math.random() * params[0].floor().intValue())); } }; - /** - * The caret / pow operator, ^ - */ - public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { - @Override - public boolean matchesParams(NumberInterface[] params) { - NumberInterface zero = fromInt(params[0].getClass(), 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(NumberInterface[] params) { - NumberInterface zero = fromInt(params[0].getClass(), 0); - if (params[0].compareTo(zero) == 0) - return zero; - else if (params[1].compareTo(zero) == 0) - return fromInt(params[0].getClass(), 1); - //Detect integer bases: - if (params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0 - && FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0 - && FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0) { - NumberInterface[] newParams = {params[0], params[1].fractionalPart()}; - return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams)); - } - return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1])); - } - }; - /** - * The square root function. - */ - public static final NumberFunction FUNCTION_SQRT = new NumberFunction() { - @Override - public boolean matchesParams(NumberInterface[] params) { - return params.length == 1; - } - - @Override - public NumberInterface applyInternal(NumberInterface[] params) { - return OP_CARET.apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); - } - }; private static final HashMap, ArrayList> FACTORIAL_LISTS = new HashMap<>(); /** * The exponential function, exp(1) = e^1 = 2.71... @@ -415,6 +385,36 @@ public class StandardPlugin extends Plugin { } } }; + /** + * The caret / pow operator, ^ + */ + public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { + @Override + public boolean matchesParams(NumberInterface[] params) { + NumberInterface zero = fromInt(params[0].getClass(), 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(NumberInterface[] params) { + NumberInterface zero = fromInt(params[0].getClass(), 0); + if (params[0].compareTo(zero) == 0) + return zero; + else if (params[1].compareTo(zero) == 0) + return fromInt(params[0].getClass(), 1); + //Detect integer bases: + if (params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0 + && FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0 + && FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0) { + NumberInterface[] newParams = {params[0], params[1].fractionalPart()}; + return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams)); + } + return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1])); + } + }; /** * The sine function (the argument is interpreted in radians). */ diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt b/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt index 31f1028..754589b 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/Operator.kt @@ -10,4 +10,4 @@ package org.nwapw.abacus.function * @param precedence the precedence of this operator, used for order of operations. */ open class Operator(val associativity: OperatorAssociativity, val type: OperatorType, - val precedence: Int) \ No newline at end of file + val precedence: Int) \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt index 4988c2a..5e219c1 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt @@ -8,7 +8,7 @@ package org.nwapw.abacus.function.applicable * @param the type of the parameters passed to this applicable. * @param the return type of the applicable. */ -interface Applicable { +interface Applicable { /** * Checks if the given applicable can be used with the given parameters. @@ -16,6 +16,7 @@ interface Applicable { * @return whether the array can be used with applyInternal. */ fun matchesParams(params: Array): Boolean + /** * Applies the applicable object to the given parameters, * without checking for compatibility. @@ -32,7 +33,7 @@ interface Applicable { * @return the result of the operation, or null if parameters do not match. */ fun apply(vararg params: T): O? { - if(!matchesParams(params)) return null + if (!matchesParams(params)) return null return applyInternal(params) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt index f1a3036..6cef5e1 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/ReducerApplicable.kt @@ -11,7 +11,7 @@ import org.nwapw.abacus.tree.Reducer * @param the return type of the application. * @param the required type of the reducer. */ -interface ReducerApplicable { +interface ReducerApplicable { /** * Checks if this applicable can be applied to the @@ -19,6 +19,7 @@ interface ReducerApplicable { * @param params the parameters to check. */ fun matchesParams(params: Array): Boolean + /** * Applies this applicable to the given arguments, and reducer. * @param reducer the reducer to use in the application. @@ -26,6 +27,7 @@ interface ReducerApplicable { * @return the result of the application. */ fun applyWithReducerInternal(reducer: Reducer, params: Array): O? + /** * Applies this applicable to the given arguments, and reducer, * if the arguments and reducer are compatible with this applicable. @@ -34,7 +36,7 @@ interface ReducerApplicable { * @return the result of the application, or null if the arguments are incompatible. */ fun applyWithReducer(reducer: Reducer, vararg params: T): O? { - if(!matchesParams(params)) return null + if (!matchesParams(params)) return null return applyWithReducerInternal(reducer, params) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt index 5c059ae..5f99494 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/CallNode.kt @@ -19,9 +19,9 @@ abstract class CallNode(val callTo: String) : TreeNode() { val buffer = StringBuffer() buffer.append(callTo) buffer.append("(") - for(i in 0 until children.size){ + for (i in 0 until children.size) { buffer.append(children[i].toString()) - buffer.append(if(i != children.size - 1) ", " else ")") + buffer.append(if (i != children.size - 1) ", " else ")") } return buffer.toString() } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt index 624a50a..3507568 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberNode.kt @@ -1,7 +1,5 @@ package org.nwapw.abacus.tree -import org.nwapw.abacus.number.NumberInterface - /** * A tree node that holds a single number value. *