mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 16:45:21 +00:00
Compare commits
4 Commits
function-d
...
sig-fig
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8b3559cec | ||
|
|
4cf4ba98a8 | ||
|
|
68fbcd2d7c | ||
|
|
ed92b382f0 |
@@ -1,8 +1,3 @@
|
|||||||
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'
|
||||||
|
|
||||||
@@ -12,7 +7,6 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
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'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package org.nwapw.abacus.function;
|
|
||||||
|
|
||||||
public enum DocumentationType {
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
|
|
||||||
}
|
|
||||||
76
src/main/java/org/nwapw/abacus/function/Operator.java
Normal file
76
src/main/java/org/nwapw/abacus/function/Operator.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
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 operatorType the type of this operator, like binary infix or unary postfix.
|
||||||
|
* @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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,9 +5,7 @@ import javafx.beans.value.ChangeListener;
|
|||||||
import javafx.beans.value.ObservableValue;
|
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.Node;
|
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.control.cell.CheckBoxListCell;
|
import javafx.scene.control.cell.CheckBoxListCell;
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
@@ -15,8 +13,6 @@ 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.function.Documentation;
|
|
||||||
import org.nwapw.abacus.function.DocumentationType;
|
|
||||||
import org.nwapw.abacus.number.ComputationInterruptedException;
|
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.ClassFinder;
|
||||||
@@ -27,10 +23,7 @@ import org.nwapw.abacus.tree.TreeNode;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -79,8 +72,6 @@ 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;
|
||||||
@@ -102,10 +93,6 @@ public class AbacusController implements PluginListener {
|
|||||||
private ListView<ToggleablePlugin> enabledPluginView;
|
private ListView<ToggleablePlugin> enabledPluginView;
|
||||||
@FXML
|
@FXML
|
||||||
private TextField computationLimitField;
|
private TextField computationLimitField;
|
||||||
@FXML
|
|
||||||
private ListView<Documentation> functionListView;
|
|
||||||
@FXML
|
|
||||||
private TextField functionListSearchField;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The list of history entries, created by the users.
|
* The list of history entries, created by the users.
|
||||||
@@ -123,14 +110,6 @@ 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<Documentation> functionList;
|
|
||||||
/**
|
|
||||||
* The filtered list displayed to the user.
|
|
||||||
*/
|
|
||||||
private FilteredList<Documentation> functionFilter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The abacus instance used for changing the plugin configuration.
|
* The abacus instance used for changing the plugin configuration.
|
||||||
@@ -233,12 +212,7 @@ public class AbacusController implements PluginListener {
|
|||||||
return new ToggleablePlugin(true, string);
|
return new ToggleablePlugin(true, string);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
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.matches(newValue))));
|
|
||||||
functionListView.setCellFactory(param -> new DocumentationCell());
|
|
||||||
historyData = FXCollections.observableArrayList();
|
historyData = FXCollections.observableArrayList();
|
||||||
historyTable.setItems(historyData);
|
historyTable.setItems(historyData);
|
||||||
numberImplementationOptions = FXCollections.observableArrayList();
|
numberImplementationOptions = FXCollections.observableArrayList();
|
||||||
@@ -353,15 +327,10 @@ public class AbacusController implements PluginListener {
|
|||||||
plugin.enabledProperty().addListener(e -> changesMade = true);
|
plugin.enabledProperty().addListener(e -> changesMade = true);
|
||||||
enabledPlugins.add(plugin);
|
enabledPlugins.add(plugin);
|
||||||
}
|
}
|
||||||
PluginManager pluginManager = abacus.getPluginManager();
|
|
||||||
functionList.addAll(manager.getAllFunctions().stream().map(name -> pluginManager.documentationFor(name, DocumentationType.FUNCTION))
|
|
||||||
.collect(Collectors.toCollection(ArrayList::new)));
|
|
||||||
functionList.sort(Comparator.comparing(Documentation::getCodeName));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUnload(PluginManager manager) {
|
public void onUnload(PluginManager manager) {
|
||||||
functionList.clear();
|
|
||||||
enabledPlugins.clear();
|
enabledPlugins.clear();
|
||||||
numberImplementationOptions.clear();
|
numberImplementationOptions.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.scene.control.Label;
|
|
||||||
import javafx.scene.control.ListCell;
|
|
||||||
import javafx.scene.control.TitledPane;
|
|
||||||
import javafx.scene.layout.VBox;
|
|
||||||
import org.nwapw.abacus.function.Documentation;
|
|
||||||
|
|
||||||
public class DocumentationCell extends ListCell<Documentation> {
|
|
||||||
|
|
||||||
private Label codeNameLabel;
|
|
||||||
private Label nameLabel;
|
|
||||||
private Label description;
|
|
||||||
private Label longDescription;
|
|
||||||
private TitledPane titledPane;
|
|
||||||
|
|
||||||
public DocumentationCell(){
|
|
||||||
VBox vbox = new VBox();
|
|
||||||
vbox.setSpacing(10);
|
|
||||||
titledPane = new TitledPane();
|
|
||||||
codeNameLabel = new Label();
|
|
||||||
nameLabel = new Label();
|
|
||||||
description = new Label();
|
|
||||||
longDescription = new Label();
|
|
||||||
codeNameLabel.setWrapText(true);
|
|
||||||
nameLabel.setWrapText(true);
|
|
||||||
description.setWrapText(true);
|
|
||||||
longDescription.setWrapText(true);
|
|
||||||
vbox.getChildren().add(codeNameLabel);
|
|
||||||
vbox.getChildren().add(nameLabel);
|
|
||||||
vbox.getChildren().add(description);
|
|
||||||
vbox.getChildren().add(longDescription);
|
|
||||||
titledPane.textProperty().bindBidirectional(codeNameLabel.textProperty());
|
|
||||||
titledPane.setContent(vbox);
|
|
||||||
titledPane.setExpanded(false);
|
|
||||||
titledPane.prefWidthProperty().bind(widthProperty());
|
|
||||||
|
|
||||||
visibleProperty().addListener((a, b, c) -> titledPane.setExpanded(false));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void updateItem(Documentation item, boolean empty) {
|
|
||||||
super.updateItem(item, empty);
|
|
||||||
if(empty){
|
|
||||||
codeNameLabel.setText("");
|
|
||||||
nameLabel.setText("");
|
|
||||||
description.setText("");
|
|
||||||
longDescription.setText("");
|
|
||||||
setGraphic(null);
|
|
||||||
} else {
|
|
||||||
codeNameLabel.setText(item.getCodeName());
|
|
||||||
nameLabel.setText(item.getName());
|
|
||||||
description.setText(item.getDescription());
|
|
||||||
longDescription.setText(item.getLongDescription());
|
|
||||||
setGraphic(titledPane);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -128,5 +128,9 @@ public class NaiveNumber extends 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -255,4 +255,11 @@ public abstract class NumberInterface {
|
|||||||
return promoteToInternal(toClass);
|
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();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
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.MathContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A number that uses a BigDecimal to store its value,
|
* A number that uses a BigDecimal to store its value,
|
||||||
@@ -22,6 +22,21 @@ public class PreciseNumber extends 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.
|
||||||
*/
|
*/
|
||||||
@@ -48,7 +63,7 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getMaxPrecision() {
|
public int getMaxPrecision() {
|
||||||
return 65;
|
return internalContext.getPrecision();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -58,7 +73,7 @@ public class PreciseNumber extends NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NumberInterface divideInternal(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
|
||||||
@@ -147,7 +162,11 @@ public class PreciseNumber extends 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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.nwapw.abacus.plugin;
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
import org.nwapw.abacus.function.Documentation;
|
|
||||||
import org.nwapw.abacus.function.DocumentationType;
|
|
||||||
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.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
@@ -96,15 +94,6 @@ public abstract class Plugin {
|
|||||||
manager.registerNumberImplementation(name, implementation);
|
manager.registerNumberImplementation(name, implementation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* To be used in load(). Registers a documentation instance
|
|
||||||
* used to explain some element of the plugin to the user.
|
|
||||||
* @param documentation the documentation instance.
|
|
||||||
*/
|
|
||||||
protected final void registerDocumentation(Documentation documentation){
|
|
||||||
manager.registerDocumentation(documentation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the PluginManager for the given function name.
|
* Searches the PluginManager for the given function name.
|
||||||
* This can be used by the plugins internally in order to call functions
|
* This can be used by the plugins internally in order to call functions
|
||||||
@@ -141,17 +130,6 @@ public abstract class Plugin {
|
|||||||
return manager.numberImplementationFor(name);
|
return manager.numberImplementationFor(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches the PluginManager for the given documentation name and type.
|
|
||||||
*
|
|
||||||
* @param name the name for which to search.
|
|
||||||
* @param type the type of documentation to search for.
|
|
||||||
* @return the found documentation, or null if none was found.
|
|
||||||
*/
|
|
||||||
protected final Documentation documentationFor(String name, DocumentationType type){
|
|
||||||
return manager.documentationFor(name, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the plugin manager for a Pi value for the given number implementation.
|
* Searches the plugin manager for a Pi value for the given number implementation.
|
||||||
* This is done so that number implementations with various degrees of precision
|
* This is done so that number implementations with various degrees of precision
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package org.nwapw.abacus.plugin;
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
import org.nwapw.abacus.Abacus;
|
import org.nwapw.abacus.Abacus;
|
||||||
import org.nwapw.abacus.function.Documentation;
|
|
||||||
import org.nwapw.abacus.function.DocumentationType;
|
|
||||||
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.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
@@ -36,10 +34,6 @@ public class PluginManager {
|
|||||||
* The map of number implementations registered by the plugins.
|
* The map of number implementations registered by the plugins.
|
||||||
*/
|
*/
|
||||||
private Map<String, NumberImplementation> registeredNumberImplementations;
|
private Map<String, NumberImplementation> registeredNumberImplementations;
|
||||||
/**
|
|
||||||
* The map of documentation for functions registered by the plugins.
|
|
||||||
*/
|
|
||||||
private Set<Documentation> registeredDocumentation;
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
@@ -71,7 +65,6 @@ public class PluginManager {
|
|||||||
registeredFunctions = new HashMap<>();
|
registeredFunctions = new HashMap<>();
|
||||||
registeredOperators = new HashMap<>();
|
registeredOperators = new HashMap<>();
|
||||||
registeredNumberImplementations = new HashMap<>();
|
registeredNumberImplementations = new HashMap<>();
|
||||||
registeredDocumentation = new HashSet<>();
|
|
||||||
cachedInterfaceImplementations = new HashMap<>();
|
cachedInterfaceImplementations = new HashMap<>();
|
||||||
cachedPi = new HashMap<>();
|
cachedPi = new HashMap<>();
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
@@ -104,15 +97,6 @@ public class PluginManager {
|
|||||||
registeredNumberImplementations.put(name, implementation);
|
registeredNumberImplementations.put(name, implementation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Registers the given documentation with the plugin manager,
|
|
||||||
* making it accessible to the plugin manager etc.
|
|
||||||
* @param documentation the documentation to register.
|
|
||||||
*/
|
|
||||||
public void registerDocumentation(Documentation documentation){
|
|
||||||
registeredDocumentation.add(documentation);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the function registered under the given name.
|
* Gets the function registered under the given name.
|
||||||
* @param name the name of the function.
|
* @param name the name of the function.
|
||||||
@@ -140,27 +124,6 @@ public class PluginManager {
|
|||||||
return registeredNumberImplementations.get(name);
|
return registeredNumberImplementations.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the documentation for the given entity of the given type.
|
|
||||||
* @param name the name of the entity to search for.
|
|
||||||
* @param type the type that this entity is, to filter out similarly named documentation.
|
|
||||||
* @return the documentation object.
|
|
||||||
*/
|
|
||||||
public Documentation documentationFor(String name, DocumentationType type){
|
|
||||||
Documentation toReturn = null;
|
|
||||||
for(Documentation entry : registeredDocumentation){
|
|
||||||
if(entry.getCodeName().equals(name) && entry.getType() == type) {
|
|
||||||
toReturn = entry;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(toReturn == null){
|
|
||||||
toReturn = new Documentation(name, "", "", "", type);
|
|
||||||
registerDocumentation(toReturn);
|
|
||||||
}
|
|
||||||
return toReturn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number implementation for the given implementation class.
|
* Gets the number implementation for the given implementation class.
|
||||||
*
|
*
|
||||||
@@ -246,10 +209,6 @@ public class PluginManager {
|
|||||||
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
if (disabledPlugins.contains(plugin.getClass().getName())) continue;
|
||||||
plugin.disable();
|
plugin.disable();
|
||||||
}
|
}
|
||||||
registeredFunctions.clear();
|
|
||||||
registeredOperators.clear();
|
|
||||||
registeredNumberImplementations.clear();
|
|
||||||
registeredDocumentation.clear();
|
|
||||||
cachedInterfaceImplementations.clear();
|
cachedInterfaceImplementations.clear();
|
||||||
cachedPi.clear();
|
cachedPi.clear();
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
package org.nwapw.abacus.plugin;
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
import org.nwapw.abacus.function.*;
|
import org.nwapw.abacus.function.Function;
|
||||||
import org.nwapw.abacus.lexing.pattern.Match;
|
import org.nwapw.abacus.function.Operator;
|
||||||
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
|
import org.nwapw.abacus.function.OperatorType;
|
||||||
import org.nwapw.abacus.number.NaiveNumber;
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.number.PreciseNumber;
|
import org.nwapw.abacus.number.PreciseNumber;
|
||||||
import org.nwapw.abacus.parsing.Parser;
|
|
||||||
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
|
||||||
import org.nwapw.abacus.tree.TokenType;
|
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,7 +188,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
@@ -210,7 +207,7 @@ 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 = fromInt(number.getClass(), 1);
|
//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.
|
||||||
@@ -304,16 +301,11 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface maxError = getMaxError(params[0]);
|
NumberInterface maxError = params[0].getMaxError();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
if (params[0].signum() <= 0) {
|
if (params[0].signum() < 0) {
|
||||||
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
NumberInterface[] negatedParams = {params[0].negate()};
|
||||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
return fromInt(params[0].getClass(), 1).divide(applyInternal(negatedParams));
|
||||||
n++;
|
|
||||||
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
|
||||||
sum = sum.add(currentTerm);
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
} else {
|
} else {
|
||||||
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
//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.
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
@@ -355,7 +347,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
||||||
//Detect integer bases:
|
//Detect integer bases:
|
||||||
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
|
if(params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
|
||||||
&& FUNCTION_ABS.apply(params[0]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 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){
|
&& FUNCTION_ABS.apply(params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0){
|
||||||
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
|
NumberInterface[] newParams = {params[0], params[1].fractionalPart()};
|
||||||
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
|
return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(newParams));
|
||||||
@@ -479,16 +471,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the maximum error based on the precision of the class of number.
|
|
||||||
*
|
|
||||||
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
|
||||||
* @return the maximum error.
|
|
||||||
*/
|
|
||||||
private static NumberInterface getMaxError(NumberInterface number) {
|
|
||||||
return fromInt(number.getClass(), 10).intPow(-number.getMaxPrecision());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factorial function that uses memoization for each number class; it efficiently
|
* A factorial function that uses memoization for each number class; it efficiently
|
||||||
* computes factorials of non-negative integers.
|
* computes factorials of non-negative integers.
|
||||||
@@ -520,7 +502,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
private static NumberInterface sinTaylor(NumberInterface x) {
|
private static NumberInterface sinTaylor(NumberInterface x) {
|
||||||
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
||||||
NumberInterface maxError = getMaxError(x);
|
NumberInterface maxError = x.getMaxError();
|
||||||
int n = 1;
|
int n = 1;
|
||||||
do {
|
do {
|
||||||
n += 2;
|
n += 2;
|
||||||
@@ -586,35 +568,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
registerFunction("sec", functionSec);
|
registerFunction("sec", functionSec);
|
||||||
registerFunction("csc", functionCsc);
|
registerFunction("csc", functionCsc);
|
||||||
registerFunction("cot", functionCot);
|
registerFunction("cot", functionCot);
|
||||||
|
|
||||||
registerDocumentation(new Documentation("abs", "Absolute Value", "Finds the distance " +
|
|
||||||
"from zero of a number.", "Given a number, this function finds the distance form " +
|
|
||||||
"zero of a number, effectively turning negative numbers into positive ones.\n\n" +
|
|
||||||
"Example: abs(-2) -> 2", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("exp", "Exponentiate", "Brings e to the given power.",
|
|
||||||
"This function evaluates e to the power of the given value, and is the inverse " +
|
|
||||||
"of the natural logarithm.\n\n" +
|
|
||||||
"Example: exp(1) -> 2.718...", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("ln", "Natural Logarithm", "Gets the natural " +
|
|
||||||
"logarithm of the given value.", "The natural logarithm of a number is " +
|
|
||||||
"the power that e has to be brought to to be equal to the number.\n\n" +
|
|
||||||
"Example: ln(2.718) -> 1", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("sqrt", "Square Root", "Finds the square root " +
|
|
||||||
"of the number.", "A square root a of a number is defined such that a times a is equal " +
|
|
||||||
"to that number.\n\n" +
|
|
||||||
"Example: sqrt(4) -> 2", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("sin", "Sine", "Computes the sine of the given angle, " +
|
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("cos", "Cosine", "Computes the cosine of the given angle, " +
|
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("tan", "Tangent", "Computes the tangent of the given angle, " +
|
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("sec", "Secant", "Computes the secant of the given angle, " +
|
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("csc", "Cosecant", "Computes the cosecant of the given angle, " +
|
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
|
||||||
registerDocumentation(new Documentation("cot", "Cotangent", "Computes the cotangent of the given angle, " +
|
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
108
src/main/java/org/nwapw/abacus/tree/BinaryNode.java
Normal file
108
src/main/java/org/nwapw/abacus/tree/BinaryNode.java
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
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 + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
85
src/main/java/org/nwapw/abacus/tree/FunctionNode.java
Normal file
85
src/main/java/org/nwapw/abacus/tree/FunctionNode.java
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/main/java/org/nwapw/abacus/tree/NumberNode.java
Normal file
49
src/main/java/org/nwapw/abacus/tree/NumberNode.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/java/org/nwapw/abacus/tree/TreeNode.java
Normal file
17
src/main/java/org/nwapw/abacus/tree/TreeNode.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
63
src/main/java/org/nwapw/abacus/tree/UnaryNode.java
Normal file
63
src/main/java/org/nwapw/abacus/tree/UnaryNode.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package org.nwapw.abacus.function
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A data class used for storing information about a function.
|
|
||||||
*
|
|
||||||
* The Documentation class holds the information necessary to display the information
|
|
||||||
* about a function to the user.
|
|
||||||
*
|
|
||||||
* @param codeName the name of the function as it occurs in code.
|
|
||||||
* @param name the name of the function in English.
|
|
||||||
* @param description the short description of this function.
|
|
||||||
* @param longDescription the full description of this function.
|
|
||||||
* @param type the things this documentation maps to.
|
|
||||||
*/
|
|
||||||
data class Documentation(val codeName: String, val name: String,
|
|
||||||
val description: String, val longDescription: String,
|
|
||||||
val type: DocumentationType) {
|
|
||||||
|
|
||||||
fun matches(other: String): Boolean {
|
|
||||||
return codeName.toLowerCase().contains(other.toLowerCase()) ||
|
|
||||||
name.toLowerCase().contains(other.toLowerCase()) ||
|
|
||||||
description.toLowerCase().contains(other.toLowerCase()) ||
|
|
||||||
longDescription.toLowerCase().contains(other.toLowerCase())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
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)
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
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") + ")"
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A tree node.
|
|
||||||
*/
|
|
||||||
abstract class TreeNode {
|
|
||||||
|
|
||||||
abstract fun <T: Any> reduce(reducer: Reducer<T>) : T?
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -60,15 +60,6 @@
|
|||||||
</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>
|
||||||
|
|
||||||
|
|||||||
7
src/test/java/org/nwapw/abacus/tests/CalculationTests.java
Normal file → Executable file
7
src/test/java/org/nwapw/abacus/tests/CalculationTests.java
Normal file → Executable file
@@ -88,8 +88,8 @@ public class CalculationTests {
|
|||||||
public void testExp(){
|
public void testExp(){
|
||||||
testOutput("exp0", "exp(0)", "1");
|
testOutput("exp0", "exp(0)", "1");
|
||||||
testOutput("exp1", "exp(1)", "2.718281828459045235360287471352662497757247");
|
testOutput("exp1", "exp(1)", "2.718281828459045235360287471352662497757247");
|
||||||
testOutput("exp300", "exp(300)", "19424263952412559365842088360176992193662086");
|
testOutput("exp300", "exp(300)", "1.9424263952412559365842088360176992193662086");
|
||||||
testOutput("exp300", "exp(300)", "19424263952412559365842088360176992193662086");
|
testOutput("exp(-500)", "exp((500)`)", "7.1245764067412855315491573771227552469277568");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -99,6 +99,9 @@ public class CalculationTests {
|
|||||||
testOutput("2^1", "(2^1)", "2");
|
testOutput("2^1", "(2^1)", "2");
|
||||||
testOutput("2^-1", "(2^(1)`)", "0.5");
|
testOutput("2^-1", "(2^(1)`)", "0.5");
|
||||||
testOutput("2^50", "(2^50)", "112589990684262");
|
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)");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user