mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-13 18:35:19 +00:00
Format code.
This commit is contained in:
@@ -20,9 +20,10 @@ public class ClassFinder {
|
||||
|
||||
/**
|
||||
* Loads all the plugin classes from the given plugin folder.
|
||||
*
|
||||
* @param filePath the path for the plugin folder.
|
||||
* @return the list of all loaded classes.
|
||||
* @throws IOException thrown if an error occurred scanning the plugin folder.
|
||||
* @throws IOException thrown if an error occurred scanning the plugin folder.
|
||||
* @throws ClassNotFoundException thrown if the class listed in the file doesn't get loaded.
|
||||
*/
|
||||
public static List<Class<?>> loadJars(String filePath) throws IOException, ClassNotFoundException {
|
||||
@@ -31,19 +32,20 @@ public class ClassFinder {
|
||||
|
||||
/**
|
||||
* Loads all the plugin classes from the given plugin folder.
|
||||
*
|
||||
* @param pluginFolderPath the folder in which to look for plugins.
|
||||
* @return the list of all loaded classes.
|
||||
* @throws IOException thrown if an error occurred scanning the plugin folder.
|
||||
* @throws IOException thrown if an error occurred scanning the plugin folder.
|
||||
* @throws ClassNotFoundException thrown if the class listed in the file doesn't get loaded.
|
||||
*/
|
||||
public static List<Class<?>> loadJars(File pluginFolderPath) throws IOException, ClassNotFoundException {
|
||||
ArrayList<Class<?>> toReturn = new ArrayList<>();
|
||||
if(!pluginFolderPath.exists()) return toReturn;
|
||||
if (!pluginFolderPath.exists()) return toReturn;
|
||||
ArrayList<File> files = Files.walk(pluginFolderPath.toPath())
|
||||
.map(Path::toFile)
|
||||
.filter(f -> f.getName().endsWith(".jar"))
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
for (File file : files){
|
||||
for (File file : files) {
|
||||
toReturn.addAll(loadJar(file));
|
||||
}
|
||||
return toReturn;
|
||||
@@ -51,9 +53,10 @@ public class ClassFinder {
|
||||
|
||||
/**
|
||||
* Loads the classes from a single path, given by the file.
|
||||
*
|
||||
* @param jarLocation the location of the jar to load.
|
||||
* @return the list of loaded classes loaded from the jar.
|
||||
* @throws IOException thrown if there was an error reading the file
|
||||
* @throws IOException thrown if there was an error reading the file
|
||||
* @throws ClassNotFoundException thrown if the class could not be loaded.
|
||||
*/
|
||||
public static List<Class<?>> loadJar(File jarLocation) throws IOException, ClassNotFoundException {
|
||||
|
||||
@@ -39,10 +39,12 @@ public abstract class Plugin {
|
||||
*/
|
||||
private boolean enabled;
|
||||
|
||||
private Plugin(){ }
|
||||
private Plugin() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new plugin with the given PluginManager.
|
||||
*
|
||||
* @param manager the manager controlling this plugin.
|
||||
*/
|
||||
public Plugin(PluginManager manager) {
|
||||
@@ -55,30 +57,34 @@ public abstract class Plugin {
|
||||
|
||||
/**
|
||||
* Gets the list of functions provided by this plugin.
|
||||
*
|
||||
* @return the list of registered functions.
|
||||
*/
|
||||
public final Set<String> providedFunctions(){
|
||||
public final Set<String> providedFunctions() {
|
||||
return functions.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of functions provided by this plugin.
|
||||
*
|
||||
* @return the list of registered functions.
|
||||
*/
|
||||
public final Set<String> providedOperators(){
|
||||
public final Set<String> providedOperators() {
|
||||
return operators.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of all numbers provided by this plugin.
|
||||
*
|
||||
* @return the list of registered numbers.
|
||||
*/
|
||||
public final Set<String> providedNumbers(){
|
||||
public final Set<String> providedNumbers() {
|
||||
return numbers.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a function under the given function name.
|
||||
*
|
||||
* @param functionName the name of the function to get
|
||||
* @return the function, or null if this plugin doesn't provide it.
|
||||
*/
|
||||
@@ -88,6 +94,7 @@ public abstract class Plugin {
|
||||
|
||||
/**
|
||||
* Gets an operator under the given operator name.
|
||||
*
|
||||
* @param operatorName the name of the operator to get.
|
||||
* @return the operator, or null if this plugin doesn't provide it.
|
||||
*/
|
||||
@@ -97,10 +104,11 @@ public abstract class Plugin {
|
||||
|
||||
/**
|
||||
* Gets the class under the given name.
|
||||
*
|
||||
* @param numberName the name of the class.
|
||||
* @return the class, or null if the plugin doesn't provide it.
|
||||
*/
|
||||
public final Class<? extends NumberInterface> getNumber(String numberName){
|
||||
public final Class<? extends NumberInterface> getNumber(String numberName) {
|
||||
return numbers.get(numberName);
|
||||
}
|
||||
|
||||
@@ -108,8 +116,8 @@ public abstract class Plugin {
|
||||
* Enables the function, loading the necessary instances
|
||||
* of functions.
|
||||
*/
|
||||
public final void enable(){
|
||||
if(enabled) return;
|
||||
public final void enable() {
|
||||
if (enabled) return;
|
||||
onEnable();
|
||||
enabled = true;
|
||||
}
|
||||
@@ -118,8 +126,8 @@ public abstract class Plugin {
|
||||
* Disables the plugin, clearing loaded data store by default
|
||||
* and calling its disable() method.
|
||||
*/
|
||||
public final void disable(){
|
||||
if(!enabled) return;
|
||||
public final void disable() {
|
||||
if (!enabled) return;
|
||||
onDisable();
|
||||
functions.clear();
|
||||
operators.clear();
|
||||
@@ -129,7 +137,8 @@ public abstract class Plugin {
|
||||
/**
|
||||
* To be used in load(). Registers a 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 function implementation.
|
||||
*/
|
||||
protected final void registerFunction(String name, Function toRegister) {
|
||||
@@ -140,7 +149,8 @@ public abstract class Plugin {
|
||||
* To be used in load(). Registers an operator abstract class
|
||||
* with the plugin internally, which makes it accessible to
|
||||
* the plugin manager.
|
||||
* @param name the name of the operator.
|
||||
*
|
||||
* @param name the name of the operator.
|
||||
* @param operator the operator to register.
|
||||
*/
|
||||
protected final void registerOperator(String name, Operator operator) {
|
||||
@@ -152,10 +162,11 @@ public abstract class Plugin {
|
||||
* with the plugin internally, which makes it possible
|
||||
* for the user to select it as an "implementation" for the
|
||||
* number that they would like to use.
|
||||
* @param name the name to register it under.
|
||||
*
|
||||
* @param name the name to register it under.
|
||||
* @param toRegister the class to register.
|
||||
*/
|
||||
protected final void registerNumber(String name, Class<? extends NumberInterface> toRegister){
|
||||
protected final void registerNumber(String name, Class<? extends NumberInterface> toRegister) {
|
||||
numbers.put(name, toRegister);
|
||||
}
|
||||
|
||||
@@ -163,6 +174,7 @@ public abstract class Plugin {
|
||||
* Searches the PluginManager for the given function name.
|
||||
* This can be used by the plugins internally in order to call functions
|
||||
* they do not provide.
|
||||
*
|
||||
* @param name the name for which to search
|
||||
* @return the resulting function, or null if none was found for that name.
|
||||
*/
|
||||
@@ -174,6 +186,7 @@ public abstract class Plugin {
|
||||
* Searches the PluginManager for the given operator name.
|
||||
* This can be used by the plugins internally in order to call
|
||||
* operations they do not provide.
|
||||
*
|
||||
* @param name the name for which to search
|
||||
* @return the resulting operator, or null if none was found for that name.
|
||||
*/
|
||||
|
||||
@@ -7,12 +7,14 @@ public interface PluginListener {
|
||||
|
||||
/**
|
||||
* Called when the PluginManager loads plugins.
|
||||
*
|
||||
* @param manager the manager that fired the event.
|
||||
*/
|
||||
public void onLoad(PluginManager manager);
|
||||
|
||||
/**
|
||||
* Called when the PluginManager unloads all its plugins.
|
||||
*
|
||||
* @param manager the manager that fired the event.
|
||||
*/
|
||||
public void onUnload(PluginManager manager);
|
||||
|
||||
@@ -56,7 +56,7 @@ public class PluginManager {
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
*/
|
||||
public PluginManager(){
|
||||
public PluginManager() {
|
||||
loadedPluginClasses = new HashSet<>();
|
||||
plugins = new HashSet<>();
|
||||
cachedFunctions = new HashMap<>();
|
||||
@@ -73,23 +73,24 @@ public class PluginManager {
|
||||
* list of items of the type using the setFunction and getting the value
|
||||
* of it is available via getFunction. If the value is contained
|
||||
* in the cache, it returns the cached value instead.
|
||||
* @param plugins the plugin list to search.
|
||||
* @param cache the cache to use
|
||||
*
|
||||
* @param plugins the plugin list to search.
|
||||
* @param cache the cache to use
|
||||
* @param setFunction the function to retrieve a set of available T's from the plugin
|
||||
* @param getFunction the function to get the T value under the given name
|
||||
* @param name the name to search for
|
||||
* @param <T> the type of element being search
|
||||
* @param name the name to search for
|
||||
* @param <T> the type of element being search
|
||||
* @return the retrieved element, or null if it was not found.
|
||||
*/
|
||||
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
||||
java.util.function.Function<Plugin, Set<String>> setFunction,
|
||||
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
||||
String name){
|
||||
if(cache.containsKey(name)) return cache.get(name);
|
||||
String name) {
|
||||
if (cache.containsKey(name)) return cache.get(name);
|
||||
|
||||
T loadedValue = null;
|
||||
for(Plugin plugin : plugins){
|
||||
if(setFunction.apply(plugin).contains(name)){
|
||||
for (Plugin plugin : plugins) {
|
||||
if (setFunction.apply(plugin).contains(name)) {
|
||||
loadedValue = getFunction.apply(plugin, name);
|
||||
break;
|
||||
}
|
||||
@@ -98,39 +99,44 @@ public class PluginManager {
|
||||
cache.put(name, loadedValue);
|
||||
return loadedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a function under the given name.
|
||||
*
|
||||
* @param name the name of the function
|
||||
* @return the function under the given name.
|
||||
*/
|
||||
public Function functionFor(String name){
|
||||
public Function functionFor(String name) {
|
||||
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an operator under the given name.
|
||||
*
|
||||
* @param name the name of the operator.
|
||||
* @return the operator under the given name.
|
||||
*/
|
||||
public Operator operatorFor(String name){
|
||||
public Operator operatorFor(String name) {
|
||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a numer implementation under the given name.
|
||||
*
|
||||
* @param name the name of the implementation.
|
||||
* @return the implementation class
|
||||
*/
|
||||
public Class<? extends NumberInterface> numberFor(String name){
|
||||
public Class<? extends NumberInterface> numberFor(String name) {
|
||||
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an instance of Plugin that already has been instantiated.
|
||||
*
|
||||
* @param plugin the plugin to add.
|
||||
*/
|
||||
public void addInstantiated(Plugin plugin){
|
||||
if(loadedPluginClasses.contains(plugin.getClass())) return;
|
||||
public void addInstantiated(Plugin plugin) {
|
||||
if (loadedPluginClasses.contains(plugin.getClass())) return;
|
||||
plugins.add(plugin);
|
||||
loadedPluginClasses.add(plugin.getClass());
|
||||
}
|
||||
@@ -138,10 +144,11 @@ public class PluginManager {
|
||||
/**
|
||||
* Instantiates a class of plugin, and adds it to this
|
||||
* plugin manager.
|
||||
*
|
||||
* @param newClass the new class to instantiate.
|
||||
*/
|
||||
public void addClass(Class<?> newClass){
|
||||
if(!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return;
|
||||
public void addClass(Class<?> newClass) {
|
||||
if (!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return;
|
||||
try {
|
||||
addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
@@ -152,9 +159,9 @@ public class PluginManager {
|
||||
/**
|
||||
* Loads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void load(){
|
||||
for(Plugin plugin : plugins) plugin.enable();
|
||||
for(Plugin plugin : plugins){
|
||||
public void load() {
|
||||
for (Plugin plugin : plugins) plugin.enable();
|
||||
for (Plugin plugin : plugins) {
|
||||
allFunctions.addAll(plugin.providedFunctions());
|
||||
allOperators.addAll(plugin.providedOperators());
|
||||
allNumbers.addAll(plugin.providedNumbers());
|
||||
@@ -165,8 +172,8 @@ public class PluginManager {
|
||||
/**
|
||||
* Unloads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void unload(){
|
||||
for(Plugin plugin : plugins) plugin.disable();
|
||||
public void unload() {
|
||||
for (Plugin plugin : plugins) plugin.disable();
|
||||
allFunctions.clear();
|
||||
allOperators.clear();
|
||||
allNumbers.clear();
|
||||
@@ -176,13 +183,14 @@ public class PluginManager {
|
||||
/**
|
||||
* Reloads all the plugins in the PluginManager.
|
||||
*/
|
||||
public void reload(){
|
||||
public void reload() {
|
||||
unload();
|
||||
reload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the functions loaded by the Plugin Manager.
|
||||
*
|
||||
* @return the set of all functions that were loaded.
|
||||
*/
|
||||
public Set<String> getAllFunctions() {
|
||||
@@ -191,6 +199,7 @@ public class PluginManager {
|
||||
|
||||
/**
|
||||
* Gets all the operators loaded by the Plugin Manager.
|
||||
*
|
||||
* @return the set of all operators that were loaded.
|
||||
*/
|
||||
public Set<String> getAllOperators() {
|
||||
@@ -199,6 +208,7 @@ public class PluginManager {
|
||||
|
||||
/**
|
||||
* Gets all the number implementations loaded by the Plugin Manager
|
||||
*
|
||||
* @return the set of all implementations that were loaded
|
||||
*/
|
||||
public Set<String> getAllNumbers() {
|
||||
@@ -207,18 +217,20 @@ public class PluginManager {
|
||||
|
||||
/**
|
||||
* Adds a plugin change listener to this plugin manager.
|
||||
*
|
||||
* @param listener the listener to add.
|
||||
*/
|
||||
public void addListener(PluginListener listener){
|
||||
public void addListener(PluginListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the plugin change listener from this plugin manager.
|
||||
*
|
||||
* @param listener the listener to remove.
|
||||
*/
|
||||
public void removeListener(PluginListener listener){
|
||||
public void removeListener(PluginListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class StandardPlugin extends Plugin {
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface sum = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
for (int i = 1; i < params.length; i++) {
|
||||
sum = sum.add(params[i]);
|
||||
}
|
||||
return sum;
|
||||
@@ -42,7 +42,7 @@ public class StandardPlugin extends Plugin {
|
||||
return params[0].subtract(params[1]);
|
||||
}
|
||||
});
|
||||
public static final Operator OP_MULTIPLY = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
||||
public static final Operator OP_MULTIPLY = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length >= 1;
|
||||
@@ -51,13 +51,13 @@ public class StandardPlugin extends Plugin {
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface product = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
for (int i = 1; i < params.length; i++) {
|
||||
product = product.multiply(params[i]);
|
||||
}
|
||||
return product;
|
||||
}
|
||||
});
|
||||
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
||||
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length >= 1;
|
||||
@@ -66,23 +66,12 @@ public class StandardPlugin extends Plugin {
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface product = params[0];
|
||||
for(int i = 1; i < params.length; i++){
|
||||
for (int i = 1; i < params.length; i++) {
|
||||
product = product.multiply(params[i]);
|
||||
}
|
||||
return product;
|
||||
}
|
||||
});
|
||||
public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1]));
|
||||
}
|
||||
});
|
||||
public static final Operator OP_FACTORIAL = new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() {
|
||||
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||
@Override
|
||||
@@ -92,13 +81,13 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
if(params[0].signum() == 0){
|
||||
if (params[0].signum() == 0) {
|
||||
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
||||
}
|
||||
NumberInterface factorial = params[0];
|
||||
NumberInterface multiplier = params[0];
|
||||
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
||||
while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
|
||||
while ((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1) {
|
||||
factorial = factorial.multiply(multiplier);
|
||||
}
|
||||
return factorial;
|
||||
@@ -131,7 +120,7 @@ public class StandardPlugin extends Plugin {
|
||||
boolean takeReciprocal = params[0].signum() == -1;
|
||||
params[0] = FUNCTION_ABS.apply(params[0]);
|
||||
NumberInterface sum = sumSeries(params[0], StandardPlugin::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
||||
if(takeReciprocal){
|
||||
if (takeReciprocal) {
|
||||
sum = NaiveNumber.ONE.promoteTo(sum.getClass()).divide(sum);
|
||||
}
|
||||
return sum;
|
||||
@@ -147,19 +136,18 @@ public class StandardPlugin extends Plugin {
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface param = params[0];
|
||||
int powersOf2 = 0;
|
||||
while(FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 0){
|
||||
if(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
||||
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 0) {
|
||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
||||
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||
powersOf2++;
|
||||
if(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||
break;
|
||||
//No infinite loop for you.
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||
powersOf2--;
|
||||
if(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||
break;
|
||||
//No infinite loop for you.
|
||||
}
|
||||
@@ -174,14 +162,14 @@ public class StandardPlugin extends Plugin {
|
||||
* @param x value at which the series is evaluated. 0 < x < 2. (x=2 is convergent but impractical.)
|
||||
* @return the partial sum.
|
||||
*/
|
||||
private NumberInterface getLogPartialSum(NumberInterface x){
|
||||
private NumberInterface getLogPartialSum(NumberInterface x) {
|
||||
NumberInterface maxError = getMaxError(x);
|
||||
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
||||
NumberInterface currentTerm = x, sum = x;
|
||||
int n = 1;
|
||||
while(FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){
|
||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
||||
n++;
|
||||
currentTerm = currentTerm.multiply(x).multiply((new NaiveNumber(n-1)).promoteTo(x.getClass())).divide((new NaiveNumber(n)).promoteTo(x.getClass())).negate();
|
||||
currentTerm = currentTerm.multiply(x).multiply((new NaiveNumber(n - 1)).promoteTo(x.getClass())).divide((new NaiveNumber(n)).promoteTo(x.getClass())).negate();
|
||||
sum = sum.add(currentTerm);
|
||||
}
|
||||
return sum;
|
||||
@@ -192,7 +180,7 @@ public class StandardPlugin extends Plugin {
|
||||
* @param number a number of the same type as the return type. (Used for precision.)
|
||||
* @return the value of log(2) with the appropriate precision.
|
||||
*/
|
||||
private NumberInterface getLog2(NumberInterface number){
|
||||
private NumberInterface getLog2(NumberInterface number) {
|
||||
NumberInterface maxError = getMaxError(number);
|
||||
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
||||
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
|
||||
@@ -201,7 +189,7 @@ public class StandardPlugin extends Plugin {
|
||||
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
||||
int n = 0;
|
||||
while(a.compareTo(maxError) >= 1){
|
||||
while (a.compareTo(maxError) >= 1) {
|
||||
n++;
|
||||
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
||||
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
||||
@@ -211,6 +199,17 @@ public class StandardPlugin extends Plugin {
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1]));
|
||||
}
|
||||
});
|
||||
public static final Function FUNCTION_SQRT = new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
@@ -227,6 +226,65 @@ public class StandardPlugin extends Plugin {
|
||||
super(manager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nth term of the Taylor series (centered at 0) of e^x
|
||||
*
|
||||
* @param n the term required (n >= 0).
|
||||
* @param x the real number at which the series is evaluated.
|
||||
* @return the nth term of the series.
|
||||
*/
|
||||
private static NumberInterface getExpSeriesTerm(int n, NumberInterface x) {
|
||||
return x.intPow(n).divide(OP_FACTORIAL.getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of terms needed to evaluate the exponential function (at x)
|
||||
* such that the error is at most maxError.
|
||||
*
|
||||
* @param maxError Maximum error permissible (This should probably be positive.)
|
||||
* @param x where the function is evaluated.
|
||||
* @return the number of terms needed to evaluated the exponential function.
|
||||
*/
|
||||
private static int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
||||
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
||||
//The variables LHS and RHS refer to the above inequality.
|
||||
int n = 0;
|
||||
x = FUNCTION_ABS.apply(x);
|
||||
NumberInterface LHS = x, RHS = maxError;
|
||||
while (LHS.compareTo(RHS) > 0) {
|
||||
n++;
|
||||
LHS = LHS.multiply(x);
|
||||
RHS = RHS.multiply(new NaiveNumber(n + 1).promoteTo(RHS.getClass()));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
||||
*
|
||||
* @param x the value at which the series is evaluated.
|
||||
* @param nthTermFunction the function that returns the nth term of the series, in the format term(x, n).
|
||||
* @param n the number of terms in the partial sum.
|
||||
* @return the value of the partial sum that has the same class as x.
|
||||
*/
|
||||
private static NumberInterface sumSeries(NumberInterface x, BiFunction<Integer, NumberInterface, NumberInterface> nthTermFunction, int n) {
|
||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(x.getClass());
|
||||
for (int i = 0; i <= n; i++) {
|
||||
sum = sum.add(nthTermFunction.apply(i, x));
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum error based on the precision of the class of number.
|
||||
*
|
||||
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
||||
* @return the maximum error.
|
||||
*/
|
||||
private static NumberInterface getMaxError(NumberInterface number) {
|
||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
registerNumber("naive", NaiveNumber.class);
|
||||
@@ -242,7 +300,7 @@ public class StandardPlugin extends Plugin {
|
||||
registerFunction("abs", FUNCTION_ABS);
|
||||
registerFunction("exp", FUNCTION_EXP);
|
||||
registerFunction("ln", FUNCTION_LN);
|
||||
registerFunction("sqrt",FUNCTION_SQRT);
|
||||
registerFunction("sqrt", FUNCTION_SQRT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -250,60 +308,4 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nth term of the Taylor series (centered at 0) of e^x
|
||||
* @param n the term required (n >= 0).
|
||||
* @param x the real number at which the series is evaluated.
|
||||
* @return the nth term of the series.
|
||||
*/
|
||||
private static NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
||||
return x.intPow(n).divide(OP_FACTORIAL.getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of terms needed to evaluate the exponential function (at x)
|
||||
* such that the error is at most maxError.
|
||||
* @param maxError Maximum error permissible (This should probably be positive.)
|
||||
* @param x where the function is evaluated.
|
||||
* @return the number of terms needed to evaluated the exponential function.
|
||||
*/
|
||||
private static int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
||||
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
||||
//The variables LHS and RHS refer to the above inequality.
|
||||
int n = 0;
|
||||
x = FUNCTION_ABS.apply(x);
|
||||
NumberInterface LHS = x, RHS = maxError;
|
||||
while (LHS.compareTo(RHS) > 0) {
|
||||
n++;
|
||||
LHS = LHS.multiply(x);
|
||||
RHS = RHS.multiply(new NaiveNumber(n + 1).promoteTo(RHS.getClass()));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
||||
* @param x the value at which the series is evaluated.
|
||||
* @param nthTermFunction the function that returns the nth term of the series, in the format term(x, n).
|
||||
* @param n the number of terms in the partial sum.
|
||||
* @return the value of the partial sum that has the same class as x.
|
||||
*/
|
||||
private static NumberInterface sumSeries(NumberInterface x, BiFunction<Integer, NumberInterface, NumberInterface> nthTermFunction, int n){
|
||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(x.getClass());
|
||||
for(int i = 0; i <= n; i++){
|
||||
sum = sum.add(nthTermFunction.apply(i, x));
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum error based on the precision of the class of number.
|
||||
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
||||
* @return the maximum error.
|
||||
*/
|
||||
private static NumberInterface getMaxError(NumberInterface number){
|
||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user