mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 16:15:19 +00:00
Compare commits
15 Commits
javafx
...
plugins-up
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c03e191c36 | ||
| 5de9453bec | |||
| d205651332 | |||
| 6f99f07150 | |||
| 2cf41c1029 | |||
| 76677ef494 | |||
| 0cd40b028a | |||
|
|
1ee8c7d231 | ||
|
|
f97d16c640 | ||
|
|
a0bba03c2c | ||
|
|
8666e96420 | ||
|
|
fd40e6b297 | ||
|
|
79ccd61af3 | ||
|
|
699ba9e193 | ||
|
|
e43f223086 |
@@ -1,6 +1,6 @@
|
||||
package org.nwapw.abacus;
|
||||
|
||||
import org.nwapw.abacus.config.ConfigurationObject;
|
||||
import org.nwapw.abacus.config.Configuration;
|
||||
import org.nwapw.abacus.fx.AbacusApplication;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
@@ -9,7 +9,7 @@ import org.nwapw.abacus.parsing.ShuntingYardParser;
|
||||
import org.nwapw.abacus.parsing.TreeBuilder;
|
||||
import org.nwapw.abacus.plugin.ClassFinder;
|
||||
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.TreeNode;
|
||||
|
||||
@@ -46,7 +46,7 @@ public class Abacus {
|
||||
/**
|
||||
* The configuration loaded from a file.
|
||||
*/
|
||||
private ConfigurationObject configuration;
|
||||
private Configuration configuration;
|
||||
/**
|
||||
* The tree builder used to construct a tree
|
||||
* from a string.
|
||||
@@ -59,24 +59,30 @@ public class Abacus {
|
||||
public Abacus() {
|
||||
pluginManager = new PluginManager();
|
||||
numberReducer = new NumberReducer(this);
|
||||
configuration = new ConfigurationObject(CONFIG_FILE);
|
||||
configuration.save(CONFIG_FILE);
|
||||
configuration = new Configuration(CONFIG_FILE);
|
||||
configuration.saveTo(CONFIG_FILE);
|
||||
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
||||
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
||||
|
||||
pluginManager.addListener(lexerTokenizer);
|
||||
pluginManager.addListener(shuntingYardParser);
|
||||
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||
//pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||
/*
|
||||
try {
|
||||
ClassFinder.loadJars("plugins")
|
||||
.forEach(plugin -> pluginManager.addClass(plugin));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}//*/
|
||||
pluginManager.load();
|
||||
}
|
||||
|
||||
public void loadClass(Class<?> newClass){
|
||||
pluginManager.addClass(newClass);
|
||||
}
|
||||
public void unloadClass(Class<?> newClass){
|
||||
pluginManager.removeClass(newClass);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
AbacusApplication.launch(AbacusApplication.class, args);
|
||||
}
|
||||
@@ -114,7 +120,7 @@ public class Abacus {
|
||||
*
|
||||
* @return the configuration object.
|
||||
*/
|
||||
public ConfigurationObject getConfiguration() {
|
||||
public Configuration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,83 @@
|
||||
package org.nwapw.abacus.config;
|
||||
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import com.moandjiezana.toml.TomlWriter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Serializable class that will be used to load TOML
|
||||
* configurations.
|
||||
* The configuration object that stores
|
||||
* options that the user can change.
|
||||
*/
|
||||
public class Configuration {
|
||||
|
||||
/**
|
||||
* The type of number this calculator should use.
|
||||
* The TOML writer used to write this configuration to a file.
|
||||
*/
|
||||
public String numberType;
|
||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
||||
/**
|
||||
* The TOML reader used to load this config from a file.
|
||||
*/
|
||||
private static final Toml TOML_READER = new Toml();
|
||||
|
||||
/**
|
||||
* The implementation of the number that should be used.
|
||||
*/
|
||||
private String numberImplementation = "naive";
|
||||
|
||||
/**
|
||||
* Creates a new configuration with the given values.
|
||||
* @param numberImplementation the number implementation, like "naive" or "precise"
|
||||
*/
|
||||
public Configuration(String numberImplementation){
|
||||
this.numberImplementation = numberImplementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a configuration from a given file, keeping non-specified fields default.
|
||||
* @param fromFile the file to load from.
|
||||
*/
|
||||
public Configuration(File fromFile){
|
||||
if(!fromFile.exists()) return;
|
||||
copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the values from the given configuration into this one.
|
||||
* @param otherConfiguration the configuration to copy from.
|
||||
*/
|
||||
public void copyFrom(Configuration otherConfiguration){
|
||||
this.numberImplementation = otherConfiguration.numberImplementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves this configuration to the given file, creating
|
||||
* any directories that do not exist.
|
||||
* @param file the file to save to.
|
||||
*/
|
||||
public void saveTo(File file){
|
||||
if(file.getParentFile() != null) file.getParentFile().mkdirs();
|
||||
try {
|
||||
TOML_WRITER.write(this, file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number implementation from this configuration.
|
||||
* @return the number implementation.
|
||||
*/
|
||||
public String getNumberImplementation() {
|
||||
return numberImplementation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number implementation for the configuration
|
||||
* @param numberImplementation the number implementation.
|
||||
*/
|
||||
public void setNumberImplementation(String numberImplementation) {
|
||||
this.numberImplementation = numberImplementation;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,111 +0,0 @@
|
||||
package org.nwapw.abacus.config;
|
||||
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import com.moandjiezana.toml.TomlWriter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A configuration object, which essentially
|
||||
* manages saving, loading, and getting values
|
||||
* from the configuration. While Configuration is
|
||||
* the data model, this is the interface with it.
|
||||
*/
|
||||
public class ConfigurationObject {
|
||||
|
||||
/**
|
||||
* The writer used to store the configuration.
|
||||
*/
|
||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
||||
/**
|
||||
* The configuration instance being modeled.
|
||||
*/
|
||||
private Configuration configuration;
|
||||
|
||||
/**
|
||||
* Creates a new configuration object with the given config.
|
||||
*
|
||||
* @param config the config to use.
|
||||
*/
|
||||
public ConfigurationObject(Configuration config) {
|
||||
setup(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a configuration object by attempting to
|
||||
* load a config from the given path, using the
|
||||
* default configuration otherwise.
|
||||
*
|
||||
* @param path the path to attempt to load.
|
||||
*/
|
||||
public ConfigurationObject(File path) {
|
||||
Configuration config;
|
||||
if (!path.exists()) {
|
||||
config = getDefaultConfig();
|
||||
} else {
|
||||
Toml parse = new Toml();
|
||||
parse.read(path);
|
||||
config = parse.to(Configuration.class);
|
||||
}
|
||||
setup(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new configuration object with the
|
||||
* default configuration.
|
||||
*/
|
||||
public ConfigurationObject() {
|
||||
setup(getDefaultConfig());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the ConfigurationObject.
|
||||
* different constructors do different things,
|
||||
* but they all lead here.
|
||||
*
|
||||
* @param configuration the configuration to set up with.
|
||||
*/
|
||||
private void setup(Configuration configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a default configuration.
|
||||
*
|
||||
* @return the newly created default configuration.
|
||||
*/
|
||||
private Configuration getDefaultConfig() {
|
||||
configuration = new Configuration();
|
||||
configuration.numberType = "naive";
|
||||
return configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the implementation the user has requested to
|
||||
* represent their numbers.
|
||||
*
|
||||
* @return the implementation name.
|
||||
*/
|
||||
public String getNumberImplementation() {
|
||||
return configuration.numberType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the ConfigurationObject to the given file.
|
||||
*
|
||||
* @param toFile the file to save ot.
|
||||
* @return true if the save succeed, false if otherwise.
|
||||
*/
|
||||
public boolean save(File toFile) {
|
||||
if (toFile.getParentFile() != null) toFile.getParentFile().mkdirs();
|
||||
try {
|
||||
TOML_WRITER.write(configuration, toFile);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,10 @@ import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
* The main application class for JavaFX responsible for loading
|
||||
* and displaying the fxml file.
|
||||
*/
|
||||
public class AbacusApplication extends Application {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,12 +8,25 @@ import javafx.scene.text.Text;
|
||||
import javafx.util.Callback;
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.ClassFinder;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* The controller for the abacus FX UI, responsible
|
||||
* for all the user interaction.
|
||||
*/
|
||||
public class AbacusController {
|
||||
|
||||
/**
|
||||
* Constant string that is displayed if the text could not be lexed or parsed.
|
||||
*/
|
||||
private static final String ERR_SYNTAX = "Syntax Error";
|
||||
/**
|
||||
* Constant string that is displayed if the tree could not be reduced.
|
||||
*/
|
||||
private static final String ERR_EVAL = "Evaluation Error";
|
||||
|
||||
@FXML
|
||||
@@ -30,9 +43,26 @@ public class AbacusController {
|
||||
private TextField inputField;
|
||||
@FXML
|
||||
private Button inputButton;
|
||||
@FXML
|
||||
private ComboBox<String> numberImplementationBox;
|
||||
@FXML
|
||||
private Button loadButton;
|
||||
@FXML
|
||||
private Button unloadButton;
|
||||
@FXML
|
||||
private TextField loadField;
|
||||
|
||||
/**
|
||||
* The list of history entries, created by the users.
|
||||
*/
|
||||
private ObservableList<HistoryModel> historyData;
|
||||
|
||||
/**
|
||||
* The abacus instance used for calculations and all
|
||||
* other main processing code.
|
||||
*/
|
||||
private ObservableList<String> numberImplementationOptions;
|
||||
|
||||
private Abacus abacus;
|
||||
|
||||
@FXML
|
||||
@@ -40,9 +70,15 @@ public class AbacusController {
|
||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||
param -> new CopyableCell<>();
|
||||
|
||||
abacus = new Abacus();
|
||||
historyData = FXCollections.observableArrayList();
|
||||
historyTable.setItems(historyData);
|
||||
numberImplementationOptions = FXCollections.observableArrayList();
|
||||
numberImplementationBox.setItems(numberImplementationOptions);
|
||||
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
||||
-> {
|
||||
abacus.getConfiguration().setNumberImplementation(newValue);
|
||||
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
||||
});
|
||||
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
||||
inputColumn.setCellFactory(cellFactory);
|
||||
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
||||
@@ -50,6 +86,12 @@ public class AbacusController {
|
||||
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
||||
outputColumn.setCellFactory(cellFactory);
|
||||
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
||||
|
||||
abacus = new Abacus();
|
||||
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
||||
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
||||
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
||||
}
|
||||
|
||||
@FXML
|
||||
@@ -73,5 +115,38 @@ public class AbacusController {
|
||||
inputButton.setDisable(false);
|
||||
inputField.setText("");
|
||||
}
|
||||
@FXML
|
||||
private void loadClass(){
|
||||
try {
|
||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
||||
String name = "";
|
||||
//String name = plugin.getName();
|
||||
while(!(name.indexOf('/') ==-1)){
|
||||
name=name.substring(name.indexOf('/')+1);
|
||||
}
|
||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
||||
//abacus.loadClass(plugin);
|
||||
}
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@FXML
|
||||
private void unloadClass(){
|
||||
try {
|
||||
for(Class<?> plugin :ClassFinder.loadJars("plugins")){
|
||||
String name = plugin.getName();
|
||||
while(!(name.indexOf('/') ==-1)){
|
||||
name=name.substring(name.indexOf('/')+1);
|
||||
}
|
||||
if(loadField.getText().equals("")||loadField.getText().equals(name)||(loadField.getText()+".class").equals(name)){
|
||||
//abacus.unloadClass(plugin);
|
||||
}
|
||||
}
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,8 +6,17 @@ import javafx.scene.input.MouseEvent;
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
|
||||
/**
|
||||
* A cell that copies its value to the clipboard
|
||||
* when double clicked.
|
||||
* @param <S> The type of the table view generic type.
|
||||
* @param <T> The type of the value contained in the cell.
|
||||
*/
|
||||
public class CopyableCell<S, T> extends TableCell<S, T> {
|
||||
|
||||
/**
|
||||
* Creates a new copyable cell.
|
||||
*/
|
||||
public CopyableCell(){
|
||||
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
|
||||
if(event.getClickCount() == 2){
|
||||
|
||||
@@ -3,12 +3,33 @@ 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();
|
||||
@@ -18,23 +39,47 @@ public class HistoryModel {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -93,6 +93,11 @@ public class NaiveNumber implements NumberInterface {
|
||||
return this.compareTo(ZERO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int ceiling() {
|
||||
return (int) Math.ceil(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if (toClass == this.getClass()) return this;
|
||||
|
||||
@@ -79,6 +79,12 @@ public interface NumberInterface {
|
||||
*/
|
||||
int signum();
|
||||
|
||||
/**
|
||||
* Returns the least integer greater than or equal to the number.
|
||||
* @return the least integer >= the number, if int can hold the value.
|
||||
*/
|
||||
int ceiling();
|
||||
|
||||
/**
|
||||
* Promotes this class to another number class.
|
||||
*
|
||||
|
||||
@@ -3,6 +3,10 @@ package org.nwapw.abacus.number;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* A number that uses a BigDecimal to store its value,
|
||||
* leading to infinite possible precision.
|
||||
*/
|
||||
public class PreciseNumber implements NumberInterface {
|
||||
|
||||
/**
|
||||
@@ -44,12 +48,12 @@ public class PreciseNumber implements NumberInterface {
|
||||
|
||||
@Override
|
||||
public int getMaxPrecision() {
|
||||
return 54;
|
||||
return 65;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface multiply(NumberInterface multiplier) {
|
||||
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
|
||||
return new PreciseNumber(this.value.multiply(((PreciseNumber) multiplier).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,6 +98,11 @@ public class PreciseNumber implements NumberInterface {
|
||||
return value.signum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int ceiling() {
|
||||
return (int) Math.ceil(value.doubleValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface negate() {
|
||||
return new PreciseNumber(value.negate());
|
||||
@@ -109,7 +118,7 @@ public class PreciseNumber implements NumberInterface {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
BigDecimal rounded = value.setScale(getMaxPrecision() - 4, RoundingMode.HALF_UP);
|
||||
BigDecimal rounded = value.setScale(getMaxPrecision() - 15, RoundingMode.HALF_UP);
|
||||
return rounded.stripTrailingZeros().toPlainString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +140,11 @@ public class PluginManager {
|
||||
plugins.add(plugin);
|
||||
loadedPluginClasses.add(plugin.getClass());
|
||||
}
|
||||
public void removeInstantiated(Plugin plugin){
|
||||
if (loadedPluginClasses.contains(plugin.getClass())) return;
|
||||
plugins.remove(plugin);
|
||||
loadedPluginClasses.remove(plugin.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a class of plugin, and adds it to this
|
||||
@@ -155,6 +160,14 @@ public class PluginManager {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public void removeClass(Class<?> newClass){
|
||||
if (!Plugin.class.isAssignableFrom(newClass) || newClass == Plugin.class) return;
|
||||
try {
|
||||
removeInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all the plugins in the PluginManager.
|
||||
|
||||
@@ -8,6 +8,8 @@ import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.number.PreciseNumber;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
@@ -16,6 +18,8 @@ import java.util.function.BiFunction;
|
||||
*/
|
||||
public class StandardPlugin extends Plugin {
|
||||
|
||||
private static HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> factorialLists = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||
|
||||
/**
|
||||
* The addition operator, +
|
||||
*/
|
||||
@@ -152,17 +156,40 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
boolean takeReciprocal = params[0].signum() == -1;
|
||||
params[0] = FUNCTION_ABS.apply(params[0]);
|
||||
NumberInterface sum = sumSeries(params[0], StandardPlugin::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
||||
if (takeReciprocal) {
|
||||
sum = NaiveNumber.ONE.promoteTo(sum.getClass()).divide(sum);
|
||||
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())), right = maxError;
|
||||
do{
|
||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
||||
n++;
|
||||
nextNumerator = nextNumerator.multiply(params[0]);
|
||||
left = left.multiply(params[0]);
|
||||
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
|
||||
right = right.multiply(nextN);
|
||||
//System.out.println(left + ", " + right);
|
||||
}
|
||||
while(left.compareTo(right) > 0);
|
||||
//System.out.println(n+1);
|
||||
return sum;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* The natural log function, ln(exp(1)) = 1
|
||||
* The natural log function.
|
||||
*/
|
||||
public static final Function FUNCTION_LN = new Function() {
|
||||
@Override
|
||||
@@ -203,11 +230,12 @@ public class StandardPlugin extends Plugin {
|
||||
private NumberInterface getLogPartialSum(NumberInterface x) {
|
||||
NumberInterface maxError = getMaxError(x);
|
||||
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
||||
NumberInterface currentTerm = x, sum = x;
|
||||
NumberInterface currentNumerator = x, currentTerm = x, sum = x;
|
||||
int n = 1;
|
||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
||||
n++;
|
||||
currentTerm = currentTerm.multiply(x).multiply((new NaiveNumber(n - 1)).promoteTo(x.getClass())).divide((new NaiveNumber(n)).promoteTo(x.getClass())).negate();
|
||||
currentNumerator = currentNumerator.multiply(x).negate();
|
||||
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
|
||||
sum = sum.add(currentTerm);
|
||||
}
|
||||
return sum;
|
||||
@@ -238,7 +266,7 @@ public class StandardPlugin extends Plugin {
|
||||
}
|
||||
};
|
||||
/**
|
||||
* The square root function, sqrt(4) = 2
|
||||
* The square root function.
|
||||
*/
|
||||
public static final Function FUNCTION_SQRT = new Function() {
|
||||
@Override
|
||||
@@ -256,39 +284,6 @@ public class StandardPlugin extends Plugin {
|
||||
super(manager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nth term of the Taylor series (centered at 0) of e^x
|
||||
*
|
||||
* @param n the term required (n >= 0).
|
||||
* @param x the real number at which the series is evaluated.
|
||||
* @return the nth term of the series.
|
||||
*/
|
||||
private static NumberInterface getExpSeriesTerm(int n, NumberInterface x) {
|
||||
return x.intPow(n).divide(OP_FACTORIAL.getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of terms needed to evaluate the exponential function (at x)
|
||||
* such that the error is at most maxError.
|
||||
*
|
||||
* @param maxError Maximum error permissible (This should probably be positive.)
|
||||
* @param x where the function is evaluated.
|
||||
* @return the number of terms needed to evaluated the exponential function.
|
||||
*/
|
||||
private static int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
||||
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
||||
//The variables LHS and RHS refer to the above inequality.
|
||||
int n = 0;
|
||||
x = FUNCTION_ABS.apply(x);
|
||||
NumberInterface LHS = x, RHS = maxError;
|
||||
while (LHS.compareTo(RHS) > 0) {
|
||||
n++;
|
||||
LHS = LHS.multiply(x);
|
||||
RHS = RHS.multiply(new NaiveNumber(n + 1).promoteTo(RHS.getClass()));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
||||
*
|
||||
@@ -338,4 +333,19 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
}
|
||||
|
||||
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||
if(!factorialLists.containsKey(numberClass)){
|
||||
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||
}
|
||||
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
||||
if(n >= list.size()){
|
||||
while(list.size() < n + 16){
|
||||
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
||||
}
|
||||
}
|
||||
return list.get(n);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.text.Text?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
<BorderPane xmlns="http://javafx.com/javafx"
|
||||
xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||
@@ -39,7 +41,16 @@
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
</Tab>
|
||||
<Tab text="Settings" closable="false"/>
|
||||
<Tab text="Settings" closable="false">
|
||||
<GridPane hgap="10" vgap="10">
|
||||
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
||||
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
||||
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
||||
<Button fx:id="loadButton" text="Load" GridPane.rowIndex="1" GridPane.columnIndex="0" maxWidth = "Infinity" onAction="#loadClass"/>
|
||||
<Button fx:id="unloadButton" text="Unload" GridPane.rowIndex="1" GridPane.columnIndex="1" maxWidth = "Infinity" onAction="#unloadClass"/>
|
||||
<TextField fx:id="loadField" GridPane.rowIndex="1" GridPane.columnIndex="2"/>
|
||||
</GridPane>
|
||||
</Tab>
|
||||
</TabPane>
|
||||
</center>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user