mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 07:20:09 -08:00
Format code.
This commit is contained in:
parent
e172108476
commit
6a65e66935
|
@ -1,9 +1,7 @@
|
||||||
package org.nwapw.abacus;
|
package org.nwapw.abacus;
|
||||||
|
|
||||||
import org.nwapw.abacus.config.Configuration;
|
import org.nwapw.abacus.config.Configuration;
|
||||||
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.PromotionManager;
|
import org.nwapw.abacus.number.PromotionManager;
|
||||||
import org.nwapw.abacus.parsing.LexerTokenizer;
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
||||||
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
||||||
|
@ -14,8 +12,6 @@ import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
import org.nwapw.abacus.tree.NumberReducer;
|
import org.nwapw.abacus.tree.NumberReducer;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main calculator class. This is responsible
|
* The main calculator class. This is responsible
|
||||||
* for piecing together all of the components, allowing
|
* for piecing together all of the components, allowing
|
||||||
|
@ -72,6 +68,7 @@ public class Abacus {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the promotion manager.
|
* Gets the promotion manager.
|
||||||
|
*
|
||||||
* @return the promotion manager.
|
* @return the promotion manager.
|
||||||
*/
|
*/
|
||||||
public PromotionManager getPromotionManager() {
|
public PromotionManager getPromotionManager() {
|
||||||
|
@ -139,12 +136,13 @@ public class Abacus {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation.
|
* Gets the number implementation.
|
||||||
|
*
|
||||||
* @return the number implementation to use for creating numbers.
|
* @return the number implementation to use for creating numbers.
|
||||||
*/
|
*/
|
||||||
public NumberImplementation getNumberImplementation(){
|
public NumberImplementation getNumberImplementation() {
|
||||||
NumberImplementation selectedImplementation =
|
NumberImplementation selectedImplementation =
|
||||||
pluginManager.numberImplementationFor(configuration.getNumberImplementation());
|
pluginManager.numberImplementationFor(configuration.getNumberImplementation());
|
||||||
if(selectedImplementation != null) return selectedImplementation;
|
if (selectedImplementation != null) return selectedImplementation;
|
||||||
return DEFAULT_IMPLEMENTATION;
|
return DEFAULT_IMPLEMENTATION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,13 +50,13 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||||
for (String operator : manager.getAllOperators()) {
|
for (String operator : manager.getAllOperators()) {
|
||||||
lexer.register(Pattern.sanitize(operator), TokenType.OP);
|
lexer.register(Pattern.sanitize(operator), TokenType.OP);
|
||||||
}
|
}
|
||||||
for (String operator : manager.getAllTreeValueOperators()){
|
for (String operator : manager.getAllTreeValueOperators()) {
|
||||||
lexer.register(Pattern.sanitize(operator), TokenType.TREE_VALUE_OP);
|
lexer.register(Pattern.sanitize(operator), TokenType.TREE_VALUE_OP);
|
||||||
}
|
}
|
||||||
for (String function : manager.getAllFunctions()) {
|
for (String function : manager.getAllFunctions()) {
|
||||||
lexer.register(Pattern.sanitize(function), TokenType.FUNCTION);
|
lexer.register(Pattern.sanitize(function), TokenType.FUNCTION);
|
||||||
}
|
}
|
||||||
for (String function : manager.getAllTreeValueFunctions()){
|
for (String function : manager.getAllTreeValueFunctions()) {
|
||||||
lexer.register(Pattern.sanitize(function), TokenType.TREE_VALUE_FUNCTION);
|
lexer.register(Pattern.sanitize(function), TokenType.TREE_VALUE_FUNCTION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,13 +66,13 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||||
for (String operator : manager.getAllOperators()) {
|
for (String operator : manager.getAllOperators()) {
|
||||||
lexer.unregister(Pattern.sanitize(operator), TokenType.OP);
|
lexer.unregister(Pattern.sanitize(operator), TokenType.OP);
|
||||||
}
|
}
|
||||||
for (String operator : manager.getAllTreeValueOperators()){
|
for (String operator : manager.getAllTreeValueOperators()) {
|
||||||
lexer.unregister(Pattern.sanitize(operator), TokenType.TREE_VALUE_OP);
|
lexer.unregister(Pattern.sanitize(operator), TokenType.TREE_VALUE_OP);
|
||||||
}
|
}
|
||||||
for (String function : manager.getAllFunctions()) {
|
for (String function : manager.getAllFunctions()) {
|
||||||
lexer.unregister(Pattern.sanitize(function), TokenType.FUNCTION);
|
lexer.unregister(Pattern.sanitize(function), TokenType.FUNCTION);
|
||||||
}
|
}
|
||||||
for (String function : manager.getAllTreeValueFunctions()){
|
for (String function : manager.getAllTreeValueFunctions()) {
|
||||||
lexer.unregister(Pattern.sanitize(function), TokenType.TREE_VALUE_FUNCTION);
|
lexer.unregister(Pattern.sanitize(function), TokenType.TREE_VALUE_FUNCTION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -134,7 +134,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
TreeNode right = constructRecursive(matches);
|
TreeNode right = constructRecursive(matches);
|
||||||
TreeNode left = constructRecursive(matches);
|
TreeNode left = constructRecursive(matches);
|
||||||
if (left == null || right == null) return null;
|
if (left == null || right == null) return null;
|
||||||
if(matchType == TokenType.OP) {
|
if (matchType == TokenType.OP) {
|
||||||
return new NumberBinaryNode(operator, left, right);
|
return new NumberBinaryNode(operator, left, right);
|
||||||
} else {
|
} else {
|
||||||
return new TreeValueBinaryNode(operator, left, right);
|
return new TreeValueBinaryNode(operator, left, right);
|
||||||
|
@ -142,7 +142,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
} else {
|
} else {
|
||||||
TreeNode applyTo = constructRecursive(matches);
|
TreeNode applyTo = constructRecursive(matches);
|
||||||
if (applyTo == null) return null;
|
if (applyTo == null) return null;
|
||||||
if(matchType == TokenType.OP){
|
if (matchType == TokenType.OP) {
|
||||||
return new NumberUnaryNode(operator, applyTo);
|
return new NumberUnaryNode(operator, applyTo);
|
||||||
} else {
|
} else {
|
||||||
return new TreeValueUnaryNode(operator, applyTo);
|
return new TreeValueUnaryNode(operator, applyTo);
|
||||||
|
@ -155,7 +155,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||||
} else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) {
|
} else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) {
|
||||||
String functionName = match.getContent();
|
String functionName = match.getContent();
|
||||||
CallNode node;
|
CallNode node;
|
||||||
if(matchType == TokenType.FUNCTION){
|
if (matchType == TokenType.FUNCTION) {
|
||||||
node = new FunctionNode(functionName);
|
node = new FunctionNode(functionName);
|
||||||
} else {
|
} else {
|
||||||
node = new TreeValueFunctionNode(functionName);
|
node = new TreeValueFunctionNode(functionName);
|
||||||
|
|
|
@ -241,7 +241,7 @@ public class PluginManager {
|
||||||
* @param name the class for which to find the implementation name.
|
* @param name the class for which to find the implementation name.
|
||||||
* @return the implementation name.
|
* @return the implementation name.
|
||||||
*/
|
*/
|
||||||
public String interfaceImplementationNameFor(Class<? extends NumberInterface> name){
|
public String interfaceImplementationNameFor(Class<? extends NumberInterface> name) {
|
||||||
return interfaceImplementationNames.get(name);
|
return interfaceImplementationNames.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import org.nwapw.abacus.number.PreciseNumber;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.function.BiFunction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The plugin providing standard functions such as addition and subtraction to
|
* The plugin providing standard functions such as addition and subtraction to
|
||||||
|
@ -24,7 +23,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params[0].add(params[1]);
|
return params[0].add(params[1]);
|
||||||
|
@ -39,7 +38,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params[0].subtract(params[1]);
|
return params[0].subtract(params[1]);
|
||||||
|
@ -55,7 +54,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params[0].negate();
|
return params[0].negate();
|
||||||
|
@ -70,7 +69,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params[0].multiply(params[1]);
|
return params[0].multiply(params[1]);
|
||||||
|
@ -90,21 +89,6 @@ public class StandardPlugin extends Plugin {
|
||||||
return new NaiveNumber(Math.PI);
|
return new NaiveNumber(Math.PI);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
|
||||||
* The square root function.
|
|
||||||
*/
|
|
||||||
public static final NumberFunction FUNCTION_SQRT = new NumberFunction() {
|
|
||||||
@Override
|
|
||||||
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
|
||||||
return OP_CARET.apply(implementation, params[0], implementation.instanceForString(".5"));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* The implementation for the infinite-precision BigDecimal.
|
* The implementation for the infinite-precision BigDecimal.
|
||||||
*/
|
*/
|
||||||
|
@ -147,10 +131,10 @@ public class StandardPlugin extends Plugin {
|
||||||
public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) {
|
public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) {
|
||||||
@Override
|
@Override
|
||||||
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
|
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params.length == 2 && params[1].compareTo(implementation.instanceForString(Integer.toString( 0))) != 0;
|
return params.length == 2 && params[1].compareTo(implementation.instanceForString(Integer.toString(0))) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params[0].divide(params[1]);
|
return params[0].divide(params[1]);
|
||||||
|
@ -168,7 +152,7 @@ public class StandardPlugin extends Plugin {
|
||||||
&& params[0].signum() >= 0;
|
&& params[0].signum() >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
if (params[0].signum() == 0) {
|
if (params[0].signum() == 0) {
|
||||||
|
@ -199,7 +183,7 @@ public class StandardPlugin extends Plugin {
|
||||||
&& params[1].fractionalPart().signum() == 0;
|
&& params[1].fractionalPart().signum() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
if (params[0].compareTo(params[1]) < 0 ||
|
if (params[0].compareTo(params[1]) < 0 ||
|
||||||
|
@ -230,7 +214,7 @@ public class StandardPlugin extends Plugin {
|
||||||
&& params[1].fractionalPart().signum() == 0;
|
&& params[1].fractionalPart().signum() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return OP_NPR.apply(implementation, params).divide(OP_FACTORIAL.apply(implementation, params[1]));
|
return OP_NPR.apply(implementation, params).divide(OP_FACTORIAL.apply(implementation, params[1]));
|
||||||
|
@ -245,7 +229,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return params[0].multiply(implementation.instanceForString(Integer.toString(params[0].signum())));
|
return params[0].multiply(implementation.instanceForString(Integer.toString(params[0].signum())));
|
||||||
|
@ -260,7 +244,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1 && params[0].compareTo(implementation.instanceForString("0")) > 0;
|
return params.length == 1 && params[0].compareTo(implementation.instanceForString("0")) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
NumberInterface param = params[0];
|
NumberInterface param = params[0];
|
||||||
|
@ -341,12 +325,58 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return implementation.instanceForString(Long.toString(Math.round(Math.random() * params[0].floor().intValue())));
|
return implementation.instanceForString(Long.toString(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(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
|
NumberInterface zero = implementation.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(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
|
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(implementation, params[1]).compareTo(implementation.instanceForString(Integer.toString(Integer.MAX_VALUE))) < 0
|
||||||
|
&& FUNCTION_ABS.apply(implementation, params[1]).compareTo(implementation.instanceForString("1")) >= 0) {
|
||||||
|
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
|
||||||
|
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(implementation, newParams));
|
||||||
|
}
|
||||||
|
return FUNCTION_EXP.apply(implementation, FUNCTION_LN.apply(implementation, FUNCTION_ABS.apply(implementation, params[0])).multiply(params[1]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The square root function.
|
||||||
|
*/
|
||||||
|
public static final NumberFunction FUNCTION_SQRT = new NumberFunction() {
|
||||||
|
@Override
|
||||||
|
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
|
return OP_CARET.apply(implementation, params[0], implementation.instanceForString(".5"));
|
||||||
|
}
|
||||||
|
};
|
||||||
private static final HashMap<NumberImplementation, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
|
private static final HashMap<NumberImplementation, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
|
||||||
/**
|
/**
|
||||||
* The exponential function, exp(1) = e^1 = 2.71...
|
* The exponential function, exp(1) = e^1 = 2.71...
|
||||||
|
@ -357,7 +387,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
NumberInterface maxError = params[0].getMaxError();
|
NumberInterface maxError = params[0].getMaxError();
|
||||||
|
@ -386,37 +416,6 @@ 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(NumberImplementation implementation, NumberInterface[] params) {
|
|
||||||
NumberInterface zero = implementation.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(NumberImplementation implementation, NumberInterface[] params) {
|
|
||||||
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(implementation, params[1]).compareTo(implementation.instanceForString(Integer.toString(Integer.MAX_VALUE))) < 0
|
|
||||||
&& FUNCTION_ABS.apply(implementation, params[1]).compareTo(implementation.instanceForString("1")) >= 0) {
|
|
||||||
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
|
|
||||||
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(implementation, newParams));
|
|
||||||
}
|
|
||||||
return FUNCTION_EXP.apply(implementation, FUNCTION_LN.apply(implementation, FUNCTION_ABS.apply(implementation, params[0])).multiply(params[1]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* The sine function (the argument is interpreted in radians).
|
* The sine function (the argument is interpreted in radians).
|
||||||
*/
|
*/
|
||||||
|
@ -426,7 +425,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
NumberInterface pi = piFor(params[0].getClass());
|
NumberInterface pi = piFor(params[0].getClass());
|
||||||
|
@ -451,7 +450,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return functionSin.apply(implementation, piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
|
return functionSin.apply(implementation, piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
|
||||||
|
@ -467,7 +466,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return functionSin.apply(implementation, params[0]).divide(functionCos.apply(implementation, params[0]));
|
return functionSin.apply(implementation, params[0]).divide(functionCos.apply(implementation, params[0]));
|
||||||
|
@ -482,7 +481,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return implementation.instanceForString("1").divide(functionCos.apply(implementation, params[0]));
|
return implementation.instanceForString("1").divide(functionCos.apply(implementation, params[0]));
|
||||||
|
@ -497,7 +496,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return implementation.instanceForString("1").divide(functionSin.apply(implementation, params[0]));
|
return implementation.instanceForString("1").divide(functionSin.apply(implementation, params[0]));
|
||||||
|
@ -512,7 +511,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return functionCos.apply(implementation, params[0]).divide(functionSin.apply(implementation, params[0]));
|
return functionCos.apply(implementation, params[0]).divide(functionSin.apply(implementation, params[0]));
|
||||||
|
@ -529,7 +528,7 @@ public class StandardPlugin extends Plugin {
|
||||||
&& FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) <= 0;
|
&& FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
if (FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString(".8")) >= 0) {
|
if (FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString(".8")) >= 0) {
|
||||||
|
@ -544,9 +543,9 @@ public class StandardPlugin extends Plugin {
|
||||||
while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(summandBound) > 0) {
|
while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(summandBound) > 0) {
|
||||||
exponent += 2;
|
exponent += 2;
|
||||||
power = power.multiply(multiplier);
|
power = power.multiply(multiplier);
|
||||||
coefficient = coefficient.multiply(implementation.instanceForString(Integer.toString( exponent - 2)))
|
coefficient = coefficient.multiply(implementation.instanceForString(Integer.toString(exponent - 2)))
|
||||||
.divide(implementation.instanceForString(Integer.toString( exponent - 1)));
|
.divide(implementation.instanceForString(Integer.toString(exponent - 1)));
|
||||||
currentTerm = power.multiply(coefficient).divide(implementation.instanceForString(Integer.toString( exponent)));
|
currentTerm = power.multiply(coefficient).divide(implementation.instanceForString(Integer.toString(exponent)));
|
||||||
sum = sum.add(currentTerm);
|
sum = sum.add(currentTerm);
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
|
@ -562,7 +561,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) <= 0;
|
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
return piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
|
return piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
|
||||||
|
@ -579,7 +578,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) >= 0;
|
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
NumberInterface[] reciprocalParamArr = {implementation.instanceForString("1").divide(params[0])};
|
NumberInterface[] reciprocalParamArr = {implementation.instanceForString("1").divide(params[0])};
|
||||||
|
@ -596,7 +595,7 @@ public class StandardPlugin extends Plugin {
|
||||||
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) >= 0;
|
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
|
||||||
NumberInterface[] reciprocalParamArr = {implementation.instanceForString("1").divide(params[0])};
|
NumberInterface[] reciprocalParamArr = {implementation.instanceForString("1").divide(params[0])};
|
||||||
|
@ -639,7 +638,7 @@ public class StandardPlugin extends Plugin {
|
||||||
while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0) {
|
while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0) {
|
||||||
n += 2;
|
n += 2;
|
||||||
currentPower = currentPower.multiply(multiplier);
|
currentPower = currentPower.multiply(multiplier);
|
||||||
currentTerm = currentPower.divide(implementation.instanceForString(Integer.toString( n)));
|
currentTerm = currentPower.divide(implementation.instanceForString(Integer.toString(n)));
|
||||||
sum = sum.add(currentTerm);
|
sum = sum.add(currentTerm);
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
|
@ -671,7 +670,7 @@ public class StandardPlugin extends Plugin {
|
||||||
* computes factorials of non-negative integers.
|
* computes factorials of non-negative integers.
|
||||||
*
|
*
|
||||||
* @param implementation type of number to return.
|
* @param implementation type of number to return.
|
||||||
* @param n non-negative integer.
|
* @param n non-negative integer.
|
||||||
* @return a number of numClass with value n factorial.
|
* @return a number of numClass with value n factorial.
|
||||||
*/
|
*/
|
||||||
public static NumberInterface factorial(NumberImplementation implementation, int n) {
|
public static NumberInterface factorial(NumberImplementation implementation, int n) {
|
||||||
|
@ -684,7 +683,7 @@ public class StandardPlugin extends Plugin {
|
||||||
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(implementation);
|
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(implementation);
|
||||||
if (n >= list.size()) {
|
if (n >= list.size()) {
|
||||||
while (list.size() < n + 16) {
|
while (list.size() < n + 16) {
|
||||||
list.add(list.get(list.size() - 1).multiply(implementation.instanceForString(Integer.toString( list.size()))));
|
list.add(list.get(list.size() - 1).multiply(implementation.instanceForString(Integer.toString(list.size()))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list.get(n);
|
return list.get(n);
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
import org.nwapw.abacus.Abacus;
|
||||||
import org.nwapw.abacus.function.*;
|
import org.nwapw.abacus.function.NumberFunction;
|
||||||
|
import org.nwapw.abacus.function.NumberOperator;
|
||||||
|
import org.nwapw.abacus.function.TreeValueFunction;
|
||||||
|
import org.nwapw.abacus.function.TreeValueOperator;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.number.PromotionManager;
|
import org.nwapw.abacus.number.PromotionManager;
|
||||||
import org.nwapw.abacus.number.PromotionResult;
|
import org.nwapw.abacus.number.PromotionResult;
|
||||||
|
@ -31,14 +34,14 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||||
PromotionManager manager = abacus.getPromotionManager();
|
PromotionManager manager = abacus.getPromotionManager();
|
||||||
if (node instanceof NumberNode) {
|
if (node instanceof NumberNode) {
|
||||||
return abacus.getNumberImplementation().instanceForString(((NumberNode) node).getNumber());
|
return abacus.getNumberImplementation().instanceForString(((NumberNode) node).getNumber());
|
||||||
} else if(node instanceof VariableNode) {
|
} else if (node instanceof VariableNode) {
|
||||||
return abacus.getNumberImplementation().instanceForString("0");
|
return abacus.getNumberImplementation().instanceForString("0");
|
||||||
} else if (node instanceof NumberBinaryNode) {
|
} else if (node instanceof NumberBinaryNode) {
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation());
|
NumberOperator operator = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation());
|
||||||
PromotionResult result = manager.promote(left, right);
|
PromotionResult result = manager.promote(left, right);
|
||||||
if(result == null) return null;
|
if (result == null) return null;
|
||||||
return operator.apply(result.getPromotedTo(), result.getItems());
|
return operator.apply(result.getPromotedTo(), result.getItems());
|
||||||
} else if (node instanceof NumberUnaryNode) {
|
} else if (node instanceof NumberUnaryNode) {
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
NumberInterface child = (NumberInterface) children[0];
|
||||||
|
@ -52,29 +55,29 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||||
NumberFunction function = abacus.getPluginManager().functionFor(((FunctionNode) node).getCallTo());
|
NumberFunction function = abacus.getPluginManager().functionFor(((FunctionNode) node).getCallTo());
|
||||||
if (function == null) return null;
|
if (function == null) return null;
|
||||||
PromotionResult result = manager.promote(convertedChildren);
|
PromotionResult result = manager.promote(convertedChildren);
|
||||||
if(result == null) return null;
|
if (result == null) return null;
|
||||||
return function.apply(result.getPromotedTo(), result.getItems());
|
return function.apply(result.getPromotedTo(), result.getItems());
|
||||||
} else if (node instanceof TreeValueFunctionNode){
|
} else if (node instanceof TreeValueFunctionNode) {
|
||||||
CallNode callNode = (CallNode) node;
|
CallNode callNode = (CallNode) node;
|
||||||
TreeNode[] realChildren = new TreeNode[callNode.getChildren().size()];
|
TreeNode[] realChildren = new TreeNode[callNode.getChildren().size()];
|
||||||
for(int i = 0; i < realChildren.length; i++){
|
for (int i = 0; i < realChildren.length; i++) {
|
||||||
realChildren[i] = callNode.getChildren().get(i);
|
realChildren[i] = callNode.getChildren().get(i);
|
||||||
}
|
}
|
||||||
TreeValueFunction function =
|
TreeValueFunction function =
|
||||||
abacus.getPluginManager().treeValueFunctionFor(callNode.getCallTo());
|
abacus.getPluginManager().treeValueFunctionFor(callNode.getCallTo());
|
||||||
if(function == null) return null;
|
if (function == null) return null;
|
||||||
return function.applyWithReducer(abacus.getNumberImplementation(), this, realChildren);
|
return function.applyWithReducer(abacus.getNumberImplementation(), this, realChildren);
|
||||||
} else if (node instanceof TreeValueBinaryNode) {
|
} else if (node instanceof TreeValueBinaryNode) {
|
||||||
BinaryNode binaryNode = (BinaryNode) node;
|
BinaryNode binaryNode = (BinaryNode) node;
|
||||||
TreeValueOperator operator = abacus.getPluginManager()
|
TreeValueOperator operator = abacus.getPluginManager()
|
||||||
.treeValueOperatorFor(binaryNode.getOperation());
|
.treeValueOperatorFor(binaryNode.getOperation());
|
||||||
if(operator == null) return null;
|
if (operator == null) return null;
|
||||||
return operator.applyWithReducer(abacus.getNumberImplementation(), this, binaryNode.getLeft(), binaryNode.getRight());
|
return operator.applyWithReducer(abacus.getNumberImplementation(), this, binaryNode.getLeft(), binaryNode.getRight());
|
||||||
} else if(node instanceof TreeValueUnaryNode) {
|
} else if (node instanceof TreeValueUnaryNode) {
|
||||||
UnaryNode unaryNode = (UnaryNode) node;
|
UnaryNode unaryNode = (UnaryNode) node;
|
||||||
TreeValueOperator operator = abacus.getPluginManager()
|
TreeValueOperator operator = abacus.getPluginManager()
|
||||||
.treeValueOperatorFor(unaryNode.getOperation());
|
.treeValueOperatorFor(unaryNode.getOperation());
|
||||||
if(operator == null) return null;
|
if (operator == null) return null;
|
||||||
return operator.applyWithReducer(abacus.getNumberImplementation(), this, unaryNode.getApplyTo());
|
return operator.applyWithReducer(abacus.getNumberImplementation(), this, unaryNode.getApplyTo());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user