mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-23 16:00:09 -08:00
Format code.
This commit is contained in:
parent
ce484cfd43
commit
b78707a0f4
|
@ -41,6 +41,7 @@ public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new configuration with the given values.
|
* Creates a new configuration with the given values.
|
||||||
|
*
|
||||||
* @param numberImplementation the number implementation, like "naive" or "precise"
|
* @param numberImplementation the number implementation, like "naive" or "precise"
|
||||||
* @param disabledPlugins the list of disabled plugins.
|
* @param disabledPlugins the list of disabled plugins.
|
||||||
*/
|
*/
|
||||||
|
@ -51,6 +52,7 @@ public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads a configuration from a given file, keeping non-specified fields default.
|
* Loads a configuration from a given file, keeping non-specified fields default.
|
||||||
|
*
|
||||||
* @param fromFile the file to load from.
|
* @param fromFile the file to load from.
|
||||||
*/
|
*/
|
||||||
public Configuration(File fromFile) {
|
public Configuration(File fromFile) {
|
||||||
|
@ -60,6 +62,7 @@ public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies the values from the given configuration into this one.
|
* Copies the values from the given configuration into this one.
|
||||||
|
*
|
||||||
* @param otherConfiguration the configuration to copy from.
|
* @param otherConfiguration the configuration to copy from.
|
||||||
*/
|
*/
|
||||||
public void copyFrom(Configuration otherConfiguration) {
|
public void copyFrom(Configuration otherConfiguration) {
|
||||||
|
@ -70,6 +73,7 @@ public class Configuration {
|
||||||
/**
|
/**
|
||||||
* Saves this configuration to the given file, creating
|
* Saves this configuration to the given file, creating
|
||||||
* any directories that do not exist.
|
* any directories that do not exist.
|
||||||
|
*
|
||||||
* @param file the file to save to.
|
* @param file the file to save to.
|
||||||
*/
|
*/
|
||||||
public void saveTo(File file) {
|
public void saveTo(File file) {
|
||||||
|
@ -83,6 +87,7 @@ public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation from this configuration.
|
* Gets the number implementation from this configuration.
|
||||||
|
*
|
||||||
* @return the number implementation.
|
* @return the number implementation.
|
||||||
*/
|
*/
|
||||||
public String getNumberImplementation() {
|
public String getNumberImplementation() {
|
||||||
|
@ -91,6 +96,7 @@ public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the number implementation for the configuration
|
* Sets the number implementation for the configuration
|
||||||
|
*
|
||||||
* @param numberImplementation the number implementation.
|
* @param numberImplementation the number implementation.
|
||||||
*/
|
*/
|
||||||
public void setNumberImplementation(String numberImplementation) {
|
public void setNumberImplementation(String numberImplementation) {
|
||||||
|
@ -99,6 +105,7 @@ public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of disabled plugins.
|
* Gets the list of disabled plugins.
|
||||||
|
*
|
||||||
* @return the list of disabled plugins.
|
* @return the list of disabled plugins.
|
||||||
*/
|
*/
|
||||||
public Set<String> getDisabledPlugins() {
|
public Set<String> getDisabledPlugins() {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import java.awt.datatransfer.StringSelection;
|
||||||
/**
|
/**
|
||||||
* A cell that copies its value to the clipboard
|
* A cell that copies its value to the clipboard
|
||||||
* when double clicked.
|
* when double clicked.
|
||||||
|
*
|
||||||
* @param <S> The type of the table view generic type.
|
* @param <S> The type of the table view generic type.
|
||||||
* @param <T> The type of the value contained in the cell.
|
* @param <T> The type of the value contained in the cell.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -26,6 +26,7 @@ public class HistoryModel {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new history model with the given variables.
|
* Creates a new history model with the given variables.
|
||||||
|
*
|
||||||
* @param input the user input
|
* @param input the user input
|
||||||
* @param parsed the parsed input
|
* @param parsed the parsed input
|
||||||
* @param output the program output.
|
* @param output the program output.
|
||||||
|
@ -41,13 +42,16 @@ public class HistoryModel {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the input property.
|
* Gets the input property.
|
||||||
|
*
|
||||||
* @return the input property.
|
* @return the input property.
|
||||||
*/
|
*/
|
||||||
public StringProperty inputProperty() {
|
public StringProperty inputProperty() {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the input.
|
* Gets the input.
|
||||||
|
*
|
||||||
* @return the input.
|
* @return the input.
|
||||||
*/
|
*/
|
||||||
public String getInput() {
|
public String getInput() {
|
||||||
|
@ -56,13 +60,16 @@ public class HistoryModel {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the parsed input property.
|
* Gets the parsed input property.
|
||||||
|
*
|
||||||
* @return the parsed input property.
|
* @return the parsed input property.
|
||||||
*/
|
*/
|
||||||
public StringProperty parsedProperty() {
|
public StringProperty parsedProperty() {
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the parsed input.
|
* Gets the parsed input.
|
||||||
|
*
|
||||||
* @return the parsed input.
|
* @return the parsed input.
|
||||||
*/
|
*/
|
||||||
public String getParsed() {
|
public String getParsed() {
|
||||||
|
@ -71,13 +78,16 @@ public class HistoryModel {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the output property.
|
* Gets the output property.
|
||||||
|
*
|
||||||
* @return the output property.
|
* @return the output property.
|
||||||
*/
|
*/
|
||||||
public StringProperty outputProperty() {
|
public StringProperty outputProperty() {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the program output.
|
* Gets the program output.
|
||||||
|
*
|
||||||
* @return the output.
|
* @return the output.
|
||||||
*/
|
*/
|
||||||
public String getOutput() {
|
public String getOutput() {
|
||||||
|
|
|
@ -20,6 +20,7 @@ public class ToggleablePlugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new toggleable plugin with the given properties.
|
* Creates a new toggleable plugin with the given properties.
|
||||||
|
*
|
||||||
* @param enabled the enabled / disabled state at the beginning.
|
* @param enabled the enabled / disabled state at the beginning.
|
||||||
* @param className the name of the class this plugin toggles.
|
* @param className the name of the class this plugin toggles.
|
||||||
*/
|
*/
|
||||||
|
@ -31,6 +32,7 @@ public class ToggleablePlugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the enabled property of this plugin.
|
* Gets the enabled property of this plugin.
|
||||||
|
*
|
||||||
* @return the enabled property.
|
* @return the enabled property.
|
||||||
*/
|
*/
|
||||||
public BooleanProperty enabledProperty() {
|
public BooleanProperty enabledProperty() {
|
||||||
|
@ -39,6 +41,7 @@ public class ToggleablePlugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if this plugin entry should be enabled.
|
* Checks if this plugin entry should be enabled.
|
||||||
|
*
|
||||||
* @return whether this plugin will be enabled.
|
* @return whether this plugin will be enabled.
|
||||||
*/
|
*/
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
|
@ -47,6 +50,7 @@ public class ToggleablePlugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the class name this plugin toggles.
|
* Gets the class name this plugin toggles.
|
||||||
|
*
|
||||||
* @return the class name that should be disabled.
|
* @return the class name that should be disabled.
|
||||||
*/
|
*/
|
||||||
public String getClassName() {
|
public String getClassName() {
|
||||||
|
|
|
@ -26,6 +26,7 @@ public class NaiveNumber implements NumberInterface {
|
||||||
public NaiveNumber(String value) {
|
public NaiveNumber(String value) {
|
||||||
this(Double.parseDouble(value));
|
this(Double.parseDouble(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new NaiveNumber with the given value.
|
* Creates a new NaiveNumber with the given value.
|
||||||
*
|
*
|
||||||
|
|
|
@ -81,18 +81,21 @@ public interface NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the least integer greater than or equal to the number.
|
* Returns the least integer greater than or equal to the number.
|
||||||
|
*
|
||||||
* @return the least integer >= the number, if int can hold the value.
|
* @return the least integer >= the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
NumberInterface ceiling();
|
NumberInterface ceiling();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the greatest integer less than or equal to the number.
|
* Return the greatest integer less than or equal to the number.
|
||||||
|
*
|
||||||
* @return the greatest int >= the number, if int can hold the value.
|
* @return the greatest int >= the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
NumberInterface floor();
|
NumberInterface floor();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the fractional part of the number.
|
* Returns the fractional part of the number.
|
||||||
|
*
|
||||||
* @return the fractional part of the number.
|
* @return the fractional part of the number.
|
||||||
*/
|
*/
|
||||||
NumberInterface fractionalPart();
|
NumberInterface fractionalPart();
|
||||||
|
@ -100,6 +103,7 @@ public interface NumberInterface {
|
||||||
/**
|
/**
|
||||||
* Returns the integer representation of this number, discarding any fractional part,
|
* Returns the integer representation of this number, discarding any fractional part,
|
||||||
* if int can hold the value.
|
* if int can hold the value.
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
int intValue();
|
int intValue();
|
||||||
|
|
|
@ -26,6 +26,7 @@ public abstract class NumberImplementation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new number implementation with the given data.
|
* Creates a new number implementation with the given data.
|
||||||
|
*
|
||||||
* @param implementation the implementation class.
|
* @param implementation the implementation class.
|
||||||
* @param priority the priority, higher -> more likely to be converted into.
|
* @param priority the priority, higher -> more likely to be converted into.
|
||||||
*/
|
*/
|
||||||
|
@ -37,6 +38,7 @@ public abstract class NumberImplementation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of all promotion paths this implementation can take.
|
* Gets the list of all promotion paths this implementation can take.
|
||||||
|
*
|
||||||
* @return the map of documentation paths.
|
* @return the map of documentation paths.
|
||||||
*/
|
*/
|
||||||
public final Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> getPromotionPaths() {
|
public final Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> getPromotionPaths() {
|
||||||
|
@ -45,6 +47,7 @@ public abstract class NumberImplementation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the implementation class used by this implementation.
|
* Gets the implementation class used by this implementation.
|
||||||
|
*
|
||||||
* @return the implementation class.
|
* @return the implementation class.
|
||||||
*/
|
*/
|
||||||
public final Class<? extends NumberInterface> getImplementation() {
|
public final Class<? extends NumberInterface> getImplementation() {
|
||||||
|
@ -53,6 +56,7 @@ public abstract class NumberImplementation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the priority of this number implementation.
|
* Gets the priority of this number implementation.
|
||||||
|
*
|
||||||
* @return the priority.
|
* @return the priority.
|
||||||
*/
|
*/
|
||||||
public final int getPriority() {
|
public final int getPriority() {
|
||||||
|
@ -61,6 +65,7 @@ public abstract class NumberImplementation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract function to create a new instance from a string.
|
* Abstract function to create a new instance from a string.
|
||||||
|
*
|
||||||
* @param string the string to create a number from.
|
* @param string the string to create a number from.
|
||||||
* @return the resulting number.
|
* @return the resulting number.
|
||||||
*/
|
*/
|
||||||
|
@ -68,6 +73,7 @@ public abstract class NumberImplementation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the instance of pi with the given implementation.
|
* Get the instance of pi with the given implementation.
|
||||||
|
*
|
||||||
* @return pi
|
* @return pi
|
||||||
*/
|
*/
|
||||||
public abstract NumberInterface instanceForPi();
|
public abstract NumberInterface instanceForPi();
|
||||||
|
|
|
@ -160,6 +160,7 @@ public abstract class Plugin {
|
||||||
/**
|
/**
|
||||||
* To be used in load(). Registers a new number implementation with the plugin.
|
* To be used in load(). Registers a new number implementation with the plugin.
|
||||||
* This makes it accessible to the plugin manager.
|
* This makes it accessible to the plugin manager.
|
||||||
|
*
|
||||||
* @param name the name of the implementation.
|
* @param name the name of the implementation.
|
||||||
* @param implementation the actual implementation class to register.
|
* @param implementation the actual implementation class to register.
|
||||||
*/
|
*/
|
||||||
|
@ -208,6 +209,7 @@ public abstract class Plugin {
|
||||||
* This is done so that number implementations with various degrees of precision
|
* This is done so that number implementations with various degrees of precision
|
||||||
* can provide their own pi values, without losing said precision by
|
* can provide their own pi values, without losing said precision by
|
||||||
* promoting NaiveNumbers.
|
* promoting NaiveNumbers.
|
||||||
|
*
|
||||||
* @param forClass the class to which to find the pi instance.
|
* @param forClass the class to which to find the pi instance.
|
||||||
* @return the pi value for the given class.
|
* @return the pi value for the given class.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -141,6 +141,7 @@ public class PluginManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation under the given name.
|
* Gets the number implementation under the given name.
|
||||||
|
*
|
||||||
* @param name the name of the implementation.
|
* @param name the name of the implementation.
|
||||||
* @return the implementation.
|
* @return the implementation.
|
||||||
*/
|
*/
|
||||||
|
@ -151,6 +152,7 @@ public class PluginManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation for the given implementation class.
|
* Gets the number implementation for the given implementation class.
|
||||||
|
*
|
||||||
* @param name the class for which to find the implementation.
|
* @param name the class for which to find the implementation.
|
||||||
* @return the implementation.
|
* @return the implementation.
|
||||||
*/
|
*/
|
||||||
|
@ -173,6 +175,7 @@ public class PluginManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the mathematical constant pi for the given implementation class.
|
* Gets the mathematical constant pi for the given implementation class.
|
||||||
|
*
|
||||||
* @param forClass the class for which to find pi.
|
* @param forClass the class for which to find pi.
|
||||||
* @return pi
|
* @return pi
|
||||||
*/
|
*/
|
||||||
|
@ -308,6 +311,7 @@ public class PluginManager {
|
||||||
/**
|
/**
|
||||||
* Gets a list of all the plugin class files that have been
|
* Gets a list of all the plugin class files that have been
|
||||||
* added to the plugin manager.
|
* added to the plugin manager.
|
||||||
|
*
|
||||||
* @return the list of all the added plugin classes.
|
* @return the list of all the added plugin classes.
|
||||||
*/
|
*/
|
||||||
public Set<Class<?>> getLoadedPluginClasses() {
|
public Set<Class<?>> getLoadedPluginClasses() {
|
||||||
|
|
|
@ -18,8 +18,6 @@ import java.util.function.BiFunction;
|
||||||
*/
|
*/
|
||||||
public class StandardPlugin extends Plugin {
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The addition operator, +
|
* The addition operator, +
|
||||||
*/
|
*/
|
||||||
|
@ -129,20 +127,6 @@ public class StandardPlugin extends Plugin {
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
|
||||||
* The caret / pow operator, ^
|
|
||||||
*/
|
|
||||||
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]));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
/**
|
/**
|
||||||
* The absolute value function, abs(-3) = 3
|
* The absolute value function, abs(-3) = 3
|
||||||
*/
|
*/
|
||||||
|
@ -157,49 +141,6 @@ public class StandardPlugin extends Plugin {
|
||||||
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
|
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
|
||||||
* The exponential function, exp(1) = e^1 = 2.71...
|
|
||||||
*/
|
|
||||||
public static final Function FUNCTION_EXP = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
NumberInterface maxError = getMaxError(params[0]);
|
|
||||||
int n = 0;
|
|
||||||
if(params[0].signum() <= 0){
|
|
||||||
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
/**
|
/**
|
||||||
* The natural log function.
|
* The natural log function.
|
||||||
*/
|
*/
|
||||||
|
@ -291,109 +232,6 @@ public class StandardPlugin extends Plugin {
|
||||||
return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* The sine function (the argument is interpreted in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionSin = 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], 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){
|
|
||||||
theta = pi.subtract(theta);
|
|
||||||
}
|
|
||||||
//System.out.println(theta);
|
|
||||||
return sinTaylor(theta);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The cosine function (the argument is in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionCos = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
return functionSin.apply(getPi(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
|
|
||||||
.subtract(params[0]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The tangent function (the argument is in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionTan = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
return functionSin.apply(params[0]).divide(functionCos.apply(params[0]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The secant function (the argument is in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionSec = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionCos.apply(params[0]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The cosecant function (the argument is in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionCsc = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionSin.apply(params[0]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The cotangent function (the argument is in radians).
|
|
||||||
*/
|
|
||||||
public final Function functionCot = new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
return functionCos.apply(params[0]).divide(functionCos.apply(params[0]));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The implementation for double-based naive numbers.
|
* The implementation for double-based naive numbers.
|
||||||
*/
|
*/
|
||||||
|
@ -408,7 +246,6 @@ public class StandardPlugin extends Plugin {
|
||||||
return new NaiveNumber(Math.PI);
|
return new NaiveNumber(Math.PI);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The implementation for the infinite-precision BigDecimal.
|
* The implementation for the infinite-precision BigDecimal.
|
||||||
*/
|
*/
|
||||||
|
@ -445,6 +282,158 @@ public class StandardPlugin extends Plugin {
|
||||||
return C.divide(sum);
|
return C.divide(sum);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
|
||||||
|
/**
|
||||||
|
* The exponential function, exp(1) = e^1 = 2.71...
|
||||||
|
*/
|
||||||
|
public static final Function FUNCTION_EXP = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
NumberInterface maxError = getMaxError(params[0]);
|
||||||
|
int n = 0;
|
||||||
|
if (params[0].signum() <= 0) {
|
||||||
|
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The caret / pow operator, ^
|
||||||
|
*/
|
||||||
|
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]));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
/**
|
||||||
|
* The sine function (the argument is interpreted in radians).
|
||||||
|
*/
|
||||||
|
public final Function functionSin = 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], 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) {
|
||||||
|
theta = pi.subtract(theta);
|
||||||
|
}
|
||||||
|
//System.out.println(theta);
|
||||||
|
return sinTaylor(theta);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The cosine function (the argument is in radians).
|
||||||
|
*/
|
||||||
|
public final Function functionCos = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return functionSin.apply(getPi(params[0].getClass()).divide(new NaiveNumber(2).promoteTo(params[0].getClass()))
|
||||||
|
.subtract(params[0]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The tangent function (the argument is in radians).
|
||||||
|
*/
|
||||||
|
public final Function functionTan = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return functionSin.apply(params[0]).divide(functionCos.apply(params[0]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The secant function (the argument is in radians).
|
||||||
|
*/
|
||||||
|
public final Function functionSec = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionCos.apply(params[0]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The cosecant function (the argument is in radians).
|
||||||
|
*/
|
||||||
|
public final Function functionCsc = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionSin.apply(params[0]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The cotangent function (the argument is in radians).
|
||||||
|
*/
|
||||||
|
public final Function functionCot = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return functionCos.apply(params[0]).divide(functionCos.apply(params[0]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public StandardPlugin(PluginManager manager) {
|
public StandardPlugin(PluginManager manager) {
|
||||||
super(manager);
|
super(manager);
|
||||||
|
@ -476,6 +465,64 @@ public class StandardPlugin extends Plugin {
|
||||||
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 (!FACTORIAL_LISTS.containsKey(numberClass)) {
|
||||||
|
FACTORIAL_LISTS.put(numberClass, new ArrayList<>());
|
||||||
|
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
|
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
|
}
|
||||||
|
ArrayList<NumberInterface> list = FACTORIAL_LISTS.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 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 pi) {
|
||||||
|
NumberInterface twoPi = pi.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;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
||||||
|
@ -505,59 +552,4 @@ public class StandardPlugin extends Plugin {
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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(!FACTORIAL_LISTS.containsKey(numberClass)){
|
|
||||||
FACTORIAL_LISTS.put(numberClass, new ArrayList<>());
|
|
||||||
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
|
||||||
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
|
||||||
}
|
|
||||||
ArrayList<NumberInterface> list = FACTORIAL_LISTS.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 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 pi){
|
|
||||||
NumberInterface twoPi = pi.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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user