Stop using fromInt and the promoteTo function.

This commit is contained in:
Danila Fedorin 2017-09-01 18:07:28 -07:00
parent 2b700d3911
commit e172108476
4 changed files with 84 additions and 160 deletions

View File

@ -114,15 +114,6 @@ public class NaiveNumber extends NumberInterface {
return (int) value; return (int) value;
} }
@Override
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
if (toClass == this.getClass()) return this;
else if (toClass == PreciseNumber.class) {
return new PreciseNumber(Double.toString(value));
}
return null;
}
public String toString() { public String toString() {
double shiftBy = Math.pow(10, 10); double shiftBy = Math.pow(10, 10);
return Double.toString(Math.round(value * shiftBy) / shiftBy); return Double.toString(Math.round(value * shiftBy) / shiftBy);

View File

@ -236,29 +236,6 @@ public abstract class NumberInterface {
*/ */
public abstract int intValue(); public abstract int intValue();
/**
* Promotes this class to another number class.
*
* @param toClass the class to promote to.
* @return the resulting new instance.
*/
@Deprecated
protected abstract NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass);
/**
* Promotes this class to another number class. Also, checks if the
* thread has been interrupted, and if so, throws
* an exception.
*
* @param toClass the class to promote to.
* @return the resulting new instance.
*/
@Deprecated
public final NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
checkInterrupted();
return promoteToInternal(toClass);
}
/** /**
* Returns the smallest error this instance can tolerate depending * Returns the smallest error this instance can tolerate depending
* on its precision and value. * on its precision and value.

View File

@ -152,14 +152,6 @@ public class PreciseNumber extends NumberInterface {
return new PreciseNumber(value.negate()); return new PreciseNumber(value.negate());
} }
@Override
public NumberInterface promoteToInternal(Class<? extends NumberInterface> toClass) {
if (toClass == this.getClass()) {
return this;
}
return null;
}
@Override @Override
public String toString() { public String toString() {
return value.round(outputContext).toString(); return value.round(outputContext).toString();

View File

@ -102,7 +102,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return OP_CARET.apply(implementation, params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass()))); return OP_CARET.apply(implementation, params[0], implementation.instanceForString(".5"));
} }
}; };
/** /**
@ -141,17 +141,13 @@ public class StandardPlugin extends Plugin {
return C.divide(sum); return C.divide(sum);
} }
}; };
/**
* Stores objects of NumberInterface with integer values for reuse.
*/
private final static HashMap<Class<? extends NumberInterface>, HashMap<Integer, NumberInterface>> integerValues = new HashMap<>();
/** /**
* The division operator, / * The division operator, /
*/ */
public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 2 && params[1].compareTo(fromInt(params[0].getClass(), 0)) != 0; return params.length == 2 && params[1].compareTo(implementation.instanceForString(Integer.toString( 0))) != 0;
} }
@ -168,7 +164,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 1 return params.length == 1
&& params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0 && params[0].fractionalPart().compareTo(implementation.instanceForString("0")) == 0
&& params[0].signum() >= 0; && params[0].signum() >= 0;
} }
@ -176,9 +172,9 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
if (params[0].signum() == 0) { if (params[0].signum() == 0) {
return fromInt(params[0].getClass(), 1); return implementation.instanceForString("1");
} }
NumberInterface one = fromInt(params[0].getClass(), 1); NumberInterface one = implementation.instanceForString("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.
@ -208,18 +204,18 @@ public class StandardPlugin extends Plugin {
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
if (params[0].compareTo(params[1]) < 0 || if (params[0].compareTo(params[1]) < 0 ||
params[0].signum() < 0 || params[0].signum() < 0 ||
(params[0].signum() == 0 && params[1].signum() != 0)) return fromInt(params[0].getClass(), 0); (params[0].signum() == 0 && params[1].signum() != 0)) return implementation.instanceForString("0");
NumberInterface total = fromInt(params[0].getClass(), 1); NumberInterface total = implementation.instanceForString("1");
NumberInterface multiplyBy = params[0]; NumberInterface multiplyBy = params[0];
NumberInterface remainingMultiplications = params[1]; NumberInterface remainingMultiplications = params[1];
NumberInterface halfway = params[0].divide(fromInt(params[0].getClass(), 2)); NumberInterface halfway = params[0].divide(implementation.instanceForString("2"));
if (remainingMultiplications.compareTo(halfway) > 0) { if (remainingMultiplications.compareTo(halfway) > 0) {
remainingMultiplications = params[0].subtract(remainingMultiplications); remainingMultiplications = params[0].subtract(remainingMultiplications);
} }
while (remainingMultiplications.signum() > 0) { while (remainingMultiplications.signum() > 0) {
total = total.multiply(multiplyBy); total = total.multiply(multiplyBy);
remainingMultiplications = remainingMultiplications.subtract(fromInt(params[0].getClass(), 1)); remainingMultiplications = remainingMultiplications.subtract(implementation.instanceForString("1"));
multiplyBy = multiplyBy.subtract(fromInt(params[0].getClass(), 1)); multiplyBy = multiplyBy.subtract(implementation.instanceForString("1"));
} }
return total; return total;
} }
@ -252,7 +248,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return params[0].multiply(fromInt(params[0].getClass(), params[0].signum())); return params[0].multiply(implementation.instanceForString(Integer.toString(params[0].signum())));
} }
}; };
/** /**
@ -261,25 +257,25 @@ public class StandardPlugin extends Plugin {
public static final NumberFunction FUNCTION_LN = new NumberFunction() { public static final NumberFunction FUNCTION_LN = new NumberFunction() {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 1 && params[0].compareTo(fromInt(params[0].getClass(), 0)) > 0; return params.length == 1 && params[0].compareTo(implementation.instanceForString("0")) > 0;
} }
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
NumberInterface param = params[0]; NumberInterface param = params[0];
NumberInterface one = fromInt(param.getClass(), 1); NumberInterface one = implementation.instanceForString("1");
int powersOf2 = 0; int powersOf2 = 0;
while (FUNCTION_ABS.apply(implementation, param.subtract(one)).compareTo(new NaiveNumber(0.1).promoteTo(param.getClass())) >= 0) { while (FUNCTION_ABS.apply(implementation, param.subtract(one)).compareTo(implementation.instanceForString(".1")) >= 0) {
if (param.subtract(one).signum() == 1) { if (param.subtract(one).signum() == 1) {
param = param.divide(fromInt(param.getClass(), 2)); param = param.divide(implementation.instanceForString("2"));
powersOf2++; powersOf2++;
if (param.subtract(one).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(implementation.instanceForString("2"));
powersOf2--; powersOf2--;
if (param.subtract(one).signum() != -1) { if (param.subtract(one).signum() != -1) {
break; break;
@ -287,7 +283,7 @@ public class StandardPlugin extends Plugin {
} }
} }
} }
return getLog2(implementation, param).multiply(fromInt(param.getClass(), powersOf2)).add(getLogPartialSum(implementation, param)); return getLog2(implementation, param).multiply(implementation.instanceForString(Integer.toString(powersOf2))).add(getLogPartialSum(implementation, param));
} }
/** /**
@ -299,13 +295,13 @@ public class StandardPlugin extends Plugin {
private NumberInterface getLogPartialSum(NumberImplementation implementation, NumberInterface x) { private NumberInterface getLogPartialSum(NumberImplementation implementation, NumberInterface x) {
NumberInterface maxError = x.getMaxError(); NumberInterface maxError = x.getMaxError();
x = x.subtract(fromInt(x.getClass(), 1)); //Terms used are for log(x+1). x = x.subtract(implementation.instanceForString("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(implementation, currentTerm).compareTo(maxError) > 0) { while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0) {
n++; n++;
currentNumerator = currentNumerator.multiply(x).negate(); currentNumerator = currentNumerator.multiply(x).negate();
currentTerm = currentNumerator.divide(fromInt(x.getClass(), n)); currentTerm = currentNumerator.divide(implementation.instanceForString(Integer.toString(n)));
sum = sum.add(currentTerm); sum = sum.add(currentTerm);
} }
return sum; return sum;
@ -318,19 +314,19 @@ public class StandardPlugin extends Plugin {
*/ */
private NumberInterface getLog2(NumberImplementation implementation, NumberInterface number) { private NumberInterface getLog2(NumberImplementation implementation, NumberInterface number) {
NumberInterface maxError = number.getMaxError(); NumberInterface maxError = number.getMaxError();
//NumberInterface errorBound = fromInt(number.getClass(), 1); //NumberInterface errorBound = implementation.instanceForString("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 = implementation.instanceForString("1"), b = a, c = a;
NumberInterface sum = fromInt(number.getClass(), 0); NumberInterface sum = implementation.instanceForString("0");
NumberInterface one = fromInt(number.getClass(), 1); NumberInterface one = implementation.instanceForString("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(implementation.instanceForString("3"));
b = b.divide(fromInt(number.getClass(), 4)); b = b.divide(implementation.instanceForString("4"));
c = one.divide(fromInt(number.getClass(), n)); c = one.divide(implementation.instanceForString(Integer.toString(n)));
sum = sum.add(a.add(b).multiply(c)); sum = sum.add(a.add(b).multiply(c));
} }
return sum; return sum;
@ -348,10 +344,10 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return fromInt(params[0].getClass(), (int) Math.round(Math.random() * params[0].floor().intValue())); return implementation.instanceForString(Long.toString(Math.round(Math.random() * params[0].floor().intValue())));
} }
}; };
private static final HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>(); private static final HashMap<NumberImplementation, ArrayList<NumberInterface>> FACTORIAL_LISTS = new HashMap<>();
/** /**
* The exponential function, exp(1) = e^1 = 2.71... * The exponential function, exp(1) = e^1 = 2.71...
*/ */
@ -368,19 +364,19 @@ public class StandardPlugin extends Plugin {
int n = 0; int n = 0;
if (params[0].signum() < 0) { if (params[0].signum() < 0) {
NumberInterface[] negatedParams = {params[0].negate()}; NumberInterface[] negatedParams = {params[0].negate()};
return fromInt(params[0].getClass(), 1).divide(applyInternal(implementation, negatedParams)); return implementation.instanceForString("1").divide(applyInternal(implementation, negatedParams));
} 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 = fromInt(params[0].getClass(), 1); NumberInterface sum = implementation.instanceForString("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(implementation.instanceForString("3").intPow(params[0].ceiling().intValue())), right = maxError;
do { do {
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n + 1))); sum = sum.add(nextNumerator.divide(factorial(implementation, n + 1)));
n++; n++;
nextNumerator = nextNumerator.multiply(params[0]); nextNumerator = nextNumerator.multiply(params[0]);
left = left.multiply(params[0]); left = left.multiply(params[0]);
NumberInterface nextN = fromInt(params[0].getClass(), n + 1); NumberInterface nextN = implementation.instanceForString(Integer.toString(n + 1));
right = right.multiply(nextN); right = right.multiply(nextN);
//System.out.println(left + ", " + right); //System.out.println(left + ", " + right);
} }
@ -396,7 +392,7 @@ public class StandardPlugin extends Plugin {
public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0); NumberInterface zero = implementation.instanceForString("0");
return params.length == 2 return params.length == 2
&& !(params[0].compareTo(zero) == 0 && !(params[0].compareTo(zero) == 0
&& params[1].compareTo(zero) == 0) && params[1].compareTo(zero) == 0)
@ -406,15 +402,15 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
NumberInterface zero = fromInt(params[0].getClass(), 0); NumberInterface zero = implementation.instanceForString("0");
if (params[0].compareTo(zero) == 0) if (params[0].compareTo(zero) == 0)
return zero; return zero;
else if (params[1].compareTo(zero) == 0) else if (params[1].compareTo(zero) == 0)
return fromInt(params[0].getClass(), 1); return implementation.instanceForString("1");
//Detect integer bases: //Detect integer bases:
if (params[0].fractionalPart().compareTo(fromInt(params[0].getClass(), 0)) == 0 if (params[0].fractionalPart().compareTo(implementation.instanceForString("0")) == 0
&& FUNCTION_ABS.apply(implementation, params[1]).compareTo(fromInt(params[0].getClass(), Integer.MAX_VALUE)) < 0 && FUNCTION_ABS.apply(implementation, params[1]).compareTo(implementation.instanceForString(Integer.toString(Integer.MAX_VALUE))) < 0
&& FUNCTION_ABS.apply(implementation, params[1]).compareTo(fromInt(params[1].getClass(), 1)) >= 0) { && FUNCTION_ABS.apply(implementation, params[1]).compareTo(implementation.instanceForString("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(implementation, newParams)); return params[0].intPow(params[1].floor().intValue()).multiply(applyInternal(implementation, newParams));
} }
@ -434,12 +430,12 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
NumberInterface pi = piFor(params[0].getClass()); NumberInterface pi = piFor(params[0].getClass());
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2)); NumberInterface twoPi = pi.multiply(implementation.instanceForString("2"));
NumberInterface theta = getSmallAngle(implementation, params[0], pi); NumberInterface theta = getSmallAngle(implementation, params[0], pi);
//System.out.println(theta); //System.out.println(theta);
if (theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0) { if (theta.compareTo(pi.multiply(implementation.instanceForString("1.5"))) >= 0) {
theta = theta.subtract(twoPi); theta = theta.subtract(twoPi);
} else if (theta.compareTo(pi.divide(fromInt(pi.getClass(), 2))) > 0) { } else if (theta.compareTo(pi.divide(implementation.instanceForString("2"))) > 0) {
theta = pi.subtract(theta); theta = pi.subtract(theta);
} }
//System.out.println(theta); //System.out.println(theta);
@ -458,7 +454,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return functionSin.apply(implementation, piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) return functionSin.apply(implementation, piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
.subtract(params[0])); .subtract(params[0]));
} }
}; };
@ -489,7 +485,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return fromInt(params[0].getClass(), 1).divide(functionCos.apply(implementation, params[0])); return implementation.instanceForString("1").divide(functionCos.apply(implementation, params[0]));
} }
}; };
/** /**
@ -504,7 +500,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return fromInt(params[0].getClass(), 1).divide(functionSin.apply(implementation, params[0])); return implementation.instanceForString("1").divide(functionSin.apply(implementation, params[0]));
} }
}; };
/** /**
@ -530,27 +526,27 @@ public class StandardPlugin extends Plugin {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 1 return params.length == 1
&& FUNCTION_ABS.apply(implementation, params[0]).compareTo(fromInt(params[0].getClass(), 1)) <= 0; && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) <= 0;
} }
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
if (FUNCTION_ABS.apply(implementation, params[0]).compareTo(new NaiveNumber(0.8).promoteTo(params[0].getClass())) >= 0) { if (FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString(".8")) >= 0) {
NumberInterface[] newParams = {FUNCTION_SQRT.apply(implementation, fromInt(params[0].getClass(), 1).subtract(params[0].multiply(params[0])))}; NumberInterface[] newParams = {FUNCTION_SQRT.apply(implementation, implementation.instanceForString("1").subtract(params[0].multiply(params[0])))};
return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) return piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
.subtract(applyInternal(implementation, newParams)).multiply(fromInt(params[0].getClass(), params[0].signum())); .subtract(applyInternal(implementation, newParams)).multiply(implementation.instanceForString(Integer.toString(params[0].signum())));
} }
NumberInterface currentTerm = params[0], sum = currentTerm, NumberInterface currentTerm = params[0], sum = currentTerm,
multiplier = currentTerm.multiply(currentTerm), summandBound = sum.getMaxError().multiply(fromInt(sum.getClass(), 1).subtract(multiplier)), multiplier = currentTerm.multiply(currentTerm), summandBound = sum.getMaxError().multiply(implementation.instanceForString("1").subtract(multiplier)),
power = currentTerm, coefficient = fromInt(params[0].getClass(), 1); power = currentTerm, coefficient = implementation.instanceForString("1");
int exponent = 1; int exponent = 1;
while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(summandBound) > 0) { while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(summandBound) > 0) {
exponent += 2; exponent += 2;
power = power.multiply(multiplier); power = power.multiply(multiplier);
coefficient = coefficient.multiply(fromInt(params[0].getClass(), exponent - 2)) coefficient = coefficient.multiply(implementation.instanceForString(Integer.toString( exponent - 2)))
.divide(fromInt(params[0].getClass(), exponent - 1)); .divide(implementation.instanceForString(Integer.toString( exponent - 1)));
currentTerm = power.multiply(coefficient).divide(fromInt(power.getClass(), exponent)); currentTerm = power.multiply(coefficient).divide(implementation.instanceForString(Integer.toString( exponent)));
sum = sum.add(currentTerm); sum = sum.add(currentTerm);
} }
return sum; return sum;
@ -563,13 +559,13 @@ public class StandardPlugin extends Plugin {
public final NumberFunction functionArccos = new NumberFunction() { public final NumberFunction functionArccos = new NumberFunction() {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(fromInt(params[0].getClass(), 1)) <= 0; return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) <= 0;
} }
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) return piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
.subtract(functionArcsin.apply(implementation, params)); .subtract(functionArcsin.apply(implementation, params));
} }
}; };
@ -580,13 +576,13 @@ public class StandardPlugin extends Plugin {
public final NumberFunction functionArccsc = new NumberFunction() { public final NumberFunction functionArccsc = new NumberFunction() {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(fromInt(params[0].getClass(), 1)) >= 0; return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) >= 0;
} }
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
NumberInterface[] reciprocalParamArr = {fromInt(params[0].getClass(), 1).divide(params[0])}; NumberInterface[] reciprocalParamArr = {implementation.instanceForString("1").divide(params[0])};
return functionArcsin.apply(implementation, reciprocalParamArr); return functionArcsin.apply(implementation, reciprocalParamArr);
} }
}; };
@ -597,13 +593,13 @@ public class StandardPlugin extends Plugin {
public final NumberFunction functionArcsec = new NumberFunction() { public final NumberFunction functionArcsec = new NumberFunction() {
@Override @Override
public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) { public boolean matchesParams(NumberImplementation implementation, NumberInterface[] params) {
return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(fromInt(params[0].getClass(), 1)) >= 0; return params.length == 1 && FUNCTION_ABS.apply(implementation, params[0]).compareTo(implementation.instanceForString("1")) >= 0;
} }
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
NumberInterface[] reciprocalParamArr = {fromInt(params[0].getClass(), 1).divide(params[0])}; NumberInterface[] reciprocalParamArr = {implementation.instanceForString("1").divide(params[0])};
return functionArccos.apply(implementation, reciprocalParamArr); return functionArccos.apply(implementation, reciprocalParamArr);
} }
}; };
@ -624,18 +620,18 @@ public class StandardPlugin extends Plugin {
NumberInterface[] negatedParams = {params[0].negate()}; NumberInterface[] negatedParams = {params[0].negate()};
return applyInternal(implementation, negatedParams).negate(); return applyInternal(implementation, negatedParams).negate();
} }
if (params[0].compareTo(fromInt(params[0].getClass(), 1)) > 0) { if (params[0].compareTo(implementation.instanceForString("1")) > 0) {
NumberInterface[] reciprocalParams = {fromInt(params[0].getClass(), 1).divide(params[0])}; NumberInterface[] reciprocalParams = {implementation.instanceForString("1").divide(params[0])};
return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) return piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
.subtract(applyInternal(implementation, reciprocalParams)); .subtract(applyInternal(implementation, reciprocalParams));
} }
if (params[0].compareTo(fromInt(params[0].getClass(), 1)) == 0) { if (params[0].compareTo(implementation.instanceForString("1")) == 0) {
return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 4)); return piFor(params[0].getClass()).divide(implementation.instanceForString("4"));
} }
if (params[0].compareTo(new NaiveNumber(0.9).promoteTo(params[0].getClass())) >= 0) { if (params[0].compareTo(implementation.instanceForString(".9")) >= 0) {
NumberInterface[] newParams = {params[0].multiply(fromInt(params[0].getClass(), 2)) NumberInterface[] newParams = {params[0].multiply(implementation.instanceForString("2"))
.divide(fromInt(params[0].getClass(), 1).subtract(params[0].multiply(params[0])))}; .divide(implementation.instanceForString("1").subtract(params[0].multiply(params[0])))};
return applyInternal(implementation, newParams).divide(fromInt(params[0].getClass(), 2)); return applyInternal(implementation, newParams).divide(implementation.instanceForString("2"));
} }
NumberInterface currentPower = params[0], currentTerm = currentPower, sum = currentTerm, NumberInterface currentPower = params[0], currentTerm = currentPower, sum = currentTerm,
maxError = params[0].getMaxError(), multiplier = currentPower.multiply(currentPower).negate(); maxError = params[0].getMaxError(), multiplier = currentPower.multiply(currentPower).negate();
@ -643,7 +639,7 @@ public class StandardPlugin extends Plugin {
while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0) { while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0) {
n += 2; n += 2;
currentPower = currentPower.multiply(multiplier); currentPower = currentPower.multiply(multiplier);
currentTerm = currentPower.divide(fromInt(currentPower.getClass(), n)); currentTerm = currentPower.divide(implementation.instanceForString(Integer.toString( n)));
sum = sum.add(currentTerm); sum = sum.add(currentTerm);
} }
return sum; return sum;
@ -661,7 +657,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) { public NumberInterface applyInternal(NumberImplementation implementation, NumberInterface[] params) {
return piFor(params[0].getClass()).divide(fromInt(params[0].getClass(), 2)) return piFor(params[0].getClass()).divide(implementation.instanceForString("2"))
.subtract(functionArctan.apply(implementation, params)); .subtract(functionArctan.apply(implementation, params));
} }
}; };
@ -670,40 +666,25 @@ public class StandardPlugin extends Plugin {
super(manager); super(manager);
} }
/**
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
*
* @param x the value at which the series is evaluated.
* @param nthTermFunction the function that returns the nth term of the series, in the format term(x, n).
* @param n the number of terms in the partial sum.
* @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) {
NumberInterface sum = fromInt(x.getClass(), 0);
for (int i = 0; i <= n; i++) {
sum = sum.add(nthTermFunction.apply(i, x));
}
return sum;
}
/** /**
* 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.
* *
* @param numberClass type of number to return. * @param implementation type of number to return.
* @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(NumberImplementation implementation, int n) {
if (!FACTORIAL_LISTS.containsKey(numberClass)) {
FACTORIAL_LISTS.put(numberClass, new ArrayList<>()); if (!FACTORIAL_LISTS.containsKey(implementation)) {
FACTORIAL_LISTS.get(numberClass).add(fromInt(numberClass, 1)); FACTORIAL_LISTS.put(implementation, new ArrayList<>());
FACTORIAL_LISTS.get(numberClass).add(fromInt(numberClass, 1)); FACTORIAL_LISTS.get(implementation).add(implementation.instanceForString("1"));
FACTORIAL_LISTS.get(implementation).add(implementation.instanceForString("1"));
} }
ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(numberClass); ArrayList<NumberInterface> list = FACTORIAL_LISTS.get(implementation);
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(fromInt(numberClass, list.size()))); list.add(list.get(list.size() - 1).multiply(implementation.instanceForString(Integer.toString( list.size()))));
} }
} }
return list.get(n); return list.get(n);
@ -722,7 +703,7 @@ public class StandardPlugin extends Plugin {
do { do {
n += 2; n += 2;
power = power.multiply(multiplier); power = power.multiply(multiplier);
currentTerm = power.divide(factorial(x.getClass(), n)); currentTerm = power.divide(factorial(implementation, n));
sum = sum.add(currentTerm); sum = sum.add(currentTerm);
} while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0); } while (FUNCTION_ABS.apply(implementation, currentTerm).compareTo(maxError) > 0);
return sum; return sum;
@ -735,7 +716,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(NumberImplementation implementation, NumberInterface phi, NumberInterface pi) { private static NumberInterface getSmallAngle(NumberImplementation implementation, NumberInterface phi, NumberInterface pi) {
NumberInterface twoPi = pi.multiply(fromInt(pi.getClass(), 2)); NumberInterface twoPi = pi.multiply(implementation.instanceForString("2"));
NumberInterface theta = FUNCTION_ABS.apply(implementation, phi).subtract(twoPi NumberInterface theta = FUNCTION_ABS.apply(implementation, phi).subtract(twoPi
.multiply(FUNCTION_ABS.apply(implementation, phi).divide(twoPi).floor())); //Now theta is in [0, 2pi). .multiply(FUNCTION_ABS.apply(implementation, phi).divide(twoPi).floor())); //Now theta is in [0, 2pi).
if (phi.signum() < 0) { if (phi.signum() < 0) {
@ -744,23 +725,6 @@ public class StandardPlugin extends Plugin {
return theta; return theta;
} }
/**
* Returns a number of class numType with value n.
*
* @param numType class of number to return.
* @param n value of returned number.
* @return numClass instance with value n.
*/
private static NumberInterface fromInt(Class<? extends NumberInterface> numType, int n) {
if (!integerValues.containsKey(numType)) {
integerValues.put(numType, new HashMap<>());
}
if (!integerValues.get(numType).containsKey(n)) {
integerValues.get(numType).put(n, new NaiveNumber(n).promoteTo(numType));
}
return integerValues.get(numType).get(n);
}
@Override @Override
public void onEnable() { public void onEnable() {
registerNumberImplementation("naive", IMPLEMENTATION_NAIVE); registerNumberImplementation("naive", IMPLEMENTATION_NAIVE);
@ -849,6 +813,6 @@ public class StandardPlugin extends Plugin {
@Override @Override
public void onDisable() { public void onDisable() {
FACTORIAL_LISTS.clear();
} }
} }