## 6 Utilities## 6.1 OverviewThe org.apache.commons.math4.util package collects a group of array utilities, value transformers, and numerical routines used by implementation classes in commons-math. ## 6.2 Double array utilities
To maintain statistics based on a "rolling" window of values, a resizable
array implementation was developed and is provided for reuse in the
The
ResizableDoubleArray class provides a configurable, array-backed
implementation of the ## 6.3 int/double hash map
The
OpenIntToDoubleHashMap class provides a specialized hash map
implementation for int/double. This implementation has a much smaller memory
overhead than standard ## 6.4 Continued Fractions
The
ContinuedFraction class provides a generic way to create and evaluate
continued fractions. The easiest way to create a continued fraction is
to subclass As an example, the constant π can be computed using a continued fraction. The following anonymous class provides the implementation: ContinuedFraction c = new ContinuedFraction() { public double getA(int n, double x) { switch(n) { case 0: return 3.0; default: return 6.0; } } public double getB(int n, double x) { double y = (2.0 * n) - 1.0; return y * y; } }
Then, to evalute π, simply call any of the For a more practical use of continued fractions, consider the exponential function. The following anonymous class provides its implementation: ContinuedFraction c = new ContinuedFraction() { public double getA(int n, double x) { if (n % 2 == 0) { switch(n) { case 0: return 1.0; default: return 2.0; } } else { return n; } } public double getB(int n, double x) { if (n % 2 == 0) { return -x; } else { return x; } } }
Then, to evalute ## 6.5 Binomial coefficients, factorials, Stirling numbers and other common math functionsA collection of reusable math functions is provided in the ArithmeticUtils utility class. ArithmeticUtils currently includes methods to compute the following: -
Binomial coefficients -- "n choose k" available as an (exact) long value,
`binomialCoefficient(int, int)` for small n, k; as a double,`binomialCoefficientDouble(int, int)` for larger values; and in a "super-sized" version,`binomialCoefficientLog(int, int)` that returns the natural logarithm of the value. -
Stirling numbers of the second kind -- S(n,k) as an exact long value
`stirlingS2(int, int)` for small n, k. -
Factorials -- like binomial coefficients, these are available as exact long
values,
`factorial(int)` ; doubles,`factorialDouble(int)` ; or logs,`factorialLog(int)` . - Least common multiple and greatest common denominator functions.
## 6.6 Fast mathematical functions
Apache Commons Math provides a faster, more accurate, portable alternative
to the regular
FastMath is a drop-in replacement for both Math and StrictMath. This
means that for any method in Math (say FastMath speed is achieved by relying heavily on optimizing compilers to native code present in many JVM todays and use of large tables. Precomputed literal arrays are provided in this class to speed up load time. These precomputed tables are used in the default configuration, to improve speed even at first use of the class. If users prefer to compute the tables automatically at load time, they can change a compile-time constant. This will increase class load time at first use, but this overhead will occur only once per run, regardless of the number of subsequent calls to computation methods. Note that FastMath is extensively used inside Apache Commons Math, so by calling some algorithms, the one-shot overhead when the constant is set to false will occur regardless of the end-user calling FastMath methods directly or not. Performance figures for a specific JVM and hardware can be evaluated by running the FastMathTestPerformance tests in the test directory of the source distribution.
FastMath accuracy should be mostly independent of the JVM as it relies only
on IEEE-754 basic operations and on embedded tables. Almost all operations
are accurate to about 0.5 ulp throughout the domain range. This statement, of
course is only a rough global observed behavior, it is FastMath additionally implements the following methods not found in Math/StrictMath: - asinh(double)
- acosh(double)
- atanh(double)
- pow(double,int)
- copySign(double, double)
- getExponent(double)
- nextAfter(double,double)
- nextUp(double)
- scalb(double, int)
- copySign(float, float)
- getExponent(float)
- nextAfter(float,double)
- nextUp(float)
- scalb(float, int)
## 6.7 MiscellaneousThe MultidimensionalCounter is a utility class that converts a set of indices (identifying points in a multidimensional space) to a single index (e.g. identifying a location in a one-dimensional array. |