mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
22 Commits
function-d
...
plugin-add
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c448048f2 | |||
| fd21014c39 | |||
| 021e569491 | |||
| e6f5af3727 | |||
| 4e042bd0eb | |||
| d6f4838f05 | |||
| 61475a24d9 | |||
| c498a5b643 | |||
| 5e3daaed43 | |||
| b99ad5a09a | |||
| ff8701a7bf | |||
| 7dcc80fcae | |||
| d10536155b | |||
| 536cac7b23 | |||
|
|
b6e4c6d2ea | ||
|
|
f8b3559cec | ||
|
|
4cf4ba98a8 | ||
| 12710c625b | |||
| e71b037195 | |||
| fe92929856 | |||
|
|
68fbcd2d7c | ||
|
|
ed92b382f0 |
@@ -222,7 +222,7 @@ public class AbacusController implements PluginListener {
|
|||||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
||||||
param -> new CopyableCell<>();
|
param -> new CopyableCell<>();
|
||||||
Callback<ListView<ToggleablePlugin>, ListCell<ToggleablePlugin>> pluginCellFactory =
|
Callback<ListView<ToggleablePlugin>, ListCell<ToggleablePlugin>> pluginCellFactory =
|
||||||
param -> new CheckBoxListCell<>(ToggleablePlugin::enabledProperty, new StringConverter<ToggleablePlugin>() {
|
param -> new CheckBoxListCell<>(ToggleablePlugin::getEnabledProperty, new StringConverter<ToggleablePlugin>() {
|
||||||
@Override
|
@Override
|
||||||
public String toString(ToggleablePlugin object) {
|
public String toString(ToggleablePlugin object) {
|
||||||
return object.getClassName().substring(object.getClassName().lastIndexOf('.') + 1);
|
return object.getClassName().substring(object.getClassName().lastIndexOf('.') + 1);
|
||||||
@@ -230,7 +230,7 @@ public class AbacusController implements PluginListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ToggleablePlugin fromString(String string) {
|
public ToggleablePlugin fromString(String string) {
|
||||||
return new ToggleablePlugin(true, string);
|
return new ToggleablePlugin(string, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
functionList = FXCollections.observableArrayList();
|
functionList = FXCollections.observableArrayList();
|
||||||
@@ -249,11 +249,11 @@ public class AbacusController implements PluginListener {
|
|||||||
enabledPluginView.setItems(enabledPlugins);
|
enabledPluginView.setItems(enabledPlugins);
|
||||||
enabledPluginView.setCellFactory(pluginCellFactory);
|
enabledPluginView.setCellFactory(pluginCellFactory);
|
||||||
inputColumn.setCellFactory(cellFactory);
|
inputColumn.setCellFactory(cellFactory);
|
||||||
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
inputColumn.setCellValueFactory(cell -> cell.getValue().getInputProperty());
|
||||||
parsedColumn.setCellFactory(cellFactory);
|
parsedColumn.setCellFactory(cellFactory);
|
||||||
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
parsedColumn.setCellValueFactory(cell -> cell.getValue().getParsedProperty());
|
||||||
outputColumn.setCellFactory(cellFactory);
|
outputColumn.setCellFactory(cellFactory);
|
||||||
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
outputColumn.setCellValueFactory(cell -> cell.getValue().getOutputProperty());
|
||||||
coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
|
if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
|
||||||
});
|
});
|
||||||
@@ -261,13 +261,7 @@ public class AbacusController implements PluginListener {
|
|||||||
abacus = new Abacus(new Configuration(CONFIG_FILE));
|
abacus = new Abacus(new Configuration(CONFIG_FILE));
|
||||||
PluginManager abacusPluginManager = abacus.getPluginManager();
|
PluginManager abacusPluginManager = abacus.getPluginManager();
|
||||||
abacusPluginManager.addListener(this);
|
abacusPluginManager.addListener(this);
|
||||||
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
performScan();
|
||||||
try {
|
|
||||||
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
abacusPluginManager.reload();
|
|
||||||
|
|
||||||
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
|
computationLimitField.setText(Double.toString(abacus.getConfiguration().getComputationDelay()));
|
||||||
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
|
computationLimitField.textProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
@@ -317,6 +311,19 @@ public class AbacusController implements PluginListener {
|
|||||||
reloadAlertShown = false;
|
reloadAlertShown = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
public void performScan(){
|
||||||
|
PluginManager abacusPluginManager = abacus.getPluginManager();
|
||||||
|
abacusPluginManager.removeAll();
|
||||||
|
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
||||||
|
try {
|
||||||
|
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
abacusPluginManager.reload();
|
||||||
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
public void performReload() {
|
public void performReload() {
|
||||||
alertIfApplyNeeded(true);
|
alertIfApplyNeeded(true);
|
||||||
@@ -349,8 +356,8 @@ public class AbacusController implements PluginListener {
|
|||||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
numberImplementationBox.getSelectionModel().select(toSelect);
|
||||||
for (Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()) {
|
for (Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()) {
|
||||||
String fullName = pluginClass.getName();
|
String fullName = pluginClass.getName();
|
||||||
ToggleablePlugin plugin = new ToggleablePlugin(!disabledPlugins.contains(fullName), fullName);
|
ToggleablePlugin plugin = new ToggleablePlugin(fullName, !disabledPlugins.contains(fullName));
|
||||||
plugin.enabledProperty().addListener(e -> changesMade = true);
|
plugin.getEnabledProperty().addListener(e -> changesMade = true);
|
||||||
enabledPlugins.add(plugin);
|
enabledPlugins.add(plugin);
|
||||||
}
|
}
|
||||||
PluginManager pluginManager = abacus.getPluginManager();
|
PluginManager pluginManager = abacus.getPluginManager();
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class DocumentationCell extends ListCell<Documentation> {
|
|||||||
titledPane.textProperty().bindBidirectional(codeNameLabel.textProperty());
|
titledPane.textProperty().bindBidirectional(codeNameLabel.textProperty());
|
||||||
titledPane.setContent(vbox);
|
titledPane.setContent(vbox);
|
||||||
titledPane.setExpanded(false);
|
titledPane.setExpanded(false);
|
||||||
titledPane.prefWidthProperty().bind(widthProperty());
|
titledPane.prefWidthProperty().bind(widthProperty().subtract(32));
|
||||||
|
|
||||||
visibleProperty().addListener((a, b, c) -> titledPane.setExpanded(false));
|
visibleProperty().addListener((a, b, c) -> titledPane.setExpanded(false));
|
||||||
}
|
}
|
||||||
@@ -54,5 +54,6 @@ public class DocumentationCell extends ListCell<Documentation> {
|
|||||||
longDescription.setText(item.getLongDescription());
|
longDescription.setText(item.getLongDescription());
|
||||||
setGraphic(titledPane);
|
setGraphic(titledPane);
|
||||||
}
|
}
|
||||||
|
titledPane.setExpanded(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,97 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.beans.property.SimpleStringProperty;
|
|
||||||
import javafx.beans.property.StringProperty;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The data model used for storing history entries.
|
|
||||||
*/
|
|
||||||
public class HistoryModel {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The property used for displaying the column
|
|
||||||
* for the user input.
|
|
||||||
*/
|
|
||||||
private final StringProperty input;
|
|
||||||
/**
|
|
||||||
* The property used for displaying the column
|
|
||||||
* that contains the parsed input.
|
|
||||||
*/
|
|
||||||
private final StringProperty parsed;
|
|
||||||
/**
|
|
||||||
* The property used for displaying the column
|
|
||||||
* that contains the program output.
|
|
||||||
*/
|
|
||||||
private final StringProperty output;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new history model with the given variables.
|
|
||||||
*
|
|
||||||
* @param input the user input
|
|
||||||
* @param parsed the parsed input
|
|
||||||
* @param output the program output.
|
|
||||||
*/
|
|
||||||
public HistoryModel(String input, String parsed, String output) {
|
|
||||||
this.input = new SimpleStringProperty();
|
|
||||||
this.parsed = new SimpleStringProperty();
|
|
||||||
this.output = new SimpleStringProperty();
|
|
||||||
this.input.setValue(input);
|
|
||||||
this.parsed.setValue(parsed);
|
|
||||||
this.output.setValue(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the input property.
|
|
||||||
*
|
|
||||||
* @return the input property.
|
|
||||||
*/
|
|
||||||
public StringProperty inputProperty() {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the input.
|
|
||||||
*
|
|
||||||
* @return the input.
|
|
||||||
*/
|
|
||||||
public String getInput() {
|
|
||||||
return input.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the parsed input property.
|
|
||||||
*
|
|
||||||
* @return the parsed input property.
|
|
||||||
*/
|
|
||||||
public StringProperty parsedProperty() {
|
|
||||||
return parsed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the parsed input.
|
|
||||||
*
|
|
||||||
* @return the parsed input.
|
|
||||||
*/
|
|
||||||
public String getParsed() {
|
|
||||||
return parsed.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the output property.
|
|
||||||
*
|
|
||||||
* @return the output property.
|
|
||||||
*/
|
|
||||||
public StringProperty outputProperty() {
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the program output.
|
|
||||||
*
|
|
||||||
* @return the output.
|
|
||||||
*/
|
|
||||||
public String getOutput() {
|
|
||||||
return output.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.beans.property.BooleanProperty;
|
|
||||||
import javafx.beans.property.SimpleBooleanProperty;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class that represents an entry in the plugin check box list.
|
|
||||||
* The changes from this property are written to the config on application.
|
|
||||||
*/
|
|
||||||
public class ToggleablePlugin {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The property that determines whether the plugin will be enabled.
|
|
||||||
*/
|
|
||||||
private final BooleanProperty enabled;
|
|
||||||
/**
|
|
||||||
* The name of the class this entry toggles.
|
|
||||||
*/
|
|
||||||
private final String className;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new toggleable plugin with the given properties.
|
|
||||||
*
|
|
||||||
* @param enabled the enabled / disabled state at the beginning.
|
|
||||||
* @param className the name of the class this plugin toggles.
|
|
||||||
*/
|
|
||||||
public ToggleablePlugin(boolean enabled, String className) {
|
|
||||||
this.enabled = new SimpleBooleanProperty();
|
|
||||||
this.enabled.setValue(enabled);
|
|
||||||
this.className = className;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the enabled property of this plugin.
|
|
||||||
*
|
|
||||||
* @return the enabled property.
|
|
||||||
*/
|
|
||||||
public BooleanProperty enabledProperty() {
|
|
||||||
return enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if this plugin entry should be enabled.
|
|
||||||
*
|
|
||||||
* @return whether this plugin will be enabled.
|
|
||||||
*/
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return enabled.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the class name this plugin toggles.
|
|
||||||
*
|
|
||||||
* @return the class name that should be disabled.
|
|
||||||
*/
|
|
||||||
public String getClassName() {
|
|
||||||
return className;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -240,6 +240,7 @@ public abstract class NumberInterface {
|
|||||||
* @param toClass the class to promote to.
|
* @param toClass the class to promote to.
|
||||||
* @return the resulting new instance.
|
* @return the resulting new instance.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
|
protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -250,9 +251,17 @@ public abstract class NumberInterface {
|
|||||||
* @param toClass the class to promote to.
|
* @param toClass the class to promote to.
|
||||||
* @return the resulting new instance.
|
* @return the resulting new instance.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
checkInterrupted();
|
checkInterrupted();
|
||||||
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ public abstract class NumberImplementation {
|
|||||||
/**
|
/**
|
||||||
* The list of paths through which this implementation can be promoted.
|
* The list of paths through which this implementation can be promoted.
|
||||||
*/
|
*/
|
||||||
protected Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
private Map<Class<? extends NumberInterface>, Function<NumberInterface, NumberInterface>> promotionPaths;
|
||||||
/**
|
/**
|
||||||
* The implementation class for this implementation.
|
* The implementation class for this implementation.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -224,6 +224,24 @@ public class PluginManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the plugin with the given class from the manager.
|
||||||
|
* @param toRemove the plugin to remove.
|
||||||
|
*/
|
||||||
|
public void removeClass(Class<? extends Plugin> toRemove){
|
||||||
|
if(!loadedPluginClasses.contains(toRemove)) return;
|
||||||
|
plugins.removeIf(plugin -> plugin.getClass() == toRemove);
|
||||||
|
loadedPluginClasses.remove(toRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all plugins from this plugin manager.
|
||||||
|
*/
|
||||||
|
public void removeAll(){
|
||||||
|
loadedPluginClasses.clear();
|
||||||
|
plugins.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all the plugins in the PluginManager.
|
* Loads all the plugins in the PluginManager.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import org.nwapw.abacus.tree.TokenType;
|
|||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
@@ -97,7 +98,7 @@ 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 == 2 && params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) != 0;
|
return params.length == 2 && params[1].compareTo(fromInt(params[0].getClass(), 0)) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -113,7 +114,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 1
|
return params.length == 1
|
||||||
&& params[0].fractionalPart().compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
&& params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0
|
||||||
&& params[0].signum() >= 0;
|
&& params[0].signum() >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,10 +123,11 @@ public class StandardPlugin extends Plugin {
|
|||||||
if (params[0].signum() == 0) {
|
if (params[0].signum() == 0) {
|
||||||
return fromInt(params[0].getClass(), 1);
|
return fromInt(params[0].getClass(), 1);
|
||||||
}
|
}
|
||||||
|
NumberInterface one = fromInt(params[0].getClass(), 1);
|
||||||
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 ((multiplier = multiplier.subtract(one)).signum() == 1) {
|
||||||
factorial = factorial.multiply(multiplier);
|
factorial = factorial.multiply(multiplier);
|
||||||
}
|
}
|
||||||
return factorial;
|
return factorial;
|
||||||
@@ -136,6 +138,51 @@ public class StandardPlugin extends Plugin {
|
|||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
/**
|
||||||
|
* The permutation operator.
|
||||||
|
*/
|
||||||
|
public static final Operator OP_NPR = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0, new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 2 && params[0].fractionalPart().signum() == 0
|
||||||
|
&& params[1].fractionalPart().signum() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(params[0].compareTo(params[1]) < 0 ||
|
||||||
|
params[0].signum() < 0 ||
|
||||||
|
(params[0].signum() == 0 && params[1].signum() != 0)) return fromInt(params[0].getClass(), 0);
|
||||||
|
NumberInterface total = fromInt(params[0].getClass(), 1);
|
||||||
|
NumberInterface multiplyBy = params[0];
|
||||||
|
NumberInterface remainingMultiplications = params[1];
|
||||||
|
NumberInterface halfway = params[0].divide(fromInt(params[0].getClass(), 2));
|
||||||
|
if(remainingMultiplications.compareTo(halfway) > 0){
|
||||||
|
remainingMultiplications = params[0].subtract(remainingMultiplications);
|
||||||
|
}
|
||||||
|
while(remainingMultiplications.signum() > 0){
|
||||||
|
total = total.multiply(multiplyBy);
|
||||||
|
remainingMultiplications = remainingMultiplications.subtract(fromInt(params[0].getClass(), 1));
|
||||||
|
multiplyBy = multiplyBy.subtract(fromInt(params[0].getClass(), 1));
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
/**
|
||||||
|
* The combination operator.
|
||||||
|
*/
|
||||||
|
public static final Operator OP_NCR = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0, new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 2 && params[0].fractionalPart().signum() == 0
|
||||||
|
&& params[1].fractionalPart().signum() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return OP_NPR.getFunction().apply(params).divide(OP_FACTORIAL.getFunction().apply(params[1]));
|
||||||
|
}
|
||||||
|
});
|
||||||
/**
|
/**
|
||||||
* The absolute value function, abs(-3) = 3
|
* The absolute value function, abs(-3) = 3
|
||||||
*/
|
*/
|
||||||
@@ -147,7 +194,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
|
return params[0].multiply(fromInt(params[0].getClass(), params[0].signum()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -156,31 +203,32 @@ public class StandardPlugin extends Plugin {
|
|||||||
public static final Function FUNCTION_LN = new Function() {
|
public static final Function FUNCTION_LN = new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 1 && params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) > 0;
|
return params.length == 1 && params[0].compareTo(fromInt(params[0].getClass(), 0)) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
NumberInterface param = params[0];
|
NumberInterface param = params[0];
|
||||||
|
NumberInterface one = fromInt(param.getClass(), 1);
|
||||||
int powersOf2 = 0;
|
int powersOf2 = 0;
|
||||||
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
|
while (FUNCTION_ABS.apply(param.subtract(one)).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) {
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
if (param.subtract(one).signum() == 1) {
|
||||||
param = param.divide(fromInt(param.getClass(), 2));
|
param = param.divide(fromInt(param.getClass(), 2));
|
||||||
powersOf2++;
|
powersOf2++;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
if (param.subtract(one).signum() != 1) {
|
||||||
break;
|
break;
|
||||||
//No infinite loop for you.
|
//No infinite loop for you.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
param = param.multiply(fromInt(param.getClass(), 2));
|
param = param.multiply(fromInt(param.getClass(), 2));
|
||||||
powersOf2--;
|
powersOf2--;
|
||||||
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != -1) {
|
if (param.subtract(one).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));
|
return getLog2(param).multiply(fromInt(param.getClass(), powersOf2)).add(getLogPartialSum(param));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,14 +239,14 @@ 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(fromInt(x.getClass(), 1)); //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) {
|
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
||||||
n++;
|
n++;
|
||||||
currentNumerator = currentNumerator.multiply(x).negate();
|
currentNumerator = currentNumerator.multiply(x).negate();
|
||||||
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
|
currentTerm = currentNumerator.divide(fromInt(x.getClass(), n));
|
||||||
sum = sum.add(currentTerm);
|
sum = sum.add(currentTerm);
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
@@ -210,19 +258,20 @@ 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.
|
||||||
//a is also an error bound.
|
//a is also an error bound.
|
||||||
NumberInterface a = fromInt(number.getClass(), 1), b = a, c = a;
|
NumberInterface a = fromInt(number.getClass(), 1), b = a, c = a;
|
||||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
NumberInterface sum = fromInt(number.getClass(), 0);
|
||||||
|
NumberInterface one = fromInt(number.getClass(), 1);
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while (a.compareTo(maxError) >= 1) {
|
while (a.compareTo(maxError) >= 1) {
|
||||||
n++;
|
n++;
|
||||||
a = a.divide(fromInt(number.getClass(), 3));
|
a = a.divide(fromInt(number.getClass(), 3));
|
||||||
b = b.divide(fromInt(number.getClass(), 4));
|
b = b.divide(fromInt(number.getClass(), 4));
|
||||||
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
|
c = one.divide(fromInt(number.getClass(), n));
|
||||||
sum = sum.add(a.add(b).multiply(c));
|
sum = sum.add(a.add(b).multiply(c));
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
@@ -242,6 +291,20 @@ public class StandardPlugin extends Plugin {
|
|||||||
return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* Gets a random number smaller or equal to the given number's integer value.
|
||||||
|
*/
|
||||||
|
public static final Function FUNCTION_RAND_INT = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return fromInt(params[0].getClass(), (int) Math.round(Math.random() * params[0].floor().intValue()));
|
||||||
|
}
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* The implementation for double-based naive numbers.
|
* The implementation for double-based naive numbers.
|
||||||
*/
|
*/
|
||||||
@@ -281,10 +344,10 @@ public class StandardPlugin extends Plugin {
|
|||||||
.negate();
|
.negate();
|
||||||
for (int i = 0; i < termsNeeded; i++) {
|
for (int i = 0; i < termsNeeded; i++) {
|
||||||
M = M
|
M = M
|
||||||
.multiply(new NaiveNumber(12 * i + 2).promoteTo(PreciseNumber.class))
|
.multiply(new PreciseNumber((12 * i + 2) + ""))
|
||||||
.multiply(new NaiveNumber(12 * i + 6).promoteTo(PreciseNumber.class))
|
.multiply(new PreciseNumber((12 * i + 6) + ""))
|
||||||
.multiply(new NaiveNumber(12 * i + 10).promoteTo(PreciseNumber.class))
|
.multiply(new PreciseNumber((12 * i + 10) + ""))
|
||||||
.divide(new NaiveNumber(Math.pow(i + 1, 3)).promoteTo(PreciseNumber.class));
|
.divide(new PreciseNumber(Math.pow(i + 1, 3) + ""));
|
||||||
L = L.add(lSummand);
|
L = L.add(lSummand);
|
||||||
X = X.multiply(xMultiplier);
|
X = X.multiply(xMultiplier);
|
||||||
sum = sum.add(M.multiply(L).divide(X));
|
sum = sum.add(M.multiply(L).divide(X));
|
||||||
@@ -304,20 +367,15 @@ 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.
|
||||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
NumberInterface sum = fromInt(params[0].getClass(), 1);
|
||||||
NumberInterface nextNumerator = params[0];
|
NumberInterface nextNumerator = params[0];
|
||||||
NumberInterface left = params[0].multiply(fromInt(params[0].getClass(), 3).intPow(params[0].ceiling().intValue())), right = maxError;
|
NumberInterface left = params[0].multiply(fromInt(params[0].getClass(), 3).intPow(params[0].ceiling().intValue())), right = maxError;
|
||||||
do {
|
do {
|
||||||
@@ -325,7 +383,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
n++;
|
n++;
|
||||||
nextNumerator = nextNumerator.multiply(params[0]);
|
nextNumerator = nextNumerator.multiply(params[0]);
|
||||||
left = left.multiply(params[0]);
|
left = left.multiply(params[0]);
|
||||||
NumberInterface nextN = (new NaiveNumber(n + 1)).promoteTo(params[0].getClass());
|
NumberInterface nextN = fromInt(params[0].getClass(), n + 1);
|
||||||
right = right.multiply(nextN);
|
right = right.multiply(nextN);
|
||||||
//System.out.println(left + ", " + right);
|
//System.out.println(left + ", " + right);
|
||||||
}
|
}
|
||||||
@@ -341,21 +399,23 @@ public class StandardPlugin extends Plugin {
|
|||||||
public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
NumberInterface zero = fromInt(params[0].getClass(), 0);
|
||||||
return params.length == 2
|
return params.length == 2
|
||||||
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
&& !(params[0].compareTo(zero) == 0
|
||||||
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0)
|
&& params[1].compareTo(zero) == 0)
|
||||||
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) != 0);
|
&& !(params[0].signum() == -1 && params[1].fractionalPart().compareTo(zero) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
if (params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
NumberInterface zero = fromInt(params[0].getClass(), 0);
|
||||||
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
if (params[0].compareTo(zero) == 0)
|
||||||
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
return zero;
|
||||||
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
else if (params[1].compareTo(zero) == 0)
|
||||||
|
return fromInt(params[0].getClass(), 1);
|
||||||
//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));
|
||||||
@@ -427,7 +487,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionCos.apply(params[0]));
|
return fromInt(params[0].getClass(), 1).divide(functionCos.apply(params[0]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -441,7 +501,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return NaiveNumber.ONE.promoteTo(params[0].getClass()).divide(functionSin.apply(params[0]));
|
return fromInt(params[0].getClass(), 1).divide(functionSin.apply(params[0]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@@ -472,23 +532,13 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return the value of the partial sum that has the same class as x.
|
* @return the value of the partial sum that has the same class as x.
|
||||||
*/
|
*/
|
||||||
private static NumberInterface sumSeries(NumberInterface x, BiFunction<Integer, NumberInterface, NumberInterface> nthTermFunction, int n) {
|
private static NumberInterface sumSeries(NumberInterface x, BiFunction<Integer, NumberInterface, NumberInterface> nthTermFunction, int n) {
|
||||||
NumberInterface sum = NaiveNumber.ZERO.promoteTo(x.getClass());
|
NumberInterface sum = fromInt(x.getClass(), 0);
|
||||||
for (int i = 0; i <= n; i++) {
|
for (int i = 0; i <= n; i++) {
|
||||||
sum = sum.add(nthTermFunction.apply(i, x));
|
sum = sum.add(nthTermFunction.apply(i, x));
|
||||||
}
|
}
|
||||||
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.
|
||||||
@@ -500,13 +550,13 @@ 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 (!FACTORIAL_LISTS.containsKey(numberClass)) {
|
if (!FACTORIAL_LISTS.containsKey(numberClass)) {
|
||||||
FACTORIAL_LISTS.put(numberClass, new ArrayList<>());
|
FACTORIAL_LISTS.put(numberClass, new ArrayList<>());
|
||||||
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
FACTORIAL_LISTS.get(numberClass).add(fromInt(numberClass, 1));
|
||||||
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
FACTORIAL_LISTS.get(numberClass).add(fromInt(numberClass, 1));
|
||||||
}
|
}
|
||||||
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass);
|
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass);
|
||||||
if (n >= list.size()) {
|
if (n >= list.size()) {
|
||||||
while (list.size() < n + 16) {
|
while (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(fromInt(numberClass, list.size())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list.get(n);
|
return list.get(n);
|
||||||
@@ -520,7 +570,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;
|
||||||
@@ -538,7 +588,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return theta in [0, 2pi) that differs from phi by a multiple of 2pi.
|
* @return theta in [0, 2pi) that differs from phi by a multiple of 2pi.
|
||||||
*/
|
*/
|
||||||
private static NumberInterface getSmallAngle(NumberInterface phi, NumberInterface pi) {
|
private static NumberInterface getSmallAngle(NumberInterface phi, NumberInterface pi) {
|
||||||
NumberInterface twoPi = pi.multiply(new NaiveNumber("2").promoteTo(phi.getClass()));
|
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2));
|
||||||
NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi
|
NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi
|
||||||
.multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi).
|
.multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi).
|
||||||
if (phi.signum() < 0) {
|
if (phi.signum() < 0) {
|
||||||
@@ -576,6 +626,9 @@ public class StandardPlugin extends Plugin {
|
|||||||
registerOperator("^", OP_CARET);
|
registerOperator("^", OP_CARET);
|
||||||
registerOperator("!", OP_FACTORIAL);
|
registerOperator("!", OP_FACTORIAL);
|
||||||
|
|
||||||
|
registerOperator("nPr", OP_NPR);
|
||||||
|
registerOperator("nCr", OP_NCR);
|
||||||
|
|
||||||
registerFunction("abs", FUNCTION_ABS);
|
registerFunction("abs", FUNCTION_ABS);
|
||||||
registerFunction("exp", FUNCTION_EXP);
|
registerFunction("exp", FUNCTION_EXP);
|
||||||
registerFunction("ln", FUNCTION_LN);
|
registerFunction("ln", FUNCTION_LN);
|
||||||
@@ -587,6 +640,8 @@ public class StandardPlugin extends Plugin {
|
|||||||
registerFunction("csc", functionCsc);
|
registerFunction("csc", functionCsc);
|
||||||
registerFunction("cot", functionCot);
|
registerFunction("cot", functionCot);
|
||||||
|
|
||||||
|
registerFunction("random_int", FUNCTION_RAND_INT);
|
||||||
|
|
||||||
registerDocumentation(new Documentation("abs", "Absolute Value", "Finds the distance " +
|
registerDocumentation(new Documentation("abs", "Absolute Value", "Finds the distance " +
|
||||||
"from zero of a number.", "Given a number, this function finds the distance form " +
|
"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" +
|
"zero of a number, effectively turning negative numbers into positive ones.\n\n" +
|
||||||
@@ -615,6 +670,12 @@ public class StandardPlugin extends Plugin {
|
|||||||
"in radians.", "", DocumentationType.FUNCTION));
|
"in radians.", "", DocumentationType.FUNCTION));
|
||||||
registerDocumentation(new Documentation("cot", "Cotangent", "Computes the cotangent of the given angle, " +
|
registerDocumentation(new Documentation("cot", "Cotangent", "Computes the cotangent of the given angle, " +
|
||||||
"in radians.", "", DocumentationType.FUNCTION));
|
"in radians.", "", DocumentationType.FUNCTION));
|
||||||
|
registerDocumentation(new Documentation("random_int", "Random Integer", "Generates a random integer [0, n].",
|
||||||
|
"Generates a pseudorandom number using the standard JVM random mechanism, keeping it less than or " +
|
||||||
|
"equal to the given number.\n\n" +
|
||||||
|
"Example: random_int(5) -> 4\n" +
|
||||||
|
"random_int(5) -> 3\n" +
|
||||||
|
"random_int(5) -> 3\n", DocumentationType.FUNCTION));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface used to reduce a tree into a single value.
|
|
||||||
*
|
|
||||||
* @param <T> the value to reduce into.
|
|
||||||
*/
|
|
||||||
public interface Reducer<T> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reduces the given tree into a single value of type T.
|
|
||||||
*
|
|
||||||
* @param node the node being passed in to be reduced.
|
|
||||||
* @param children the already-reduced children of this node.
|
|
||||||
* @return the resulting value from the reduce.
|
|
||||||
*/
|
|
||||||
public T reduceNode(TreeNode node, Object... children);
|
|
||||||
|
|
||||||
}
|
|
||||||
32
src/main/kotlin/org/nwapw/abacus/fx/HistoryModel.kt
Normal file
32
src/main/kotlin/org/nwapw/abacus/fx/HistoryModel.kt
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package org.nwapw.abacus.fx
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleStringProperty
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A model representing an input / output in the calculator.
|
||||||
|
*
|
||||||
|
* The HistoryModel class stores a record of a single user-provided input,
|
||||||
|
* its parsed form as it was interpreted by the calculator, and the output
|
||||||
|
* that was provided by the calculator. These are represented as properties
|
||||||
|
* to allow easy access by JavaFX cells.
|
||||||
|
*
|
||||||
|
* @param input the user input
|
||||||
|
* @param parsed the parsed version of the input.
|
||||||
|
* @param output the output string.
|
||||||
|
*/
|
||||||
|
class HistoryModel(input: String, parsed: String, output: String){
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property that holds the input.
|
||||||
|
*/
|
||||||
|
val inputProperty = SimpleStringProperty(input)
|
||||||
|
/**
|
||||||
|
* The property that holds the parsed input.
|
||||||
|
*/
|
||||||
|
val parsedProperty = SimpleStringProperty(parsed)
|
||||||
|
/**
|
||||||
|
* The property that holds the output.
|
||||||
|
*/
|
||||||
|
val outputProperty = SimpleStringProperty(output)
|
||||||
|
|
||||||
|
}
|
||||||
31
src/main/kotlin/org/nwapw/abacus/fx/ToggleablePlugin.kt
Normal file
31
src/main/kotlin/org/nwapw/abacus/fx/ToggleablePlugin.kt
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package org.nwapw.abacus.fx
|
||||||
|
|
||||||
|
import javafx.beans.property.SimpleBooleanProperty
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A model representing a plugin that can be disabled or enabled.
|
||||||
|
*
|
||||||
|
* ToggleablePlugin is a model that is used to present to the user the option
|
||||||
|
* of disabling / enabling plugins. The class name in this plugin is stored if
|
||||||
|
* its "enabledPropery" is false, essentially blacklisting the plugin.
|
||||||
|
*
|
||||||
|
* @param className the name of the class that this model concerns.
|
||||||
|
* @param enabled whether or not the model should start enabled.
|
||||||
|
*/
|
||||||
|
class ToggleablePlugin (val className: String, enabled: Boolean) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property used to interact with JavaFX components.
|
||||||
|
*/
|
||||||
|
val enabledProperty = SimpleBooleanProperty(enabled)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether this plugin is currently enabled or not.
|
||||||
|
*
|
||||||
|
* @return true if it is enabled, false otherwise.
|
||||||
|
*/
|
||||||
|
fun isEnabled(): Boolean {
|
||||||
|
return enabledProperty.value
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ data class FunctionNode(val function: String) : TreeNode() {
|
|||||||
val children: MutableList<TreeNode> = mutableListOf()
|
val children: MutableList<TreeNode> = mutableListOf()
|
||||||
|
|
||||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||||
val children = Array<Any?>(children.size, { children[it].reduce(reducer) ?: return null; })
|
val children = Array<Any>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||||
return reducer.reduceNode(this, *children)
|
return reducer.reduceNode(this, *children)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
src/main/kotlin/org/nwapw/abacus/tree/Reducer.kt
Normal file
19
src/main/kotlin/org/nwapw/abacus/tree/Reducer.kt
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package org.nwapw.abacus.tree
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reducer interface that takes a tree and returns a single value.
|
||||||
|
*
|
||||||
|
* The reducer walks the tree, visiting the children first, converting them into
|
||||||
|
* a value, and then attempts to reduce the parent. Eventually, the single final value is returned.
|
||||||
|
*/
|
||||||
|
interface Reducer <out T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces the given tree node, given its already reduced children.
|
||||||
|
*
|
||||||
|
* @param treeNode the tree node to reduce.
|
||||||
|
* @param children the list of children, of type T.
|
||||||
|
*/
|
||||||
|
fun reduceNode(treeNode: TreeNode, vararg children: Any) : T?
|
||||||
|
|
||||||
|
}
|
||||||
@@ -57,6 +57,7 @@
|
|||||||
<Button text="Apply" onAction="#performSave"/>
|
<Button text="Apply" onAction="#performSave"/>
|
||||||
<Button text="Reload Plugins" onAction="#performReload"/>
|
<Button text="Reload Plugins" onAction="#performReload"/>
|
||||||
<Button text="Apply and Reload" onAction="#performSaveAndReload"/>
|
<Button text="Apply and Reload" onAction="#performSaveAndReload"/>
|
||||||
|
<Button text="Scan Plugins" onAction="#performScan"/>
|
||||||
</FlowPane>
|
</FlowPane>
|
||||||
</GridPane>
|
</GridPane>
|
||||||
</Tab>
|
</Tab>
|
||||||
|
|||||||
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