1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Format code.

This commit is contained in:
Danila Fedorin 2017-08-04 14:29:24 -07:00
parent a7e84bc946
commit d1a084363e
6 changed files with 152 additions and 152 deletions

View File

@ -87,7 +87,7 @@ public class AbacusController implements PluginListener {
private ObservableList<String> numberImplementationOptions; private ObservableList<String> numberImplementationOptions;
/** /**
<<<<<<< HEAD * <<<<<<< HEAD
* The list of plugin objects that can be toggled on and off, * The list of plugin objects that can be toggled on and off,
* and, when reloaded, get added to the plugin manager's black list. * and, when reloaded, get added to the plugin manager's black list.
*/ */
@ -186,10 +186,10 @@ public class AbacusController implements PluginListener {
} }
@FXML @FXML
private void performCalculation(){ private void performCalculation() {
Runnable calculator = new Runnable(){ Runnable calculator = new Runnable() {
public void run() { public void run() {
if(delay>0) { if (delay > 0) {
Runnable timer = new Runnable() { Runnable timer = new Runnable() {
public void run() { public void run() {
long gap = (long) (delay * 1000); long gap = (long) (delay * 1000);
@ -207,16 +207,16 @@ public class AbacusController implements PluginListener {
Platform.runLater(() -> inputButton.setDisable(true)); Platform.runLater(() -> inputButton.setDisable(true));
TreeNode constructedTree = abacus.parseString(inputField.getText()); TreeNode constructedTree = abacus.parseString(inputField.getText());
if (constructedTree == null) { if (constructedTree == null) {
Platform.runLater(() ->outputText.setText(ERR_SYNTAX)); Platform.runLater(() -> outputText.setText(ERR_SYNTAX));
Platform.runLater(() -> inputButton.setDisable(false)); Platform.runLater(() -> inputButton.setDisable(false));
//return; //return;
}else { } else {
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree); NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
if (evaluatedNumber == null) { if (evaluatedNumber == null) {
if(Thread.currentThread().isInterrupted()){ if (Thread.currentThread().isInterrupted()) {
Platform.runLater(() -> outputText.setText(ERR_STOP)); Platform.runLater(() -> outputText.setText(ERR_STOP));
Platform.runLater(() -> inputButton.setDisable(false)); Platform.runLater(() -> inputButton.setDisable(false));
}else { } else {
Platform.runLater(() -> outputText.setText(ERR_EVAL)); Platform.runLater(() -> outputText.setText(ERR_EVAL));
Platform.runLater(() -> inputButton.setDisable(false)); Platform.runLater(() -> inputButton.setDisable(false));
//return; //return;
@ -232,14 +232,15 @@ public class AbacusController implements PluginListener {
calculating = false; calculating = false;
} }
}; };
if(!calculating) { if (!calculating) {
calcThread = new Thread(calculator); calcThread = new Thread(calculator);
calcThread.setName("calcThread"); calcThread.setName("calcThread");
calcThread.start(); calcThread.start();
} }
} }
@FXML @FXML
private void stopCalculation(){ private void stopCalculation() {
calcThread.interrupt(); calcThread.interrupt();
calculating = false; calculating = false;
//Platform.runLater(() ->inputButton.setDisable(false)); //Platform.runLater(() ->inputButton.setDisable(false));

View File

@ -29,12 +29,12 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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()) if (Thread.currentThread().isInterrupted())
return null; return null;
} }
return sum; return sum;
@ -51,7 +51,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return params[0].subtract(params[1]); return params[0].subtract(params[1]);
@ -68,7 +68,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return params[0].negate(); return params[0].negate();
} }
@ -84,12 +84,12 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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()) if (Thread.currentThread().isInterrupted())
return null; return null;
} }
return product; return product;
@ -106,7 +106,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return params[0].divide(params[1]); return params[0].divide(params[1]);
} }
@ -125,7 +125,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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());
@ -133,10 +133,10 @@ public class StandardPlugin extends Plugin {
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 (!Thread.currentThread().isInterrupted()&&(multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass())))!=null&&multiplier.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()) if (Thread.currentThread().isInterrupted())
return null; return null;
return factorial; return factorial;
/*if(!storedList.containsKey(params[0].getClass())){ /*if(!storedList.containsKey(params[0].getClass())){
@ -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 * The absolute value function, abs(-3) = 3
*/ */
public static final Function FUNCTION_ABS = new Function() { public static final Function FUNCTION_ABS = new Function() {
@ -183,53 +157,11 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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()));
} }
}; };
/**
* 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. * The natural log function.
*/ */
@ -241,30 +173,30 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
NumberInterface param = params[0]; NumberInterface param = params[0];
int powersOf2 = 0; int powersOf2 = 0;
NumberInterface check; NumberInterface check;
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) { 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) { 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 ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())))==null||check.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 ((check = param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())))==null||check.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.
} }
} }
} }
NumberInterface check2; 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) 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 check;
return null; return null;
} }
@ -277,21 +209,21 @@ public class StandardPlugin extends Plugin {
*/ */
private NumberInterface getLogPartialSum(NumberInterface x) { private NumberInterface getLogPartialSum(NumberInterface x) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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;
NumberInterface check; NumberInterface check;
while (!Thread.currentThread().isInterrupted()&&(check = FUNCTION_ABS.apply(currentTerm))!=null&&check.compareTo(maxError) > 0) { while (!Thread.currentThread().isInterrupted() && (check = FUNCTION_ABS.apply(currentTerm)) != null && check.compareTo(maxError) > 0) {
n++; n++;
if((currentNumerator = currentNumerator.multiply(x))==null||(currentNumerator = currentNumerator.negate())==null) if ((currentNumerator = currentNumerator.multiply(x)) == null || (currentNumerator = currentNumerator.negate()) == null)
return 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()) if (Thread.currentThread().isInterrupted())
return null; return null;
return sum; return sum;
} }
@ -302,7 +234,7 @@ public class StandardPlugin extends Plugin {
* @return the value of log(2) with the appropriate precision. * @return the value of log(2) with the appropriate precision.
*/ */
private NumberInterface getLog2(NumberInterface number) { private NumberInterface getLog2(NumberInterface number) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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());
@ -312,16 +244,16 @@ 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 (!Thread.currentThread().isInterrupted()&&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()));
NumberInterface check; NumberInterface check;
if(a==null||(check = a.add(b))==null||(check = check.multiply(c))==null||(sum = sum.add(check))==null) if (a == null || (check = a.add(b)) == null || (check = check.multiply(c)) == null || (sum = sum.add(check)) == null)
return null; return null;
} }
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return sum; return sum;
} }
@ -391,6 +323,74 @@ public class StandardPlugin extends Plugin {
} }
}; };
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>(); 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). * The sine function (the argument is interpreted in radians).
*/ */
@ -525,21 +525,21 @@ public class StandardPlugin extends Plugin {
* @param n non-negative integer. * @param n non-negative integer.
* @return a number of numClass with value n factorial. * @return a number of numClass with value n factorial.
*/ */
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){ public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
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(NaiveNumber.ONE.promoteTo(numberClass));
FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass)); FACTORIAL_LISTS.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
} }
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass); ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass);
if(n >= list.size()){ if (n >= list.size()) {
while(!Thread.currentThread().isInterrupted()&&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()) if (Thread.currentThread().isInterrupted())
return null; return null;
return list.get(n); return list.get(n);
} }
@ -579,6 +579,28 @@ public class StandardPlugin extends Plugin {
return theta; 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 @Override
public void onEnable() { public void onEnable() {
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE); registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
@ -608,26 +630,4 @@ public class StandardPlugin extends Plugin {
public void onDisable() { 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;
}
} }

View File

@ -92,13 +92,13 @@ public class BinaryNode extends TreeNode {
@Override @Override
public <T> T reduce(Reducer<T> reducer) { public <T> T reduce(Reducer<T> reducer) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; 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;
T a = reducer.reduceNode(this, leftReduce, rightReduce); T a = reducer.reduceNode(this, leftReduce, rightReduce);
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return a; return a;
} }

View File

@ -62,15 +62,15 @@ 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()) if (Thread.currentThread().isInterrupted())
return null; 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 (Thread.currentThread().isInterrupted()||reducedChildren[i] == null) return null; if (Thread.currentThread().isInterrupted() || reducedChildren[i] == null) return null;
} }
T a = reducer.reduceNode(this, reducedChildren); T a = reducer.reduceNode(this, reducedChildren);
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return a; return a;
} }

View File

@ -33,12 +33,12 @@ public class UnaryNode extends TreeNode {
@Override @Override
public <T> T reduce(Reducer<T> reducer) { public <T> T reduce(Reducer<T> reducer) {
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
Object reducedChild = applyTo.reduce(reducer); Object reducedChild = applyTo.reduce(reducer);
if (reducedChild == null) return null; if (reducedChild == null) return null;
T a = reducer.reduceNode(this, reducedChild); T a = reducer.reduceNode(this, reducedChild);
if(Thread.currentThread().isInterrupted()) if (Thread.currentThread().isInterrupted())
return null; return null;
return a; return a;
} }

View File

@ -2,14 +2,10 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?> <?import javafx.scene.control.*?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?> <?import javafx.scene.text.Text?>
<?import javafx.scene.layout.HBox?> <BorderPane xmlns:fx="http://javafx.com/fxml"
<?import javafx.scene.layout.GridPane?> xmlns="http://javafx.com/javafx"
<?import javafx.scene.layout.FlowPane?>
<BorderPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.nwapw.abacus.fx.AbacusController"> fx:controller="org.nwapw.abacus.fx.AbacusController">
<center> <center>
<TabPane fx:id="coreTabPane"> <TabPane fx:id="coreTabPane">
@ -46,13 +42,16 @@
</Tab> </Tab>
<Tab fx:id="settingsTab" text="Settings" closable="false"> <Tab fx:id="settingsTab" text="Settings" closable="false">
<GridPane hgap="10" vgap="10"> <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"/> <Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/> <ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
<ListView fx:id="enabledPluginView" <ListView fx:id="enabledPluginView"
GridPane.rowIndex="1" GridPane.columnIndex="0" GridPane.rowIndex="1" GridPane.columnIndex="0"
GridPane.columnSpan="2" maxHeight="100"/> 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="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"/>