2017-07-25 21:50:41 -07:00
|
|
|
package org.nwapw.abacus.plugin;
|
2017-07-24 13:44:38 -07:00
|
|
|
|
2017-07-25 21:57:14 -07:00
|
|
|
import org.nwapw.abacus.function.Function;
|
2017-07-27 14:15:45 -07:00
|
|
|
import org.nwapw.abacus.function.Operator;
|
|
|
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
2017-07-28 10:26:25 -07:00
|
|
|
import org.nwapw.abacus.function.OperatorType;
|
2017-07-25 21:50:41 -07:00
|
|
|
import org.nwapw.abacus.number.NaiveNumber;
|
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
2017-07-28 22:17:22 -07:00
|
|
|
import org.nwapw.abacus.number.PreciseNumber;
|
2017-07-24 13:44:38 -07:00
|
|
|
|
2017-08-01 15:36:54 -07:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2017-07-31 14:49:25 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
2017-07-26 15:26:06 -07:00
|
|
|
import java.util.function.BiFunction;
|
|
|
|
|
2017-07-26 10:10:37 -07:00
|
|
|
/**
|
|
|
|
* The plugin providing standard functions such as addition and subtraction to
|
|
|
|
* the calculator.
|
|
|
|
*/
|
2017-07-25 21:50:41 -07:00
|
|
|
public class StandardPlugin extends Plugin {
|
2017-07-24 13:44:38 -07:00
|
|
|
|
2017-07-31 14:49:25 -07:00
|
|
|
private static HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> factorialLists = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
2017-08-01 15:36:54 -07:00
|
|
|
public final static String PI_STR = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200056812714526356082778577134275778960917363717872146844090122495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837297804995105973173281609631859502445945534690830264252230825334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115956286388235378759375195778185778053217122680661300192787661119590921642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151557485724245415069595082953311686172785588907509838175463746493931925506040092770167113900984882401285836160356370766010471018194295559619894676783744944825537977472684710404753464620804668425906949129331367702898915210475216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992458631503028618297455570674983850549458858692699569092721079750930295532116534498720275596023648066549911988183479775356636980742654252786255181841757467289097777279380008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179049460165346680498862723279178608578438382796797668145410095388378636095068006422512520511739298489608412848862694560424196528502221066118630674427862203919494504712371378696095636437191728746776465757396241389086583264599581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745530506820349625245174939965143142980919065925093722169646151570985838741059788595977297549893016175392846813826868386894277415599185592524595395943104997252468084598727364469584865383673622262609912460805124388439045124413654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767889525213852254995466672782398645659611635488623057745649803559363456817432411251507606947945109659609402522887971089314566913686722874894056010150330861792868092087476091782493858900971490967598526136554978189312978482168299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610213596953623144295248493718711014576540359027993440374200731057853906219838744780847848968332144571386875194350643021845319104848100537061468067491927819119793995206141966342875444064374512371819217999839101591956181467514269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672218256259966150142150306803844773454920260541466592520149744285073251866600213243408819071048633173464965145390579626856100550810665879699816357473638405257145910289706414011097120628043903975951567715770042033786993600723055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816909152801735067127485832228718352093539657251210835791513698820914442100675103346711031412671113699086585163983150197016515116851714376576183515565088490998985998238734552833163550764791853589322618548963213293308985706420467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325974636673058360414281388303203824903758985243744170291327656180937734440307074692112019130203303801976211011004492932151608424448596376698389522868478312355265821314495768572624334418930396864262434107732269780280731891544110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647
|
|
|
|
static HashMap<Class<? extends NumberInterface>, NumberInterface> piValues = new HashMap<Class<? extends NumberInterface>, NumberInterface>();
|
2017-07-31 14:49:25 -07:00
|
|
|
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The addition operator, +
|
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Operator OP_ADD = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length >= 1;
|
|
|
|
}
|
2017-07-24 14:48:16 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
NumberInterface sum = params[0];
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int i = 1; i < params.length; i++) {
|
2017-07-30 21:10:11 -07:00
|
|
|
sum = sum.add(params[i]);
|
2017-07-24 14:48:16 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
});
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The subtraction operator, -
|
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Operator OP_SUBTRACT = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 2;
|
|
|
|
}
|
2017-07-24 14:48:16 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
return params[0].subtract(params[1]);
|
|
|
|
}
|
|
|
|
});
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The multiplication operator, *
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public static final Operator OP_MULTIPLY = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length >= 1;
|
|
|
|
}
|
2017-07-24 14:48:16 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
NumberInterface product = params[0];
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int i = 1; i < params.length; i++) {
|
2017-07-30 21:10:11 -07:00
|
|
|
product = product.multiply(params[i]);
|
2017-07-24 14:48:16 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return product;
|
|
|
|
}
|
|
|
|
});
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The division operator, /
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public static final Operator OP_DIVIDE = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1, new Function() {
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length >= 1;
|
|
|
|
}
|
2017-07-24 14:48:16 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
NumberInterface product = params[0];
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int i = 1; i < params.length; i++) {
|
2017-07-30 21:10:11 -07:00
|
|
|
product = product.multiply(params[i]);
|
2017-07-24 14:48:16 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return product;
|
|
|
|
}
|
|
|
|
});
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The factorial operator, !
|
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Operator OP_FACTORIAL = new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() {
|
|
|
|
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 1;
|
|
|
|
}
|
2017-07-27 16:55:18 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
2017-07-30 21:11:32 -07:00
|
|
|
if (params[0].signum() == 0) {
|
2017-07-30 21:10:11 -07:00
|
|
|
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
2017-07-27 16:55:18 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
NumberInterface factorial = params[0];
|
|
|
|
NumberInterface multiplier = params[0];
|
|
|
|
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
2017-07-30 21:11:32 -07:00
|
|
|
while ((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1) {
|
2017-07-30 21:10:11 -07:00
|
|
|
factorial = factorial.multiply(multiplier);
|
2017-07-25 11:12:25 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return factorial;
|
2017-07-27 13:39:19 -07:00
|
|
|
/*if(!storedList.containsKey(params[0].getClass())){
|
|
|
|
storedList.put(params[0].getClass(), new ArrayList<NumberInterface>());
|
|
|
|
storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass()));
|
|
|
|
storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass()));
|
|
|
|
}*/
|
2017-07-30 21:10:11 -07:00
|
|
|
}
|
|
|
|
});
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The caret / pow operator, ^
|
|
|
|
*/
|
2017-07-30 21:12:50 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
return FUNCTION_EXP.apply(FUNCTION_LN.apply(params[0]).multiply(params[1]));
|
|
|
|
}
|
|
|
|
});
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
|
|
|
* The absolute value function, abs(-3) = 3
|
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Function FUNCTION_ABS = new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 1;
|
|
|
|
}
|
2017-07-26 15:26:06 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
|
|
|
|
}
|
|
|
|
};
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
2017-07-31 10:28:39 -07:00
|
|
|
* The exponential function, exp(1) = e^1 = 2.71...
|
2017-07-30 21:15:01 -07:00
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Function FUNCTION_EXP = new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 1;
|
|
|
|
}
|
2017-07-27 13:04:41 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
2017-07-31 14:49:25 -07:00
|
|
|
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());
|
2017-07-31 22:56:55 -07:00
|
|
|
NumberInterface nextNumerator = params[0];
|
|
|
|
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
|
2017-07-31 14:49:25 -07:00
|
|
|
do{
|
2017-07-31 22:56:55 -07:00
|
|
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
2017-07-31 14:49:25 -07:00
|
|
|
n++;
|
2017-07-31 22:56:55 -07:00
|
|
|
nextNumerator = nextNumerator.multiply(params[0]);
|
2017-07-31 14:49:25 -07:00
|
|
|
left = left.multiply(params[0]);
|
2017-07-31 22:56:55 -07:00
|
|
|
NumberInterface nextN = (new NaiveNumber(n+1)).promoteTo(params[0].getClass());
|
2017-07-31 14:49:25 -07:00
|
|
|
right = right.multiply(nextN);
|
2017-07-31 22:56:55 -07:00
|
|
|
//System.out.println(left + ", " + right);
|
2017-07-31 14:49:25 -07:00
|
|
|
}
|
|
|
|
while(left.compareTo(right) > 0);
|
2017-07-31 23:09:11 -07:00
|
|
|
//System.out.println(n+1);
|
2017-07-31 14:49:25 -07:00
|
|
|
return sum;
|
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
}
|
|
|
|
};
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
2017-07-31 14:49:25 -07:00
|
|
|
* The natural log function.
|
2017-07-30 21:15:01 -07:00
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Function FUNCTION_LN = new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 1;
|
|
|
|
}
|
2017-07-27 13:04:41 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
NumberInterface param = params[0];
|
|
|
|
int powersOf2 = 0;
|
2017-07-30 21:11:32 -07:00
|
|
|
while (FUNCTION_ABS.apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 0) {
|
|
|
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
2017-07-30 21:10:11 -07:00
|
|
|
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
|
|
|
powersOf2++;
|
2017-07-30 21:11:32 -07:00
|
|
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
2017-07-30 21:10:11 -07:00
|
|
|
break;
|
|
|
|
//No infinite loop for you.
|
2017-07-27 13:04:41 -07:00
|
|
|
}
|
2017-07-30 21:11:32 -07:00
|
|
|
} else {
|
2017-07-30 21:10:11 -07:00
|
|
|
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
|
|
|
|
powersOf2--;
|
2017-07-30 21:11:32 -07:00
|
|
|
if (param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
2017-07-30 21:10:11 -07:00
|
|
|
break;
|
|
|
|
//No infinite loop for you.
|
2017-07-27 13:04:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return getLog2(param).multiply((new NaiveNumber(powersOf2)).promoteTo(param.getClass())).add(getLogPartialSum(param));
|
|
|
|
}
|
2017-07-27 13:04:41 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
/**
|
|
|
|
* Returns the partial sum of the Taylor series for logx (around x=1).
|
|
|
|
* Automatically determines the number of terms needed based on the precision of x.
|
|
|
|
* @param x value at which the series is evaluated. 0 < x < 2. (x=2 is convergent but impractical.)
|
|
|
|
* @return the partial sum.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
private NumberInterface getLogPartialSum(NumberInterface x) {
|
2017-07-30 21:10:11 -07:00
|
|
|
NumberInterface maxError = getMaxError(x);
|
|
|
|
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
2017-07-31 12:39:56 -07:00
|
|
|
NumberInterface currentNumerator = x, currentTerm = x, sum = x;
|
2017-07-30 21:10:11 -07:00
|
|
|
int n = 1;
|
2017-07-30 21:11:32 -07:00
|
|
|
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
2017-07-30 21:10:11 -07:00
|
|
|
n++;
|
2017-07-31 12:39:56 -07:00
|
|
|
currentNumerator = currentNumerator.multiply(x).negate();
|
|
|
|
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
|
2017-07-30 21:10:11 -07:00
|
|
|
sum = sum.add(currentTerm);
|
2017-07-27 13:04:41 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return sum;
|
|
|
|
}
|
2017-07-27 13:04:41 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
/**
|
|
|
|
* Returns natural log of 2 to the required precision of the class of number.
|
|
|
|
* @param number a number of the same type as the return type. (Used for precision.)
|
|
|
|
* @return the value of log(2) with the appropriate precision.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
private NumberInterface getLog2(NumberInterface number) {
|
2017-07-30 21:10:11 -07:00
|
|
|
NumberInterface maxError = getMaxError(number);
|
|
|
|
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
|
|
|
//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.
|
|
|
|
//a is also an error bound.
|
|
|
|
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
|
|
|
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
|
|
|
int n = 0;
|
2017-07-30 21:11:32 -07:00
|
|
|
while (a.compareTo(maxError) >= 1) {
|
2017-07-30 21:10:11 -07:00
|
|
|
n++;
|
|
|
|
a = a.divide((new NaiveNumber(3)).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()));
|
|
|
|
sum = sum.add(a.add(b).multiply(c));
|
2017-07-27 13:04:41 -07:00
|
|
|
}
|
2017-07-30 21:10:11 -07:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
};
|
2017-07-30 21:15:01 -07:00
|
|
|
/**
|
2017-07-31 14:53:41 -07:00
|
|
|
* The square root function.
|
2017-07-30 21:15:01 -07:00
|
|
|
*/
|
2017-07-30 21:10:11 -07:00
|
|
|
public static final Function FUNCTION_SQRT = new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 1;
|
|
|
|
}
|
2017-07-27 13:17:22 -07:00
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
return OP_CARET.getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
|
|
|
}
|
|
|
|
};
|
2017-07-27 13:47:51 -07:00
|
|
|
|
2017-08-01 15:36:54 -07:00
|
|
|
public static final Function FUNCTION_SIN = new Function() {
|
|
|
|
@Override
|
|
|
|
protected boolean matchesParams(NumberInterface[] params) {
|
|
|
|
return params.length == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
|
|
NumberInterface pi = getPi(params[0].getClass());
|
|
|
|
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
|
|
|
NumberInterface theta = getSmallAngle(params[0]);
|
|
|
|
if(theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0){
|
|
|
|
theta = theta.subtract(twoPi);
|
|
|
|
}
|
|
|
|
else if(theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0){
|
|
|
|
theta = pi.subtract(theta);
|
|
|
|
}
|
|
|
|
System.out.println(theta);
|
|
|
|
return sinTaylor(theta);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-30 21:10:11 -07:00
|
|
|
public StandardPlugin(PluginManager manager) {
|
|
|
|
super(manager);
|
|
|
|
}
|
|
|
|
|
2017-07-26 15:26:06 -07:00
|
|
|
/**
|
|
|
|
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
|
|
|
* @param x the value at which the series is evaluated.
|
2017-07-26 15:26:06 -07:00
|
|
|
* @param nthTermFunction the function that returns the nth term of the series, in the format term(x, n).
|
2017-07-30 21:11:32 -07:00
|
|
|
* @param n the number of terms in the partial sum.
|
2017-07-26 15:26:06 -07:00
|
|
|
* @return the value of the partial sum that has the same class as x.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
private static NumberInterface sumSeries(NumberInterface x, BiFunction<Integer, NumberInterface, NumberInterface> nthTermFunction, int n) {
|
2017-07-26 15:26:06 -07:00
|
|
|
NumberInterface sum = NaiveNumber.ZERO.promoteTo(x.getClass());
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int i = 0; i <= n; i++) {
|
2017-07-26 15:26:06 -07:00
|
|
|
sum = sum.add(nthTermFunction.apply(i, x));
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum error based on the precision of the class of number.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-26 15:26:06 -07:00
|
|
|
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
2017-07-27 14:06:25 -07:00
|
|
|
* @return the maximum error.
|
2017-07-26 15:26:06 -07:00
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
private static NumberInterface getMaxError(NumberInterface number) {
|
2017-07-28 20:04:13 -07:00
|
|
|
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.getMaxPrecision());
|
2017-07-26 15:26:06 -07:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:11:32 -07:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
|
|
|
registerNumber("naive", NaiveNumber.class);
|
|
|
|
registerNumber("precise", PreciseNumber.class);
|
|
|
|
|
|
|
|
registerOperator("+", OP_ADD);
|
|
|
|
registerOperator("-", OP_SUBTRACT);
|
|
|
|
registerOperator("*", OP_MULTIPLY);
|
|
|
|
registerOperator("/", OP_DIVIDE);
|
|
|
|
registerOperator("^", OP_CARET);
|
|
|
|
registerOperator("!", OP_FACTORIAL);
|
|
|
|
|
|
|
|
registerFunction("abs", FUNCTION_ABS);
|
|
|
|
registerFunction("exp", FUNCTION_EXP);
|
|
|
|
registerFunction("ln", FUNCTION_LN);
|
|
|
|
registerFunction("sqrt", FUNCTION_SQRT);
|
2017-08-01 15:36:54 -07:00
|
|
|
registerFunction("sin", FUNCTION_SIN);
|
2017-07-30 21:11:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-31 14:49:25 -07:00
|
|
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
|
|
|
if(!factorialLists.containsKey(numberClass)){
|
|
|
|
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
|
|
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
|
|
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
|
|
|
}
|
|
|
|
ArrayList<NumberInterface> list = factorialLists.get(numberClass);
|
|
|
|
if(n >= list.size()){
|
|
|
|
while(list.size() < n + 16){
|
|
|
|
list.add(list.get(list.size()-1).multiply(new NaiveNumber(list.size()).promoteTo(numberClass)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list.get(n);
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:36:54 -07:00
|
|
|
/**
|
|
|
|
* Returns the value of the Taylor series for sin (centered at 0) at x.
|
|
|
|
* @param x where the series is evaluated.
|
|
|
|
* @return the value of the series
|
|
|
|
*/
|
|
|
|
private static NumberInterface sinTaylor(NumberInterface x){
|
|
|
|
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
|
|
|
NumberInterface maxError = getMaxError(x);
|
|
|
|
int n = 1;
|
|
|
|
do{
|
|
|
|
n += 2;
|
|
|
|
power = power.multiply(multiplier);
|
|
|
|
currentTerm = power.divide(factorial(x.getClass(), n));
|
|
|
|
sum = sum.add(currentTerm);
|
|
|
|
} while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0);
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an approximation of Pi, with appropriate accuracy for given number class.
|
|
|
|
* @param numClass type of number.
|
|
|
|
* @return A number of class numClass, with value approximately Pi = 3.1415...
|
|
|
|
*/
|
|
|
|
public static NumberInterface getPi(Class<? extends NumberInterface> numClass){
|
|
|
|
if(!piValues.containsKey(numClass)){
|
|
|
|
int precision = NaiveNumber.ZERO.promoteTo(numClass).getMaxPrecision();
|
|
|
|
try {
|
|
|
|
piValues.put(numClass, numClass.getConstructor(String.class).newInstance(PI_STR.
|
|
|
|
substring(0, precision+3)));
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return piValues.get(numClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an equivalent angle in the interval [0, 2pi)
|
|
|
|
* @param phi an angle (in radians).
|
|
|
|
* @return theta in [0, 2pi) that differs from phi by a multiple of 2pi.
|
|
|
|
*/
|
|
|
|
private static NumberInterface getSmallAngle(NumberInterface phi){
|
|
|
|
NumberInterface twoPi = getPi(phi.getClass()).multiply(new NaiveNumber("2").promoteTo(phi.getClass()));
|
|
|
|
NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi
|
|
|
|
.multiply(new NaiveNumber(FUNCTION_ABS.apply(phi).divide(twoPi).floor()).promoteTo(phi.getClass()))); //Now theta is in [0, 2pi).
|
|
|
|
if(phi.signum() < 0){
|
|
|
|
theta = twoPi.subtract(theta);
|
|
|
|
}
|
|
|
|
return theta;
|
|
|
|
}
|
2017-07-24 13:44:38 -07:00
|
|
|
}
|