public class Math extends Object
| Modifier and Type | Field and Description |
|---|---|
static long[] |
FIBONACCI_SEQUENCE
An array of the Fibonacci sequence up the maximum value
representable as a long integer.
|
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.
|
| Modifier and Type | Method and Description |
|---|---|
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 |
logBase2ToNaturalLog(double x)
Converts a log base 2 logarithm to a natural logarithm.
|
static double |
logSumOfExponentials(double[] xs)
This method returns the log of the sum of the natural
exponentiated values in the specified array.
|
static double |
max(double... xs)
Returns the maximum value of an element in xs.
|
static int |
max(int... xs)
Returns the maximum value of an element in the specified array.
|
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 |
relativeAbsoluteDifference(double x,
double y)
Returns the relative absolute difference between the specified
values, defined to be:
relAbsDiff(x,y) = abs(x-y) / (abs(x) + abs(y))
|
static double |
sum(double... xs)
Returns the sum of the specified array of double values.
|
static int |
sum(int... xs)
Returns the sum of the specified integer array.
|
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 double LN_2
public static final double LOG2_E
Math.E.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 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)
logBase2ToNaturalLog(double).
If the input is x = ln z, then
the return value is log2 z.
Recall that log2 z = ln z / ln 2.
x - Natural log of value.public static double logBase2ToNaturalLog(double x)
naturalLogToBase2Log(double).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 - Variable length list of values, or an array of values.public static double minimum(double... xs)
Double.NaN.xs - Variable length list of values, or an array.public static double maximum(double... xs)
Double.NaN.xs - Variable length list of values, or an array.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 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.public static double relativeAbsoluteDifference(double x,
double y)
relAbsDiff(x,y) = abs(x-y) / (abs(x) + abs(y))
x - First value.y - Second value.public static double logSumOfExponentials(double[] xs)
But the result is not calculated directly. Instead, the calculation performed is:logSumOfExponentials(xs) = log ( Σi exp(xs[i]) )
which produces the same result, but is much more arithmetically stable, because the largest value for whichlogSumOfExponentials(xs) = max(xs) + log ( Σi exp(xs[i] - max(xs)) )
exp()
is calculated is 0.0.
Values of Double.NEGATIVE_INFINITY are treated as
having exponentials of 0 and logs of negative infinity.
That is, they are ignored for the purposes of this computation.
xs - Array of values.public static double max(double... xs)
Double.NaN, or if the input array is empty,
the result is Double.NaN.xs - Array in which to find maximum.public static int max(int... xs)
xs - Array in which to find maximum.ArrayIndexOutOfBoundsException - If the specified array does
not contai at least one element.public static int sum(int... xs)
xs - Array of integers to sum.Copyright © 2016 Alias-i, Inc.. All rights reserved.