|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.aliasi.util.Math
public class Math
A math utility class with static methods.
| Field Summary | |
|---|---|
static long[] |
FIBONACCI_SEQUENCE
An array of the Fibonacci sequence. |
static double |
GOLDEN_RATIO
The value of the golden ratio. |
static double |
LN_2
The natural logarithm of 2. |
static double |
LOG2_E
The log base 2 of the constant e, which is the base of the natural logarithm. |
| Method Summary | |
|---|---|
static void |
assertFiniteNonNegative(String label,
double x)
Throws an illegal argument exception if the specified double value is not a finite non-negative number. |
static int |
byteAsUnsigned(byte b)
Returns the integer value of reading the specified byte as an unsigned value. |
static double |
digamma(double x)
Returns the value of the digamma function for the specified value. |
static boolean |
isPrime(int num)
Returns true if the specified number is prime. |
static double |
log2(double x)
Returns the log base 2 of the specivied value. |
static double |
log2BinomialCoefficient(long n,
long m)
Returns the log (base 2) of the binomial coefficient of the specified arguments. |
static double |
log2Factorial(long n)
Returns the log (base 2) of the factorial of the specified long integer. |
static double |
log2Gamma(double z)
Returns the log (base 2) of the Γ function. |
static double |
maximum(double[] xs)
Returns the maximum of the specified array of double values. |
static double |
minimum(double[] xs)
Returns the minimum of the specified array of double values. |
static double |
naturalLogToBase2Log(double x)
Converts a natural logarithm to a base 2 logarithm. |
static int |
nextPrime(int num)
Returns the smallest prime number that is strictly larger than the specified integer. |
static double |
sum(double[] xs)
Returns the sum of the specified array of double values. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final double GOLDEN_RATIO
φ = (φ + 1) / φNote that this is a quadratic equation (multiply both sides by φ) with the solution roughly
1.61803399.
See the following for a fascinating tour of the properties of the golden ratio:
public static final long[] FIBONACCI_SEQUENCE
SoFIBONACCI_SEQUENCE[0] = 1 FIBONACCI_SEQUENCE[1] = 2 FIBONACCI_SEQUENCE[n+2] = FIBONACCI_SEQUENCE[n+1] + FIBONACCI_SEQUENCE[n]
FIBONACCI_SEQUENCE[0] represents the second
Fibonacci number in the traditional numbering. The inital entries
are:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597,
2584, ...
The length of the array is 91, and the largest value is:
FIBONACCI_SEQUENCE[90] = 7540113804746346429
See the following references for more information on the fascinating properties of Fibonacci numbers:
public static final double LN_2
public static final double LOG2_E
Math.E.
| Method Detail |
|---|
public static boolean isPrime(int num)
true if the specified number is prime. A
prime is a positive number greater than 1 with no
divisors other than 1 and itself, thus
{2,3,5,7,11,13,...}.
num - Number to test for primality.
true if the specified number is prime.public static int nextPrime(int num)
isPrime(int) for the
definition of primality.
num - Base from which to look for the next prime.
public static double naturalLogToBase2Log(double x)
x = ln z, then
the return value is log2 z.
Recall that log2 z = ln z / ln 2.
- Parameters:
x - Natural log of value.
- Returns:
- Log base 2 of value.
public static double log2(double x)
x - Value whose log is taken.
public static int byteAsUnsigned(byte b)
Byte.MIN_VALUE.
b - Byte to convert.
public static double log2Factorial(long n)
n is defined for
n > 0 by:
n!
= Πi < 0 <= n i
Taking logs of both sides gives:
log2 n!
= Σi < 0 <= n
log2 i
By convention, 0! is taken to be 1, and hence ln 0! = 0.
n - Specified long integer.
IllegalArgumentException - If the argument is negative.public static double sum(double[] xs)
xs - Array of values to sum.
public static double minimum(double[] xs)
Double.NaN.
xs - Array of values.
public static double maximum(double[] xs)
Double.NaN.
xs - Array of values.
public static double log2BinomialCoefficient(long n,
long m)
m from a
set of n objects, which is pronounced "n choose
m", and is given by:
choose(n,m) = n! / ( m! * (n-m)!)
log2 choose(n,m)
= log2 n - log2 m
- log2 (n-m)
public static void assertFiniteNonNegative(String label,
double x)
label - Label of variable for exception message.x - Double value to check.
IllegalArgumentException - If the specified double value
is infinite, is not a number, or is negative.public static double log2Gamma(double z)
Γ(z) = ∫0∞ tz-1 * e-t dt
The Γ function is the continuous generalization of the factorial
function, so that for real numbers z > 0:
Γ(z+1) = z * Γ(z)
In particular, integers n >= 0, we have:
Γ(n+1) = n!
In general, Γ satisfies:
Γ(z) = π / (sin(π * z) * Γ(1-z))
This method uses the Lanczos approximation which is accurate
nearly to the full power of double-precision arithmetic. The
Lanczos approximation is used for inputs in the range
[0.5,1.5], converting numbers less than 0.5 using
the above formulas, and reducing arguments greater than 1.5
using the factorial-like expansion above.
For more information on the Γ function and its computation, see:
z - The argument to the gamma function.
Γ(z).public static double digamma(double x)
The digamma function is the derivative of the log of the
gamma function; see the method documentation for log2Gamma(double) for more information on the gamma function
itself.
Ψ(z) = d log Γ(z) / dz = Γ'(z) / Γ(z)
The numerical approximation is derived from:
x - Value at which to evaluate the digamma function.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||