1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-25 16:15:19 +00:00

Compare commits

..

1 Commits

3 changed files with 213 additions and 75 deletions

View File

@@ -17,6 +17,18 @@ import java.util.Set;
*/
public abstract class Plugin {
/**
* A hash map of functions mapped to their string names.
*/
private Map<String, Function> functions;
/**
* A hash map of operators mapped to their string names.
*/
private Map<String, Operator> operators;
/**
* The map of the number implementations this plugin provides.
*/
private Map<String, NumberImplementation> numberImplementations;
/**
* The plugin manager in which to search for functions
* not inside this package,
@@ -37,9 +49,69 @@ public abstract class Plugin {
*/
public Plugin(PluginManager manager) {
this.manager = manager;
functions = new HashMap<>();
operators = new HashMap<>();
numberImplementations = new HashMap<>();
enabled = false;
}
/**
* Gets the list of functions provided by this plugin.
*
* @return the list of registered functions.
*/
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() {
return operators.keySet();
}
/**
* Gets the list of number implementations provided by this plugin.
*
* @return the list of registered number implementations.
*/
public final Set<String> providedNumberImplementations() {
return numberImplementations.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.
*/
public final Function getFunction(String functionName) {
return functions.get(functionName);
}
/**
* 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.
*/
public final Operator getOperator(String operatorName) {
return operators.get(operatorName);
}
/**
* Gets the number implementation under the given name.
*
* @param name the name of the number implementation to look up.
* @return the number implementation associated with that name, or null if the plugin doesn't provide it.
*/
public final NumberImplementation getNumberImplementation(String name) {
return numberImplementations.get(name);
}
/**
* Enables the function, loading the necessary instances
* of functions.
@@ -57,6 +129,8 @@ public abstract class Plugin {
public final void disable() {
if (!enabled) return;
onDisable();
functions.clear();
operators.clear();
enabled = false;
}
@@ -68,7 +142,7 @@ public abstract class Plugin {
* @param toRegister the function implementation.
*/
protected final void registerFunction(String name, Function toRegister) {
manager.registerFunction(name, toRegister);
functions.put(name, toRegister);
}
/**
@@ -80,7 +154,7 @@ public abstract class Plugin {
* @param operator the operator to register.
*/
protected final void registerOperator(String name, Operator operator) {
manager.registerOperator(name, operator);
operators.put(name, operator);
}
/**
@@ -91,7 +165,7 @@ public abstract class Plugin {
* @param implementation the actual implementation class to register.
*/
protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
manager.registerNumberImplementation(name, implementation);
numberImplementations.put(name, implementation);
}
/**
@@ -139,7 +213,7 @@ public abstract class Plugin {
* @param forClass the class to which to find the pi instance.
* @return the pi value for the given class.
*/
protected final NumberInterface piFor(Class<? extends NumberInterface> forClass) {
protected final NumberInterface getPi(Class<? extends NumberInterface> forClass) {
return manager.piFor(forClass);
}

View File

@@ -23,17 +23,20 @@ public class PluginManager {
*/
private Set<Plugin> plugins;
/**
* The map of functions registered by the plugins.
* List of functions that have been cached,
* that is, found in a plugin and returned.
*/
private Map<String, Function> registeredFunctions;
private Map<String, Function> cachedFunctions;
/**
* The map of operators registered by the plugins
* List of operators that have been cached,
* that is, found in a plugin and returned.
*/
private Map<String, Operator> registeredOperators;
private Map<String, Operator> cachedOperators;
/**
* The map of number implementations registered by the plugins.
* The list of number implementations that have
* been cached, that is, found in a plugin and returned.
*/
private Map<String, NumberImplementation> registeredNumberImplementations;
private Map<String, NumberImplementation> cachedNumberImplementations;
/**
* The list of number implementations that have been
* found by their implementation class.
@@ -43,6 +46,18 @@ public class PluginManager {
* The pi values for each implementation class that have already been computer.
*/
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi;
/**
* List of all functions loaded by the plugins.
*/
private Set<String> allFunctions;
/**
* List of all operators loaded by the plugins.
*/
private Set<String> allOperators;
/**
* List of all the number implementations loaded by the plugins.
*/
private Set<String> allNumberImplementations;
/**
* The list of plugin listeners attached to this instance.
*/
@@ -62,66 +77,79 @@ public class PluginManager {
this.abacus = abacus;
loadedPluginClasses = new HashSet<>();
plugins = new HashSet<>();
registeredFunctions = new HashMap<>();
registeredOperators = new HashMap<>();
registeredNumberImplementations = new HashMap<>();
cachedFunctions = new HashMap<>();
cachedOperators = new HashMap<>();
cachedNumberImplementations = new HashMap<>();
cachedInterfaceImplementations = new HashMap<>();
cachedPi = new HashMap<>();
allFunctions = new HashSet<>();
allOperators = new HashSet<>();
allNumberImplementations = new HashSet<>();
listeners = new HashSet<>();
}
/**
* Registers a function under the given name.
* @param name the name of the function.
* @param function the function to register.
* Searches the plugin list for a certain value, retrieving the Plugin's
* 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 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 <K> the type of key that the cache is indexed by.
* @return the retrieved element, or null if it was not found.
*/
public void registerFunction(String name, Function function){
registeredFunctions.put(name, function);
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
java.util.function.Function<Plugin, Set<K>> setFunction,
java.util.function.BiFunction<Plugin, K, T> getFunction,
K name) {
if (cache.containsKey(name)) return cache.get(name);
T loadedValue = null;
for (Plugin plugin : plugins) {
if (setFunction.apply(plugin).contains(name)) {
loadedValue = getFunction.apply(plugin, name);
break;
}
}
cache.put(name, loadedValue);
return loadedValue;
}
/**
* Registers an operator under the given name.
* 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) {
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
}
/**
* Gets an operator under the given name.
*
* @param name the name of the operator.
* @param operator the operator to register.
* @return the operator under the given name.
*/
public void registerOperator(String name, Operator operator){
registeredOperators.put(name, operator);
public Operator operatorFor(String name) {
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
}
/**
* Registers a number implementation under the given name.
* @param name the name of the number implementation.
* @param implementation the number implementation to register.
* Gets the number implementation under the given name.
*
* @param name the name of the implementation.
* @return the implementation.
*/
public void registerNumberImplementation(String name, NumberImplementation implementation){
registeredNumberImplementations.put(name, implementation);
}
/**
* Gets the function registered under the given name.
* @param name the name of the function.
* @return the function, or null if it was not found.
*/
public Function functionFor(String name){
return registeredFunctions.get(name);
}
/**
* Gets the operator registered under the given name.
* @param name the name of the operator.
* @return the operator, or null if it was not found.
*/
public Operator operatorFor(String name){
return registeredOperators.get(name);
}
/**
* Gets the number implementation registered under the given name.
* @param name the name of the number implementation.
* @return the number implementation, or null if it was not found.
*/
public NumberImplementation numberImplementationFor(String name){
return registeredNumberImplementations.get(name);
public NumberImplementation numberImplementationFor(String name) {
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations,
Plugin::getNumberImplementation, name);
}
/**
@@ -133,11 +161,14 @@ public class PluginManager {
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
NumberImplementation toReturn = null;
for(String key : registeredNumberImplementations.keySet()){
NumberImplementation implementation = registeredNumberImplementations.get(key);
if(implementation.getImplementation() == name) {
toReturn = implementation;
break;
outside:
for (Plugin plugin : plugins) {
for (String implementationName : plugin.providedNumberImplementations()) {
NumberImplementation implementation = plugin.getNumberImplementation(implementationName);
if (implementation.getImplementation().equals(name)) {
toReturn = implementation;
break outside;
}
}
}
cachedInterfaceImplementations.put(name, toReturn);
@@ -196,6 +227,12 @@ public class PluginManager {
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
plugin.enable();
}
for (Plugin plugin : plugins) {
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
allFunctions.addAll(plugin.providedFunctions());
allOperators.addAll(plugin.providedOperators());
allNumberImplementations.addAll(plugin.providedNumberImplementations());
}
listeners.forEach(e -> e.onLoad(this));
}
@@ -209,8 +246,14 @@ public class PluginManager {
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
plugin.disable();
}
cachedFunctions.clear();
cachedOperators.clear();
cachedNumberImplementations.clear();
cachedInterfaceImplementations.clear();
cachedPi.clear();
allFunctions.clear();
allOperators.clear();
allNumberImplementations.clear();
listeners.forEach(e -> e.onUnload(this));
}
@@ -228,7 +271,7 @@ public class PluginManager {
* @return the set of all functions that were loaded.
*/
public Set<String> getAllFunctions() {
return registeredFunctions.keySet();
return allFunctions;
}
/**
@@ -237,7 +280,7 @@ public class PluginManager {
* @return the set of all operators that were loaded.
*/
public Set<String> getAllOperators() {
return registeredOperators.keySet();
return allOperators;
}
/**
@@ -246,7 +289,7 @@ public class PluginManager {
* @return the set of all implementations that were loaded.
*/
public Set<String> getAllNumberImplementations() {
return registeredNumberImplementations.keySet();
return allNumberImplementations;
}
/**

View File

@@ -18,6 +18,11 @@ import java.util.function.BiFunction;
*/
public class StandardPlugin extends Plugin {
/**
* Stores objects of NumberInterface with integer values for reuse.
*/
private final static HashMap<Class<? extends NumberInterface>, HashMap<Integer, NumberInterface>> integerValues = new HashMap<>();
/**
* The addition operator, +
*/
@@ -112,7 +117,7 @@ public class StandardPlugin extends Plugin {
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
if (params[0].signum() == 0) {
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
return fromInt(params[0].getClass(), 1);
}
NumberInterface factorial = params[0];
NumberInterface multiplier = params[0];
@@ -157,14 +162,14 @@ public class StandardPlugin extends Plugin {
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) {
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
param = param.divide(fromInt(param.getClass(), 2));
powersOf2++;
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
break;
//No infinite loop for you.
}
} else {
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
param = param.multiply(fromInt(param.getClass(), 2));
powersOf2--;
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
break;
@@ -203,17 +208,17 @@ public class StandardPlugin extends Plugin {
*/
private NumberInterface getLog2(NumberInterface number) {
NumberInterface maxError = getMaxError(number);
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
//NumberInterface errorBound = fromInt(number.getClass(), 1);
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
//In the following, a=1/3^n, b=1/4^n, c = 1/n.
//a is also an error bound.
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
NumberInterface a = fromInt(number.getClass(), 1), b = a, c = a;
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
int n = 0;
while (a.compareTo(maxError) >= 1) {
n++;
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
a = a.divide(fromInt(number.getClass(), 3));
b = b.divide(fromInt(number.getClass(), 4));
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
sum = sum.add(a.add(b).multiply(c));
}
@@ -311,7 +316,7 @@ public class StandardPlugin extends Plugin {
//right and left refer to lhs and rhs in the above inequality.
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
NumberInterface nextNumerator = params[0];
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
NumberInterface left = params[0].multiply(fromInt(params[0].getClass(), 3).intPow(params[0].ceiling().intValue())), right = maxError;
do {
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
n++;
@@ -358,13 +363,13 @@ public class StandardPlugin extends Plugin {
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface pi = piFor(params[0].getClass());
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
NumberInterface pi = getPi(params[0].getClass());
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2));
NumberInterface theta = getSmallAngle(params[0], pi);
//System.out.println(theta);
if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) {
theta = theta.subtract(twoPi);
} else if (theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0) {
} else if (theta.compareTo(pi.divide(fromInt(pi.getClass(), 2))) > 0) {
theta = pi.subtract(theta);
}
//System.out.println(theta);
@@ -382,7 +387,7 @@ public class StandardPlugin extends Plugin {
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
return functionSin.apply(piFor(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
return functionSin.apply(getPi(params[0].getClass()).divide(fromInt(params[0].getClass(), 2))
.subtract(params[0]));
}
};
@@ -470,7 +475,7 @@ public class StandardPlugin extends Plugin {
* @return the maximum error.
*/
private static NumberInterface getMaxError(NumberInterface number) {
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
return fromInt(number.getClass(), 10).intPow(-number.getMaxPrecision());
}
/**
@@ -531,6 +536,22 @@ public class StandardPlugin extends Plugin {
return theta;
}
/**
* Returns a number of class numType with value n.
* @param numType class of number to return.
* @param n value of returned number.
* @return numClass instance with value n.
*/
private static NumberInterface fromInt(Class<? extends NumberInterface> numType, int n){
if(!integerValues.containsKey(numType)){
integerValues.put(numType, new HashMap<>());
}
if(!integerValues.get(numType).containsKey(n)){
integerValues.get(numType).put(n, new NaiveNumber(n).promoteTo(numType));
}
return integerValues.get(numType).get(n);
}
@Override
public void onEnable() {
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);