mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
16 Commits
config-rew
...
stoppable-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8bf60f383 | ||
|
|
4369eba107 | ||
|
|
eff7be0204 | ||
|
|
9d5f9d901c | ||
|
|
dad546c5b5 | ||
| c7b5d4c4fc | |||
|
|
86533d53c9 | ||
| c2ae0b4138 | |||
| 16938b4e06 | |||
| d964fbfb6f | |||
| 9713f24ed2 | |||
| 5de9453bec | |||
| 6f99f07150 | |||
| 2cf41c1029 | |||
| 21d88fe256 | |||
| 3d61ead0f6 |
@@ -5,10 +5,10 @@ Summer project for NWAPW.
|
|||||||
Created by Arthur Drobot, Danila Fedorin and Riley Jones.
|
Created by Arthur Drobot, Danila Fedorin and Riley Jones.
|
||||||
|
|
||||||
## Project Description
|
## Project Description
|
||||||
Abacus is a calculator built with extensibility and usability in mind. It provides a plugin interface, via Java, as Lua provides too difficult to link up to the Java core. The description of the internals of the project can be found on the wiki page.
|
Abacus is a calculator built with extensibility and usability in mind. It provides a plugin interface, via Java, as Lua proves too difficult to link up to the Java core. The description of the internals of the project can be found on the wiki page.
|
||||||
|
|
||||||
## Current State
|
## Current State
|
||||||
Abacus is being built for the Northwest Advanced Programming Workshop, a 3 week program in which students work in treams to complete a single project, following principles of agile development. Because of its short timeframe, Abacus is not even close to completed state. Below is a list of the current features and problems.
|
Abacus is being built for the Northwest Advanced Programming Workshop, a 3 week program in which students work in teams to complete a single project, following principles of agile development. Because of its short timeframe, Abacus is not even close to completed state. Below is a list of the current features and problems.
|
||||||
- [x] Basic number class
|
- [x] Basic number class
|
||||||
- [x] Implementation of basic functions
|
- [x] Implementation of basic functions
|
||||||
- [x] Implementation of `exp`, `ln`, `sqrt` using the basic functions and Taylor Series
|
- [x] Implementation of `exp`, `ln`, `sqrt` using the basic functions and Taylor Series
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ package org.nwapw.abacus.function;
|
|||||||
* The type of an operator, describing how it should behave.
|
* The type of an operator, describing how it should behave.
|
||||||
*/
|
*/
|
||||||
public enum OperatorType {
|
public enum OperatorType {
|
||||||
BINARY_INFIX, UNARY_POSTFIX
|
BINARY_INFIX, UNARY_POSTFIX, UNARY_PREFIX
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ import javafx.scene.Parent;
|
|||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main application class for JavaFX responsible for loading
|
||||||
|
* and displaying the fxml file.
|
||||||
|
*/
|
||||||
public class AbacusApplication extends Application {
|
public class AbacusApplication extends Application {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.nwapw.abacus.fx;
|
package org.nwapw.abacus.fx;
|
||||||
|
|
||||||
|
import javafx.application.Platform;
|
||||||
import javafx.collections.FXCollections;
|
import javafx.collections.FXCollections;
|
||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
@@ -11,11 +12,24 @@ import org.nwapw.abacus.number.NumberInterface;
|
|||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The controller for the abacus FX UI, responsible
|
||||||
|
* for all the user interaction.
|
||||||
|
*/
|
||||||
public class AbacusController {
|
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";
|
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";
|
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";
|
||||||
@FXML
|
@FXML
|
||||||
private TableView<HistoryModel> historyTable;
|
private TableView<HistoryModel> historyTable;
|
||||||
@FXML
|
@FXML
|
||||||
@@ -33,10 +47,31 @@ public class AbacusController {
|
|||||||
@FXML
|
@FXML
|
||||||
private ComboBox<String> numberImplementationBox;
|
private ComboBox<String> numberImplementationBox;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The list of history entries, created by the users.
|
||||||
|
*/
|
||||||
private ObservableList<HistoryModel> historyData;
|
private ObservableList<HistoryModel> historyData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The abacus instance used for calculations and all
|
||||||
|
* other main processing code.
|
||||||
|
*/
|
||||||
private ObservableList<String> numberImplementationOptions;
|
private ObservableList<String> numberImplementationOptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread used for calculating.
|
||||||
|
*/
|
||||||
|
private Thread calcThread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the calculator is calculating.
|
||||||
|
*/
|
||||||
|
private boolean calculating;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Seconds delayed for timer;
|
||||||
|
*/
|
||||||
|
private double delay = 0;
|
||||||
private Abacus abacus;
|
private Abacus abacus;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
@@ -70,24 +105,63 @@ public class AbacusController {
|
|||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private void performCalculation(){
|
private void performCalculation(){
|
||||||
inputButton.setDisable(true);
|
Runnable calculator = new Runnable(){
|
||||||
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
public void run() {
|
||||||
if(constructedTree == null){
|
if(delay>0) {
|
||||||
outputText.setText(ERR_SYNTAX);
|
Runnable timer = new Runnable() {
|
||||||
inputButton.setDisable(false);
|
public void run() {
|
||||||
return;
|
long gap = (long) (delay * 1000);
|
||||||
}
|
long startTime = System.currentTimeMillis();
|
||||||
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
while (System.currentTimeMillis() - startTime <= gap) {
|
||||||
if(evaluatedNumber == null){
|
}
|
||||||
outputText.setText(ERR_EVAL);
|
stopCalculation();
|
||||||
inputButton.setDisable(false);
|
}
|
||||||
return;
|
};
|
||||||
}
|
Thread maxTime = new Thread(timer);
|
||||||
outputText.setText(evaluatedNumber.toString());
|
maxTime.setName("maxTime");
|
||||||
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
maxTime.start();
|
||||||
|
}
|
||||||
|
calculating = true;
|
||||||
|
Platform.runLater(() -> inputButton.setDisable(true));
|
||||||
|
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||||
|
if (constructedTree == null) {
|
||||||
|
Platform.runLater(() ->outputText.setText(ERR_SYNTAX));
|
||||||
|
Platform.runLater(() -> inputButton.setDisable(false));
|
||||||
|
//return;
|
||||||
|
}else {
|
||||||
|
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
||||||
|
if (evaluatedNumber == null) {
|
||||||
|
if(Thread.currentThread().isInterrupted()){
|
||||||
|
Platform.runLater(() -> outputText.setText(ERR_STOP));
|
||||||
|
Platform.runLater(() -> inputButton.setDisable(false));
|
||||||
|
}else {
|
||||||
|
Platform.runLater(() -> outputText.setText(ERR_EVAL));
|
||||||
|
Platform.runLater(() -> inputButton.setDisable(false));
|
||||||
|
//return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Platform.runLater(() -> outputText.setText(evaluatedNumber.toString()));
|
||||||
|
|
||||||
inputButton.setDisable(false);
|
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
||||||
inputField.setText("");
|
|
||||||
|
Platform.runLater(() -> inputButton.setDisable(false));
|
||||||
|
Platform.runLater(() -> inputField.setText(""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
calculating = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if(!calculating) {
|
||||||
|
calcThread = new Thread(calculator);
|
||||||
|
calcThread.setName("calcThread");
|
||||||
|
calcThread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@FXML
|
||||||
|
private void stopCalculation(){
|
||||||
|
calcThread.interrupt();
|
||||||
|
calculating = false;
|
||||||
|
//Platform.runLater(() ->inputButton.setDisable(false));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,17 @@ import javafx.scene.input.MouseEvent;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.datatransfer.StringSelection;
|
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> {
|
public class CopyableCell<S, T> extends TableCell<S, T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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){
|
||||||
|
|||||||
@@ -3,12 +3,33 @@ package org.nwapw.abacus.fx;
|
|||||||
import javafx.beans.property.SimpleStringProperty;
|
import javafx.beans.property.SimpleStringProperty;
|
||||||
import javafx.beans.property.StringProperty;
|
import javafx.beans.property.StringProperty;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data model used for storing history entries.
|
||||||
|
*/
|
||||||
public class HistoryModel {
|
public class HistoryModel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* for the user input.
|
||||||
|
*/
|
||||||
private final StringProperty input;
|
private final StringProperty input;
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* that contains the parsed input.
|
||||||
|
*/
|
||||||
private final StringProperty parsed;
|
private final StringProperty parsed;
|
||||||
|
/**
|
||||||
|
* The property used for displaying the column
|
||||||
|
* that contains the program output.
|
||||||
|
*/
|
||||||
private final StringProperty 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){
|
public HistoryModel(String input, String parsed, String output){
|
||||||
this.input = new SimpleStringProperty();
|
this.input = new SimpleStringProperty();
|
||||||
this.parsed = new SimpleStringProperty();
|
this.parsed = new SimpleStringProperty();
|
||||||
@@ -18,23 +39,47 @@ public class HistoryModel {
|
|||||||
this.output.setValue(output);
|
this.output.setValue(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the input property.
|
||||||
|
* @return the input property.
|
||||||
|
*/
|
||||||
public StringProperty inputProperty() {
|
public StringProperty inputProperty() {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Gets the input.
|
||||||
|
* @return the input.
|
||||||
|
*/
|
||||||
public String getInput() {
|
public String getInput() {
|
||||||
return input.get();
|
return input.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the parsed input property.
|
||||||
|
* @return the parsed input property.
|
||||||
|
*/
|
||||||
public StringProperty parsedProperty() {
|
public StringProperty parsedProperty() {
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Gets the parsed input.
|
||||||
|
* @return the parsed input.
|
||||||
|
*/
|
||||||
public String getParsed() {
|
public String getParsed() {
|
||||||
return parsed.get();
|
return parsed.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the output property.
|
||||||
|
* @return the output property.
|
||||||
|
*/
|
||||||
public StringProperty outputProperty() {
|
public StringProperty outputProperty() {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Gets the program output.
|
||||||
|
* @return the output.
|
||||||
|
*/
|
||||||
public String getOutput() {
|
public String getOutput() {
|
||||||
return output.get();
|
return output.get();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package org.nwapw.abacus.number;
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.MathContext;
|
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A number that uses a BigDecimal to store its value,
|
||||||
|
* leading to infinite possible precision.
|
||||||
|
*/
|
||||||
public class PreciseNumber implements NumberInterface {
|
public class PreciseNumber implements NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -55,9 +55,12 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
||||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||||
|
TokenType previousType;
|
||||||
|
TokenType matchType = null;
|
||||||
while (!from.isEmpty()) {
|
while (!from.isEmpty()) {
|
||||||
Match<TokenType> match = from.remove(0);
|
Match<TokenType> match = from.remove(0);
|
||||||
TokenType matchType = match.getType();
|
previousType = matchType;
|
||||||
|
matchType = match.getType();
|
||||||
if (matchType == TokenType.NUM) {
|
if (matchType == TokenType.NUM) {
|
||||||
output.add(match);
|
output.add(match);
|
||||||
} else if (matchType == TokenType.FUNCTION) {
|
} else if (matchType == TokenType.FUNCTION) {
|
||||||
@@ -74,7 +77,13 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!tokenStack.empty()) {
|
if(tokenString.equals("-") && (previousType == null || previousType == TokenType.OP ||
|
||||||
|
previousType == TokenType.OPEN_PARENTH)){
|
||||||
|
from.add(0, new Match<>("`", TokenType.OP));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!tokenStack.empty() && type == OperatorType.BINARY_INFIX) {
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
TokenType otherMatchType = otherMatch.getType();
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||||
@@ -103,8 +112,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
}
|
}
|
||||||
while (!tokenStack.empty()) {
|
while (!tokenStack.empty()) {
|
||||||
Match<TokenType> match = tokenStack.peek();
|
Match<TokenType> match = tokenStack.peek();
|
||||||
TokenType matchType = match.getType();
|
TokenType newMatchType = match.getType();
|
||||||
if (!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION)) return null;
|
||||||
output.add(tokenStack.pop());
|
output.add(tokenStack.pop());
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
@@ -127,11 +136,11 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
TreeNode right = constructRecursive(matches);
|
TreeNode right = constructRecursive(matches);
|
||||||
TreeNode left = constructRecursive(matches);
|
TreeNode left = constructRecursive(matches);
|
||||||
if (left == null || right == null) return null;
|
if (left == null || right == null) return null;
|
||||||
else return new BinaryInfixNode(operator, left, right);
|
else return new BinaryNode(operator, left, right);
|
||||||
} else {
|
} else {
|
||||||
TreeNode applyTo = constructRecursive(matches);
|
TreeNode applyTo = constructRecursive(matches);
|
||||||
if (applyTo == null) return null;
|
if (applyTo == null) return null;
|
||||||
else return new UnaryPrefixNode(operator, applyTo);
|
else return new UnaryNode(operator, applyTo);
|
||||||
}
|
}
|
||||||
} else if (matchType == TokenType.NUM) {
|
} else if (matchType == TokenType.NUM) {
|
||||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||||
|
|||||||
@@ -31,9 +31,13 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
NumberInterface sum = params[0];
|
NumberInterface sum = params[0];
|
||||||
for (int i = 1; i < params.length; i++) {
|
for (int i = 1; i < params.length; i++) {
|
||||||
sum = sum.add(params[i]);
|
sum = sum.add(params[i]);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
@@ -49,7 +53,26 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
return params[0].subtract(params[1]);
|
return params[0].subtract(params[1]);
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
/**
|
||||||
|
* The negation operator, -
|
||||||
|
*/
|
||||||
|
public static final Operator OP_NEGATE = new Operator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0, new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
return params[0].negate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
@@ -63,9 +86,13 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
NumberInterface product = params[0];
|
NumberInterface product = params[0];
|
||||||
for (int i = 1; i < params.length; i++) {
|
for (int i = 1; i < params.length; i++) {
|
||||||
product = product.multiply(params[i]);
|
product = product.multiply(params[i]);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
@@ -76,16 +103,14 @@ public class StandardPlugin extends Plugin {
|
|||||||
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length == 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface product = params[0];
|
if(Thread.currentThread().isInterrupted())
|
||||||
for (int i = 1; i < params.length; i++) {
|
return null;
|
||||||
product = product.multiply(params[i]);
|
return params[0].divide(params[1]);
|
||||||
}
|
|
||||||
return product;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
@@ -100,15 +125,19 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
if (params[0].signum() == 0) {
|
if (params[0].signum() == 0) {
|
||||||
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
||||||
}
|
}
|
||||||
NumberInterface factorial = params[0];
|
NumberInterface factorial = params[0];
|
||||||
NumberInterface multiplier = params[0];
|
NumberInterface multiplier = params[0];
|
||||||
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
||||||
while ((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1) {
|
while (!Thread.currentThread().isInterrupted()&&(multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass())))!=null&&multiplier.signum() == 1) {
|
||||||
factorial = factorial.multiply(multiplier);
|
factorial = factorial.multiply(multiplier);
|
||||||
}
|
}
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
return factorial;
|
return factorial;
|
||||||
/*if(!storedList.containsKey(params[0].getClass())){
|
/*if(!storedList.containsKey(params[0].getClass())){
|
||||||
storedList.put(params[0].getClass(), new ArrayList<NumberInterface>());
|
storedList.put(params[0].getClass(), new ArrayList<NumberInterface>());
|
||||||
@@ -128,7 +157,12 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1]));
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
NumberInterface check;
|
||||||
|
if((check = FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0])))!=null&&(check = check.multiply(params[1]))!=null)
|
||||||
|
return check;
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
@@ -142,6 +176,8 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
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()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -156,14 +192,17 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
NumberInterface maxError = getMaxError(params[0]);
|
NumberInterface maxError = getMaxError(params[0]);
|
||||||
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 currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||||
while(FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0){
|
NumberInterface check;
|
||||||
|
while((check = FUNCTION_ABS.apply(currentTerm))!=null && (check.compareTo(maxError) > 0)){
|
||||||
n++;
|
n++;
|
||||||
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
if(Thread.currentThread().isInterrupted()||(currentTerm = currentTerm.multiply(params[0]))==null||(currentTerm = currentTerm.divide((new NaiveNumber(n)).promoteTo(params[0].getClass())))==null||(sum = (sum.add(currentTerm)))==null)
|
||||||
sum = sum.add(currentTerm);
|
return null;
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
@@ -172,18 +211,28 @@ public class StandardPlugin extends Plugin {
|
|||||||
//right and left refer to lhs and rhs in the above inequality.
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||||
NumberInterface nextNumerator = params[0];
|
NumberInterface nextNumerator = params[0];
|
||||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
|
//NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
|
||||||
|
NumberInterface check;
|
||||||
|
if((check =intPow(new NaiveNumber(3).promoteTo(params[0].getClass()),params[0].getClass(),(new NaiveNumber(params[0].ceiling())).promoteTo(params[0].getClass())))==null)
|
||||||
|
return null;
|
||||||
|
NumberInterface left = params[0].multiply(check), right = maxError;
|
||||||
do{
|
do{
|
||||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
if((check = factorial(params[0].getClass(),n+1))==null||(check = nextNumerator.divide(check))==null||(sum = sum.add(check))==null)
|
||||||
|
return null;
|
||||||
n++;
|
n++;
|
||||||
nextNumerator = nextNumerator.multiply(params[0]);
|
if((nextNumerator = nextNumerator.multiply(params[0]))==null)
|
||||||
left = left.multiply(params[0]);
|
return null;
|
||||||
|
if((left = left.multiply(params[0]))==null)
|
||||||
|
return null;
|
||||||
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
|
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
|
||||||
right = right.multiply(nextN);
|
if((right = right.multiply(nextN))==null)
|
||||||
|
return null;
|
||||||
//System.out.println(left + ", " + right);
|
//System.out.println(left + ", " + right);
|
||||||
}
|
}
|
||||||
while(left.compareTo(right) > 0);
|
while(!Thread.currentThread().isInterrupted()&&left.compareTo(right) > 0);
|
||||||
//System.out.println(n+1);
|
//System.out.println(n+1);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,26 +248,32 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
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) {
|
NumberInterface check;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
while (!Thread.currentThread().isInterrupted()&&(check = FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))))!=null&&(check.compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass()))) >= 0) {
|
||||||
|
if ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())))!=null&&check.signum() == 1) {
|
||||||
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||||
powersOf2++;
|
powersOf2++;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
if ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())))==null||check.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(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||||
powersOf2--;
|
powersOf2--;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
if ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())))==null||check.signum() != 1) {
|
||||||
break;
|
break;
|
||||||
//No infinite loop for you.
|
//No infinite loop for you.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getLog2(param).multiply((new NaiveNumber(powersOf2)).promoteTo(param.getClass())).add(getLogPartialSum(param));
|
NumberInterface check2;
|
||||||
|
if(!Thread.currentThread().isInterrupted()&&(check = getLog2(param))!=null&&(check = check.multiply((new NaiveNumber(powersOf2).promoteTo(param.getClass()))))!=null&&(check2 = getLogPartialSum(param))!=null&&(check = check.add(check2))!=null)
|
||||||
|
return check;
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -228,16 +283,23 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return the partial sum.
|
* @return the partial sum.
|
||||||
*/
|
*/
|
||||||
private NumberInterface getLogPartialSum(NumberInterface x) {
|
private NumberInterface getLogPartialSum(NumberInterface x) {
|
||||||
|
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
NumberInterface maxError = getMaxError(x);
|
NumberInterface maxError = getMaxError(x);
|
||||||
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;
|
||||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
NumberInterface check;
|
||||||
|
while (!Thread.currentThread().isInterrupted()&&(check = FUNCTION_ABS.apply(currentTerm))!=null&&check.compareTo(maxError) > 0) {
|
||||||
n++;
|
n++;
|
||||||
currentNumerator = currentNumerator.multiply(x).negate();
|
if((currentNumerator = currentNumerator.multiply(x))==null||(currentNumerator = currentNumerator.negate())==null)
|
||||||
|
return null;
|
||||||
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
|
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
|
||||||
sum = sum.add(currentTerm);
|
sum = sum.add(currentTerm);
|
||||||
}
|
}
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +309,8 @@ 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) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
NumberInterface maxError = getMaxError(number);
|
NumberInterface maxError = getMaxError(number);
|
||||||
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
||||||
//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)
|
||||||
@@ -255,13 +319,17 @@ public class StandardPlugin extends Plugin {
|
|||||||
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), 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 (!Thread.currentThread().isInterrupted()&&a.compareTo(maxError) >= 1) {
|
||||||
n++;
|
n++;
|
||||||
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
||||||
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
||||||
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));
|
NumberInterface check;
|
||||||
|
if(a==null||(check = a.add(b))==null||(check = check.multiply(c))==null||(sum = sum.add(check))==null)
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -317,6 +385,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
registerOperator("+", OP_ADD);
|
registerOperator("+", OP_ADD);
|
||||||
registerOperator("-", OP_SUBTRACT);
|
registerOperator("-", OP_SUBTRACT);
|
||||||
|
registerOperator("`", OP_NEGATE);
|
||||||
registerOperator("*", OP_MULTIPLY);
|
registerOperator("*", OP_MULTIPLY);
|
||||||
registerOperator("/", OP_DIVIDE);
|
registerOperator("/", OP_DIVIDE);
|
||||||
registerOperator("^", OP_CARET);
|
registerOperator("^", OP_CARET);
|
||||||
@@ -334,18 +403,43 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
if(!factorialLists.containsKey(numberClass)){
|
if(!factorialLists.containsKey(numberClass)){
|
||||||
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
factorialLists.put(numberClass, new ArrayList<>());
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
}
|
}
|
||||||
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
||||||
if(n >= list.size()){
|
if(n >= list.size()){
|
||||||
while(list.size() < n + 16){
|
while(!Thread.currentThread().isInterrupted()&&list.size() < n + 16){
|
||||||
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
return list.get(n);
|
return list.get(n);
|
||||||
}
|
}
|
||||||
|
public static NumberInterface intPow(NumberInterface number, Class<? extends NumberInterface> numberClass,NumberInterface exponent) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
if (exponent.compareTo((new NaiveNumber(0)).promoteTo(numberClass))==0) {
|
||||||
|
return (new NaiveNumber(1)).promoteTo(numberClass);
|
||||||
|
}
|
||||||
|
boolean takeReciprocal = exponent.compareTo((new NaiveNumber(0)).promoteTo(numberClass))<0;
|
||||||
|
exponent = FUNCTION_ABS.apply(exponent);
|
||||||
|
NumberInterface power = number;
|
||||||
|
for(NumberInterface currentExponent =(new NaiveNumber(1)).promoteTo(numberClass);currentExponent.compareTo(exponent)<0;currentExponent = currentExponent.add((new NaiveNumber(1)).promoteTo(numberClass))){
|
||||||
|
power = power.multiply(number);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (takeReciprocal) {
|
||||||
|
power = (new NaiveNumber(1)).promoteTo(numberClass).divide(power);
|
||||||
|
}
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
return power;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package org.nwapw.abacus.tree;
|
|||||||
/**
|
/**
|
||||||
* A tree node that represents an operation being applied to two operands.
|
* A tree node that represents an operation being applied to two operands.
|
||||||
*/
|
*/
|
||||||
public class BinaryInfixNode extends TreeNode {
|
public class BinaryNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
@@ -18,7 +18,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private BinaryInfixNode() {
|
private BinaryNode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +27,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation) {
|
public BinaryNode(String operation) {
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
* @param left the left node of the expression.
|
* @param left the left node of the expression.
|
||||||
* @param right the right node of the expression.
|
* @param right the right node of the expression.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
|
public BinaryNode(String operation, TreeNode left, TreeNode right) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
@@ -92,10 +92,15 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
T leftReduce = left.reduce(reducer);
|
T leftReduce = left.reduce(reducer);
|
||||||
T rightReduce = right.reduce(reducer);
|
T rightReduce = right.reduce(reducer);
|
||||||
if (leftReduce == null || rightReduce == null) return null;
|
if (leftReduce == null || rightReduce == null) return null;
|
||||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
T a = reducer.reduceNode(this, leftReduce, rightReduce);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -62,12 +62,17 @@ public class FunctionNode extends TreeNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
Object[] reducedChildren = new Object[children.size()];
|
Object[] reducedChildren = new Object[children.size()];
|
||||||
for (int i = 0; i < reducedChildren.length; i++) {
|
for (int i = 0; i < reducedChildren.length; i++) {
|
||||||
reducedChildren[i] = children.get(i).reduce(reducer);
|
reducedChildren[i] = children.get(i).reduce(reducer);
|
||||||
if (reducedChildren[i] == null) return null;
|
if (Thread.currentThread().isInterrupted()||reducedChildren[i] == null) return null;
|
||||||
}
|
}
|
||||||
return reducer.reduceNode(this, reducedChildren);
|
T a = reducer.reduceNode(this, reducedChildren);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -28,15 +28,15 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
|||||||
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||||
if (node instanceof NumberNode) {
|
if (node instanceof NumberNode) {
|
||||||
return ((NumberNode) node).getNumber();
|
return ((NumberNode) node).getNumber();
|
||||||
} else if (node instanceof BinaryInfixNode) {
|
} else if (node instanceof BinaryNode) {
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
Function function = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()).getFunction();
|
||||||
if (function == null) return null;
|
if (function == null) return null;
|
||||||
return function.apply(left, right);
|
return function.apply(left, right);
|
||||||
} else if (node instanceof UnaryPrefixNode) {
|
} else if (node instanceof UnaryNode) {
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
NumberInterface child = (NumberInterface) children[0];
|
||||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
Function functionn = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()).getFunction();
|
||||||
if (functionn == null) return null;
|
if (functionn == null) return null;
|
||||||
return functionn.apply(child);
|
return functionn.apply(child);
|
||||||
} else if (node instanceof FunctionNode) {
|
} else if (node instanceof FunctionNode) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
public class UnaryPrefixNode extends TreeNode {
|
public class UnaryNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation this node will apply.
|
* The operation this node will apply.
|
||||||
@@ -16,7 +16,7 @@ public class UnaryPrefixNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation) {
|
public UnaryNode(String operation) {
|
||||||
this(operation, null);
|
this(operation, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,16 +26,21 @@ public class UnaryPrefixNode extends TreeNode {
|
|||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
* @param applyTo the node to apply the function to.
|
* @param applyTo the node to apply the function to.
|
||||||
*/
|
*/
|
||||||
public UnaryPrefixNode(String operation, TreeNode applyTo) {
|
public UnaryNode(String operation, TreeNode applyTo) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.applyTo = applyTo;
|
this.applyTo = applyTo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
Object reducedChild = applyTo.reduce(reducer);
|
Object reducedChild = applyTo.reduce(reducer);
|
||||||
if (reducedChild == null) return null;
|
if (reducedChild == null) return null;
|
||||||
return reducer.reduceNode(this, reducedChild);
|
T a = reducer.reduceNode(this, reducedChild);
|
||||||
|
if(Thread.currentThread().isInterrupted())
|
||||||
|
return null;
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,6 +37,8 @@
|
|||||||
<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="#stopCalculation"/>
|
||||||
</VBox>
|
</VBox>
|
||||||
</bottom>
|
</bottom>
|
||||||
</BorderPane>
|
</BorderPane>
|
||||||
|
|||||||
Reference in New Issue
Block a user