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

Compare commits

..

76 Commits

Author SHA1 Message Date
Arthur Drobot
9192e671f4 Write a RationalNumber class. 2017-08-09 18:37:42 -07:00
Arthur Drobot
b6e4c6d2ea Merge branch 'sig-fig' 2017-08-08 09:46:46 -07:00
Arthur Drobot
f8b3559cec Add some tests to pow. 2017-08-08 09:45:13 -07:00
Arthur Drobot
4cf4ba98a8 Adjust unit tests. Set the number of significant figures in the output to 50. 2017-08-08 09:27:10 -07:00
12710c625b Add missing comments. 2017-08-07 22:48:56 -07:00
e71b037195 Switch the ToggleablePlugin class to Kotlin to avoid boilerplate. 2017-08-07 22:44:16 -07:00
fe92929856 Switch HistoryModel to Kotlin to avoid boilerplate. 2017-08-07 22:39:35 -07:00
ff7d90967e Fix toString errors failing unit tests. 2017-08-07 19:22:35 -07:00
355a91d690 Rewrite the Operator in Kotlin. 2017-08-07 19:16:51 -07:00
5f0fba15eb Add comments to the newly created Kotlin implementations. 2017-08-07 19:11:13 -07:00
3bdc0e2ae5 Rewrite tree nodes in Kotlin. Documentation pending. 2017-08-07 18:57:43 -07:00
Arthur Drobot
68fbcd2d7c Modify exp to use the non-alternating series and take a reciprocal in the case of a negative argument. 2017-08-07 18:13:18 -07:00
Arthur Drobot
ed92b382f0 Rewrite precise number to limit significant figures rather than decimal places. Add getMaxError to NumberInterface; modify numbers and StandardPlugin appropriately. 2017-08-07 17:58:18 -07:00
e54b5cdd66 Add Kotlin support to gradle. 2017-08-07 17:33:56 -07:00
fd87cb66a3 Merge branch 'master' of github.com:DanilaFe/abacus 2017-08-07 14:27:29 -07:00
200f4c7288 Add a filter. 2017-08-07 14:12:41 -07:00
Arthur Drobot
1cd544e712 Merge branch 'master' of https://github.com/DanilaFe/abacus 2017-08-07 14:08:17 -07:00
Arthur Drobot
a8c70a6bbe Add checking of negative bases raised to non-integer powers in matchesParams in OP_CARET. 2017-08-07 14:05:15 -07:00
f28e915c9a Fix not clearing unloaded plugins. 2017-08-07 13:55:24 -07:00
7a0863380a Add a list of functions to the controller. 2017-08-07 13:55:13 -07:00
Arthur Drobot
8a9df051cf Optimize pow for integer bases. 2017-08-07 13:43:12 -07:00
Arthur Drobot
4eda15b3fb Modify some functions in PreciseNumber for consistency. 2017-08-07 13:41:45 -07:00
c9e0d4f8d3 Fix broken documentation,. 2017-08-07 11:18:01 -07:00
213d7af10b Merge branch 'remove-caching'
# Conflicts:
#	src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java
2017-08-07 11:14:11 -07:00
585a3839c1 Merge branch 'time-limit' 2017-08-07 11:12:12 -07:00
205b73f62c Add missing comments. 2017-08-07 11:11:41 -07:00
1cd332b97d Remove output. 2017-08-07 10:58:07 -07:00
314552f95a Read delay input from input field, and kill delay thread. 2017-08-07 10:58:04 -07:00
c5ec521996 Fix tests to work with the configuration. 2017-08-07 10:57:51 -07:00
4712bbfded Add a setting to the timeout delay. 2017-08-07 10:57:48 -07:00
7ae7f6d9a5 Add a timer to the computation thread to stop it from running. 2017-08-07 10:57:04 -07:00
Arthur Drobot
d0ccb8b625 Improve some comments of NumberInterface functions. 2017-08-07 10:54:27 -07:00
Arthur Drobot
7d5efa1fe6 Add fromInt function into StandardPlugin and replace existing instantiations and promotions of NaiveNumbers with integer values with fromInt calls. 2017-08-07 10:33:16 -07:00
35254d3e99 Rename getPi to piFor. 2017-08-06 21:56:49 -07:00
44f018060d Remove the useless caching in the plugin manager and the maps in plugins 2017-08-06 21:55:09 -07:00
arthur326
6a15c266c4 Fix typo in cot function. 2017-08-06 18:13:01 -07:00
9f61fc5dbe Remove the correct unused intPow function. 2017-08-05 18:23:24 -07:00
bae6ee5526 Revert "Remove the NumberInterface::intPow method."
0c16bb4e9b
2017-08-05 18:22:43 -07:00
4f94700aef Remove the NumberInterface::intPow method. 2017-08-05 18:11:16 -07:00
b7152da58d Merge branch 'stoppable-alternate' 2017-08-05 17:58:26 -07:00
d17a8a9fa7 Add missing javadoc. 2017-08-05 17:29:35 -07:00
71c9f0d141 Merge branch 'documentation' 2017-08-05 17:28:08 -07:00
fb02984e60 Decrease test intensity to prevent travis CI from killing gradle. 2017-08-05 17:24:16 -07:00
a9ac4681f0 Use a specific configuration instead of chancing it with a local file. 2017-08-05 17:13:03 -07:00
62d7053441 Get rid of unnecessary supplier. 2017-08-05 17:09:12 -07:00
f3cbb600ac Remove the default load-from-file behavior from the Abacus core. 2017-08-05 17:04:07 -07:00
abc0e2d59f Add tests for more complex functions. 2017-08-05 16:57:52 -07:00
f7d1be086b Add tests for basic operations. 2017-08-05 16:57:22 -07:00
21a925d6d2 Write two functions to help test the code. 2017-08-05 16:56:50 -07:00
0d21898f20 Make loading plugins a non-core part of Abacus, avoiding desktop APIs. 2017-08-05 16:21:02 -07:00
3e39087fde Add numerous documentation fixes. 2017-08-05 16:15:30 -07:00
a984f2960d Prevent leaving unparsed tokens ignored, throwing error instead. 2017-08-05 16:01:08 -07:00
a6832e09f4 Fix mismatched parentheses causing exceptions. 2017-08-05 15:59:49 -07:00
0bcb3b25d9 Fix exception handling. 2017-08-05 15:58:43 -07:00
2f5f967be4 Add new comments. 2017-08-05 15:19:39 -07:00
72a2a8f1c1 Set output string correctly. 2017-08-05 14:43:24 -07:00
58fc94e9d0 Fix clearing input field. 2017-08-05 14:36:42 -07:00
9cedb100ad Re-introduce arthur326's fix for ln speed. 2017-08-05 14:34:57 -07:00
99be2d80f1 Run calculations via thread, and stop thread if necessary. 2017-08-05 14:34:31 -07:00
2523b9b04b Add a stop button. 2017-08-05 13:57:27 -07:00
cd60c9d52f Convert NumberInterface into abstract class, and check for interruption. 2017-08-05 13:54:06 -07:00
23a3eb88f1 Remove old stopping code. 2017-08-05 13:26:29 -07:00
508e98413d Fix caret just multiplying. 2017-08-04 14:54:01 -07:00
d06f611a2e Remove leftover merge conflict marker. 2017-08-04 14:48:08 -07:00
c541eaab97 Format code. 2017-08-04 14:29:24 -07:00
0058ec9c71 Merge branch 'stoppable-new'
# Conflicts:
#	src/main/java/org/nwapw/abacus/fx/AbacusController.java
#	src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java
2017-08-04 14:28:22 -07:00
Riley Jones
f8bf60f383 Fix exp function 2017-08-04 13:52:41 -07:00
Riley Jones
4369eba107 Allow all standard functions to end early 2017-08-04 13:45:29 -07:00
385a0c960d Merge the new edge cases. 2017-08-04 13:32:06 -07:00
Arthur Drobot
d7ae1a80f1 Merge branch 'master' of https://github.com/DanilaFe/abacus 2017-08-04 13:30:05 -07:00
Arthur Drobot
e4a45c0ec4 Add handling of edge cases to pow. 2017-08-04 13:29:53 -07:00
22cf99d23d Add missing comment. 2017-08-04 13:21:28 -07:00
39b36f84e0 Format code. 2017-08-04 13:20:57 -07:00
Riley Jones
eff7be0204 StandardPlugin uses null 2017-08-03 23:59:23 -07:00
Riley Jones
9d5f9d901c Plugin fixes 2017-08-03 15:16:26 -07:00
Riley Jones
dad546c5b5 Add stop button 2017-08-03 14:04:09 -07:00
37 changed files with 1317 additions and 1116 deletions

7
build.gradle Normal file → Executable file
View File

@@ -1,3 +1,8 @@
plugins {
id 'java'
id 'application'
id 'org.jetbrains.kotlin.jvm' version '1.1.3'
}
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'application' apply plugin: 'application'
@@ -6,7 +11,9 @@ repositories {
} }
dependencies { dependencies {
compile group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
compile 'com.moandjiezana.toml:toml4j:0.7.1' compile 'com.moandjiezana.toml:toml4j:0.7.1'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8"
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
} }

View File

@@ -6,16 +6,12 @@ import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.parsing.LexerTokenizer; import org.nwapw.abacus.parsing.LexerTokenizer;
import org.nwapw.abacus.parsing.ShuntingYardParser; import org.nwapw.abacus.parsing.ShuntingYardParser;
import org.nwapw.abacus.parsing.TreeBuilder; import org.nwapw.abacus.parsing.TreeBuilder;
import org.nwapw.abacus.plugin.ClassFinder;
import org.nwapw.abacus.plugin.NumberImplementation; import org.nwapw.abacus.plugin.NumberImplementation;
import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.plugin.StandardPlugin; import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.tree.NumberReducer; import org.nwapw.abacus.tree.NumberReducer;
import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.TreeNode;
import java.io.File;
import java.io.IOException;
/** /**
* The main calculator class. This is responsible * The main calculator class. This is responsible
* for piecing together all of the components, allowing * for piecing together all of the components, allowing
@@ -23,11 +19,10 @@ import java.io.IOException;
*/ */
public class Abacus { public class Abacus {
public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE;
/** /**
* The file used for saving and loading configuration. * The default number implementation to be used if no other one is available / selected.
*/ */
public static final File CONFIG_FILE = new File("config.toml"); public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE;
/** /**
* The plugin manager responsible for * The plugin manager responsible for
@@ -51,26 +46,19 @@ public class Abacus {
/** /**
* Creates a new instance of the Abacus calculator. * Creates a new instance of the Abacus calculator.
*
* @param configuration the configuration object for this Abacus instance.
*/ */
public Abacus() { public Abacus(Configuration configuration) {
pluginManager = new PluginManager(this); pluginManager = new PluginManager(this);
numberReducer = new NumberReducer(this); numberReducer = new NumberReducer(this);
configuration = new Configuration(CONFIG_FILE); this.configuration = new Configuration(configuration);
configuration.saveTo(CONFIG_FILE);
LexerTokenizer lexerTokenizer = new LexerTokenizer(); LexerTokenizer lexerTokenizer = new LexerTokenizer();
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this); ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser); treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
pluginManager.addListener(lexerTokenizer);
pluginManager.addListener(shuntingYardParser); pluginManager.addListener(shuntingYardParser);
pluginManager.addInstantiated(new StandardPlugin(pluginManager)); pluginManager.addListener(lexerTokenizer);
try {
ClassFinder.loadJars("plugins")
.forEach(plugin -> pluginManager.addClass(plugin));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
pluginManager.load();
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -20,7 +20,7 @@ public class Configuration {
*/ */
private static final String DEFAULT_CONFIG = private static final String DEFAULT_CONFIG =
"numberImplementation = \"naive\"\n" + "numberImplementation = \"naive\"\n" +
"disabledPlugins = []"; "disabledPlugins = []";
/** /**
* The defaults TOML object, parsed from the string. * The defaults TOML object, parsed from the string.
*/ */
@@ -30,6 +30,10 @@ public class Configuration {
*/ */
private static final TomlWriter TOML_WRITER = new TomlWriter(); private static final TomlWriter TOML_WRITER = new TomlWriter();
/**
* The computation delay for which the thread can run without interruption.
*/
private double computationDelay = 0;
/** /**
* The implementation of the number that should be used. * The implementation of the number that should be used.
*/ */
@@ -40,29 +44,44 @@ public class Configuration {
private Set<String> disabledPlugins = new HashSet<>(); private Set<String> disabledPlugins = new HashSet<>();
/** /**
* Creates a new configuration with the given values. * Creates a new configuration form the given configuration.
* @param numberImplementation the number implementation, like "naive" or "precise" *
* @param disabledPlugins the list of disabled plugins. * @param copyFrom the configuration to copy.
*/ */
public Configuration(String numberImplementation, String[] disabledPlugins){ public Configuration(Configuration copyFrom){
copyFrom(copyFrom);
}
/**
* Creates a new configuration with the given values.
*
* @param computationDelay the delay before the computation gets killed.
* @param numberImplementation the number implementation, like "naive" or "precise"
* @param disabledPlugins the list of disabled plugins.
*/
public Configuration(double computationDelay, String numberImplementation, String[] disabledPlugins) {
this.computationDelay = computationDelay;
this.numberImplementation = numberImplementation; this.numberImplementation = numberImplementation;
this.disabledPlugins.addAll(Arrays.asList(disabledPlugins)); this.disabledPlugins.addAll(Arrays.asList(disabledPlugins));
} }
/** /**
* 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) {
if(!fromFile.exists()) return; if (!fromFile.exists()) return;
copyFrom(new Toml(DEFAULT_TOML).read(fromFile).to(Configuration.class)); copyFrom(new Toml(DEFAULT_TOML).read(fromFile).to(Configuration.class));
} }
/** /**
* 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) {
this.computationDelay = otherConfiguration.computationDelay;
this.numberImplementation = otherConfiguration.numberImplementation; this.numberImplementation = otherConfiguration.numberImplementation;
this.disabledPlugins.addAll(otherConfiguration.disabledPlugins); this.disabledPlugins.addAll(otherConfiguration.disabledPlugins);
} }
@@ -70,10 +89,11 @@ 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) {
if(file.getParentFile() != null) file.getParentFile().mkdirs(); if (file.getParentFile() != null) file.getParentFile().mkdirs();
try { try {
TOML_WRITER.write(this, file); TOML_WRITER.write(this, file);
} catch (IOException e) { } catch (IOException e) {
@@ -81,8 +101,18 @@ public class Configuration {
} }
} }
/**
* Gets the value of this configuration as a string.
*
* @return the string that represents this configuration.
*/
public String asTomlString(){
return TOML_WRITER.write(this);
}
/** /**
* 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 +121,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,10 +130,30 @@ 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() {
return disabledPlugins; return disabledPlugins;
} }
/**
* Gets the computation delay specified in the configuration.
*
* @return the computaton delay.
*/
public double getComputationDelay() {
return computationDelay;
}
/**
* Sets the computation delay.
*
* @param computationDelay the new computation delay.
*/
public void setComputationDelay(double computationDelay) {
this.computationDelay = computationDelay;
}
} }

View File

@@ -1,75 +0,0 @@
package org.nwapw.abacus.function;
/**
* A class that represents a single infix operator.
*/
public class Operator {
/**
* The associativity of the operator.
*/
private OperatorAssociativity associativity;
/**
* The type of this operator.
*/
private OperatorType type;
/**
* The precedence of the operator.
*/
private int precedence;
/**
* The function that is called by this operator.
*/
private Function function;
/**
* Creates a new operator with the given parameters.
*
* @param associativity the associativity of the operator.
* @param precedence the precedence of the operator.
* @param function the function that the operator calls.
*/
public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function) {
this.associativity = associativity;
this.type = operatorType;
this.precedence = precedence;
this.function = function;
}
/**
* Gets the operator's associativity.
*
* @return the associativity.
*/
public OperatorAssociativity getAssociativity() {
return associativity;
}
/**
* Gets the operator's type.
*
* @return the type.
*/
public OperatorType getType() {
return type;
}
/**
* Gets the operator's precedence.
*
* @return the precedence.
*/
public int getPrecedence() {
return precedence;
}
/**
* Gets the operator's function.
*
* @return the function.
*/
public Function getFunction() {
return function;
}
}

View File

@@ -12,13 +12,25 @@ import javafx.stage.Stage;
*/ */
public class AbacusApplication extends Application { public class AbacusApplication extends Application {
/**
* The controller currently managing the application.
*/
private AbacusController controller;
@Override @Override
public void start(Stage primaryStage) throws Exception { public void start(Stage primaryStage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml")); FXMLLoader loader = new FXMLLoader(getClass().getResource("/abacus.fxml"));
Parent parent = loader.load();
controller = loader.getController();
Scene mainScene = new Scene(parent, 320, 480); Scene mainScene = new Scene(parent, 320, 480);
primaryStage.setScene(mainScene); primaryStage.setScene(mainScene);
primaryStage.setTitle("Abacus"); primaryStage.setTitle("Abacus");
primaryStage.show(); primaryStage.show();
} }
@Override
public void stop() throws Exception {
super.stop();
controller.performStop();
}
} }

View File

@@ -1,7 +1,11 @@
package org.nwapw.abacus.fx; package org.nwapw.abacus.fx;
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.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxListCell; import javafx.scene.control.cell.CheckBoxListCell;
@@ -10,11 +14,16 @@ import javafx.util.Callback;
import javafx.util.StringConverter; import javafx.util.StringConverter;
import org.nwapw.abacus.Abacus; import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.config.Configuration; import org.nwapw.abacus.config.Configuration;
import org.nwapw.abacus.number.ComputationInterruptedException;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.ClassFinder;
import org.nwapw.abacus.plugin.PluginListener; import org.nwapw.abacus.plugin.PluginListener;
import org.nwapw.abacus.plugin.PluginManager; import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.TreeNode;
import java.io.File;
import java.io.IOException;
import java.util.Set; import java.util.Set;
@@ -24,6 +33,10 @@ import java.util.Set;
*/ */
public class AbacusController implements PluginListener { public class AbacusController implements PluginListener {
/**
* The file used for saving and loading configuration.
*/
public static final File CONFIG_FILE = new File("config.toml");
/** /**
* The title for the apply alert dialog. * The title for the apply alert dialog.
*/ */
@@ -36,7 +49,7 @@ public class AbacusController implements PluginListener {
* The text for the dialog that is shown if settings haven't been applied. * The text for the dialog that is shown if settings haven't been applied.
*/ */
private static final String APPLY_MSG_TEXT = "You have made changes to the configuration, however, you haven't pressed \"Apply\". " + private static final String APPLY_MSG_TEXT = "You have made changes to the configuration, however, you haven't pressed \"Apply\". " +
"The changes to the configuration will not be present in the calculator until \"Apply\" is pressed."; "The changes to the configuration will not be present in the calculator until \"Apply\" is pressed.";
/** /**
* Constant string that is displayed if the text could not be lexed or parsed. * Constant string that is displayed if the text could not be lexed or parsed.
*/ */
@@ -45,7 +58,14 @@ public class AbacusController implements PluginListener {
* Constant string that is displayed if the tree could not be reduced. * Constant string that is displayed if the tree could not be reduced.
*/ */
private static final String ERR_EVAL = "Evaluation Error"; private static final String ERR_EVAL = "Evaluation Error";
/**
* Constant string that is displayed if the calculations are stopped before they are done.
*/
private static final String ERR_STOP = "Stopped";
/**
* Constant string that is displayed if the calculations are interrupted by an exception.
*/
private static final String ERR_EXCEPTION = "Exception Thrown";
@FXML @FXML
private TabPane coreTabPane; private TabPane coreTabPane;
@FXML @FXML
@@ -53,6 +73,8 @@ public class AbacusController implements PluginListener {
@FXML @FXML
private Tab settingsTab; private Tab settingsTab;
@FXML @FXML
private Tab functionListTab;
@FXML
private TableView<HistoryModel> historyTable; private TableView<HistoryModel> historyTable;
@FXML @FXML
private TableColumn<HistoryModel, String> inputColumn; private TableColumn<HistoryModel, String> inputColumn;
@@ -67,9 +89,17 @@ public class AbacusController implements PluginListener {
@FXML @FXML
private Button inputButton; private Button inputButton;
@FXML @FXML
private Button stopButton;
@FXML
private ComboBox<String> numberImplementationBox; private ComboBox<String> numberImplementationBox;
@FXML @FXML
private ListView<ToggleablePlugin> enabledPluginView; private ListView<ToggleablePlugin> enabledPluginView;
@FXML
private TextField computationLimitField;
@FXML
private ListView<String> functionListView;
@FXML
private TextField functionListSearchField;
/** /**
* The list of history entries, created by the users. * The list of history entries, created by the users.
@@ -87,6 +117,14 @@ public class AbacusController implements PluginListener {
* and, when reloaded, get added to the plugin manager's black list. * and, when reloaded, get added to the plugin manager's black list.
*/ */
private ObservableList<ToggleablePlugin> enabledPlugins; private ObservableList<ToggleablePlugin> enabledPlugins;
/**
* The list of functions that are registered in the calculator.
*/
private ObservableList<String> functionList;
/**
* The filtered list displayed to the user.
*/
private FilteredList<String> functionFilter;
/** /**
* The abacus instance used for changing the plugin configuration. * The abacus instance used for changing the plugin configuration.
@@ -105,24 +143,80 @@ public class AbacusController implements PluginListener {
* The alert shown when a press to "apply" is needed. * The alert shown when a press to "apply" is needed.
*/ */
private Alert reloadAlert; 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.
*/
private final Runnable CALCULATION_RUNNABLE = new Runnable() {
private String attemptCalculation(){
try {
TreeNode constructedTree = abacus.parseString(inputField.getText());
if (constructedTree == null) {
return ERR_SYNTAX;
}
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
if (evaluatedNumber == null) {
return ERR_EVAL;
}
String resultingString = evaluatedNumber.toString();
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), resultingString));
return resultingString;
} catch (ComputationInterruptedException exception) {
return ERR_STOP;
} catch (RuntimeException exception){
exception.printStackTrace();
return ERR_EXCEPTION;
}
}
@Override
public void run() {
String calculation = attemptCalculation();
Platform.runLater(() -> {
outputText.setText(calculation);
inputField.setText("");
inputButton.setDisable(false);
stopButton.setDisable(true);
});
}
};
/**
* The thread that is waiting to pause the calculation.
*/
private Thread computationLimitThread;
/**
* The thread in which the computation runs.
*/
private Thread calculationThread;
/** /**
* Alerts the user if the changes they made * Alerts the user if the changes they made
* have not yet been applied. * have not yet been applied.
*/ */
private void alertIfApplyNeeded(boolean ignorePrevious){ private void alertIfApplyNeeded(boolean ignorePrevious) {
if(changesMade && (!reloadAlertShown || ignorePrevious)) { if (changesMade && (!reloadAlertShown || ignorePrevious)) {
reloadAlertShown = true; reloadAlertShown = true;
reloadAlert.showAndWait(); reloadAlert.showAndWait();
} }
} }
@FXML @FXML
public void initialize(){ public void initialize() {
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory = Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
param -> new CopyableCell<>(); param -> new CopyableCell<>();
Callback<ListView<ToggleablePlugin>, ListCell<ToggleablePlugin>> pluginCellFactory = Callback<ListView<ToggleablePlugin>, ListCell<ToggleablePlugin>> pluginCellFactory =
param -> new CheckBoxListCell<>(ToggleablePlugin::enabledProperty, new StringConverter<ToggleablePlugin>() { param -> new CheckBoxListCell<>(ToggleablePlugin::getEnabledProperty, new StringConverter<ToggleablePlugin>() {
@Override @Override
public String toString(ToggleablePlugin object) { public String toString(ToggleablePlugin object) {
return object.getClassName().substring(object.getClassName().lastIndexOf('.') + 1); return object.getClassName().substring(object.getClassName().lastIndexOf('.') + 1);
@@ -130,10 +224,15 @@ public class AbacusController implements PluginListener {
@Override @Override
public ToggleablePlugin fromString(String string) { public ToggleablePlugin fromString(String string) {
return new ToggleablePlugin(true, string); return new ToggleablePlugin(string, true);
} }
}); });
functionList = FXCollections.observableArrayList();
functionFilter = new FilteredList<>(functionList, (s) -> true);
functionListView.setItems(functionFilter);
functionListSearchField.textProperty().addListener((observable, oldValue, newValue) ->
functionFilter.setPredicate((newValue.length() == 0) ? ((s) -> true) : ((s) -> s.contains(newValue))));
historyData = FXCollections.observableArrayList(); historyData = FXCollections.observableArrayList();
historyTable.setItems(historyData); historyTable.setItems(historyData);
numberImplementationOptions = FXCollections.observableArrayList(); numberImplementationOptions = FXCollections.observableArrayList();
@@ -144,18 +243,34 @@ public class AbacusController implements PluginListener {
enabledPluginView.setItems(enabledPlugins); enabledPluginView.setItems(enabledPlugins);
enabledPluginView.setCellFactory(pluginCellFactory); enabledPluginView.setCellFactory(pluginCellFactory);
inputColumn.setCellFactory(cellFactory); inputColumn.setCellFactory(cellFactory);
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty()); inputColumn.setCellValueFactory(cell -> cell.getValue().getInputProperty());
parsedColumn.setCellFactory(cellFactory); parsedColumn.setCellFactory(cellFactory);
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty()); parsedColumn.setCellValueFactory(cell -> cell.getValue().getParsedProperty());
outputColumn.setCellFactory(cellFactory); outputColumn.setCellFactory(cellFactory);
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty()); outputColumn.setCellValueFactory(cell -> cell.getValue().getOutputProperty());
coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if(oldValue.equals(settingsTab)) alertIfApplyNeeded(true); if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
}); });
abacus = new Abacus(); abacus = new Abacus(new Configuration(CONFIG_FILE));
abacus.getPluginManager().addListener(this); PluginManager abacusPluginManager = abacus.getPluginManager();
abacus.getPluginManager().reload(); abacusPluginManager.addListener(this);
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
try {
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
abacusPluginManager.reload();
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
if(!newValue.matches("(\\d+(\\.\\d*)?)?")) {
computationLimitField.setText(oldValue);
} else {
changesMade = true;
}
});
changesMade = false; changesMade = false;
reloadAlertShown = false; reloadAlertShown = false;
@@ -167,29 +282,29 @@ public class AbacusController implements PluginListener {
} }
@FXML @FXML
private void performCalculation(){ public void performCalculation() {
inputButton.setDisable(true); inputButton.setDisable(true);
TreeNode constructedTree = abacus.parseString(inputField.getText()); stopButton.setDisable(false);
if(constructedTree == null){ calculationThread = new Thread(CALCULATION_RUNNABLE);
outputText.setText(ERR_SYNTAX); calculationThread.start();
inputButton.setDisable(false); computationLimitThread = new Thread(TIMER_RUNNABLE);
return; computationLimitThread.start();
}
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
if(evaluatedNumber == null){
outputText.setText(ERR_EVAL);
inputButton.setDisable(false);
return;
}
outputText.setText(evaluatedNumber.toString());
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
inputButton.setDisable(false);
inputField.setText("");
} }
@FXML @FXML
private void performSaveAndReload(){ public void performStop(){
if(calculationThread != null) {
calculationThread.interrupt();
calculationThread = null;
}
if(computationLimitThread != null){
computationLimitThread.interrupt();
computationLimitThread = null;
}
}
@FXML
public void performSaveAndReload() {
performSave(); performSave();
performReload(); performReload();
changesMade = false; changesMade = false;
@@ -197,21 +312,23 @@ public class AbacusController implements PluginListener {
} }
@FXML @FXML
private void performReload(){ public void performReload() {
alertIfApplyNeeded(true); alertIfApplyNeeded(true);
abacus.getPluginManager().reload(); abacus.getPluginManager().reload();
} }
@FXML @FXML
private void performSave(){ public void performSave() {
Configuration configuration = abacus.getConfiguration(); Configuration configuration = abacus.getConfiguration();
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem()); configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
Set<String> disabledPlugins = configuration.getDisabledPlugins(); Set<String> disabledPlugins = configuration.getDisabledPlugins();
disabledPlugins.clear(); disabledPlugins.clear();
for(ToggleablePlugin pluginEntry : enabledPlugins){ for (ToggleablePlugin pluginEntry : enabledPlugins) {
if(!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName()); if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
} }
configuration.saveTo(Abacus.CONFIG_FILE); if(computationLimitField.getText().matches("\\d*(\\.\\d+)?") && computationLimitField.getText().length() != 0)
configuration.setComputationDelay(Double.parseDouble(computationLimitField.getText()));
configuration.saveTo(CONFIG_FILE);
changesMade = false; changesMade = false;
reloadAlertShown = false; reloadAlertShown = false;
} }
@@ -224,17 +341,21 @@ public class AbacusController implements PluginListener {
String actualImplementation = configuration.getNumberImplementation(); String actualImplementation = configuration.getNumberImplementation();
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "<default>"; String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "<default>";
numberImplementationBox.getSelectionModel().select(toSelect); numberImplementationBox.getSelectionModel().select(toSelect);
for(Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()){ for (Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()) {
String fullName = pluginClass.getName(); String fullName = pluginClass.getName();
ToggleablePlugin plugin = new ToggleablePlugin(!disabledPlugins.contains(fullName), fullName); ToggleablePlugin plugin = new ToggleablePlugin(fullName, !disabledPlugins.contains(fullName));
plugin.enabledProperty().addListener(e -> changesMade = true); plugin.getEnabledProperty().addListener(e -> changesMade = true);
enabledPlugins.add(plugin); enabledPlugins.add(plugin);
} }
functionList.addAll(manager.getAllFunctions());
functionList.sort(String::compareTo);
} }
@Override @Override
public void onUnload(PluginManager manager) { public void onUnload(PluginManager manager) {
functionList.clear();
enabledPlugins.clear(); enabledPlugins.clear();
numberImplementationOptions.clear(); numberImplementationOptions.clear();
} }
} }

View File

@@ -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.
*/ */
@@ -17,9 +18,9 @@ public class CopyableCell<S, T> extends TableCell<S, T> {
/** /**
* Creates a new copyable cell. * Creates a new copyable cell.
*/ */
public CopyableCell(){ public CopyableCell() {
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> { addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
if(event.getClickCount() == 2){ if (event.getClickCount() == 2) {
Toolkit.getDefaultToolkit().getSystemClipboard() Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(new StringSelection(getText()), null); .setContents(new StringSelection(getText()), null);
} }

View File

@@ -1,87 +0,0 @@
package org.nwapw.abacus.fx;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
* The data model used for storing history entries.
*/
public class HistoryModel {
/**
* The property used for displaying the column
* for the user input.
*/
private final StringProperty input;
/**
* The property used for displaying the column
* that contains the parsed input.
*/
private final StringProperty parsed;
/**
* The property used for displaying the column
* that contains the program output.
*/
private final StringProperty output;
/**
* Creates a new history model with the given variables.
* @param input the user input
* @param parsed the parsed input
* @param output the program output.
*/
public HistoryModel(String input, String parsed, String output){
this.input = new SimpleStringProperty();
this.parsed = new SimpleStringProperty();
this.output = new SimpleStringProperty();
this.input.setValue(input);
this.parsed.setValue(parsed);
this.output.setValue(output);
}
/**
* Gets the input property.
* @return the input property.
*/
public StringProperty inputProperty() {
return input;
}
/**
* Gets the input.
* @return the input.
*/
public String getInput() {
return input.get();
}
/**
* Gets the parsed input property.
* @return the parsed input property.
*/
public StringProperty parsedProperty() {
return parsed;
}
/**
* Gets the parsed input.
* @return the parsed input.
*/
public String getParsed() {
return parsed.get();
}
/**
* Gets the output property.
* @return the output property.
*/
public StringProperty outputProperty() {
return output;
}
/**
* Gets the program output.
* @return the output.
*/
public String getOutput() {
return output.get();
}
}

View File

@@ -1,56 +0,0 @@
package org.nwapw.abacus.fx;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
/**
* Class that represents an entry in the plugin check box list.
* The changes from this property are written to the config on application.
*/
public class ToggleablePlugin {
/**
* The property that determines whether the plugin will be enabled.
*/
private final BooleanProperty enabled;
/**
* The name of the class this entry toggles.
*/
private final String className;
/**
* Creates a new toggleable plugin with the given properties.
* @param enabled the enabled / disabled state at the beginning.
* @param className the name of the class this plugin toggles.
*/
public ToggleablePlugin(boolean enabled, String className){
this.enabled = new SimpleBooleanProperty();
this.enabled.setValue(enabled);
this.className = className;
}
/**
* Gets the enabled property of this plugin.
* @return the enabled property.
*/
public BooleanProperty enabledProperty() {
return enabled;
}
/**
* Checks if this plugin entry should be enabled.
* @return whether this plugin will be enabled.
*/
public boolean isEnabled() {
return enabled.get();
}
/**
* Gets the class name this plugin toggles.
* @return the class name that should be disabled.
*/
public String getClassName() {
return className;
}
}

View File

@@ -15,7 +15,7 @@ public class ValueNode<T> extends PatternNode<T> {
/** /**
* Creates a new node that matches the given character. * Creates a new node that matches the given character.
* *
* @param value * @param value the character value of the node.
*/ */
public ValueNode(char value) { public ValueNode(char value) {
this.value = value; this.value = value;

View File

@@ -0,0 +1,16 @@
package org.nwapw.abacus.number;
/**
* Exception thrown when the computation is interrupted by
* the user.
*/
public class ComputationInterruptedException extends RuntimeException {
/**
* Creates a new exception of this type.
*/
public ComputationInterruptedException(){
super("Computation interrupted by user.");
}
}

View File

@@ -3,7 +3,7 @@ package org.nwapw.abacus.number;
/** /**
* An implementation of NumberInterface using a double. * An implementation of NumberInterface using a double.
*/ */
public class NaiveNumber implements NumberInterface { public class NaiveNumber extends NumberInterface {
/** /**
* The number zero. * The number zero.
@@ -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.
* *
@@ -41,32 +42,32 @@ public class NaiveNumber implements NumberInterface {
} }
@Override @Override
public NumberInterface multiply(NumberInterface multiplier) { public NumberInterface multiplyInternal(NumberInterface multiplier) {
return new NaiveNumber(value * ((NaiveNumber) multiplier).value); return new NaiveNumber(value * ((NaiveNumber) multiplier).value);
} }
@Override @Override
public NumberInterface divide(NumberInterface divisor) { public NumberInterface divideInternal(NumberInterface divisor) {
return new NaiveNumber(value / ((NaiveNumber) divisor).value); return new NaiveNumber(value / ((NaiveNumber) divisor).value);
} }
@Override @Override
public NumberInterface add(NumberInterface summand) { public NumberInterface addInternal(NumberInterface summand) {
return new NaiveNumber(value + ((NaiveNumber) summand).value); return new NaiveNumber(value + ((NaiveNumber) summand).value);
} }
@Override @Override
public NumberInterface subtract(NumberInterface subtrahend) { public NumberInterface subtractInternal(NumberInterface subtrahend) {
return new NaiveNumber(value - ((NaiveNumber) subtrahend).value); return new NaiveNumber(value - ((NaiveNumber) subtrahend).value);
} }
@Override @Override
public NumberInterface negate() { public NumberInterface negateInternal() {
return new NaiveNumber(-value); return new NaiveNumber(-value);
} }
@Override @Override
public NumberInterface intPow(int exponent) { public NumberInterface intPowInternal(int exponent) {
if (exponent == 0) { if (exponent == 0) {
return NaiveNumber.ONE; return NaiveNumber.ONE;
} }
@@ -94,27 +95,27 @@ public class NaiveNumber implements NumberInterface {
} }
@Override @Override
public NumberInterface ceiling() { public NumberInterface ceilingInternal() {
return new NaiveNumber(Math.ceil(value)); return new NaiveNumber(Math.ceil(value));
} }
@Override @Override
public NumberInterface floor() { public NumberInterface floorInternal() {
return new NaiveNumber(Math.floor(value)); return new NaiveNumber(Math.floor(value));
} }
@Override @Override
public NumberInterface fractionalPart() { public NumberInterface fractionalPartInternal() {
return new NaiveNumber(value - Math.floor(value)); return new NaiveNumber(value - Math.floor(value));
} }
@Override @Override
public int intValue() { public int intValue() {
return (int)value; return (int) value;
} }
@Override @Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) { public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
if (toClass == this.getClass()) return this; if (toClass == this.getClass()) return this;
else if (toClass == PreciseNumber.class) { else if (toClass == PreciseNumber.class) {
return new PreciseNumber(Double.toString(value)); return new PreciseNumber(Double.toString(value));
@@ -127,5 +128,9 @@ public class NaiveNumber implements NumberInterface {
return Double.toString(Math.round(value * shiftBy) / shiftBy); return Double.toString(Math.round(value * shiftBy) / shiftBy);
} }
@Override
public NumberInterface getMaxError(){
return new NaiveNumber(Math.pow(10, -18));
}
} }

View File

@@ -3,14 +3,22 @@ package org.nwapw.abacus.number;
/** /**
* An interface used to represent a number. * An interface used to represent a number.
*/ */
public interface NumberInterface { public abstract class NumberInterface {
/**
* Check if the thread was interrupted and
* throw an exception to end the computation.
*/
private static void checkInterrupted(){
if(Thread.currentThread().isInterrupted())
throw new ComputationInterruptedException();
}
/** /**
* The maximum precision to which this number operates. * The maximum precision to which this number operates.
* *
* @return the precision. * @return the precision.
*/ */
int getMaxPrecision(); public abstract int getMaxPrecision();
/** /**
* Multiplies this number by another, returning * Multiplies this number by another, returning
@@ -19,7 +27,21 @@ public interface NumberInterface {
* @param multiplier the multiplier * @param multiplier the multiplier
* @return the result of the multiplication. * @return the result of the multiplication.
*/ */
NumberInterface multiply(NumberInterface multiplier); protected abstract NumberInterface multiplyInternal(NumberInterface multiplier);
/**
* Multiplies this number by another, returning
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param multiplier the multiplier
* @return the result of the multiplication.
*/
public final NumberInterface multiply(NumberInterface multiplier){
checkInterrupted();
return multiplyInternal(multiplier);
}
/** /**
* Divides this number by another, returning * Divides this number by another, returning
@@ -28,7 +50,21 @@ public interface NumberInterface {
* @param divisor the divisor * @param divisor the divisor
* @return the result of the division. * @return the result of the division.
*/ */
NumberInterface divide(NumberInterface divisor); protected abstract NumberInterface divideInternal(NumberInterface divisor);
/**
* Divides this number by another, returning
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param divisor the divisor
* @return the result of the division.
*/
public final NumberInterface divide(NumberInterface divisor){
checkInterrupted();
return divideInternal(divisor);
}
/** /**
* Adds this number to another, returning * Adds this number to another, returning
@@ -37,7 +73,21 @@ public interface NumberInterface {
* @param summand the summand * @param summand the summand
* @return the result of the summation. * @return the result of the summation.
*/ */
NumberInterface add(NumberInterface summand); protected abstract NumberInterface addInternal(NumberInterface summand);
/**
* Adds this number to another, returning
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param summand the summand
* @return the result of the summation.
*/
public final NumberInterface add(NumberInterface summand){
checkInterrupted();
return addInternal(summand);
}
/** /**
* Subtracts another number from this number, * Subtracts another number from this number,
@@ -46,7 +96,21 @@ public interface NumberInterface {
* @param subtrahend the subtrahend. * @param subtrahend the subtrahend.
* @return the result of the subtraction. * @return the result of the subtraction.
*/ */
NumberInterface subtract(NumberInterface subtrahend); protected abstract NumberInterface subtractInternal(NumberInterface subtrahend);
/**
* Subtracts another number from this number,
* a new number instance. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param subtrahend the subtrahend.
* @return the result of the subtraction.
*/
public final NumberInterface subtract(NumberInterface subtrahend){
checkInterrupted();
return subtractInternal(subtrahend);
}
/** /**
* Returns a new instance of this number with * Returns a new instance of this number with
@@ -54,7 +118,21 @@ public interface NumberInterface {
* *
* @return the new instance. * @return the new instance.
*/ */
NumberInterface negate(); protected abstract NumberInterface negateInternal();
/**
* Returns a new instance of this number with
* the sign flipped. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @return the new instance.
*/
public final NumberInterface negate(){
checkInterrupted();
return negateInternal();
}
/** /**
* Raises this number to an integer power. * Raises this number to an integer power.
@@ -62,7 +140,20 @@ public interface NumberInterface {
* @param exponent the exponent to which to take the number. * @param exponent the exponent to which to take the number.
* @return the resulting value. * @return the resulting value.
*/ */
NumberInterface intPow(int exponent); protected abstract NumberInterface intPowInternal(int exponent);
/**
* Raises this number to an integer power. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param exponent the exponent to which to take the number.
* @return the resulting value.
*/
public final NumberInterface intPow(int exponent){
checkInterrupted();
return intPowInternal(exponent);
}
/** /**
* Compares this number to another. * Compares this number to another.
@@ -70,39 +161,78 @@ public interface NumberInterface {
* @param number the number to compare to. * @param number the number to compare to.
* @return same as Integer.compare(); * @return same as Integer.compare();
*/ */
int compareTo(NumberInterface number); public abstract int compareTo(NumberInterface number);
/** /**
* Same as Math.signum(). * Same as Math.signum().
* *
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0. * @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
*/ */
int signum(); public abstract int signum();
/** /**
* 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 greater or equal to the number, if int can hold the value.
*/ */
NumberInterface ceiling(); protected abstract NumberInterface ceilingInternal();
/**
* Returns the least integer greater than or equal to the number.
* Also, checks if the thread has been interrupted, and if so, throws
* an exception.
*
* @return the least integer bigger or equal to the number.
*/
public final NumberInterface ceiling(){
checkInterrupted();
return ceilingInternal();
}
/** /**
* 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 integer smaller or equal the number.
*/ */
NumberInterface floor(); protected abstract NumberInterface floorInternal();
/**
* Return the greatest integer less than or equal to the number.
* Also, checks if the thread has been interrupted, and if so, throws
* an exception.
*
* @return the greatest int smaller than or equal to the number.
*/
public final NumberInterface floor(){
checkInterrupted();
return floorInternal();
}
/** /**
* 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(); protected abstract NumberInterface fractionalPartInternal();
/**
* Returns the fractional part of the number, specifically x - floor(x).
* Also, checks if the thread has been interrupted,
* and if so, throws an exception.
* @return the fractional part of the number.
*/
public final NumberInterface fractionalPart(){
checkInterrupted();
return fractionalPartInternal();
}
/** /**
* 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 the integer value of this number.
*/ */
int intValue(); public abstract int intValue();
/** /**
* Promotes this class to another number class. * Promotes this class to another number class.
@@ -110,6 +240,26 @@ public interface NumberInterface {
* @param toClass the class to promote to. * @param toClass the class to promote to.
* @return the resulting new instance. * @return the resulting new instance.
*/ */
NumberInterface promoteTo(Class<? extends NumberInterface> toClass); protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
/**
* Promotes this class to another number class. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param toClass the class to promote to.
* @return the resulting new instance.
*/
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
checkInterrupted();
return promoteToInternal(toClass);
}
/**
* Returns the smallest error this instance can tolerate depending
* on its precision and value.
* @return the smallest error that should be permitted in calculations.
*/
public abstract NumberInterface getMaxError();
} }

View File

@@ -1,13 +1,14 @@
package org.nwapw.abacus.number; package org.nwapw.abacus.number;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.BigInteger;
import java.math.MathContext;
/** /**
* A number that uses a BigDecimal to store its value, * A number that uses a BigDecimal to store its value,
* leading to infinite possible precision. * leading to infinite possible precision.
*/ */
public class PreciseNumber implements NumberInterface { public class PreciseNumber extends NumberInterface {
/** /**
* The number one. * The number one.
@@ -22,6 +23,21 @@ public class PreciseNumber implements NumberInterface {
*/ */
public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN); public static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
/**
* The number of extra significant figures kept in calculations before rounding for output.
*/
private static int numExtraInternalSigFigs = 15;
/**
* MathContext that is used when rounding a number prior to output.
*/
private static MathContext outputContext = new MathContext(50);
/**
* MathContext that is actually used in calculations.
*/
private static MathContext internalContext = new MathContext(outputContext.getPrecision()+numExtraInternalSigFigs);
/** /**
* The value of the PreciseNumber. * The value of the PreciseNumber.
*/ */
@@ -46,33 +62,42 @@ public class PreciseNumber implements NumberInterface {
this.value = value; this.value = value;
} }
@Override /**
public int getMaxPrecision() { * Constructs a precise number from the given BigInteger.
return 65; *
* @param value a BigInteger object representing the value of the number.
*/
public PreciseNumber(BigInteger value) {
this.value = new BigDecimal(value);
} }
@Override @Override
public NumberInterface multiply(NumberInterface multiplier) { public int getMaxPrecision() {
return internalContext.getPrecision();
}
@Override
public NumberInterface multiplyInternal(NumberInterface multiplier) {
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value)); return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
} }
@Override @Override
public NumberInterface divide(NumberInterface divisor) { public NumberInterface divideInternal(NumberInterface divisor) {
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.getMaxPrecision(), RoundingMode.HALF_UP)); return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, internalContext));
} }
@Override @Override
public NumberInterface add(NumberInterface summand) { public NumberInterface addInternal(NumberInterface summand) {
return new PreciseNumber(value.add(((PreciseNumber) summand).value)); return new PreciseNumber(value.add(((PreciseNumber) summand).value));
} }
@Override @Override
public NumberInterface subtract(NumberInterface subtrahend) { public NumberInterface subtractInternal(NumberInterface subtrahend) {
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value)); return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
} }
@Override @Override
public NumberInterface intPow(int exponent) { public NumberInterface intPowInternal(int exponent) {
if (exponent == 0) { if (exponent == 0) {
return PreciseNumber.ONE; return PreciseNumber.ONE;
} }
@@ -99,33 +124,32 @@ public class PreciseNumber implements NumberInterface {
} }
@Override @Override
public NumberInterface ceiling() { public NumberInterface ceilingInternal() {
String str = value.toPlainString(); String str = value.toPlainString();
int decimalIndex = str.indexOf('.'); int decimalIndex = str.indexOf('.');
if(decimalIndex != -1){ if (decimalIndex != -1) {
return this.floor().add(ONE); return this.floor().add(ONE);
} }
return this; return this;
} }
@Override @Override
public NumberInterface floor() { public NumberInterface floorInternal() {
String str = value.toPlainString(); String str = value.toPlainString();
int decimalIndex = str.indexOf('.'); int decimalIndex = str.indexOf('.');
if(decimalIndex != -1){ if (decimalIndex != -1) {
return new PreciseNumber(str.substring(0, decimalIndex)); NumberInterface floor = new PreciseNumber(str.substring(0, decimalIndex));
if(signum() == -1){
floor = floor.subtract(ONE);
}
return floor;
} }
return this; return this;
} }
@Override @Override
public NumberInterface fractionalPart() { public NumberInterface fractionalPartInternal() {
String str = value.toPlainString(); return this.subtractInternal(floorInternal());
int decimalIndex = str.indexOf('.');
if(decimalIndex != -1){
return new PreciseNumber(str.substring(decimalIndex + 1));
}
return ZERO;
} }
@Override @Override
@@ -134,12 +158,12 @@ public class PreciseNumber implements NumberInterface {
} }
@Override @Override
public NumberInterface negate() { public NumberInterface negateInternal() {
return new PreciseNumber(value.negate()); return new PreciseNumber(value.negate());
} }
@Override @Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) { public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
if (toClass == this.getClass()) { if (toClass == this.getClass()) {
return this; return this;
} }
@@ -148,7 +172,11 @@ public class PreciseNumber implements NumberInterface {
@Override @Override
public String toString() { public String toString() {
BigDecimal rounded = value.setScale(getMaxPrecision() - 15, RoundingMode.HALF_UP); return value.round(outputContext).toString();
return rounded.stripTrailingZeros().toPlainString(); }
@Override
public NumberInterface getMaxError(){
return new PreciseNumber(value.ulp()).multiplyInternal(TEN.intPowInternal(value.precision()-internalContext.getPrecision()));
} }
} }

View File

@@ -0,0 +1,115 @@
package org.nwapw.abacus.number;
import org.apache.commons.math3.fraction.BigFraction;
import org.apache.commons.math3.fraction.Fraction;
import java.math.BigInteger;
public class RationalNumber extends NumberInterface{
static final RationalNumber ONE = new RationalNumber(BigFraction.ONE);
/**
* The value of the number.
*/
private BigFraction value;
/**
* Constructs a new instance with the given value.
* @param value
*/
public RationalNumber(BigFraction value){
this.value = value;
}
@Override
public int getMaxPrecision() {
return 0;
}
@Override
protected NumberInterface multiplyInternal(NumberInterface multiplier) {
return new RationalNumber(value.multiply(((RationalNumber)multiplier).value));
}
@Override
protected NumberInterface divideInternal(NumberInterface divisor) {
return new RationalNumber(value.divide(((RationalNumber)divisor).value));
}
@Override
protected NumberInterface addInternal(NumberInterface summand) {
return new RationalNumber(value.add(((RationalNumber)summand).value));
}
@Override
protected NumberInterface subtractInternal(NumberInterface subtrahend) {
return new RationalNumber(value.subtract(((RationalNumber)subtrahend).value));
}
@Override
protected NumberInterface negateInternal() {
return new RationalNumber(value.negate());
}
@Override
protected NumberInterface intPowInternal(int exponent) {
return new RationalNumber(value.pow(exponent));
}
@Override
public int compareTo(NumberInterface number) {
return value.compareTo(((RationalNumber)number).value);
}
@Override
public int signum() {
return value.getNumerator().signum();
}
@Override
protected NumberInterface ceilingInternal() {
if(value.getNumeratorAsInt() != 1){
return floorInternal().add(ONE);
}
return this;
}
@Override
protected NumberInterface floorInternal() {
BigInteger floor = value.bigDecimalValue().toBigInteger();
if(value.compareTo(BigFraction.ZERO) < 0 && value.getDenominatorAsInt() != 1){
floor = floor.subtract(BigInteger.ONE);
}
return new RationalNumber(new BigFraction(floor));
}
@Override
protected NumberInterface fractionalPartInternal() {
return this.subtractInternal(floorInternal());
}
@Override
public int intValue() {
return 0;
}
@Override
protected NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
return null;
}
@Override
public NumberInterface getMaxError() {
return toPreciseNumber().getMaxError();
}
@Override
public String toString(){
return toPreciseNumber().toString();
}
PreciseNumber toPreciseNumber(){
return (PreciseNumber) new PreciseNumber(value.getNumerator()).divideInternal(new PreciseNumber(value.getDenominator()));
}
}

View File

@@ -77,8 +77,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
continue; continue;
} }
if(tokenString.equals("-") && (previousType == null || previousType == TokenType.OP || if (tokenString.equals("-") && (previousType == null || previousType == TokenType.OP ||
previousType == TokenType.OPEN_PARENTH)){ previousType == TokenType.OPEN_PARENTH)) {
from.add(0, new Match<>("`", TokenType.OP)); from.add(0, new Match<>("`", TokenType.OP));
continue; continue;
} }
@@ -162,8 +162,10 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
@Override @Override
public TreeNode constructTree(List<Match<TokenType>> tokens) { public TreeNode constructTree(List<Match<TokenType>> tokens) {
tokens = intoPostfix(new ArrayList<>(tokens)); tokens = intoPostfix(new ArrayList<>(tokens));
if(tokens == null) return null;
Collections.reverse(tokens); Collections.reverse(tokens);
return constructRecursive(tokens); TreeNode constructedTree = constructRecursive(tokens);
return tokens.size() == 0 ? constructedTree : null;
} }
@Override @Override

View File

@@ -26,10 +26,11 @@ 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 means more likely to be converted into.
*/ */
public NumberImplementation(Class<? extends NumberInterface> implementation, int priority){ public NumberImplementation(Class<? extends NumberInterface> implementation, int priority) {
this.implementation = implementation; this.implementation = implementation;
this.priority = priority; this.priority = priority;
promotionPaths = new HashMap<>(); promotionPaths = new HashMap<>();
@@ -37,30 +38,34 @@ 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() {
return promotionPaths; return promotionPaths;
} }
/** /**
* 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() {
return implementation; return implementation;
} }
/** /**
* 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() {
return priority; return priority;
} }
/** /**
* 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();

View File

@@ -17,18 +17,6 @@ import java.util.Set;
*/ */
public abstract class Plugin { 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 * The plugin manager in which to search for functions
* not inside this package, * not inside this package,
@@ -49,69 +37,9 @@ public abstract class Plugin {
*/ */
public Plugin(PluginManager manager) { public Plugin(PluginManager manager) {
this.manager = manager; this.manager = manager;
functions = new HashMap<>();
operators = new HashMap<>();
numberImplementations = new HashMap<>();
enabled = false; 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 * Enables the function, loading the necessary instances
* of functions. * of functions.
@@ -129,8 +57,6 @@ public abstract class Plugin {
public final void disable() { public final void disable() {
if (!enabled) return; if (!enabled) return;
onDisable(); onDisable();
functions.clear();
operators.clear();
enabled = false; enabled = false;
} }
@@ -142,7 +68,7 @@ public abstract class Plugin {
* @param toRegister the function implementation. * @param toRegister the function implementation.
*/ */
protected final void registerFunction(String name, Function toRegister) { protected final void registerFunction(String name, Function toRegister) {
functions.put(name, toRegister); manager.registerFunction(name, toRegister);
} }
/** /**
@@ -154,17 +80,18 @@ public abstract class Plugin {
* @param operator the operator to register. * @param operator the operator to register.
*/ */
protected final void registerOperator(String name, Operator operator) { protected final void registerOperator(String name, Operator operator) {
operators.put(name, operator); manager.registerOperator(name, operator);
} }
/** /**
* 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.
*/ */
protected final void registerNumberImplementation(String name, NumberImplementation implementation){ protected final void registerNumberImplementation(String name, NumberImplementation implementation) {
numberImplementations.put(name, implementation); manager.registerNumberImplementation(name, implementation);
} }
/** /**
@@ -199,7 +126,7 @@ public abstract class Plugin {
* @param name the name for which to search. * @param name the name for which to search.
* @return the resulting number implementation, or null if none was found. * @return the resulting number implementation, or null if none was found.
*/ */
protected final NumberImplementation numberImplementationFor(String name){ protected final NumberImplementation numberImplementationFor(String name) {
return manager.numberImplementationFor(name); return manager.numberImplementationFor(name);
} }
@@ -208,10 +135,11 @@ 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.
*/ */
protected final NumberInterface getPi(Class<? extends NumberInterface> forClass){ protected final NumberInterface piFor(Class<? extends NumberInterface> forClass) {
return manager.piFor(forClass); return manager.piFor(forClass);
} }

View File

@@ -23,20 +23,17 @@ public class PluginManager {
*/ */
private Set<Plugin> plugins; private Set<Plugin> plugins;
/** /**
* List of functions that have been cached, * The map of functions registered by the plugins.
* that is, found in a plugin and returned.
*/ */
private Map<String, Function> cachedFunctions; private Map<String, Function> registeredFunctions;
/** /**
* List of operators that have been cached, * The map of operators registered by the plugins
* that is, found in a plugin and returned.
*/ */
private Map<String, Operator> cachedOperators; private Map<String, Operator> registeredOperators;
/** /**
* The list of number implementations that have * The map of number implementations registered by the plugins.
* been cached, that is, found in a plugin and returned.
*/ */
private Map<String, NumberImplementation> cachedNumberImplementations; private Map<String, NumberImplementation> registeredNumberImplementations;
/** /**
* The list of number implementations that have been * The list of number implementations that have been
* found by their implementation class. * found by their implementation class.
@@ -46,18 +43,6 @@ public class PluginManager {
* The pi values for each implementation class that have already been computer. * The pi values for each implementation class that have already been computer.
*/ */
private Map<Class<? extends NumberInterface>, NumberInterface> cachedPi; 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. * The list of plugin listeners attached to this instance.
*/ */
@@ -70,101 +55,89 @@ public class PluginManager {
/** /**
* Creates a new plugin manager. * Creates a new plugin manager.
*
* @param abacus the abacus instance.
*/ */
public PluginManager(Abacus abacus) { public PluginManager(Abacus abacus) {
this.abacus = abacus; this.abacus = abacus;
loadedPluginClasses = new HashSet<>(); loadedPluginClasses = new HashSet<>();
plugins = new HashSet<>(); plugins = new HashSet<>();
cachedFunctions = new HashMap<>(); registeredFunctions = new HashMap<>();
cachedOperators = new HashMap<>(); registeredOperators = new HashMap<>();
cachedNumberImplementations = new HashMap<>(); registeredNumberImplementations = new HashMap<>();
cachedInterfaceImplementations = new HashMap<>(); cachedInterfaceImplementations = new HashMap<>();
cachedPi = new HashMap<>(); cachedPi = new HashMap<>();
allFunctions = new HashSet<>();
allOperators = new HashSet<>();
allNumberImplementations = new HashSet<>();
listeners = new HashSet<>(); listeners = new HashSet<>();
} }
/** /**
* Searches the plugin list for a certain value, retrieving the Plugin's * Registers a function under the given name.
* list of items of the type using the setFunction and getting the value * @param name the name of the function.
* of it is available via getFunction. If the value is contained * @param function the function to register.
* 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.
*/ */
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache, public void registerFunction(String name, Function function){
java.util.function.Function<Plugin, Set<K>> setFunction, registeredFunctions.put(name, function);
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;
} }
/** /**
* Gets a function under the given name. * Registers an operator 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 name the name of the operator.
* @return the operator under the given name. * @param operator the operator to register.
*/ */
public Operator operatorFor(String name) { public void registerOperator(String name, Operator operator){
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name); registeredOperators.put(name, operator);
} }
/** /**
* Gets the number implementation under the given name. * Registers a number implementation under the given name.
* @param name the name of the implementation. * @param name the name of the number implementation.
* @return the implementation. * @param implementation the number implementation to register.
*/
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){ public NumberImplementation numberImplementationFor(String name){
return searchCached(plugins, cachedNumberImplementations, Plugin::providedNumberImplementations, return registeredNumberImplementations.get(name);
Plugin::getNumberImplementation, name);
} }
/** /**
* 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.
*/ */
public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name){ public NumberImplementation interfaceImplementationFor(Class<? extends NumberInterface> name) {
if(cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name); if (cachedInterfaceImplementations.containsKey(name)) return cachedInterfaceImplementations.get(name);
NumberImplementation toReturn = null; NumberImplementation toReturn = null;
outside: for(String key : registeredNumberImplementations.keySet()){
for(Plugin plugin : plugins){ NumberImplementation implementation = registeredNumberImplementations.get(key);
for(String implementationName : plugin.providedNumberImplementations()){ if(implementation.getImplementation() == name) {
NumberImplementation implementation = plugin.getNumberImplementation(implementationName); toReturn = implementation;
if(implementation.getImplementation().equals(name)) { break;
toReturn = implementation;
break outside;
}
} }
} }
cachedInterfaceImplementations.put(name, toReturn); cachedInterfaceImplementations.put(name, toReturn);
@@ -173,14 +146,15 @@ 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
*/ */
public NumberInterface piFor(Class<? extends NumberInterface> forClass){ public NumberInterface piFor(Class<? extends NumberInterface> forClass) {
if(cachedPi.containsKey(forClass)) return cachedPi.get(forClass); if (cachedPi.containsKey(forClass)) return cachedPi.get(forClass);
NumberImplementation implementation = interfaceImplementationFor(forClass); NumberImplementation implementation = interfaceImplementationFor(forClass);
NumberInterface generatedPi = null; NumberInterface generatedPi = null;
if(implementation != null){ if (implementation != null) {
generatedPi = implementation.instanceForPi(); generatedPi = implementation.instanceForPi();
} }
cachedPi.put(forClass, generatedPi); cachedPi.put(forClass, generatedPi);
@@ -219,15 +193,9 @@ public class PluginManager {
public void load() { public void load() {
Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins(); Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins();
for (Plugin plugin : plugins) { for (Plugin plugin : plugins) {
if(disabledPlugins.contains(plugin.getClass().getName())) continue; if (disabledPlugins.contains(plugin.getClass().getName())) continue;
plugin.enable(); 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)); listeners.forEach(e -> e.onLoad(this));
} }
@@ -238,17 +206,14 @@ public class PluginManager {
listeners.forEach(e -> e.onUnload(this)); listeners.forEach(e -> e.onUnload(this));
Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins(); Set<String> disabledPlugins = abacus.getConfiguration().getDisabledPlugins();
for (Plugin plugin : plugins) { for (Plugin plugin : plugins) {
if(disabledPlugins.contains(plugin.getClass().getName())) continue; if (disabledPlugins.contains(plugin.getClass().getName())) continue;
plugin.disable(); plugin.disable();
} }
cachedFunctions.clear(); registeredFunctions.clear();
cachedOperators.clear(); registeredOperators.clear();
cachedNumberImplementations.clear(); registeredNumberImplementations.clear();
cachedInterfaceImplementations.clear(); cachedInterfaceImplementations.clear();
cachedPi.clear(); cachedPi.clear();
allFunctions.clear();
allOperators.clear();
allNumberImplementations.clear();
listeners.forEach(e -> e.onUnload(this)); listeners.forEach(e -> e.onUnload(this));
} }
@@ -266,7 +231,7 @@ public class PluginManager {
* @return the set of all functions that were loaded. * @return the set of all functions that were loaded.
*/ */
public Set<String> getAllFunctions() { public Set<String> getAllFunctions() {
return allFunctions; return registeredFunctions.keySet();
} }
/** /**
@@ -275,7 +240,7 @@ public class PluginManager {
* @return the set of all operators that were loaded. * @return the set of all operators that were loaded.
*/ */
public Set<String> getAllOperators() { public Set<String> getAllOperators() {
return allOperators; return registeredOperators.keySet();
} }
/** /**
@@ -283,8 +248,8 @@ public class PluginManager {
* *
* @return the set of all implementations that were loaded. * @return the set of all implementations that were loaded.
*/ */
public Set<String> getAllNumberImplementations(){ public Set<String> getAllNumberImplementations() {
return allNumberImplementations; return registeredNumberImplementations.keySet();
} }
/** /**
@@ -308,6 +273,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() {

View File

@@ -18,7 +18,10 @@ 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<>(); /**
* 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, +
@@ -50,6 +53,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].subtract(params[1]); return params[0].subtract(params[1]);
} }
}); });
/** /**
@@ -113,7 +117,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if (params[0].signum() == 0) { if (params[0].signum() == 0) {
return (new NaiveNumber(1)).promoteTo(params[0].getClass()); return fromInt(params[0].getClass(), 1);
} }
NumberInterface factorial = params[0]; NumberInterface factorial = params[0];
NumberInterface multiplier = params[0]; NumberInterface multiplier = params[0];
@@ -129,20 +133,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 +147,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.
*/ */
@@ -213,16 +160,16 @@ public class StandardPlugin extends Plugin {
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface param = params[0]; NumberInterface param = params[0];
int powersOf2 = 0; int powersOf2 = 0;
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 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) { 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++; powersOf2++;
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) { if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
break; break;
//No infinite loop for you. //No infinite loop for you.
} }
} else { } else {
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass())); param = param.multiply(fromInt(param.getClass(), 2));
powersOf2--; powersOf2--;
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) { if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
break; break;
@@ -240,7 +187,8 @@ public class StandardPlugin extends Plugin {
* @return the partial sum. * @return the partial sum.
*/ */
private NumberInterface getLogPartialSum(NumberInterface x) { private NumberInterface getLogPartialSum(NumberInterface x) {
NumberInterface maxError = getMaxError(x);
NumberInterface maxError = x.getMaxError();
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1). x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
NumberInterface currentNumerator = x, currentTerm = x, sum = x; NumberInterface currentNumerator = x, currentTerm = x, sum = x;
int n = 1; int n = 1;
@@ -259,18 +207,18 @@ public class StandardPlugin extends Plugin {
* @return the value of log(2) with the appropriate precision. * @return the value of log(2) with the appropriate precision.
*/ */
private NumberInterface getLog2(NumberInterface number) { private NumberInterface getLog2(NumberInterface number) {
NumberInterface maxError = getMaxError(number); NumberInterface maxError = number.getMaxError();
//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) //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. //In the following, a=1/3^n, b=1/4^n, c = 1/n.
//a is also an error bound. //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()); NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
int n = 0; int n = 0;
while (a.compareTo(maxError) >= 1) { while (a.compareTo(maxError) >= 1) {
n++; n++;
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass())); a = a.divide(fromInt(number.getClass(), 3));
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass())); b = b.divide(fromInt(number.getClass(), 4));
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass())); c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
sum = sum.add(a.add(b).multiply(c)); sum = sum.add(a.add(b).multiply(c));
} }
@@ -291,109 +239,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 +253,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.
*/ */
@@ -425,19 +269,19 @@ public class StandardPlugin extends Plugin {
NumberInterface L = new PreciseNumber("13591409"); NumberInterface L = new PreciseNumber("13591409");
NumberInterface X = M; NumberInterface X = M;
NumberInterface sum = L; NumberInterface sum = L;
int termsNeeded = C.getMaxPrecision()/13 + 1; int termsNeeded = C.getMaxPrecision() / 13 + 1;
NumberInterface lSummand = new PreciseNumber("545140134"); NumberInterface lSummand = new PreciseNumber("545140134");
NumberInterface xMultiplier = new PreciseNumber("262537412") NumberInterface xMultiplier = new PreciseNumber("262537412")
.multiply(new PreciseNumber("1000000000")) .multiply(new PreciseNumber("1000000000"))
.add(new PreciseNumber("640768000")) .add(new PreciseNumber("640768000"))
.negate(); .negate();
for(int i = 0; i < termsNeeded; i++){ for (int i = 0; i < termsNeeded; i++) {
M = M M = M
.multiply(new NaiveNumber(12*i+2).promoteTo(PreciseNumber.class)) .multiply(new NaiveNumber(12 * i + 2).promoteTo(PreciseNumber.class))
.multiply(new NaiveNumber(12*i+6).promoteTo(PreciseNumber.class)) .multiply(new NaiveNumber(12 * i + 6).promoteTo(PreciseNumber.class))
.multiply(new NaiveNumber(12*i+10).promoteTo(PreciseNumber.class)) .multiply(new NaiveNumber(12 * i + 10).promoteTo(PreciseNumber.class))
.divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(PreciseNumber.class)); .divide(new NaiveNumber(Math.pow(i + 1, 3)).promoteTo(PreciseNumber.class));
L = L.add(lSummand); L = L.add(lSummand);
X = X.multiply(xMultiplier); X = X.multiply(xMultiplier);
sum = sum.add(M.multiply(L).divide(X)); sum = sum.add(M.multiply(L).divide(X));
@@ -445,6 +289,167 @@ 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 = params[0].getMaxError();
int n = 0;
if (params[0].signum() < 0) {
NumberInterface[] negatedParams = {params[0].negate()};
return fromInt(params[0].getClass(), 1).divide(applyInternal(negatedParams));
} 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(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++;
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
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0)
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) != 0);
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
if (params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
return NaiveNumber.ONE.promoteTo(params[1].getClass());
//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).
*/
public final Function functionSin = new Function() {
@Override
protected boolean matchesParams(NumberInterface[] params) {
return params.length == 1;
}
@Override
protected NumberInterface applyInternal(NumberInterface[] params) {
NumberInterface pi = piFor(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(fromInt(pi.getClass(), 2))) > 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(piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2))
.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(functionSin.apply(params[0]));
}
};
public StandardPlugin(PluginManager manager) { public StandardPlugin(PluginManager manager) {
super(manager); super(manager);
@@ -467,13 +472,77 @@ public class StandardPlugin extends Plugin {
} }
/** /**
* Returns the maximum error based on the precision of the class of number. * A factorial function that uses memoization for each number class; it efficiently
* computes factorials of non-negative integers.
* *
* @param number Any instance of the NumberInterface in question (should return an appropriate precision). * @param numberClass type of number to return.
* @return the maximum error. * @param n non-negative integer.
* @return a number of numClass with value n factorial.
*/ */
private static NumberInterface getMaxError(NumberInterface number) { public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n) {
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision()); 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 = x.getMaxError();
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;
}
/**
* 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 @Override
@@ -505,59 +574,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;
}
} }

View File

@@ -1,108 +0,0 @@
package org.nwapw.abacus.tree;
/**
* A tree node that represents an operation being applied to two operands.
*/
public class BinaryNode extends TreeNode {
/**
* The operation being applied.
*/
private String operation;
/**
* The left node of the operation.
*/
private TreeNode left;
/**
* The right node of the operation.
*/
private TreeNode right;
private BinaryNode() {
}
/**
* Creates a new operation node with the given operation
* and no child nodes.
*
* @param operation the operation.
*/
public BinaryNode(String operation) {
this(operation, null, null);
}
/**
* Creates a new operation node with the given operation
* and child nodes.
*
* @param operation the operation.
* @param left the left node of the expression.
* @param right the right node of the expression.
*/
public BinaryNode(String operation, TreeNode left, TreeNode right) {
this.operation = operation;
this.left = left;
this.right = right;
}
/**
* Gets the operation in this node.
*
* @return the operation in this node.
*/
public String getOperation() {
return operation;
}
/**
* Gets the left sub-expression of this node.
*
* @return the left node.
*/
public TreeNode getLeft() {
return left;
}
/**
* Sets the left sub-expression of this node.
*
* @param left the sub-expression to apply.
*/
public void setLeft(TreeNode left) {
this.left = left;
}
/**
* Gets the right sub-expression of this node.
*
* @return the right node.
*/
public TreeNode getRight() {
return right;
}
/**
* Sets the right sub-expression of this node.
*
* @param right the sub-expression to apply.
*/
public void setRight(TreeNode right) {
this.right = right;
}
@Override
public <T> T reduce(Reducer<T> reducer) {
T leftReduce = left.reduce(reducer);
T rightReduce = right.reduce(reducer);
if (leftReduce == null || rightReduce == null) return null;
return reducer.reduceNode(this, leftReduce, rightReduce);
}
@Override
public String toString() {
String leftString = left != null ? left.toString() : "null";
String rightString = right != null ? right.toString() : "null";
return "(" + leftString + operation + rightString + ")";
}
}

View File

@@ -1,85 +0,0 @@
package org.nwapw.abacus.tree;
import java.util.ArrayList;
import java.util.List;
/**
* A node that represents a function call.
*/
public class FunctionNode extends TreeNode {
/**
* The name of the function being called
*/
private String function;
/**
* The list of arguments to the function.
*/
private List<TreeNode> children;
/**
* Creates a function node with no function.
*/
private FunctionNode() {
}
/**
* Creates a new function node with the given function name.
*
* @param function the function name.
*/
public FunctionNode(String function) {
this.function = function;
children = new ArrayList<>();
}
/**
* Gets the function name for this node.
*
* @return the function name.
*/
public String getFunction() {
return function;
}
/**
* Adds a child to the end of this node's child list.
*
* @param node the child to add.
*/
public void appendChild(TreeNode node) {
children.add(node);
}
/**
* Adds a new child to the beginning of this node's child list.
*
* @param node the node to add.
*/
public void prependChild(TreeNode node) {
children.add(0, node);
}
@Override
public <T> T reduce(Reducer<T> reducer) {
Object[] reducedChildren = new Object[children.size()];
for (int i = 0; i < reducedChildren.length; i++) {
reducedChildren[i] = children.get(i).reduce(reducer);
if (reducedChildren[i] == null) return null;
}
return reducer.reduceNode(this, reducedChildren);
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(function);
buffer.append("(");
for (int i = 0; i < children.size(); i++) {
buffer.append(children.get(i));
buffer.append(i == children.size() - 1 ? "" : ", ");
}
buffer.append(")");
return buffer.toString();
}
}

View File

@@ -1,49 +0,0 @@
package org.nwapw.abacus.tree;
import org.nwapw.abacus.number.NumberInterface;
/**
* A node implementation that represents a single number.
*/
public class NumberNode extends TreeNode {
/**
* The number that is represented by this number node.
*/
private NumberInterface number;
/**
* Creates a number node with no number.
*/
public NumberNode() {
number = null;
}
/**
* Creates a new number node with the given double value.
*
* @param newNumber the number for which to create a number node.
*/
public NumberNode(NumberInterface newNumber) {
this.number = newNumber;
}
/**
* Gets the number value of this node.
*
* @return the number value of this node.
*/
public NumberInterface getNumber() {
return number;
}
@Override
public <T> T reduce(Reducer<T> reducer) {
return reducer.reduceNode(this);
}
@Override
public String toString() {
return number != null ? number.toString() : "null";
}
}

View File

@@ -1,17 +0,0 @@
package org.nwapw.abacus.tree;
/**
* An abstract class that represents an expression tree node.
*/
public abstract class TreeNode {
/**
* The function that reduces a tree to a single vale.
*
* @param reducer the reducer used to reduce the tree.
* @param <T> the type the reducer produces.
* @return the result of the reduction, or null on error.
*/
public abstract <T> T reduce(Reducer<T> reducer);
}

View File

@@ -1,63 +0,0 @@
package org.nwapw.abacus.tree;
public class UnaryNode extends TreeNode {
/**
* The operation this node will apply.
*/
private String operation;
/**
* The tree node to apply the operation to.
*/
private TreeNode applyTo;
/**
* Creates a new node with the given operation and no child.
*
* @param operation the operation for this node.
*/
public UnaryNode(String operation) {
this(operation, null);
}
/**
* Creates a new node with the given operation and child.
*
* @param operation the operation for this node.
* @param applyTo the node to apply the function to.
*/
public UnaryNode(String operation, TreeNode applyTo) {
this.operation = operation;
this.applyTo = applyTo;
}
@Override
public <T> T reduce(Reducer<T> reducer) {
Object reducedChild = applyTo.reduce(reducer);
if (reducedChild == null) return null;
return reducer.reduceNode(this, reducedChild);
}
/**
* Gets the operation of this node.
*
* @return the operation this node performs.
*/
public String getOperation() {
return operation;
}
/**
* Gets the node to which this node's operation applies.
*
* @return the tree node to which the operation will be applied.
*/
public TreeNode getApplyTo() {
return applyTo;
}
@Override
public String toString() {
return "(" + (applyTo == null ? "null" : applyTo.toString()) + ")" + operation;
}
}

View File

@@ -250,6 +250,6 @@ public class Window extends JFrame {
@Override @Override
public void setVisible(boolean b) { public void setVisible(boolean b) {
super.setVisible(b); super.setVisible(b);
if(b) inputField.requestFocusInWindow(); if (b) inputField.requestFocusInWindow();
} }
} }

View File

@@ -0,0 +1,14 @@
package org.nwapw.abacus.function
/**
* A single operator that can be used by Abacus.
*
* This is a data class that holds the information about a single operator, such as a plus or minus.
*
* @param associativity the associativity of this operator, used for order of operations;.
* @param type the type of this operator, used for parsing (infix / prefix / postfix and binary / unary)
* @param precedence the precedence of this operator, used for order of operations.
* @param function the function this operator applies to its arguments.
*/
data class Operator(val associativity: OperatorAssociativity, val type: OperatorType,
val precedence: Int, val function: Function)

View File

@@ -0,0 +1,32 @@
package org.nwapw.abacus.fx
import javafx.beans.property.SimpleStringProperty
/**
* A model representing an input / output in the calculator.
*
* The HistoryModel class stores a record of a single user-provided input,
* its parsed form as it was interpreted by the calculator, and the output
* that was provided by the calculator. These are represented as properties
* to allow easy access by JavaFX cells.
*
* @param input the user input
* @param parsed the parsed version of the input.
* @param output the output string.
*/
class HistoryModel(input: String, parsed: String, output: String){
/**
* The property that holds the input.
*/
val inputProperty = SimpleStringProperty(input)
/**
* The property that holds the parsed input.
*/
val parsedProperty = SimpleStringProperty(parsed)
/**
* The property that holds the output.
*/
val outputProperty = SimpleStringProperty(output)
}

View File

@@ -0,0 +1,31 @@
package org.nwapw.abacus.fx
import javafx.beans.property.SimpleBooleanProperty
/**
* A model representing a plugin that can be disabled or enabled.
*
* ToggleablePlugin is a model that is used to present to the user the option
* of disabling / enabling plugins. The class name in this plugin is stored if
* its "enabledPropery" is false, essentially blacklisting the plugin.
*
* @param className the name of the class that this model concerns.
* @param enabled whether or not the model should start enabled.
*/
class ToggleablePlugin (val className: String, enabled: Boolean) {
/**
* The property used to interact with JavaFX components.
*/
val enabledProperty = SimpleBooleanProperty(enabled)
/**
* Checks whether this plugin is currently enabled or not.
*
* @return true if it is enabled, false otherwise.
*/
fun isEnabled(): Boolean {
return enabledProperty.value
}
}

View File

@@ -0,0 +1,26 @@
package org.nwapw.abacus.tree
/**
* A tree node that holds a binary operation.
*
* This node represents any binary operation, such as binary infix or binary postfix. The only
* currently implemented into Abacus is binary infix, but that has more to do with the parser than
* this class, which doesn't care about the order that its operation and nodes were found in text.
*
* @param operation the operation this node performs on its children.
* @param left the left node.
* @param right the right node.
*/
data class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
val leftReduce = left?.reduce(reducer) ?: return null
val rightReduce = right?.reduce(reducer) ?: return null
return reducer.reduceNode(this, leftReduce, rightReduce)
}
override fun toString(): String {
return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")"
}
}

View File

@@ -0,0 +1,52 @@
package org.nwapw.abacus.tree
/**
* A tree node that holds a function call.
*
* The function call node can hold any number of children, and passes the to the appropriate reducer,
* but that is its sole purpose.
*
* @param function the function string.
*/
data class FunctionNode(val function: String) : TreeNode() {
/**
* List of function parameters added to this node.
*/
val children: MutableList<TreeNode> = mutableListOf()
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
val children = Array<Any?>(children.size, { children[it].reduce(reducer) ?: return null; })
return reducer.reduceNode(this, *children)
}
override fun toString(): String {
val buffer = StringBuffer()
buffer.append(function)
buffer.append('(')
for (i in 0 until children.size) {
buffer.append(children[i].toString())
buffer.append(if (i == children.size - 1) ")" else ",")
}
return buffer.toString()
}
/**
* Appends a child to this node's list of children.
*
* @node the node to append.
*/
fun appendChild(node: TreeNode){
children.add(node)
}
/**
* Prepends a child to this node's list of children.
*
* @node the node to prepend.
*/
fun prependChild(node: TreeNode){
children.add(0, node)
}
}

View File

@@ -0,0 +1,23 @@
package org.nwapw.abacus.tree
import org.nwapw.abacus.number.NumberInterface
/**
* A tree node that holds a single number value.
*
* This is a tree node that holds a single NumberInterface, which represents any number,
* and is not defined during compile time.
*
* @number the number value of this node.
*/
data class NumberNode(val number: NumberInterface) : TreeNode() {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
return reducer.reduceNode(this)
}
override fun toString(): String {
return number.toString()
}
}

View File

@@ -0,0 +1,10 @@
package org.nwapw.abacus.tree
/**
* A tree node.
*/
abstract class TreeNode {
abstract fun <T: Any> reduce(reducer: Reducer<T>) : T?
}

View File

@@ -0,0 +1,23 @@
package org.nwapw.abacus.tree
/**
* A tree node that holds a unary operation.
*
* This node holds a single operator applied to a single parameter, and does not care
* whether the operation was found before or after the parameter in the text.
*
* @param operation the operation applied to the given node.
* @param applyTo the node to which the operation will be applied.
*/
data class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
val reducedChild = applyTo?.reduce(reducer) ?: return null
return reducer.reduceNode(this, reducedChild)
}
override fun toString(): String {
return "(" + (applyTo?.toString() ?: "null") + ")" + operation
}
}

View File

@@ -2,14 +2,10 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?> <?import javafx.scene.text.Text?>
<?import javafx.scene.layout.HBox?> <BorderPane xmlns:fx="http://javafx.com/fxml"
<?import javafx.scene.layout.GridPane?> xmlns="http://javafx.com/javafx"
<?import javafx.scene.layout.FlowPane?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.nwapw.abacus.fx.AbacusController"> fx:controller="org.nwapw.abacus.fx.AbacusController">
<center> <center>
<TabPane fx:id="coreTabPane"> <TabPane fx:id="coreTabPane">
@@ -38,25 +34,41 @@
<TextField fx:id="inputField" onAction="#performCalculation"/> <TextField fx:id="inputField" onAction="#performCalculation"/>
<Button fx:id="inputButton" text="Calculate" maxWidth="Infinity" <Button fx:id="inputButton" text="Calculate" maxWidth="Infinity"
onAction="#performCalculation"/> onAction="#performCalculation"/>
<Button fx:id="stopButton" text="Stop" maxWidth="Infinity"
onAction="#performStop" disable="true"/>
</VBox> </VBox>
</bottom> </bottom>
</BorderPane> </BorderPane>
</Tab> </Tab>
<Tab fx:id="settingsTab" text="Settings" closable="false"> <Tab fx:id="settingsTab" text="Settings" closable="false">
<GridPane hgap="10" vgap="10"> <GridPane hgap="10" vgap="10">
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding> <padding>
<Insets left="10" right="10" top="10" bottom="10"/>
</padding>
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/> <Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/> <ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<ListView fx:id="enabledPluginView" <ListView fx:id="enabledPluginView"
GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnIndex="0"
GridPane.columnSpan="2" maxHeight="100"/> GridPane.columnSpan="2" maxHeight="100"/>
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10" vgap="10"> <Text GridPane.columnIndex="0" GridPane.rowIndex="2" text="Computation Limit"/>
<TextField fx:id="computationLimitField" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="3" hgap="10"
vgap="10">
<Button text="Apply" onAction="#performSave"/> <Button text="Apply" onAction="#performSave"/>
<Button text="Reload Plugins" onAction="#performReload"/> <Button text="Reload Plugins" onAction="#performReload"/>
<Button text="Apply and Reload" onAction="#performSaveAndReload"/> <Button text="Apply and Reload" onAction="#performSaveAndReload"/>
</FlowPane> </FlowPane>
</GridPane> </GridPane>
</Tab> </Tab>
<Tab fx:id="functionListTab" text="Functions" closable="false">
<VBox spacing="10">
<padding>
<Insets left="10" right="10" top="10" bottom="10"/>
</padding>
<TextField fx:id="functionListSearchField" maxWidth="Infinity"/>
<ListView maxWidth="Infinity" fx:id="functionListView"/>
</VBox>
</Tab>
</TabPane> </TabPane>
</center> </center>

View File

@@ -0,0 +1,107 @@
package org.nwapw.abacus.tests;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.config.Configuration;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.tree.TreeNode;
public class CalculationTests {
private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{}));
@BeforeClass
public static void prepareTests(){
abacus.getPluginManager().addInstantiated(new StandardPlugin(abacus.getPluginManager()));
abacus.getPluginManager().load();
}
private void testOutput(String input, String parseOutput, String output){
TreeNode parsedTree = abacus.parseString(input);
Assert.assertNotNull(parsedTree);
Assert.assertEquals(parsedTree.toString(), parseOutput);
NumberInterface result = abacus.evaluateTree(parsedTree);
Assert.assertNotNull(result);
Assert.assertTrue(result.toString().startsWith(output));
}
private void testEvalError(String input, String parseOutput){
TreeNode parsedTree = abacus.parseString(input);
Assert.assertNotNull(parsedTree);
Assert.assertEquals(parsedTree.toString(), parseOutput);
Assert.assertNull(abacus.evaluateTree(parsedTree));
}
@Test
public void testAddition(){
testOutput("9.5+10", "(9.5+10)", "19.5");
}
@Test
public void testSubtraction(){
testOutput("9.5-10", "(9.5-10)", "-0.5");
}
@Test
public void testMultiplication(){
testOutput("9.5*10", "(9.5*10)", "95");
}
@Test
public void testDivision(){
testOutput("9.5/2", "(9.5/2)", "4.75");
}
@Test
public void testNegation(){
testOutput("-9.5", "(9.5)`", "-9.5");
}
@Test
public void testFactorial(){
testOutput("7!", "(7)!", "5040");
}
@Test
public void testAbs(){
testOutput("abs(-1)", "abs((1)`)", "1");
testOutput("abs(1)", "abs(1)", "1");
}
@Test
public void testLn(){
testEvalError("ln(-1)", "ln((1)`)");
testOutput("ln2", "ln(2)", "0.6931471805599453094172321214581765680755");
}
@Test
public void testSqrt(){
testOutput("sqrt0", "sqrt(0)", "0");
testOutput("sqrt4", "sqrt(4)", "2");
testOutput("sqrt2", "sqrt(2)", "1.4142135623730950488016887242096980785696");
}
@Test
public void testExp(){
testOutput("exp0", "exp(0)", "1");
testOutput("exp1", "exp(1)", "2.718281828459045235360287471352662497757247");
testOutput("exp300", "exp(300)", "1.9424263952412559365842088360176992193662086");
testOutput("exp(-500)", "exp((500)`)", "7.1245764067412855315491573771227552469277568");
}
@Test
public void testPow(){
testOutput("0^2", "(0^2)", "0");
testOutput("2^0", "(2^0)", "1");
testOutput("2^1", "(2^1)", "2");
testOutput("2^-1", "(2^(1)`)", "0.5");
testOutput("2^50", "(2^50)", "112589990684262");
testOutput("7^(-sqrt2*17)", "(7^((sqrt(2)*17))`)", "4.81354609155297814551845300063563");
testEvalError("0^0", "(0^0)");
testEvalError("(-13)^.9999", "((13)`^0.9999)");
}
}

View File

@@ -4,6 +4,7 @@ import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.nwapw.abacus.Abacus; import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.config.Configuration;
import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.Operator;
import org.nwapw.abacus.function.OperatorAssociativity; import org.nwapw.abacus.function.OperatorAssociativity;
@@ -18,7 +19,7 @@ import java.util.List;
public class TokenizerTests { public class TokenizerTests {
private static Abacus abacus = new Abacus(); private static Abacus abacus = new Abacus(new Configuration(0, "precise", new String[]{}));
private static LexerTokenizer lexerTokenizer = new LexerTokenizer(); private static LexerTokenizer lexerTokenizer = new LexerTokenizer();
private static Function subtractFunction = new Function() { private static Function subtractFunction = new Function() {
@Override @Override