Package org.apache.commons.math3.util
Class FastMathCalc
java.lang.Object
org.apache.commons.math3.util.FastMathCalc
Class used to compute the classical functions tables.
- Since:
- 3.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double[]
Factorial table, for Taylor series expansions.private static final long
0x40000000 - used to split a double into two parts, both with the low order bits cleared.private static final double[][]
Coefficients for slowLog.private static final String
Table end declaration.private static final String
Table start declaration. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
buildSinCosTables
(double[] SINE_TABLE_A, double[] SINE_TABLE_B, double[] COSINE_TABLE_A, double[] COSINE_TABLE_B, int SINE_TABLE_LEN, double[] TANGENT_TABLE_A, double[] TANGENT_TABLE_B) Build the sine and cosine tables.private static void
checkLen
(int expectedLen, int actual) Check two lengths are equal.(package private) static double
expint
(int p, double[] result) Compute exp(p) for a integer p in extended precision.(package private) static String
format
(double d) Format a double.(package private) static void
printarray
(PrintStream out, String name, int expectedLen, double[] array) Print an array.(package private) static void
printarray
(PrintStream out, String name, int expectedLen, double[][] array2d) Print an array.private static void
quadMult
(double[] a, double[] b, double[] result) Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.private static void
resplit
(double[] a) Recompute a split.(package private) static double
slowCos
(double x, double[] result) For x between 0 and pi/4 compute cosine using Talor series cos(x) = 1 - x^2/2! + x^4/4! ...(package private) static double
slowexp
(double x, double[] result) For x between 0 and 1, returns exp(x), uses extended precision(package private) static double[]
slowLog
(double xi) xi in the range of [1, 2].(package private) static double
slowSin
(double x, double[] result) For x between 0 and pi/4 compute sine using Taylor expansion: sin(x) = x - x^3/3! + x^5/5! - x^7/7! ...private static void
split
(double d, double[] split) Compute split[0], split[1] such that their sum is equal to d, and split[0] has its 30 least significant bits as zero.private static void
splitAdd
(double[] a, double[] b, double[] ans) Add two numbers in split form.private static void
splitMult
(double[] a, double[] b, double[] ans) Multiply two numbers in split form.(package private) static void
splitReciprocal
(double[] in, double[] result) Compute the reciprocal of in.
-
Field Details
-
HEX_40000000
private static final long HEX_400000000x40000000 - used to split a double into two parts, both with the low order bits cleared. Equivalent to 2^30.- See Also:
-
FACT
private static final double[] FACTFactorial table, for Taylor series expansions. 0!, 1!, 2!, ... 19! -
LN_SPLIT_COEF
private static final double[][] LN_SPLIT_COEFCoefficients for slowLog. -
TABLE_START_DECL
Table start declaration.- See Also:
-
TABLE_END_DECL
Table end declaration.- See Also:
-
-
Constructor Details
-
FastMathCalc
private FastMathCalc()Private Constructor.
-
-
Method Details
-
buildSinCosTables
private static void buildSinCosTables(double[] SINE_TABLE_A, double[] SINE_TABLE_B, double[] COSINE_TABLE_A, double[] COSINE_TABLE_B, int SINE_TABLE_LEN, double[] TANGENT_TABLE_A, double[] TANGENT_TABLE_B) Build the sine and cosine tables.- Parameters:
SINE_TABLE_A
- table of the most significant part of the sinesSINE_TABLE_B
- table of the least significant part of the sinesCOSINE_TABLE_A
- table of the most significant part of the cosinesCOSINE_TABLE_B
- table of the most significant part of the cosinesSINE_TABLE_LEN
- length of the tablesTANGENT_TABLE_A
- table of the most significant part of the tangentsTANGENT_TABLE_B
- table of the most significant part of the tangents
-
slowCos
static double slowCos(double x, double[] result) For x between 0 and pi/4 compute cosine using Talor series cos(x) = 1 - x^2/2! + x^4/4! ...- Parameters:
x
- number from which cosine is requestedresult
- placeholder where to put the result in extended precision (may be null)- Returns:
- cos(x)
-
slowSin
static double slowSin(double x, double[] result) For x between 0 and pi/4 compute sine using Taylor expansion: sin(x) = x - x^3/3! + x^5/5! - x^7/7! ...- Parameters:
x
- number from which sine is requestedresult
- placeholder where to put the result in extended precision (may be null)- Returns:
- sin(x)
-
slowexp
static double slowexp(double x, double[] result) For x between 0 and 1, returns exp(x), uses extended precision- Parameters:
x
- argument of exponentialresult
- placeholder where to place exp(x) split in two terms for extra precision (i.e. exp(x) = result[0] + result[1]- Returns:
- exp(x)
-
split
private static void split(double d, double[] split) Compute split[0], split[1] such that their sum is equal to d, and split[0] has its 30 least significant bits as zero.- Parameters:
d
- number to splitsplit
- placeholder where to place the result
-
resplit
private static void resplit(double[] a) Recompute a split.- Parameters:
a
- input/out array containing the split, changed on output
-
splitMult
private static void splitMult(double[] a, double[] b, double[] ans) Multiply two numbers in split form.- Parameters:
a
- first term of multiplicationb
- second term of multiplicationans
- placeholder where to put the result
-
splitAdd
private static void splitAdd(double[] a, double[] b, double[] ans) Add two numbers in split form.- Parameters:
a
- first term of additionb
- second term of additionans
- placeholder where to put the result
-
splitReciprocal
static void splitReciprocal(double[] in, double[] result) Compute the reciprocal of in. Use the following algorithm. in = c + d. want to find x + y such that x+y = 1/(c+d) and x is much larger than y and x has several zero bits on the right. Set b = 1/(2^22), a = 1 - b. Thus (a+b) = 1. Use following identity to compute (a+b)/(c+d) (a+b)/(c+d) = a/c + (bc - ad) / (c^2 + cd) set x = a/c and y = (bc - ad) / (c^2 + cd) This will be close to the right answer, but there will be some rounding in the calculation of X. So by carefully computing 1 - (c+d)(x+y) we can compute an error and add that back in. This is done carefully so that terms of similar size are subtracted first.- Parameters:
in
- initial number, in split formresult
- placeholder where to put the result
-
quadMult
private static void quadMult(double[] a, double[] b, double[] result) Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.- Parameters:
a
- first term of the multiplicationb
- second term of the multiplicationresult
- placeholder where to put the result
-
expint
static double expint(int p, double[] result) Compute exp(p) for a integer p in extended precision.- Parameters:
p
- integer whose exponential is requestedresult
- placeholder where to put the result in extended precision- Returns:
- exp(p) in standard precision (equal to result[0] + result[1])
-
slowLog
static double[] slowLog(double xi) xi in the range of [1, 2]. 3 5 7 x+1 / x x x \ ln ----- = 2 * | x + ---- + ---- + ---- + ... | 1-x \ 3 5 7 / So, compute a Remez approximation of the following function ln ((sqrt(x)+1)/(1-sqrt(x))) / x This will be an even function with only positive coefficents. x is in the range [0 - 1/3]. Transform xi for input to the above function by setting x = (xi-1)/(xi+1). Input to the polynomial is x^2, then the result is multiplied by x.- Parameters:
xi
- number from which log is requested- Returns:
- log(xi)
-
printarray
Print an array.- Parameters:
out
- text output stream where output should be printedname
- array nameexpectedLen
- expected length of the arrayarray2d
- array data
-
printarray
Print an array.- Parameters:
out
- text output stream where output should be printedname
- array nameexpectedLen
- expected length of the arrayarray
- array data
-
format
Format a double.- Parameters:
d
- double number to format- Returns:
- formatted number
-
checkLen
Check two lengths are equal.- Parameters:
expectedLen
- expected lengthactual
- actual length- Throws:
DimensionMismatchException
- if the two lengths are not equal
-