mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
15 Commits
stoppable
...
constant-p
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f02867a4e | |||
| 691118c206 | |||
| 819fff6391 | |||
|
|
9b71f9aaf4 | ||
|
|
cf953da40a | ||
|
|
27ad10c0f1 | ||
|
|
601c4fea55 | ||
|
|
52fbfd5134 | ||
|
|
b31151384d | ||
|
|
1ee8c7d231 | ||
|
|
f97d16c640 | ||
|
|
a0bba03c2c | ||
|
|
8666e96420 | ||
|
|
fd40e6b297 | ||
|
|
79ccd61af3 |
@@ -93,6 +93,26 @@ public class NaiveNumber implements NumberInterface {
|
|||||||
return this.compareTo(ZERO);
|
return this.compareTo(ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface ceiling() {
|
||||||
|
return new NaiveNumber(Math.ceil(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface floor() {
|
||||||
|
return new NaiveNumber(Math.floor(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface fractionalPart() {
|
||||||
|
return new NaiveNumber(value - Math.floor(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int intValue() {
|
||||||
|
return (int)value;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
if (toClass == this.getClass()) return this;
|
if (toClass == this.getClass()) return this;
|
||||||
|
|||||||
@@ -79,6 +79,31 @@ public interface NumberInterface {
|
|||||||
*/
|
*/
|
||||||
int signum();
|
int signum();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the least integer greater than or equal to the number.
|
||||||
|
* @return the least integer >= the number, if int can hold the value.
|
||||||
|
*/
|
||||||
|
NumberInterface ceiling();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the greatest integer less than or equal to the number.
|
||||||
|
* @return the greatest int >= the number, if int can hold the value.
|
||||||
|
*/
|
||||||
|
NumberInterface floor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the fractional part of the number.
|
||||||
|
* @return the fractional part of the number.
|
||||||
|
*/
|
||||||
|
NumberInterface fractionalPart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the integer representation of this number, discarding any fractional part,
|
||||||
|
* if int can hold the value.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int intValue();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promotes this class to another number class.
|
* Promotes this class to another number class.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.nwapw.abacus.number;
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
public class PreciseNumber implements NumberInterface {
|
public class PreciseNumber implements NumberInterface {
|
||||||
@@ -44,12 +45,12 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getMaxPrecision() {
|
public int getMaxPrecision() {
|
||||||
return 54;
|
return 65;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface multiply(NumberInterface multiplier) {
|
public NumberInterface multiply(NumberInterface multiplier) {
|
||||||
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
|
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -94,6 +95,41 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
return value.signum();
|
return value.signum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface ceiling() {
|
||||||
|
String str = value.toPlainString();
|
||||||
|
int decimalIndex = str.indexOf('.');
|
||||||
|
if(decimalIndex != -1){
|
||||||
|
return this.floor().add(ONE);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface floor() {
|
||||||
|
String str = value.toPlainString();
|
||||||
|
int decimalIndex = str.indexOf('.');
|
||||||
|
if(decimalIndex != -1){
|
||||||
|
return new PreciseNumber(str.substring(0, decimalIndex));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface fractionalPart() {
|
||||||
|
String str = value.toPlainString();
|
||||||
|
int decimalIndex = str.indexOf('.');
|
||||||
|
if(decimalIndex != -1){
|
||||||
|
return new PreciseNumber(str.substring(decimalIndex + 1));
|
||||||
|
}
|
||||||
|
return ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int intValue() {
|
||||||
|
return value.intValue();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface negate() {
|
public NumberInterface negate() {
|
||||||
return new PreciseNumber(value.negate());
|
return new PreciseNumber(value.negate());
|
||||||
@@ -109,7 +145,7 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
BigDecimal rounded = value.setScale(getMaxPrecision() - 4, RoundingMode.HALF_UP);
|
BigDecimal rounded = value.setScale(getMaxPrecision() - 15, RoundingMode.HALF_UP);
|
||||||
return rounded.stripTrailingZeros().toPlainString();
|
return rounded.stripTrailingZeros().toPlainString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ public abstract class Plugin {
|
|||||||
* A hash map of operators mapped to their string names.
|
* A hash map of operators mapped to their string names.
|
||||||
*/
|
*/
|
||||||
private Map<String, Class<? extends NumberInterface>> numbers;
|
private Map<String, Class<? extends NumberInterface>> numbers;
|
||||||
|
/**
|
||||||
|
* A hash map of constant providers for each number type.
|
||||||
|
*/
|
||||||
|
private Map<Class<?>, java.util.function.Function<String, NumberInterface>> constantProviders;
|
||||||
/**
|
/**
|
||||||
* The plugin manager in which to search for functions
|
* The plugin manager in which to search for functions
|
||||||
* not inside this package,
|
* not inside this package,
|
||||||
@@ -52,6 +56,7 @@ public abstract class Plugin {
|
|||||||
functions = new HashMap<>();
|
functions = new HashMap<>();
|
||||||
operators = new HashMap<>();
|
operators = new HashMap<>();
|
||||||
numbers = new HashMap<>();
|
numbers = new HashMap<>();
|
||||||
|
constantProviders = new HashMap<>();
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +87,14 @@ public abstract class Plugin {
|
|||||||
return numbers.keySet();
|
return numbers.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of all constant providers provided by this plugin.
|
||||||
|
* @return the list of constant providers.
|
||||||
|
*/
|
||||||
|
public final Set<Class<?>> providedConstantProviders() {
|
||||||
|
return constantProviders.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a function under the given function name.
|
* Gets a function under the given function name.
|
||||||
*
|
*
|
||||||
@@ -112,6 +125,16 @@ public abstract class Plugin {
|
|||||||
return numbers.get(numberName);
|
return numbers.get(numberName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the constant provider for the given class.
|
||||||
|
*
|
||||||
|
* @param pluginClass the class for which to provide constants.
|
||||||
|
* @return the provider, or null, if the plugin doesn't provide it.
|
||||||
|
*/
|
||||||
|
public final java.util.function.Function<String, NumberInterface> getConstantProvider(Class<?> pluginClass){
|
||||||
|
return constantProviders.get(pluginClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the function, loading the necessary instances
|
* Enables the function, loading the necessary instances
|
||||||
* of functions.
|
* of functions.
|
||||||
@@ -131,6 +154,8 @@ public abstract class Plugin {
|
|||||||
onDisable();
|
onDisable();
|
||||||
functions.clear();
|
functions.clear();
|
||||||
operators.clear();
|
operators.clear();
|
||||||
|
numbers.clear();
|
||||||
|
constantProviders.clear();
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +195,19 @@ public abstract class Plugin {
|
|||||||
numbers.put(name, toRegister);
|
numbers.put(name, toRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To be used in load(). Registers a constant provider
|
||||||
|
* with the plugin internally, which makes it possible
|
||||||
|
* for the calculations to look up constants for each different
|
||||||
|
* number type.
|
||||||
|
* @param providerFor the class the provider works with.
|
||||||
|
* @param constantProvider the provider to register.
|
||||||
|
*/
|
||||||
|
protected final void registerConstantProvider(Class<?> providerFor,
|
||||||
|
java.util.function.Function<String, NumberInterface> constantProvider) {
|
||||||
|
constantProviders.put(providerFor, constantProvider);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the PluginManager for the given function name.
|
* Searches the PluginManager for the given function name.
|
||||||
* This can be used by the plugins internally in order to call functions
|
* This can be used by the plugins internally in order to call functions
|
||||||
@@ -194,6 +232,30 @@ public abstract class Plugin {
|
|||||||
return manager.operatorFor(name);
|
return manager.operatorFor(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the PluginManager for the given number implementation.
|
||||||
|
* This can be used by the plugins internally in order to
|
||||||
|
* find classes by name that they do not provide.
|
||||||
|
*
|
||||||
|
* @param name the name for which to search
|
||||||
|
* @return the resulting number class.
|
||||||
|
*/
|
||||||
|
protected final Class<? extends NumberInterface> numberFor(String name) {
|
||||||
|
return manager.numberFor(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the PluginManager for the given constant provider.
|
||||||
|
* This can be used by the plugins internally in order
|
||||||
|
* to find constant providers for number provider they do not provide.
|
||||||
|
*
|
||||||
|
* @param forClass the class for which to get a generator for.
|
||||||
|
* @return the resulting generator
|
||||||
|
*/
|
||||||
|
protected final java.util.function.Function<String, NumberInterface> constantProviderFor(Class<?> forClass){
|
||||||
|
return manager.constantProviderFor(forClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract method to be overridden by plugin implementation, in which the plugins
|
* Abstract method to be overridden by plugin implementation, in which the plugins
|
||||||
* are supposed to register the functions they provide and do any other
|
* are supposed to register the functions they provide and do any other
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ public class PluginManager {
|
|||||||
* been cached, that is, found in a plugin and returned.
|
* been cached, that is, found in a plugin and returned.
|
||||||
*/
|
*/
|
||||||
private Map<String, Class<? extends NumberInterface>> cachedNumbers;
|
private Map<String, Class<? extends NumberInterface>> cachedNumbers;
|
||||||
|
/**
|
||||||
|
* List of registered constant providers for every
|
||||||
|
* number class.
|
||||||
|
*/
|
||||||
|
private Map<Class<?>, java.util.function.Function<String, NumberInterface>> cachedConstantProviders;
|
||||||
/**
|
/**
|
||||||
* List of all functions loaded by the plugins.
|
* List of all functions loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
@@ -48,6 +53,10 @@ public class PluginManager {
|
|||||||
* List of all numbers loaded by the plugins.
|
* List of all numbers loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private Set<String> allNumbers;
|
private Set<String> allNumbers;
|
||||||
|
/**
|
||||||
|
* List of all the constant providers loaded by the plugins.
|
||||||
|
*/
|
||||||
|
private Set<Class<?>> allConstantProviders;
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
@@ -62,9 +71,11 @@ public class PluginManager {
|
|||||||
cachedFunctions = new HashMap<>();
|
cachedFunctions = new HashMap<>();
|
||||||
cachedOperators = new HashMap<>();
|
cachedOperators = new HashMap<>();
|
||||||
cachedNumbers = new HashMap<>();
|
cachedNumbers = new HashMap<>();
|
||||||
|
cachedConstantProviders = new HashMap<>();
|
||||||
allFunctions = new HashSet<>();
|
allFunctions = new HashSet<>();
|
||||||
allOperators = new HashSet<>();
|
allOperators = new HashSet<>();
|
||||||
allNumbers = new HashSet<>();
|
allNumbers = new HashSet<>();
|
||||||
|
allConstantProviders = new HashSet<>();
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,12 +91,13 @@ public class PluginManager {
|
|||||||
* @param getFunction the function to get the T value under the given name
|
* @param getFunction the function to get the T value under the given name
|
||||||
* @param name the name to search for
|
* @param name the name to search for
|
||||||
* @param <T> the type of element being search
|
* @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.
|
* @return the retrieved element, or null if it was not found.
|
||||||
*/
|
*/
|
||||||
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
|
||||||
java.util.function.Function<Plugin, Set<String>> setFunction,
|
java.util.function.Function<Plugin, Set<K>> setFunction,
|
||||||
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
java.util.function.BiFunction<Plugin, K, T> getFunction,
|
||||||
String name) {
|
K name) {
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
if (cache.containsKey(name)) return cache.get(name);
|
||||||
|
|
||||||
T loadedValue = null;
|
T loadedValue = null;
|
||||||
@@ -130,6 +142,15 @@ public class PluginManager {
|
|||||||
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the constant provider for the given class.
|
||||||
|
* @param forClass the class to get the provider for.
|
||||||
|
* @return the provider.
|
||||||
|
*/
|
||||||
|
public java.util.function.Function<String, NumberInterface> constantProviderFor(Class<?> forClass){
|
||||||
|
return searchCached(plugins, cachedConstantProviders, Plugin::providedConstantProviders, Plugin::getConstantProvider, forClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an instance of Plugin that already has been instantiated.
|
* Adds an instance of Plugin that already has been instantiated.
|
||||||
*
|
*
|
||||||
@@ -165,6 +186,7 @@ public class PluginManager {
|
|||||||
allFunctions.addAll(plugin.providedFunctions());
|
allFunctions.addAll(plugin.providedFunctions());
|
||||||
allOperators.addAll(plugin.providedOperators());
|
allOperators.addAll(plugin.providedOperators());
|
||||||
allNumbers.addAll(plugin.providedNumbers());
|
allNumbers.addAll(plugin.providedNumbers());
|
||||||
|
allConstantProviders.addAll(plugin.providedConstantProviders());
|
||||||
}
|
}
|
||||||
listeners.forEach(e -> e.onLoad(this));
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
}
|
}
|
||||||
@@ -177,6 +199,7 @@ public class PluginManager {
|
|||||||
allFunctions.clear();
|
allFunctions.clear();
|
||||||
allOperators.clear();
|
allOperators.clear();
|
||||||
allNumbers.clear();
|
allNumbers.clear();
|
||||||
|
allConstantProviders.clear();
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,6 +238,14 @@ public class PluginManager {
|
|||||||
return allNumbers;
|
return allNumbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the constant providers loaded by the Plugin Manager.
|
||||||
|
* @return the set of all constant providers that were loaded.
|
||||||
|
*/
|
||||||
|
public Set<Class<?>> getAllConstantProviders() {
|
||||||
|
return allConstantProviders;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a plugin change listener to this plugin manager.
|
* Adds a plugin change listener to this plugin manager.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ import org.nwapw.abacus.number.NaiveNumber;
|
|||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.number.PreciseNumber;
|
import org.nwapw.abacus.number.PreciseNumber;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,6 +19,9 @@ import java.util.function.BiFunction;
|
|||||||
*/
|
*/
|
||||||
public class StandardPlugin extends Plugin {
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
|
private static HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> factorialLists = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||||
|
static HashMap<Class<? extends NumberInterface>, NumberInterface> piValues = new HashMap<Class<? extends NumberInterface>, NumberInterface>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The addition operator, +
|
* The addition operator, +
|
||||||
*/
|
*/
|
||||||
@@ -152,17 +158,40 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
boolean takeReciprocal = params[0].signum() == -1;
|
NumberInterface maxError = getMaxError(params[0]);
|
||||||
params[0] = FUNCTION_ABS.apply(params[0]);
|
int n = 0;
|
||||||
NumberInterface sum = sumSeries(params[0], StandardPlugin::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
if(params[0].signum() <= 0){
|
||||||
if (takeReciprocal) {
|
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||||
sum = NaiveNumber.ONE.promoteTo(sum.getClass()).divide(sum);
|
while(FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){
|
||||||
|
n++;
|
||||||
|
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
||||||
|
sum = sum.add(currentTerm);
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
||||||
|
//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;
|
||||||
|
do{
|
||||||
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
||||||
|
n++;
|
||||||
|
nextNumerator = nextNumerator.multiply(params[0]);
|
||||||
|
left = left.multiply(params[0]);
|
||||||
|
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
|
||||||
|
right = right.multiply(nextN);
|
||||||
|
//System.out.println(left + ", " + right);
|
||||||
|
}
|
||||||
|
while(left.compareTo(right) > 0);
|
||||||
|
//System.out.println(n+1);
|
||||||
|
return sum;
|
||||||
}
|
}
|
||||||
return sum;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* The natural log function, ln(exp(1)) = 1
|
* The natural log function.
|
||||||
*/
|
*/
|
||||||
public static final Function FUNCTION_LN = new Function() {
|
public static final Function FUNCTION_LN = new Function() {
|
||||||
@Override
|
@Override
|
||||||
@@ -239,7 +268,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* The square root function, sqrt(4) = 2
|
* The square root function.
|
||||||
*/
|
*/
|
||||||
public static final Function FUNCTION_SQRT = new Function() {
|
public static final Function FUNCTION_SQRT = new Function() {
|
||||||
@Override
|
@Override
|
||||||
@@ -253,43 +282,36 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The sine function (the argument is interpreted in radians).
|
||||||
|
*/
|
||||||
|
public static final Function FUNCTION_SIN = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
NumberInterface pi = getPi(params[0].getClass());
|
||||||
|
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
||||||
|
NumberInterface theta = getSmallAngle(params[0]);
|
||||||
|
//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){
|
||||||
|
theta = pi.subtract(theta);
|
||||||
|
}
|
||||||
|
//System.out.println(theta);
|
||||||
|
return sinTaylor(theta);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public StandardPlugin(PluginManager manager) {
|
public StandardPlugin(PluginManager manager) {
|
||||||
super(manager);
|
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 evaluate 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.
|
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
||||||
*
|
*
|
||||||
@@ -332,6 +354,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
registerFunction("exp", FUNCTION_EXP);
|
registerFunction("exp", FUNCTION_EXP);
|
||||||
registerFunction("ln", FUNCTION_LN);
|
registerFunction("ln", FUNCTION_LN);
|
||||||
registerFunction("sqrt", FUNCTION_SQRT);
|
registerFunction("sqrt", FUNCTION_SQRT);
|
||||||
|
registerFunction("sin", FUNCTION_SIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -339,4 +362,93 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A factorial function that uses memoization for each number class; it efficiently
|
||||||
|
* computes factorials of non-negative integers.
|
||||||
|
* @param numberClass type of number to return.
|
||||||
|
* @param n non-negative integer.
|
||||||
|
* @return a number of numClass with value n factorial.
|
||||||
|
*/
|
||||||
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||||
|
if(!factorialLists.containsKey(numberClass)){
|
||||||
|
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
||||||
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
|
}
|
||||||
|
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
||||||
|
if(n >= list.size()){
|
||||||
|
while(list.size() < n + 16){
|
||||||
|
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list.get(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the Taylor series for sin (centered at 0) at x.
|
||||||
|
* @param x where the series is evaluated.
|
||||||
|
* @return the value of the series
|
||||||
|
*/
|
||||||
|
private static NumberInterface sinTaylor(NumberInterface x){
|
||||||
|
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
||||||
|
NumberInterface maxError = getMaxError(x);
|
||||||
|
int n = 1;
|
||||||
|
do{
|
||||||
|
n += 2;
|
||||||
|
power = power.multiply(multiplier);
|
||||||
|
currentTerm = power.divide(factorial(x.getClass(), n));
|
||||||
|
sum = sum.add(currentTerm);
|
||||||
|
} while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an approximation of Pi, with appropriate accuracy for given number class.
|
||||||
|
* @param numClass type of number.
|
||||||
|
* @return A number of class numClass, with value approximately Pi = 3.1415...
|
||||||
|
*/
|
||||||
|
public static NumberInterface getPi(Class<? extends NumberInterface> numClass){
|
||||||
|
if(!piValues.containsKey(numClass)){
|
||||||
|
//https://en.wikipedia.org/wiki/Chudnovsky_algorithm
|
||||||
|
NumberInterface C = FUNCTION_SQRT.apply(new NaiveNumber(10005).promoteTo(numClass)).multiply(new NaiveNumber(426880).promoteTo(numClass));
|
||||||
|
NumberInterface M = NaiveNumber.ONE.promoteTo(numClass);
|
||||||
|
NumberInterface L = new NaiveNumber(13591409).promoteTo(numClass);
|
||||||
|
NumberInterface X = M;
|
||||||
|
NumberInterface sum = L;
|
||||||
|
int termsNeeded = C.getMaxPrecision()/13 + 1;
|
||||||
|
|
||||||
|
NumberInterface lSummand = new NaiveNumber(545140134).promoteTo(L.getClass());
|
||||||
|
NumberInterface xMultiplier = new NaiveNumber(262537412).promoteTo(X.getClass())
|
||||||
|
.multiply(new NaiveNumber(1000000000).promoteTo(X.getClass()))
|
||||||
|
.add(new NaiveNumber(640768000).promoteTo(X.getClass()))
|
||||||
|
.negate();
|
||||||
|
for(int i = 0; i < termsNeeded; i++){
|
||||||
|
M = M
|
||||||
|
.multiply(new NaiveNumber(12*i+2).promoteTo(M.getClass()))
|
||||||
|
.multiply(new NaiveNumber(12*i+6).promoteTo(M.getClass()))
|
||||||
|
.multiply(new NaiveNumber(12*i+10).promoteTo(M.getClass()))
|
||||||
|
.divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(M.getClass()));
|
||||||
|
L = L.add(lSummand);
|
||||||
|
X = X.multiply(xMultiplier);
|
||||||
|
sum = sum.add(M.multiply(L).divide(X));
|
||||||
|
}
|
||||||
|
piValues.put(numClass, C.divide(sum));
|
||||||
|
}
|
||||||
|
return piValues.get(numClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an equivalent angle in the interval [0, 2pi)
|
||||||
|
* @param phi an angle (in radians).
|
||||||
|
* @return theta in [0, 2pi) that differs from phi by a multiple of 2pi.
|
||||||
|
*/
|
||||||
|
private static NumberInterface getSmallAngle(NumberInterface phi){
|
||||||
|
NumberInterface twoPi = getPi(phi.getClass()).multiply(new NaiveNumber("2").promoteTo(phi.getClass()));
|
||||||
|
NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi
|
||||||
|
.multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi).
|
||||||
|
if(phi.signum() < 0){
|
||||||
|
theta = twoPi.subtract(theta);
|
||||||
|
}
|
||||||
|
return theta;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user