mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-19 00:49:32 -08:00
Format code.
This commit is contained in:
parent
a7e84bc946
commit
d1a084363e
|
@ -87,7 +87,7 @@ public class AbacusController implements PluginListener {
|
|||
private ObservableList<String> numberImplementationOptions;
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* <<<<<<< HEAD
|
||||
* The list of plugin objects that can be toggled on and off,
|
||||
* and, when reloaded, get added to the plugin manager's black list.
|
||||
*/
|
||||
|
@ -238,6 +238,7 @@ public class AbacusController implements PluginListener {
|
|||
calcThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void stopCalculation() {
|
||||
calcThread.interrupt();
|
||||
|
|
|
@ -147,32 +147,6 @@ public class StandardPlugin extends Plugin {
|
|||
}
|
||||
});
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
* The caret / pow operator, ^
|
||||
*/
|
||||
public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2
|
||||
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
||||
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface check;
|
||||
if(Thread.currentThread().isInterrupted()) return null;
|
||||
else if (params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
||||
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
||||
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
||||
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
||||
if((check = FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0])))!=null&&(check = check.multiply(params[1]))!=null)
|
||||
return check;
|
||||
return null;
|
||||
}
|
||||
});
|
||||
/**
|
||||
* The absolute value function, abs(-3) = 3
|
||||
*/
|
||||
public static final Function FUNCTION_ABS = new Function() {
|
||||
|
@ -188,48 +162,6 @@ public class StandardPlugin extends Plugin {
|
|||
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
|
||||
}
|
||||
};
|
||||
/**
|
||||
* The exponential function, exp(1) = e^1 = 2.71...
|
||||
*/
|
||||
public static final Function FUNCTION_EXP = new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface maxError = getMaxError(params[0]);
|
||||
int n = 0;
|
||||
if (params[0].signum() <= 0) {
|
||||
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
||||
n++;
|
||||
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
||||
sum = sum.add(currentTerm);
|
||||
}
|
||||
return sum;
|
||||
} else {
|
||||
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
||||
//right and left refer to lhs and rhs in the above inequality.
|
||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||
NumberInterface nextNumerator = params[0];
|
||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
|
||||
do {
|
||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
|
||||
n++;
|
||||
nextNumerator = nextNumerator.multiply(params[0]);
|
||||
left = left.multiply(params[0]);
|
||||
NumberInterface nextN = (new NaiveNumber(n + 1)).promoteTo(params[0].getClass());
|
||||
right = right.multiply(nextN);
|
||||
//System.out.println(left + ", " + right);
|
||||
}
|
||||
while (left.compareTo(right) > 0);
|
||||
//System.out.println(n+1);
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* The natural log function.
|
||||
*/
|
||||
|
@ -391,6 +323,74 @@ public class StandardPlugin extends Plugin {
|
|||
}
|
||||
};
|
||||
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
|
||||
/**
|
||||
* The exponential function, exp(1) = e^1 = 2.71...
|
||||
*/
|
||||
public static final Function FUNCTION_EXP = new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface maxError = getMaxError(params[0]);
|
||||
int n = 0;
|
||||
if (params[0].signum() <= 0) {
|
||||
NumberInterface currentTerm = NaiveNumber.ONE.promoteTo(params[0].getClass()), sum = currentTerm;
|
||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
||||
n++;
|
||||
currentTerm = currentTerm.multiply(params[0]).divide((new NaiveNumber(n)).promoteTo(params[0].getClass()));
|
||||
sum = sum.add(currentTerm);
|
||||
}
|
||||
return sum;
|
||||
} else {
|
||||
//We need n such that x^(n+1) * 3^ceil(x) <= maxError * (n+1)!.
|
||||
//right and left refer to lhs and rhs in the above inequality.
|
||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||
NumberInterface nextNumerator = params[0];
|
||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
|
||||
do {
|
||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1)));
|
||||
n++;
|
||||
nextNumerator = nextNumerator.multiply(params[0]);
|
||||
left = left.multiply(params[0]);
|
||||
NumberInterface nextN = (new NaiveNumber(n + 1)).promoteTo(params[0].getClass());
|
||||
right = right.multiply(nextN);
|
||||
//System.out.println(left + ", " + right);
|
||||
}
|
||||
while (left.compareTo(right) > 0);
|
||||
//System.out.println(n+1);
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
};
|
||||
/**
|
||||
* <<<<<<< HEAD
|
||||
* =======
|
||||
* The caret / pow operator, ^
|
||||
*/
|
||||
public static final Operator OP_CARET = new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
return params.length == 2
|
||||
&& !(params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0
|
||||
&& params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[1].getClass())) == 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
NumberInterface check;
|
||||
if (Thread.currentThread().isInterrupted()) return null;
|
||||
else if (params[0].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
||||
return NaiveNumber.ZERO.promoteTo(params[0].getClass());
|
||||
else if (params[1].compareTo(NaiveNumber.ZERO.promoteTo(params[0].getClass())) == 0)
|
||||
return NaiveNumber.ONE.promoteTo(params[1].getClass());
|
||||
if ((check = FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]))) != null && (check = check.multiply(params[1])) != null)
|
||||
return check;
|
||||
return null;
|
||||
}
|
||||
});
|
||||
/**
|
||||
* The sine function (the argument is interpreted in radians).
|
||||
*/
|
||||
|
@ -579,6 +579,28 @@ public class StandardPlugin extends Plugin {
|
|||
return theta;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
|
||||
|
@ -608,26 +630,4 @@ public class StandardPlugin extends Plugin {
|
|||
public void onDisable() {
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,14 +2,10 @@
|
|||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.text.Text?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
<?import javafx.scene.layout.FlowPane?>
|
||||
<BorderPane xmlns="http://javafx.com/javafx"
|
||||
xmlns:fx="http://javafx.com/fxml"
|
||||
<BorderPane xmlns:fx="http://javafx.com/fxml"
|
||||
xmlns="http://javafx.com/javafx"
|
||||
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||
<center>
|
||||
<TabPane fx:id="coreTabPane">
|
||||
|
@ -46,13 +42,16 @@
|
|||
</Tab>
|
||||
<Tab fx:id="settingsTab" text="Settings" closable="false">
|
||||
<GridPane hgap="10" vgap="10">
|
||||
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
||||
<padding>
|
||||
<Insets left="10" right="10" top="10" bottom="10"/>
|
||||
</padding>
|
||||
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
||||
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
||||
<ListView fx:id="enabledPluginView"
|
||||
GridPane.rowIndex="1" GridPane.columnIndex="0"
|
||||
GridPane.columnSpan="2" maxHeight="100"/>
|
||||
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10" vgap="10">
|
||||
<FlowPane GridPane.columnIndex="0" GridPane.columnSpan="2" GridPane.rowIndex="2" hgap="10"
|
||||
vgap="10">
|
||||
<Button text="Apply" onAction="#performSave"/>
|
||||
<Button text="Reload Plugins" onAction="#performReload"/>
|
||||
<Button text="Apply and Reload" onAction="#performSaveAndReload"/>
|
||||
|
|
Loading…
Reference in New Issue
Block a user