Top Description Fields Constructors Methods
org.apache.commons.math3.util

pack-priv Class FastMathCalc

extends Object
Class Inheritance
Imports
java.io.PrintStream, org.apache.commons.math3.exception.DimensionMismatchException

Class used to compute the classical functions tables.
Since
3.0

Field Summary

Modifier and TypeField and Description
private static final double[]
FACT

Factorial table, for Taylor series expansions.

private static final long
HEX_40000000

0x40000000 - used to split a double into two parts, both with the low order bits cleared.

private static final double[][]
LN_SPLIT_COEF

Coefficients for slowLog.

private static final String
TABLE_END_DECL

Table end declaration.

private static final String
TABLE_START_DECL

Table start declaration.

Constructor Summary

AccessConstructor and Description
private
FastMathCalc()

Private Constructor.

Method Summary

Modifier and TypeMethod and Description
private static void
buildSinCosTables(double[]
table of the most significant part of the sines
SINE_TABLE_A
,
double[]
table of the least significant part of the sines
SINE_TABLE_B
,
double[]
table of the most significant part of the cosines
COSINE_TABLE_A
,
double[]
table of the most significant part of the cosines
COSINE_TABLE_B
,
int
length of the tables
SINE_TABLE_LEN
,
double[]
table of the most significant part of the tangents
TANGENT_TABLE_A
,
double[]
table of the most significant part of the tangents
TANGENT_TABLE_B
)

Build the sine and cosine tables.

private static void
checkLen(int
expected length
expectedLen
,
int
actual length
actual
)

Check two lengths are equal.

pack-priv static double

Returns:

exp(p) in standard precision (equal to result[0] + result[1])
expint
(int
integer whose exponential is requested
p
,
final double[]
placeholder where to put the result in extended precision
result
)

Compute exp(p) for a integer p in extended precision.

pack-priv static String

Returns:

formatted number
format
(double
double number to format
d
)

Format a double.

pack-priv static void
printarray(PrintStream
text output stream where output should be printed
out
,
String
array name
name
,
int
expected length of the array
expectedLen
,
double[][]
array data
array2d
)

Print an array.

pack-priv static void
printarray(PrintStream
text output stream where output should be printed
out
,
String
array name
name
,
int
expected length of the array
expectedLen
,
double[]
array data
array
)

Print an array.

private static void
quadMult(final double[]
first term of the multiplication
a
,
final double[]
second term of the multiplication
b
,
final double[]
placeholder where to put the result
result
)

Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.

private static void
resplit(final double[]
input/out array containing the split, changed on output
a
)

Recompute a split.

pack-priv static double

Returns:

cos(x)
slowCos
(final double
number from which cosine is requested
x
,
final double[]
placeholder where to put the result in extended precision (may be null)
result
)

For x between 0 and pi/4 compute cosine using Talor series cos(x) = 1 - x^2/2!

pack-priv static double

Returns:

exp(x)
slowexp
(final double
argument of exponential
x
,
final double[]
placeholder where to place exp(x) split in two terms for extra precision (i.e. exp(x) = result[0] + result[1]
result
)

For x between 0 and 1, returns exp(x), uses extended precision

pack-priv static double[]

Returns:

log(xi)
slowLog
(double
number from which log is requested
xi
)

xi in the range of [1, 2].

pack-priv static double

Returns:

sin(x)
slowSin
(final double
number from which sine is requested
x
,
final double[]
placeholder where to put the result in extended precision (may be null)
result
)

For x between 0 and pi/4 compute sine using Taylor expansion: sin(x) = x - x^3/3!

private static void
split(final double
number to split
d
,
final double[]
placeholder where to place the result
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(final double[]
first term of addition
a
,
final double[]
second term of addition
b
,
final double[]
placeholder where to put the result
ans
)

Add two numbers in split form.

private static void
splitMult(double[]
first term of multiplication
a
,
double[]
second term of multiplication
b
,
double[]
placeholder where to put the result
ans
)

Multiply two numbers in split form.

pack-priv static void
splitReciprocal(final double[]
initial number, in split form
in
,
final double[]
placeholder where to put the result
result
)

Compute the reciprocal of in. Use the following algorithm.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FACTback to summary
private static final double[] FACT

Factorial table, for Taylor series expansions. 0!, 1!, 2!, ... 19!

HEX_40000000back to summary
private static final long HEX_40000000

0x40000000 - used to split a double into two parts, both with the low order bits cleared. Equivalent to 2^30.

LN_SPLIT_COEFback to summary
private static final double[][] LN_SPLIT_COEF

Coefficients for slowLog.

TABLE_END_DECLback to summary
private static final String TABLE_END_DECL

Table end declaration.

TABLE_START_DECLback to summary
private static final String TABLE_START_DECL

Table start declaration.

Constructor Detail

FastMathCalcback to summary
private FastMathCalc()

Private Constructor.

Method Detail

buildSinCosTablesback to summary
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:double[]

table of the most significant part of the sines

SINE_TABLE_B:double[]

table of the least significant part of the sines

COSINE_TABLE_A:double[]

table of the most significant part of the cosines

COSINE_TABLE_B:double[]

table of the most significant part of the cosines

SINE_TABLE_LEN:int

length of the tables

TANGENT_TABLE_A:double[]

table of the most significant part of the tangents

TANGENT_TABLE_B:double[]

table of the most significant part of the tangents

Annotations
@SuppressWarnings:unused
checkLenback to summary
private static void checkLen(int expectedLen, int actual) throws DimensionMismatchException

Check two lengths are equal.

Parameters
expectedLen:int

expected length

actual:int

actual length

Exceptions
DimensionMismatchException:
if the two lengths are not equal
expintback to summary
pack-priv static double expint(int p, final double[] result)

Compute exp(p) for a integer p in extended precision.

Parameters
p:int

integer whose exponential is requested

result:double[]

placeholder where to put the result in extended precision

Returns:double

exp(p) in standard precision (equal to result[0] + result[1])

formatback to summary
pack-priv static String format(double d)

Format a double.

Parameters
d:double

double number to format

Returns:String

formatted number

printarrayback to summary
pack-priv static void printarray(PrintStream out, String name, int expectedLen, double[][] array2d)

Print an array.

Parameters
out:PrintStream

text output stream where output should be printed

name:String

array name

expectedLen:int

expected length of the array

array2d:double[][]

array data

printarrayback to summary
pack-priv static void printarray(PrintStream out, String name, int expectedLen, double[] array)

Print an array.

Parameters
out:PrintStream

text output stream where output should be printed

name:String

array name

expectedLen:int

expected length of the array

array:double[]

array data

quadMultback to summary
private static void quadMult(final double[] a, final double[] b, final double[] result)

Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.

Parameters
a:double[]

first term of the multiplication

b:double[]

second term of the multiplication

result:double[]

placeholder where to put the result

resplitback to summary
private static void resplit(final double[] a)

Recompute a split.

Parameters
a:double[]

input/out array containing the split, changed on output

slowCosback to summary
pack-priv static double slowCos(final double x, final 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:double

number from which cosine is requested

result:double[]

placeholder where to put the result in extended precision (may be null)

Returns:double

cos(x)

slowexpback to summary
pack-priv static double slowexp(final double x, final double[] result)

For x between 0 and 1, returns exp(x), uses extended precision

Parameters
x:double

argument of exponential

result:double[]

placeholder where to place exp(x) split in two terms for extra precision (i.e. exp(x) = result[0] + result[1]

Returns:double

exp(x)

slowLogback to summary
pack-priv 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:double

number from which log is requested

Returns:double[]

log(xi)

slowSinback to summary
pack-priv static double slowSin(final double x, final 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:double

number from which sine is requested

result:double[]

placeholder where to put the result in extended precision (may be null)

Returns:double

sin(x)

splitback to summary
private static void split(final double d, final 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:double

number to split

split:double[]

placeholder where to place the result

splitAddback to summary
private static void splitAdd(final double[] a, final double[] b, final double[] ans)

Add two numbers in split form.

Parameters
a:double[]

first term of addition

b:double[]

second term of addition

ans:double[]

placeholder where to put the result

splitMultback to summary
private static void splitMult(double[] a, double[] b, double[] ans)

Multiply two numbers in split form.

Parameters
a:double[]

first term of multiplication

b:double[]

second term of multiplication

ans:double[]

placeholder where to put the result

splitReciprocalback to summary
pack-priv static void splitReciprocal(final double[] in, final 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:double[]

initial number, in split form

result:double[]

placeholder where to put the result