1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-06-14 23:27:06 -07:00

Format code.

This commit is contained in:
Danila Fedorin 2017-08-14 19:03:52 -07:00
parent d861444d13
commit 205d5dbc77
20 changed files with 261 additions and 259 deletions

View File

@ -13,6 +13,7 @@ public abstract class NumberInterface {
if (Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
throw new ComputationInterruptedException(); throw new ComputationInterruptedException();
} }
/** /**
* The maximum precision to which this number operates. * The maximum precision to which this number operates.
* *
@ -219,6 +220,7 @@ public abstract class NumberInterface {
* Returns the fractional part of the number, specifically x - floor(x). * Returns the fractional part of the number, specifically x - floor(x).
* Also, checks if the thread has been interrupted, * Also, checks if the thread has been interrupted,
* and if so, throws an exception. * and if so, throws an exception.
*
* @return the fractional part of the number. * @return the fractional part of the number.
*/ */
public final NumberInterface fractionalPart() { public final NumberInterface fractionalPart() {
@ -260,6 +262,7 @@ public abstract class NumberInterface {
/** /**
* Returns the smallest error this instance can tolerate depending * Returns the smallest error this instance can tolerate depending
* on its precision and value. * on its precision and value.
*
* @return the smallest error that should be permitted in calculations. * @return the smallest error that should be permitted in calculations.
*/ */
public abstract NumberInterface getMaxError(); public abstract NumberInterface getMaxError();

View File

@ -6,10 +6,6 @@ import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.Operator;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/** /**
* A plugin class that can be externally implemented and loaded via the * A plugin class that can be externally implemented and loaded via the
* plugin manager. Plugins provide functionality to the calculator * plugin manager. Plugins provide functionality to the calculator
@ -99,6 +95,7 @@ public abstract class Plugin {
/** /**
* To be used in load(). Registers a documentation instance * To be used in load(). Registers a documentation instance
* used to explain some element of the plugin to the user. * used to explain some element of the plugin to the user.
*
* @param documentation the documentation instance. * @param documentation the documentation instance.
*/ */
protected final void registerDocumentation(Documentation documentation) { protected final void registerDocumentation(Documentation documentation) {

View File

@ -8,7 +8,10 @@ import org.nwapw.abacus.function.Operator;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.*; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/** /**
* A class that controls instances of plugins, allowing for them * A class that controls instances of plugins, allowing for them
@ -79,6 +82,7 @@ public class PluginManager {
/** /**
* Registers a function under the given name. * Registers a function under the given name.
*
* @param name the name of the function. * @param name the name of the function.
* @param function the function to register. * @param function the function to register.
*/ */
@ -88,6 +92,7 @@ public class PluginManager {
/** /**
* Registers an operator under the given name. * Registers an operator under the given name.
*
* @param name the name of the operator. * @param name the name of the operator.
* @param operator the operator to register. * @param operator the operator to register.
*/ */
@ -97,6 +102,7 @@ public class PluginManager {
/** /**
* Registers a number implementation under the given name. * Registers a number implementation under the given name.
*
* @param name the name of the number implementation. * @param name the name of the number implementation.
* @param implementation the number implementation to register. * @param implementation the number implementation to register.
*/ */
@ -107,6 +113,7 @@ public class PluginManager {
/** /**
* Registers the given documentation with the plugin manager, * Registers the given documentation with the plugin manager,
* making it accessible to the plugin manager etc. * making it accessible to the plugin manager etc.
*
* @param documentation the documentation to register. * @param documentation the documentation to register.
*/ */
public void registerDocumentation(Documentation documentation) { public void registerDocumentation(Documentation documentation) {
@ -115,6 +122,7 @@ public class PluginManager {
/** /**
* Gets the function registered under the given name. * Gets the function registered under the given name.
*
* @param name the name of the function. * @param name the name of the function.
* @return the function, or null if it was not found. * @return the function, or null if it was not found.
*/ */
@ -124,6 +132,7 @@ public class PluginManager {
/** /**
* Gets the operator registered under the given name. * Gets the operator registered under the given name.
*
* @param name the name of the operator. * @param name the name of the operator.
* @return the operator, or null if it was not found. * @return the operator, or null if it was not found.
*/ */
@ -133,6 +142,7 @@ public class PluginManager {
/** /**
* Gets the number implementation registered under the given name. * Gets the number implementation registered under the given name.
*
* @param name the name of the number implementation. * @param name the name of the number implementation.
* @return the number implementation, or null if it was not found. * @return the number implementation, or null if it was not found.
*/ */
@ -142,6 +152,7 @@ public class PluginManager {
/** /**
* Gets the documentation for the given entity of the given type. * Gets the documentation for the given entity of the given type.
*
* @param name the name of the entity to search for. * @param name the name of the entity to search for.
* @param type the type that this entity is, to filter out similarly named documentation. * @param type the type that this entity is, to filter out similarly named documentation.
* @return the documentation object. * @return the documentation object.
@ -226,6 +237,7 @@ public class PluginManager {
/** /**
* Removes the plugin with the given class from the manager. * Removes the plugin with the given class from the manager.
*
* @param toRemove the plugin to remove. * @param toRemove the plugin to remove.
*/ */
public void removeClass(Class<? extends Plugin> toRemove) { public void removeClass(Class<? extends Plugin> toRemove) {

View File

@ -1,19 +1,12 @@
package org.nwapw.abacus.plugin; package org.nwapw.abacus.plugin;
import org.nwapw.abacus.function.*; import org.nwapw.abacus.function.*;
import org.nwapw.abacus.lexing.pattern.Match;
import org.nwapw.abacus.number.NaiveNumber; 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 org.nwapw.abacus.parsing.Parser;
import org.nwapw.abacus.parsing.ShuntingYardParser;
import org.nwapw.abacus.tree.TokenType;
import org.nwapw.abacus.tree.TreeNode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.function.BiFunction; import java.util.function.BiFunction;
/** /**
@ -22,11 +15,6 @@ import java.util.function.BiFunction;
*/ */
public class StandardPlugin extends Plugin { 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, + * The addition operator, +
*/ */
@ -92,6 +80,75 @@ public class StandardPlugin extends Plugin {
return product; return product;
} }
}); });
/**
* The combination operator.
*/
public static final Operator OP_NCR = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0, new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
return OP_NPR.getFunction().apply(params).divide(OP_FACTORIAL.getFunction().apply(params[1]));
}
});
/**
* The implementation for double-based naive numbers.
*/
public static final NumberImplementation IMPLEMENTATION_NAIVE = new NumberImplementation(NaiveNumber.class, 0) {
@Override
public NumberInterface instanceForString(String string) {
return new NaiveNumber(string);
}
@Override
public NumberInterface instanceForPi() {
return new NaiveNumber(Math.PI);
}
};
/**
* The implementation for the infinite-precision BigDecimal.
*/
public static final NumberImplementation IMPLEMENTATION_PRECISE = new NumberImplementation(PreciseNumber.class, 0) {
@Override
public NumberInterface instanceForString(String string) {
return new PreciseNumber(string);
}
@Override
public NumberInterface instanceForPi() {
NumberInterface C = FUNCTION_SQRT.apply(new PreciseNumber("10005")).multiply(new PreciseNumber("426880"));
NumberInterface M = PreciseNumber.ONE;
NumberInterface L = new PreciseNumber("13591409");
NumberInterface X = M;
NumberInterface sum = L;
int termsNeeded = C.getMaxPrecision() / 13 + 1;
NumberInterface lSummand = new PreciseNumber("545140134");
NumberInterface xMultiplier = new PreciseNumber("262537412")
.multiply(new PreciseNumber("1000000000"))
.add(new PreciseNumber("640768000"))
.negate();
for (int i = 0; i < termsNeeded; i++) {
M = M
.multiply(new PreciseNumber((12 * i + 2) + ""))
.multiply(new PreciseNumber((12 * i + 6) + ""))
.multiply(new PreciseNumber((12 * i + 10) + ""))
.divide(new PreciseNumber(Math.pow(i + 1, 3) + ""));
L = L.add(lSummand);
X = X.multiply(xMultiplier);
sum = sum.add(M.multiply(L).divide(X));
}
return C.divide(sum);
}
};
/**
* Stores objects of NumberInterface with integer values for reuse.
*/
private final static HashMap<Class<? extends NumberInterface>, HashMap<Integer, NumberInterface>> integerValues = new HashMap<>();
/** /**
* The division operator, / * The division operator, /
*/ */
@ -168,21 +225,6 @@ public class StandardPlugin extends Plugin {
return total; return total;
} }
}); });
/**
* The combination operator.
*/
public static final Operator OP_NCR = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0, new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2 && params[0].fractionalPart().signum() == 0
&& params[1].fractionalPart().signum() == 0;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
return OP_NPR.getFunction().apply(params).divide(OP_FACTORIAL.getFunction().apply(params[1]));
}
});
/** /**
* The absolute value function, abs(-3) = 3 * The absolute value function, abs(-3) = 3
*/ */
@ -277,20 +319,6 @@ public class StandardPlugin extends Plugin {
return sum; return sum;
} }
}; };
/**
* The square root function.
*/
public static final Function FUNCTION_SQRT = new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length == 1;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
}
};
/** /**
* Gets a random number smaller or equal to the given number's integer value. * Gets a random number smaller or equal to the given number's integer value.
*/ */
@ -306,53 +334,47 @@ public class StandardPlugin extends Plugin {
} }
}; };
/** /**
* The implementation for double-based naive numbers. * The caret / pow operator, ^
*/ */
public static final NumberImplementation IMPLEMENTATION_NAIVE = new NumberImplementation(NaiveNumber.class, 0) { public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
@Override @Override
public NumberInterface instanceForString(String string) { protected boolean matchesParams(NumberInterface[] params) {
return new NaiveNumber(string); NumberInterface zero = fromInt(params[0].getClass(), 0);
return params.length == 2
&& !(params[0].compareTo(zero) == 0
&& params[1].compareTo(zero) == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(zero) != 0);
} }
@Override @Override
public NumberInterface instanceForPi() { protected NumberInterface applyInternal(NumberInterface[] params) {
return new NaiveNumber(Math.PI); NumberInterface zero = fromInt(params[0].getClass(), 0);
if (params[0].compareTo(zero) == 0)
return zero;
else if (params[1].compareTo(zero) == 0)
return fromInt(params[0].getClass(), 1);
//Detect integer bases:
if (params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0) {
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
} }
}; return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
}
});
/** /**
* The implementation for the infinite-precision BigDecimal. * The square root function.
*/ */
public static final NumberImplementation IMPLEMENTATION_PRECISE = new NumberImplementation(PreciseNumber.class, 0) { public static final Function FUNCTION_SQRT = new Function() {
@Override @Override
public NumberInterface instanceForString(String string) { protected boolean matchesParams(NumberInterface[] params) {
return new PreciseNumber(string); return params.length == 1;
} }
@Override @Override
public NumberInterface instanceForPi() { protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface C = FUNCTION_SQRT.apply(new PreciseNumber("10005")).multiply(new PreciseNumber("426880")); return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
NumberInterface M = PreciseNumber.ONE;
NumberInterface L = new PreciseNumber("13591409");
NumberInterface X = M;
NumberInterface sum = L;
int termsNeeded = C.getMaxPrecision() / 13 + 1;
NumberInterface lSummand = new PreciseNumber("545140134");
NumberInterface xMultiplier = new PreciseNumber("262537412")
.multiply(new PreciseNumber("1000000000"))
.add(new PreciseNumber("640768000"))
.negate();
for (int i = 0; i < termsNeeded; i++) {
M = M
.multiply(new PreciseNumber((12 * i + 2) + ""))
.multiply(new PreciseNumber((12 * i + 6) + ""))
.multiply(new PreciseNumber((12 * i + 10) + ""))
.divide(new PreciseNumber(Math.pow(i + 1, 3) + ""));
L = L.add(lSummand);
X = X.multiply(xMultiplier);
sum = sum.add(M.multiply(L).divide(X));
}
return C.divide(sum);
} }
}; };
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>(); private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
@ -393,36 +415,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) {
NumberInterface zero = fromInt(params[0].getClass(), 0);
return params.length == 2
&& !(params[0].compareTo(zero) == 0
&& params[1].compareTo(zero) == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(zero) != 0);
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0);
if (params[0].compareTo(zero) == 0)
return zero;
else if (params[1].compareTo(zero) == 0)
return fromInt(params[0].getClass(), 1);
//Detect integer bases:
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0){
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
}
return FUNCTION_EXP.apply(FUNCTION_LN.apply(FUNCTION_ABS.apply(params[0])).multiply(params[1]));
}
});
/** /**
* The sine function (the argument is interpreted in radians). * The sine function (the argument is interpreted in radians).
*/ */
@ -737,6 +729,7 @@ public class StandardPlugin extends Plugin {
/** /**
* Returns a number of class numType with value n. * Returns a number of class numType with value n.
*
* @param numType class of number to return. * @param numType class of number to return.
* @param n value of returned number. * @param n value of returned number.
* @return numClass instance with value n. * @return numClass instance with value n.

View File

@ -17,6 +17,10 @@ public class AbacusApplication extends Application {
*/ */
private AbacusController controller; private AbacusController controller;
public static void main(String[] args) {
launch(args);
}
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/abacus.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/abacus.fxml"));
@ -34,8 +38,4 @@ public class AbacusApplication extends Application {
controller.performStop(); controller.performStop();
} }
public static void main(String[] args){
launch(args);
}
} }

View File

@ -1,13 +1,10 @@
package org.nwapw.abacus.fx; package org.nwapw.abacus.fx;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList; import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxListCell; import javafx.scene.control.cell.CheckBoxListCell;
import javafx.scene.text.Text; import javafx.scene.text.Text;
@ -136,30 +133,6 @@ public class AbacusController implements PluginListener {
* The abacus instance used for changing the plugin configuration. * The abacus instance used for changing the plugin configuration.
*/ */
private Abacus abacus; private Abacus abacus;
/**
* Boolean which represents whether changes were made to the configuration.
*/
private boolean changesMade;
/**
* Whether an alert about changes to the configuration was already shown.
*/
private boolean reloadAlertShown;
/**
* The alert shown when a press to "apply" is needed.
*/
private Alert reloadAlert;
/**
* The runnable that takes care of killing computations that take too long.
*/
private final Runnable TIMER_RUNNABLE = () -> {
try {
Configuration abacusConfig = abacus.getConfiguration();
if(abacusConfig.getComputationDelay() == 0) return;
Thread.sleep((long) (abacusConfig.getComputationDelay() * 1000));
performStop();
} catch (InterruptedException e) { }
};
/** /**
* The runnable used to perform the calculation. * The runnable used to perform the calculation.
*/ */
@ -197,6 +170,18 @@ public class AbacusController implements PluginListener {
}); });
} }
}; };
/**
* Boolean which represents whether changes were made to the configuration.
*/
private boolean changesMade;
/**
* Whether an alert about changes to the configuration was already shown.
*/
private boolean reloadAlertShown;
/**
* The alert shown when a press to "apply" is needed.
*/
private Alert reloadAlert;
/** /**
* The thread that is waiting to pause the calculation. * The thread that is waiting to pause the calculation.
*/ */
@ -205,6 +190,18 @@ public class AbacusController implements PluginListener {
* The thread in which the computation runs. * The thread in which the computation runs.
*/ */
private Thread calculationThread; private Thread calculationThread;
/**
* The runnable that takes care of killing computations that take too long.
*/
private final Runnable TIMER_RUNNABLE = () -> {
try {
Configuration abacusConfig = abacus.getConfiguration();
if (abacusConfig.getComputationDelay() == 0) return;
Thread.sleep((long) (abacusConfig.getComputationDelay() * 1000));
performStop();
} catch (InterruptedException e) {
}
};
/** /**
* Alerts the user if the changes they made * Alerts the user if the changes they made