com.hedgehog.math.functions
Class Functions

java.lang.Object
  extended by com.hedgehog.math.functions.Functions

public class Functions
extends java.lang.Object

Title: Functions - a collection of frequently used mathematical functions.

Description: Collection of frequently used mathematical functions.

Copyright: Copyright (c) Hedgehog Software 2007-2009.

Company: Hedgehog Software.

Since:
1.0
Version:
1.1

Method Summary
static double abs(double n)
          Returns the absolute value of a double object.
static float abs(float n)
          Returns the absolute value of a float object.
static int abs(int n)
          Returns the absolute value of an int object.
static double acos(double x)
          Returns the arc-cosine of the specified value, clamping the input argument to [-1,1] to avoid NaN issues when the input is slightly larger than 1 or slightly smaller than -1.
static double acosh(double x)
          Returns the arc hyperbolic cosine of the specified number; ie Math.log(x + Math.sqrt(x * x - 1.0));
static double acot(double x)
          Returns the arc cotangent of x, which is equivalent to atan(1/x).
static double asin(double x)
          Returns the arc-sine of the specified value, clamping the input argument to [-1,1] to avoid NaN issues when the input is slightly larger than 1 or slightly smaller than -1.
static double asinh(double x)
          Returns the arc hyperbolic sine of the specified number; ie Math.log(x + Math.sqrt(x * x + 1.0));
static double atan(double x)
          Returns the arc tangent of x.
static double atan2(double y, double x)
          Returns the angle theta from the conversion of rectangular coordinates (xy) to polar coordinates (r, theta).
static double atanh(double x)
          Returns the arc hyperbolic tangent of the specified number; ie Math.log((1.0 + x) / (1.0 - x)) / 2.0;
static double average(double[] doubles)
          Returns the average of the elements of an array of doubles.
static double average(int[] ints)
          Returns the average of the elements of an array of ints.
static double bernoulliNumber(int n)
          Returns the Bernoulli number.
static double betaFunction(double x, double y)
          Returns the Beta function B(x,y)=Gamma(x)*Gamma(y)/Gamma(x+y)
static double binomialCoefficient(int k, int i)
          Returns the binomial coefficient of (k i)=k!/(i!(k-i)!)
static int bitCount1(long a)
          Counts the number of 1 bits in a 32-bit long integer (variant 1).
static long bitCount2(long a)
          Counts the number of 1 bits in a 32-bit long integer (variant 2).
static long bitCount3(long a)
          Counts the number of 1 bits in a 32-bit long integer (variant 3).
static double ceil(double x)
          Returns the smallest value that is greater than or equal to the argument and is equal to a mathematical integer.
static int ceil2(int x)
          Returns the least power of 2 greater than or equal to x.
static double clamp(double val, double min, double max)
          Clamps the specified value to the specified [min:max] range.
static int clamp(int val, int min, int max)
          Clamps the specified value to the specified [min:max] range.
static double complementErrorFunction(double x)
          Returns the complement Error function erfc(x)=1-erf(x)=Q(1/2,x^2).
static double complementIncompleteGammaFunction(double a, double x)
          Returns the complement incomplete Gamma function Q(a,x)=1-P(a,x).
static double correlation(double[] v1, double[] v2)
          Computes the linear correlation between the two specified arrays.
static double cos(double x, boolean degrees)
          Returns cos(x).
static double cosech(double x)
          Hyperbolic cosech() function.
static double cosFast0(double x, boolean degrees)
          Optimised version of cos(x).
static double cosFast1(double x, boolean degrees)
          Optimised version of cos(x).
static double cosh(double x)
          Hyperbolic cosh() function.
static double cot(double x, boolean degrees)
          Returns the cotangent of the specified value; ie 1/tan(x).
static double coth(double x)
          Hyperbolic coth() function.
static double covariance(double[] v1, double[] v2)
          Computes the covariance.
static double cubeRoot(double x)
          Returns the cube root of the specified number.
static double degreesMinutesSecondsToDegrees(double degrees, double minutes, double seconds)
          Converts {degrees,minutes,seconds} to degrees.
static double degreesMinutesSecondsToMinutes(double degrees, double minutes, double seconds)
          Converts {degrees,minutes,seconds} to minutes.
static double degreesMinutesSecondsToSeconds(double degrees, double minutes, double seconds)
          Converts {degrees,minutes,seconds} to seconds.
static TripleDouble degreesToDegreesMinutesSeconds(double degrees)
          Converts degrees to {degrees,minutes,seconds}.
static double degreesToHours(double degrees)
          Returns a number of degrees to hours by simply dividing by 15.
static double degreesToRadians(double theta)
          Converts degrees to radians.
static double difference(double a, double b, int method)
          Returns the difference between the specified numbers.
static PairInteger div(int numer, int denom)
          Returns the quotient, q, and remainder, r, of the division (numer/denom).
static java.util.Vector<HDouble> equallySpacedPointsInInterval(double a, double b, int n, boolean includeEndPoints)
          Returns a vector of n equally spaced points within the specified interval [a:b].
static double erf(double x)
           Evaluates the error function, defined as:
static double erfComplementary(double x)
          Evaluates the complementary error function.
static double errorFunction(double x)
          Returns the Error function erf(x)=P(1/2,x^2).
static double eulerNumber(int n)
          Returns the Euler number; ie 1,0,-1,0,5,0,-61,...
static double exp(double x)
          Returns the exponential of x; ie e^x.
 double expNegativeFast0(double x)
          Fast evaluation of exp(-x).
 double expNegativeFast1(double x)
          Fast evaluation of exp(-x).
 double expNegativeFast2(double x)
          Fast evaluation of exp(-x).
 double expNegativeFast3(double x)
          Fast evaluation of exp(-x).
static double fabs(double x)
          Returns the absolute value of the floating-point value.
static double factorial(int n)
          Returns the factorial of n; ie n! n! = 1 for n<=1 and n! = n(n-1)! = n(n-1)(n-2)...
static double factorialDouble(int n)
          Returns the double factorial of n; ie n!!: n!! = 1 for n=0, 1 or -1.
 int fibonacci(int n)
          Returns the Fibonacci number of the specified sequence number.
static double floor(double x)
          Returns the floor value of x.
static int floor2(int x)
          Returns the greatest power of 2 less than or equal to x.
static double fowlerAngle(double dy, double dx)
          If you require a monotonic function of the angle, rather than needing the value of the angle itself then you can avoid the arcsine and arctangent computation by using Fowler angles.
static double fractionalPart(double n)
          Returns the positive fractional part of a number.
static double gammaFunction(double z)
          Returns Gamma(z) for z>0.
static int gcd(int n, int m)
          Returns the greatest common divisor of 2 positive integers.
static double gcf(double a, double x)
          Continued fraction evaluation of complement incomplete Gamma function Q(a,x) (used by IncompleteCompleteGammaFunction()).
static double goldenRatio()
          Returns the golden ratio (1.61803...)
static double gser(double a, double x)
          Series evaluation of gamma function (used by IncompleteCompleteGammaFunction()).
static int hash(double x)
          Returns an integer hash code representing the specified double value.
static double haverSine(double x)
          Returns the haversine of x (i.e., havx=(1-cosx)/2).
static int heavisideUnitStep(int t, int c)
          Heaviside unit step function.
static double hoursToDegrees(double hours)
          Converts hours to degrees; ie degrees=15*hours.
static double hypot(double a, double b)
          Returns sqrt(a^2 + b^2) without under/overflow.
static double incompleteGammaFunction(double a, double x)
          returns the incomplete Gamma function P(a,x).
static double intermediate_abs(double a, double b, double c)
          Returns the intermediate absolute value of the specified three double values.
static double intermediate(double a, double b, double c)
          Returns the intermediate of three double objects.
static int intermediate(int a, int b, int c)
          Returns the intermediate of three int objects.
static double intervalRandom(double min, double max, int seed)
          Generates a random number in the interval [min,max).
static double invCosFast0(double x, boolean degrees)
          Optimised version of 1/cos(x).
static double invCosFast1(double x, boolean degrees)
          Optimised version of 1/cos(x).
static double invSinFast0(double x, boolean degrees)
          Optimised version of 1/sin(x).
static double invSinFast1(double x, boolean degrees)
          Optimised version of 1/sin(x).
static double invSqrt(double x)
          Evaluates the inverse square root of x; ie 1/sqrt(x).
static double invSqrtFast(double x)
          Optimised version of 1/sqrt(x).
static float invSqrtFast(float x)
          Returns the fast approximation of the inverse square root of the specified value.
static double invTanFast0(double x, boolean degrees)
          Optimised version of 1/tan(x).
static double invTanFast1(double x, boolean degrees)
          Optimised version of 1/tan(x).
static boolean isEven(int n)
          Tests whether or not a number is even.
static boolean isEven(long n)
          Tests whether or not a number is even.
static boolean isNAN(double n)
          Tests whether or not the specified number if equal to NAN(1e38).
static boolean isOdd(int n)
          Tests whether or not a number (int) is odd.
static boolean isOdd(long n)
          Tests whether or not a number (long) is odd.
static boolean isPowerOfTwo(int number)
          Tests whether or not the specified number is a power of 2 (2,4,8,16...).
static boolean isPrime(int n)
          Tests whether or not a number (int) is a prime.
static boolean isPrime(long n)
          Tests whether or not a number (long) is a prime.
static boolean isPythagoreanTriple(int x, int y, int z)
          Returns logical-true if the int point(x,y,z) is a Pythagorean triple; ie z^2=x^2+y^2.
static boolean isPythagoreanTriple(long x, long y, long z)
          Returns logical-true if the long point(x,y,z) is a Pythagorean triple; ie z^2=x^2+y^2.
static boolean isSameValue(double x, double y, double tolerance)
          Tests whether or not x and y are the same, within the specified tolerance.
static boolean isSorted(double a, double b, boolean increasingOrder)
          Tests whether or not the specified double sequence is in order.
static boolean isSorted(double a, double b, double c, boolean increasingOrder)
          Tests whether or not the specified double sequence is in order.
static boolean isSorted(int a, int b, boolean increasingOrder)
          Tests whether or not the specified sequence is in order.
static boolean isSorted(int a, int b, int c, boolean increasingOrder)
          Tests whether or not the specified sequence is in order.
static boolean isWholeNumber(double n, double tolerance)
          Tests whether or not the specified number is a whole number within the specified tolerance.
static boolean isZero(double x, double tolerance)
          Tests whether or not a floating-point number is zero.
static boolean isZero(int x)
          Tests whether or not an int number is zero.
static boolean isZero(long x)
          Tests whether or not an long number is zero.
static void leftToRightShift(java.lang.Object a, java.lang.Object b, java.lang.Object c, java.lang.Object d)
          Shifts objects (a,b,c,d) from left to right (i.e.
static double linearInterpolation(java.util.Vector<HDouble> sample_points, double u)
          Linear interpolation in 1-dimension.
static double linearInterpolation(java.util.Vector<HDouble> sample_points, double u, double v)
          Linear interpolation in 2-dimensions.
static double linearInterpolation(java.util.Vector<HDouble> sample_points, double u, double v, double w)
          Linear interpolation in 3-dimensions.
static double log(double x)
          Returns log(x); ie the natural logarithm (base e) of x.
static double log10(double x)
          Returns the base 10 logarithm of the specified number; ie log(x)/log(10).
static double log2(double x)
          Returns the log of the specified number to base 2; ie log(x)/log(2).
static double logFactorial(int n)
          Returns log(n!) = log (n * (n-1) * ...
static double logGammaFunction(double z)
          Returns ln[Gamma(z)] for z>0.
static double max(double a, double b)
          Returns the maximum of two objects.
static double max(double a, double b, double c)
          Returns the maximum of three double objects.
static int max(int a, int b)
          Returns the maximum of two objects.
static int max(int a, int b, int c)
          Returns the maximum of three int objects.
static double maxAbs(double a, double b)
          Returns the maximum absolute value of (a,b).
static double maxAbs(double a, double b, double c)
          Returns the maximum absolute value of (a,b,c).
static int maxIndex(double[] doubles)
          Returns index of maximum element in a given array of doubles.
static int maxIndex(int[] ints)
          Returns index of maximum element in a given array of integers.
static void maxSort(HDouble a, HDouble b)
          Sorts a and b so that a>=b.
static double mean(double[] vector)
          Computes the mean of the specified array of doubles.
static double min(double a, double b)
          Returns the minimum of two objects.
static double min(double a, double b, double c)
          Returns the minimum of three double objects.
static int min(int a, int b)
          Returns the minimum of two objects.
static int min(int a, int b, int c)
          Returns the minimum of three objects.
static double minAbs(double a, double b)
          Returns the min absolute value of the specified 2 values.
static double minAbs(double a, double b, double c)
          Returns the min absolute value of the specified 3 values.
static int minIndex(double[] doubles)
          Returns index of minimum element in a given array of doubles.
static int minIndex(int[] ints)
          Returns index of minimum element in a given array of integers.
static void minSort(HDouble a, HDouble b)
          Sorts a and b so that a<=b.
static double mod(double x, double n)
          Returns the mod; ie x-nfloor(x/n).
static int nearestEven(int n, boolean increasing)
          Returns the nearest even number to the specified integer in the specified direction.
static int nearestOdd(int n, boolean increasing)
          Returns the nearest odd number to the specified integer in the specified direction.
static int nearestPowerOfTwo(int number)
          Returns the nearest power of 2 number to the specified number.
static void normalise(double[] doubles)
          Normalises the doubles in the specified array by their sum.
static void normalise(double[] doubles, double sum)
          Normalises the doubles in the array using the given value.
static double normaliseDegrees180(double degrees)
          Masks an angle to the [0:180] range.
static double normaliseDegrees360(double degrees)
          Masks an angle to the [0:360] range.
static double normaliseRadians2PI(double radians)
          Makes an angle in radians to the [0:2pi] range.
static double normaliseRadiansPI(double radians)
          Masks an angle to the [0:PI] range.
static int numberDigits(int number)
          Returns the number of digits in the specified integer number.
static boolean oppositeSign(double x, double y)
          Tests whether the arguments have opposite sign.
static boolean oppositeSignOrBothZero(double x, double y, double tolerance)
          Returns logical-true if x and y have the same sign.
static boolean oppositeSignOrZero(double x, double y, double tolerance)
          Returns logical-true if x and y have the same sign.
static double[] pascalTriangle(int n)
          Returns the Pascal triangle sequence for the specified integer.
static double poisson(double nu, int n)
          Returns the poisson distribution (nu^n e^(-nu) / n!).
static double polarAngle(double x, double y, boolean degrees)
          Returns the polar angle subtended by the specified x and y values.
static double polarRadius(double x, double y)
          Returns the polar radius; ie sqrt(x^2+y^2).
static PairDouble polarToXY(double r, double theta, boolean degrees)
          Converts the specified (r,theta) polar pair to (x,y) rectangular coordinates.
static double pow(double base, double exponent)
          Returns base to exponent.
static int pow2(int n)
          Returns 2^n.
static int pow2Exponent(int x)
          Returns the value of m for which 2^m=x.
static float powFast(float base, float exponent)
          Computes a fast but less accurate approximation to Math.pow(base,exponent).
static java.util.ArrayList<HInteger> primeNumbers(int n)
          Returns a vector of all prime numbers upto and including the specified number, n.
static java.util.ArrayList<HLong> primeNumbers(long n)
          Returns a vector of all prime numbers upto and including the specified number, n.
static PairInteger quotientRemainder(int numer, int denom)
          Same as div() but a slightly more descriptive method name!
static double radiansToDegrees(double theta)
          Converts radians to degrees.
static void rightToLeftShift(java.lang.Object a, java.lang.Object b, java.lang.Object c, java.lang.Object d)
          Shifts objects (a,b,c,d) from right to left (i.e.
static boolean sameSign(double x, double y)
          Returns logical-true if x and y have the same sign.
static boolean sameSign(int x, int y)
          Returns logical-true if x and y have the same sign.
static boolean sameSignOrBothZero(double x, double y, double tolerance)
          Returns logical-true if x and y have the same sign.
static boolean sameSignOrZero(double x, double y, double tolerance)
          Returns logical-true if x and y have the same sign.
static double sec(double x, boolean degrees)
          Returns the secant of the specified value; ie 1/cos(x).
static double sech(double x)
          Hyperbolic sech() function.
static int sign(double x)
          Returns -1 if x<0, 0 if x=0 and 1 if x>0.
static double sign(double a, double b)
          If b>0 returns |a|, else returns -|a|.
static int sign(int x)
          Returns -1 if x<0, 0 if x=0 and 1 if x>0.
static double sin(double x, boolean degrees)
          Returns sin(x).
static double sinFast0(double x, boolean degrees)
          Fast sin(x).
static double sinFast1(double x, boolean degrees)
          Fast sin(x).
static double sinh(double x)
          Hyperbolic sinh() function.
static PairDouble sort(double a, double b)
          Sorts the specified double values (a,b) in increasing order.
static PairDouble sort(double a, double b, boolean sortIncreasing)
          Sorts the specified floating-point values (a,b) in increasing order.
static TripleDouble sort(double a, double b, double c)
          Sorts the specified double values (a,b,c) in increasing order.
static TripleDouble sort(double a, double b, double c, boolean sortIncreasing)
          Sorts the specified floating-point values (a,b,c) in increasing order.
static PairInteger sort(int a, int b)
          Sorts the specified integer values (a,b) in increasing order.
static PairInteger sort(int a, int b, boolean sortIncreasing)
          Sorts the specified integer values (a,b) in increasing order.
static TripleInteger sort(int a, int b, int c)
          Sorts the specified integer values (a,b,c) in increasing order.
static TripleInteger sort(int a, int b, int c, boolean sortIncreasing)
          Sorts the specified integer values (a,b,c) in increasing order.
static double sqr(double x)
          Returns the square of the specified number; ie x^2.
static double sqrt(double x)
          Returns the square root of the specified value.
static float sqrtFast0(float x)
          Fast evaluation of the square root of the specified number.
static double standardDeviation(double[] vector)
          Returns the standard deviation of the specified array and returns INFINITE if the the vector is either empty of has only one element.
static double sum(double[] doubles)
          Returns the sum of the elements of an array of doubles.
static int sum(int[] ints)
          Returns the sum of the elements of an array of integers.
static double symmetricRandom(int seed)
          Generates a random number in the interval [-1,1).
static double tan(double x, boolean degrees)
          Returns tan(x).
static double tanFast0(double x, boolean degrees)
          Optimised version of tan(x).
static double tanFast1(double x, boolean degrees)
          Optimised version of tan(x).
static double tanh(double x)
          Hyperbolic tanh() function.
static double unitRandom(int seed)
          Generates a random number in the interval [0,1).
static double variance(double[] vector)
          Computes the variance of the specified array of doubles.
static double wholePart(double n)
          Returns the whole part of a number, stripping out the negative sign if present.
static double wholePartAndSign(double n)
          Returns the whole part of a number, keeping negative sign if present.
static boolean withinAbsoluteDifference(double a, double b, double absoluteTolerance)
          Tests whether or not arguments a and b are within the specified absolute tolerance.
static boolean withinPercentageDifference(double a, double b, int percentageTolerance)
          Tests whether or not arguments a and b are within the specified percentage tolerance.
static PairDouble xyToPolar(double x, double y, boolean degrees)
          Combines polarRadius() and polarAngle() by returning a pair.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

abs

public static int abs(int n)
Returns the absolute value of an int object.

Parameters:
n - Integer to return the absolute value of.
Returns:
The absolute value of argument n.

abs

public static double abs(double n)
Returns the absolute value of a double object.

Parameters:
n - Double to return the absolute value of.
Returns:
The absolute value of argument n.

abs

public static float abs(float n)
Returns the absolute value of a float object.

Parameters:
n - Float to return the absolute value of.
Returns:
The absolute value of argument n.

bernoulliNumber

public static double bernoulliNumber(int n)
                              throws java.lang.IllegalArgumentException
Returns the Bernoulli number. For numerical efficiency the numbers are precomputed and looked up from a table. As a result they are restricted to n up to and including 32. For further details refer to http://en.wikipedia.org/wiki/Bernoulli_number.

Parameters:
n - Particular number.
Returns:
Bernoulli number Bn.
Throws:
java.lang.IllegalArgumentException - Thrown if n<0 or n>32.

eulerNumber

public static double eulerNumber(int n)
                          throws java.lang.IllegalArgumentException
Returns the Euler number; ie 1,0,-1,0,5,0,-61,... in which all odd-index numbers are 0 and even-index numbers have alternating signs. For numerical efficiency the numbers are precomputed and looked up from a table. As a result they are restricted to n up to and including 18. For further details refer to http://en.wikipedia.org/wiki/Euler_numbers.

Parameters:
n - Particular number.
Returns:
Euler number En.
Throws:
java.lang.IllegalArgumentException - Thrown if n<0 or n>18.

betaFunction

public static double betaFunction(double x,
                                  double y)
Returns the Beta function B(x,y)=Gamma(x)*Gamma(y)/Gamma(x+y)

Parameters:
x - Value x.
y - Value y.
Returns:
Value of B(x,y).

binomialCoefficient

public static double binomialCoefficient(int k,
                                         int i)
Returns the binomial coefficient of (k i)=k!/(i!(k-i)!)

Parameters:
k - Value k.
i - Value i.
Returns:
The Binomial coefficient.

errorFunction

public static double errorFunction(double x)
Returns the Error function erf(x)=P(1/2,x^2).

Parameters:
x - Value x.
Returns:
erf(x).

complementErrorFunction

public static double complementErrorFunction(double x)
Returns the complement Error function erfc(x)=1-erf(x)=Q(1/2,x^2).

Parameters:
x - Value x.
Returns:
erfc(x).

factorial

public static double factorial(int n)
Returns the factorial of n; ie n! n! = 1 for n<=1 and n! = n(n-1)! = n(n-1)(n-2)... for n>1. For n = 0, 1, 2, ... the sequence is 1, 1, 2, 6, 24, 120, 720, 5,040, ...

Parameters:
n - Number to evaluate factorial.
Returns:
Factorial(n).

factorialDouble

public static double factorialDouble(int n)
Returns the double factorial of n; ie n!!: n!! = 1 for n=0, 1 or -1. For n >= 2 n!! = n(n-2)!! = n(n-2)(n-4)... For n = 0, 1, 2, ... the sequence is 1, 1, 2, 3, 8, 15, 48, 105, 384, 945, 3840, ... For further details refer to Wikipedia or MathWorld.

Parameters:
n - Number to evaluate factorial.
Returns:
Factorial(n).
Since:
1.1

fibonacci

public int fibonacci(int n)
Returns the Fibonacci number of the specified sequence number. For example F(6)=8.

Parameters:
n - Sequence number.
Returns:
Fibonacci number.

fractionalPart

public static double fractionalPart(double n)
Returns the positive fractional part of a number.

Parameters:
n - Number to extract the fractional part.
Returns:
The fractional part of n.

degreesToRadians

public static double degreesToRadians(double theta)
Converts degrees to radians.

Parameters:
theta - Angle in degrees.
Returns:
Radian equivalent of degree argument.

radiansToDegrees

public static double radiansToDegrees(double theta)
Converts radians to degrees.

Parameters:
theta - Angle in radians.
Returns:
Degree equivalent of radian argument.

normaliseDegrees360

public static double normaliseDegrees360(double degrees)
Masks an angle to the [0:360] range.

Parameters:
degrees - The number of degrees.
Returns:
The specified angle masked to the range [0:360].

normaliseRadians2PI

public static double normaliseRadians2PI(double radians)
Makes an angle in radians to the [0:2pi] range.

Parameters:
radians - The number of radians.
Returns:
The specified angle masked to the range [0:2pi].

normaliseDegrees180

public static double normaliseDegrees180(double degrees)
Masks an angle to the [0:180] range. The method first calls normaliseDegrees() so that the specified angle is masked to the range [0:360]. The resulting angle theta is then masked to the range [0:180] via -(360-theta).

Parameters:
degrees - The number of degrees.
Returns:
The specified angle masked to the range [0:180].

normaliseRadiansPI

public static double normaliseRadiansPI(double radians)
Masks an angle to the [0:PI] range. The method first calls normaliseRadians() so that the specified angle is masked to the range [0:2PI]. The resulting angle theta is then masked to the range [0:PI] via -(2PI-theta).

Parameters:
radians - The number of radians.
Returns:
The specified angle masked to the range [0:PI].

primeNumbers

public static java.util.ArrayList<HInteger> primeNumbers(int n)
Returns a vector of all prime numbers upto and including the specified number, n.

Parameters:
n - Number up to which primes to be generated.
Returns:
A vector of primes.

primeNumbers

public static java.util.ArrayList<HLong> primeNumbers(long n)
Returns a vector of all prime numbers upto and including the specified number, n.

Parameters:
n - Number up to which primes to be generated.
Returns:
A vector of primes.

heavisideUnitStep

public static int heavisideUnitStep(int t,
                                    int c)
Heaviside unit step function. Returns 0 for t=c.

Parameters:
t - Variable t.
c - Cutoff value.
Returns:
0 for t=c.

hypot

public static double hypot(double a,
                           double b)
Returns sqrt(a^2 + b^2) without under/overflow.

Parameters:
a - Side length a.
b - Side length b.
Returns:
sqrt(a^2 + b^2).

goldenRatio

public static double goldenRatio()
Returns the golden ratio (1.61803...)

Returns:
The golden ratio.

gcd

public static int gcd(int n,
                      int m)
Returns the greatest common divisor of 2 positive integers. Refer to Budd, p79 for discussion of Euclid's gcd() algorithm.

Parameters:
n - Number n.
m - Number m.
Returns:
GCD of n and m.

div

public static PairInteger div(int numer,
                              int denom)
Returns the quotient, q, and remainder, r, of the division (numer/denom). The method name div() mimmics the similarly named C function. The remainder is defined as r=numer-(q*denom). For example; div(2008,19) = <105,13>.

Parameters:
numer - The numerator.
denom - The denominator.
Returns:
The pair.

quotientRemainder

public static PairInteger quotientRemainder(int numer,
                                            int denom)
Same as div() but a slightly more descriptive method name!

Parameters:
numer - The numerator.
denom - The denominator.
Returns:
The pair.

hash

public static int hash(double x)
Returns an integer hash code representing the specified double value.

Parameters:
x - The value to be hashed.
Returns:
The hash code.

haverSine

public static double haverSine(double x)
Returns the haversine of x (i.e., havx=(1-cosx)/2).

Parameters:
x - Value x, must be specified in radians.
Returns:
havx.

intermediate

public static int intermediate(int a,
                               int b,
                               int c)
Returns the intermediate of three int objects.

Parameters:
a - Number a.
b - Number b.
c - Number c.
Returns:
Intermidate number of a, b and c.

intermediate

public static double intermediate(double a,
                                  double b,
                                  double c)
Returns the intermediate of three double objects.

Parameters:
a - Number a.
b - Number b.
c - Number c.
Returns:
Intermidate number of a, b and c.

intermediate_abs

public static double intermediate_abs(double a,
                                      double b,
                                      double c)
Returns the intermediate absolute value of the specified three double values.

Parameters:
a - Value 1.
b - Value 2.
c - Value 3.
Returns:
Intermediate absolute value of three specified objects.

incompleteGammaFunction

public static double incompleteGammaFunction(double a,
                                             double x)
returns the incomplete Gamma function P(a,x). Refer to Press et al, p218.

Parameters:
a - Number a.
x - Number x.
Returns:
P(a,x).

complementIncompleteGammaFunction

public static double complementIncompleteGammaFunction(double a,
                                                       double x)
Returns the complement incomplete Gamma function Q(a,x)=1-P(a,x). Refer to Press et al, p218.

Parameters:
a - Number a.
x - Number x.
Returns:
Q(a,x)=1-P(a,x).

gammaFunction

public static double gammaFunction(double z)
Returns Gamma(z) for z>0. Refer to Press et al, p214.

Parameters:
z - Number x.
Returns:
Gamma(z).

gser

public static double gser(double a,
                          double x)
Series evaluation of gamma function (used by IncompleteCompleteGammaFunction()). Refer to Press et al, p218.

Parameters:
a - Number a.
x - Number x.
Returns:
Gser(a,x). Returns -1 if a is too large and internal tolerance is too small.

gcf

public static double gcf(double a,
                         double x)
Continued fraction evaluation of complement incomplete Gamma function Q(a,x) (used by IncompleteCompleteGammaFunction()). Refer to Press et al, p219

Parameters:
a - Number a.
x - Number x.
Returns:
Gcf(a,x). Returns -1 if a is too large and internal tolerance is too small.

isEven

public static boolean isEven(int n)
Tests whether or not a number is even.

Parameters:
n - Number (int) to test.
Returns:
Logical-true if n is even, else logical-false.

isEven

public static boolean isEven(long n)
Tests whether or not a number is even.

Parameters:
n - Number (long) to test.
Returns:
Logical-true if n is even, else logical-false.

isOdd

public static boolean isOdd(int n)
Tests whether or not a number (int) is odd.

Parameters:
n - Number to test.
Returns:
Logical-true if n is even, else logical-false.

isOdd

public static boolean isOdd(long n)
Tests whether or not a number (long) is odd.

Parameters:
n - Number to test.
Returns:
Logical-true if n is even, else logical-false.

isPrime

public static boolean isPrime(int n)
Tests whether or not a number (int) is a prime.

Parameters:
n - Number to test.
Returns:
Logical-true if n is a prime, else logical-false.

isPrime

public static boolean isPrime(long n)
Tests whether or not a number (long) is a prime.

Parameters:
n - Number to test.
Returns:
Logical-true if n is a prime, else logical-false.

isPythagoreanTriple

public static boolean isPythagoreanTriple(int x,
                                          int y,
                                          int z)
Returns logical-true if the int point(x,y,z) is a Pythagorean triple; ie z^2=x^2+y^2.

Parameters:
x - Value x.
y - Value y.
z - Value z.
Returns:
Logical-true if (x,y,z) is a Pythagorean triple, else logical-false.

isPythagoreanTriple

public static boolean isPythagoreanTriple(long x,
                                          long y,
                                          long z)
Returns logical-true if the long point(x,y,z) is a Pythagorean triple; ie z^2=x^2+y^2.

Parameters:
x - Value x.
y - Value y.
z - Value z.
Returns:
Logical-true if (x,y,z) is a Pythagorean triple, else logical-false.

isSorted

public static boolean isSorted(int a,
                               int b,
                               int c,
                               boolean increasingOrder)
Tests whether or not the specified sequence is in order.

Parameters:
a - First value.
b - Second value.
c - Third value.
increasingOrder - Specifies whether or not the specified triple is to be tested in increasing order (true) or decreasing order (false).
Returns:
Logical-true if the specified triple is sorted.

isSorted

public static boolean isSorted(int a,
                               int b,
                               boolean increasingOrder)
Tests whether or not the specified sequence is in order.

Parameters:
a - First value.
b - Second value.
increasingOrder - Specifies whether or not the specified pair is to be tested in increasing order (true) or decreasing order (false).
Returns:
Logical-true if the specified triple is sorted.

isSorted

public static boolean isSorted(double a,
                               double b,
                               double c,
                               boolean increasingOrder)
Tests whether or not the specified double sequence is in order.

Parameters:
a - First value.
b - Second value.
c - Third value.
increasingOrder - Specifies whether or not the specified triple is to be tested in increasing order (true) or decreasing order (false).
Returns:
Logical-true if the specified triple is sorted.

isSorted

public static boolean isSorted(double a,
                               double b,
                               boolean increasingOrder)
Tests whether or not the specified double sequence is in order.

Parameters:
a - First value.
b - Second value.
increasingOrder - Specifies whether or not the specified pair is to be tested in increasing order (true) or decreasing order (false).
Returns:
Logical-true if the specified triple is sorted.

isPowerOfTwo

public static boolean isPowerOfTwo(int number)
Tests whether or not the specified number is a power of 2 (2,4,8,16...). A number is a power of two if and only if it is the smallest number with that number of significant bits. Therefore, if you subtract 1, you know that the new number will have fewer bits, so ANDing the original number with anything less than it will give 0.

Parameters:
number - The number to test.
Returns:
Logical-true if number is a power of two, else logical-false.

nearestPowerOfTwo

public static int nearestPowerOfTwo(int number)
Returns the nearest power of 2 number to the specified number.

Parameters:
number - The number.
Returns:
The nearest power of 2 number to the specified number.

isZero

public static boolean isZero(int x)
Tests whether or not an int number is zero.

Parameters:
x - Number to test.
Returns:
Logical-true if int x is zero, else logical-false.

isZero

public static boolean isZero(long x)
Tests whether or not an long number is zero.

Parameters:
x - Number to test.
Returns:
Logical-true if int x is zero, else logical-false.

isZero

public static boolean isZero(double x,
                             double tolerance)
Tests whether or not a floating-point number is zero.

Parameters:
x - Number to test.
tolerance - Tolerance against which x is compared.
Returns:
Logical-true if x is zero within the specified tolerance

logGammaFunction

public static double logGammaFunction(double z)
                               throws java.lang.IllegalArgumentException
Returns ln[Gamma(z)] for z>0. Refer to Press et al, p214.

Parameters:
z - Number z.
Returns:
ln[Gamma(z)]
Throws:
java.lang.IllegalArgumentException - Thrown if z < 0.

logFactorial

public static double logFactorial(int n)
                           throws java.lang.IllegalArgumentException
Returns log(n!) = log (n * (n-1) * ... 2 * 1).

Parameters:
n - Number.
Returns:
log(n!).
Throws:
java.lang.IllegalArgumentException - Thrown if n < 0.

poisson

public static double poisson(double nu,
                             int n)
Returns the poisson distribution (nu^n e^(-nu) / n!).

Parameters:
nu -
n -
Returns:
poisson_nu(n).

leftToRightShift

public static void leftToRightShift(java.lang.Object a,
                                    java.lang.Object b,
                                    java.lang.Object c,
                                    java.lang.Object d)
Shifts objects (a,b,c,d) from left to right (i.e. d=c, c=b and b=a).

Parameters:
a - Number a.
b - Number b.
c - Number c.
d - Number d.

max

public static int max(int a,
                      int b)
Returns the maximum of two objects.

Parameters:
a - Number a.
b - Number b.
Returns:
The maximum of numbers a and b.

max

public static double max(double a,
                         double b)
Returns the maximum of two objects.

Parameters:
a - Number a.
b - Number b.
Returns:
The maximum of numbers a and b.

max

public static int max(int a,
                      int b,
                      int c)
Returns the maximum of three int objects.

Parameters:
a - Number a.
b - Number b.
c - Number c.
Returns:
The maximum of numbers a and b.

max

public static double max(double a,
                         double b,
                         double c)
Returns the maximum of three double objects.

Parameters:
a - Number a.
b - Number b.
c - Number c.
Returns:
The maximum of numbers a and b.

min

public static int min(int a,
                      int b)
Returns the minimum of two objects.

Parameters:
a - Number a.
b - Number b.
Returns:
The mainimum of numbers a and b.

min

public static double min(double a,
                         double b)
Returns the minimum of two objects.

Parameters:
a - Number a.
b - Number b.
Returns:
The mainimum of numbers a and b.

min

public static int min(int a,
                      int b,
                      int c)
Returns the minimum of three objects.

Parameters:
a - Number a.
b - Number b.
c - Number c.
Returns:
The minimum of numbers a and b.

min

public static double min(double a,
                         double b,
                         double c)
Returns the minimum of three double objects.

Parameters:
a - Number a.
b - Number b.
c - Number c.
Returns:
The minimum of numbers a and b.

rightToLeftShift

public static void rightToLeftShift(java.lang.Object a,
                                    java.lang.Object b,
                                    java.lang.Object c,
                                    java.lang.Object d)
Shifts objects (a,b,c,d) from right to left (i.e. a=b, b=c and c=d).

Parameters:
a - Number a.
b - number b.
c - Number c.
d - Number d.

log10

public static double log10(double x)
Returns the base 10 logarithm of the specified number; ie log(x)/log(10).

Parameters:
x - The number.
Returns:
log to base 10 of the specified number.

log2

public static double log2(double x)
Returns the log of the specified number to base 2; ie log(x)/log(2).

Parameters:
x - The number.
Returns:
The log of x to base 2.

pascalTriangle

public static double[] pascalTriangle(int n)
Returns the Pascal triangle sequence for the specified integer. For example the first few sequences are {1}, {1,1}, {1,2,1}, {1,3,3,1}, ...

Parameters:
n - Order of sequence.
Returns:
Array of Pascal triangle sequence values.

asinh

public static double asinh(double x)
Returns the arc hyperbolic sine of the specified number; ie Math.log(x + Math.sqrt(x * x + 1.0));

Parameters:
x - The number.
Returns:
asinh(x).

acosh

public static double acosh(double x)
Returns the arc hyperbolic cosine of the specified number; ie Math.log(x + Math.sqrt(x * x - 1.0));

Parameters:
x - The number.
Returns:
acosh(x).

atanh

public static double atanh(double x)
Returns the arc hyperbolic tangent of the specified number; ie Math.log((1.0 + x) / (1.0 - x)) / 2.0;

Parameters:
x - The number.
Returns:
atanh(x).

sameSign

public static boolean sameSign(int x,
                               int y)
Returns logical-true if x and y have the same sign.

Parameters:
x - Number x.
y - Number y.
Returns:
Logical-true if x and y have the same sign, else logical-false.

sameSign

public static boolean sameSign(double x,
                               double y)
Returns logical-true if x and y have the same sign.

Parameters:
x - Number x.
y - Number y.
Returns:
Logical-true if x and y have the same sign, else logical-false.

sign

public static int sign(int x)
Returns -1 if x<0, 0 if x=0 and 1 if x>0.

Parameters:
x - Number to test.
Returns:
-1 if x<0, 0 if x=0 and 1 if x>0.

sign

public static int sign(double x)
Returns -1 if x<0, 0 if x=0 and 1 if x>0.

Parameters:
x - Number to test.
Returns:
-1 if x<0, 0 if x=0 and 1 if x>0.

acos

public static double acos(double x)
Returns the arc-cosine of the specified value, clamping the input argument to [-1,1] to avoid NaN issues when the input is slightly larger than 1 or slightly smaller than -1.

Parameters:
x - Value to evaluate.
Returns:
arc-cosine, clamped to [-1:1] interval.

asin

public static double asin(double x)
Returns the arc-sine of the specified value, clamping the input argument to [-1,1] to avoid NaN issues when the input is slightly larger than 1 or slightly smaller than -1.

Parameters:
x - Value to evaluate.
Returns:
arc-sine, clamped to [-1:1] interval.

atan

public static double atan(double x)
Returns the arc tangent of x.

Parameters:
x - Value.
Returns:
Arc tangent.

atan2

public static double atan2(double y,
                           double x)
Returns the angle theta from the conversion of rectangular coordinates (xy) to polar coordinates (r, theta).

Parameters:
y - The ordinate coordinate.
x - The abscissa coordinate.
Returns:
atan2().

acot

public static double acot(double x)
Returns the arc cotangent of x, which is equivalent to atan(1/x). If x=0 then PI/2 radians is returned.

Parameters:
x - Value.
Returns:
Arc cotangent.
Since:
1.1

ceil

public static double ceil(double x)
Returns the smallest value that is greater than or equal to the argument and is equal to a mathematical integer.

Parameters:
x - Value.
Returns:
Ceiling value of x.

clamp

public static double clamp(double val,
                           double min,
                           double max)
Clamps the specified value to the specified [min:max] range. If val max then max is returned. Otherwise, val is returned.

Parameters:
val - Value to clamp.
min - Minimum value.
max - Maximum value.
Returns:
Clamped value.

clamp

public static int clamp(int val,
                        int min,
                        int max)
Clamps the specified value to the specified [min:max] range. If val max then max is returned. Otherwise, val is returned.

Parameters:
val - Value to clamp.
min - Minimum value.
max - Maximum value.
Returns:
Clamped value.

exp

public static double exp(double x)
Returns the exponential of x; ie e^x.

Parameters:
x - Value.
Returns:
exp(x).

expNegativeFast0

public double expNegativeFast0(double x)
Fast evaluation of exp(-x). Error~1e-04, speedup~25.

Parameters:
x - Value of x. Must be >0.
Returns:
exp(-x).

expNegativeFast1

public double expNegativeFast1(double x)
Fast evaluation of exp(-x). Error~1e-05, speedup~25.

Parameters:
x - Value of x. Must be >0.
Returns:
exp(-x).

expNegativeFast2

public double expNegativeFast2(double x)
Fast evaluation of exp(-x). Error~1e-06, speedup~20.

Parameters:
x - Value of x. Must be >0.
Returns:
exp(-x).

expNegativeFast3

public double expNegativeFast3(double x)
Fast evaluation of exp(-x). Error~1e-07, speedup~17.

Parameters:
x - Value of x. Must be >0.
Returns:
exp(-x).

fabs

public static double fabs(double x)
Returns the absolute value of the floating-point value. Provided for consistency with C/C++ which provide a fabs() function.

Parameters:
x - Value.
Returns:
Absolute value of x.

floor

public static double floor(double x)
Returns the floor value of x.

Parameters:
x - Value.
Returns:
The floored value of x.

floor2

public static int floor2(int x)
                  throws java.lang.IllegalArgumentException
Returns the greatest power of 2 less than or equal to x. For details refer to "Hackers Delight" by HS Warren, Addison-Wesley, 2003, p47.

Parameters:
x - Value.
Returns:
The floored value of x to the nearest power of 2.
Throws:
java.lang.IllegalArgumentException - Thrown if x is negative.

ceil2

public static int ceil2(int x)
                 throws java.lang.IllegalArgumentException
Returns the least power of 2 greater than or equal to x. For details refer to "Hackers Delight" by HS Warren, Addison-Wesley, 2003, p49.

Parameters:
x - Value.
Returns:
The ceiling value of x to the nearest power of 2.
Throws:
java.lang.IllegalArgumentException - Thrown if x is negative.

mod

public static double mod(double x,
                         double n)
                  throws java.lang.IllegalArgumentException
Returns the mod; ie x-nfloor(x/n).

Parameters:
x - Numerator.
n - Denominator.
Returns:
mod of x/n.
Throws:
java.lang.IllegalArgumentException - Thrown if n is equal to 0, within tolerance hedgehog.utility.Tolerances.ZERO.

invSqrt

public static double invSqrt(double x)
                      throws java.lang.IllegalArgumentException
Evaluates the inverse square root of x; ie 1/sqrt(x).

Parameters:
x - Value.
Returns:
1/sqrt(x).
Throws:
java.lang.IllegalArgumentException - Thrown if x<=0.0.

log

public static double log(double x)
Returns log(x); ie the natural logarithm (base e) of x.

Parameters:
x - Value.
Returns:
log(x).

pow2

public static int pow2(int n)
Returns 2^n.

Parameters:
n - Exponent.
Returns:
The value of integer 2 raised to the power n.

pow2Exponent

public static int pow2Exponent(int x)
                        throws java.lang.IllegalArgumentException
Returns the value of m for which 2^m=x. For example, if x=64 then m=6.

Parameters:
x - Value. Must be a power of 2.
Returns:
Exponent of 2^m.
Throws:
java.lang.IllegalArgumentException - Thrown if x is not a power of 2.

pow

public static double pow(double base,
                         double exponent)
Returns base to exponent.

Parameters:
base - Base value.
exponent - Exponent value.
Returns:
pow(base,exponent).

powFast

public static final float powFast(float base,
                                  float exponent)
Computes a fast but less accurate approximation to Math.pow(base,exponent).

Parameters:
base - A positive number.
exponent - Power.
Returns:
base^exponent.

sqr

public static double sqr(double x)
Returns the square of the specified number; ie x^2.

Parameters:
x - Number to calculate the square of.
Returns:
Square of x.

sqrt

public static double sqrt(double x)
Returns the square root of the specified value.

Parameters:
x - Number to calculate the square root of.
Returns:
Square root of x.

cubeRoot

public static double cubeRoot(double x)
Returns the cube root of the specified number.

Parameters:
x - Number to calculate the cube root of.
Returns:
Cube root of x.

sqrtFast0

public static float sqrtFast0(float x)
Fast evaluation of the square root of the specified number. Best accuracy is obtained for even powers of 2 (e.g. 1.0), but for other numbers the results will be slightly too big (e.g. you get 1.5 for 2.0 instead of 1.414... with 6% error). For details refer to Wikipedia.

Parameters:
x - Number to evaluate.
Returns:
Square root ox x.

sin

public static double sin(double x,
                         boolean degrees)
Returns sin(x).

Parameters:
x - Number x.
degrees - Specifies whether or not x is in degrees or radians.
Returns:
sin(x).

cos

public static double cos(double x,
                         boolean degrees)
Returns cos(x).

Parameters:
x - Number x.
degrees - Specifies whether or not x is in degrees or radians.
Returns:
cos(x).

tan

public static double tan(double x,
                         boolean degrees)
Returns tan(x).

Parameters:
x - Number x.
degrees - Specifies whether or not x is in degrees or radians.
Returns:
tan(x).

cosh

public static double cosh(double x)
Hyperbolic cosh() function.

Parameters:
x - Variable x.
Returns:
cosh(x).

sinh

public static double sinh(double x)
Hyperbolic sinh() function.

Parameters:
x - Variable x.
Returns:
sinh(x).

tanh

public static double tanh(double x)
Hyperbolic tanh() function.

Parameters:
x - Variable x.
Returns:
tanh(x).

cosech

public static double cosech(double x)
Hyperbolic cosech() function.

Parameters:
x - Variable x.
Returns:
cosech(x).

sech

public static double sech(double x)
Hyperbolic sech() function.

Parameters:
x - Variable x.
Returns:
sech(x).

coth

public static double coth(double x)
Hyperbolic coth() function.

Parameters:
x - Variable x.
Returns:
coth(x).

cot

public static double cot(double x,
                         boolean degrees)
                  throws DivisionByZeroException
Returns the cotangent of the specified value; ie 1/tan(x).

Parameters:
x - Variable x.
degrees - Specifies whether or not the returned angle is in degrees or radians.
Returns:
cot(x).
Throws:
DivisionByZeroException - Thrown if in computing cot(x), tan(x) is zero.

sec

public static double sec(double x,
                         boolean degrees)
                  throws DivisionByZeroException
Returns the secant of the specified value; ie 1/cos(x).

Parameters:
x - Variable x.
degrees - Specifies whether or not the returned angle is in degrees or radians.
Returns:
sec(x).
Throws:
DivisionByZeroException - Thrown if in computing sec(x), cos(x) is zero.

sinFast0

public static double sinFast0(double x,
                              boolean degrees)
Fast sin(x). Faster but less accurate than sinFast1().

Parameters:
x - Number.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
Value of sin(x).

sinFast1

public static double sinFast1(double x,
                              boolean degrees)
Fast sin(x). More accurate but slower than sinFast0().

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
Value of sin(x).

cosFast0

public static double cosFast0(double x,
                              boolean degrees)
Optimised version of cos(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
cos(x).

cosFast1

public static double cosFast1(double x,
                              boolean degrees)
Optimised version of cos(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
cos(x)

tanFast0

public static double tanFast0(double x,
                              boolean degrees)
Optimised version of tan(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
tan(x).

tanFast1

public static double tanFast1(double x,
                              boolean degrees)
Optimised version of tan(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
tan(x).

invSinFast0

public static double invSinFast0(double x,
                                 boolean degrees)
Optimised version of 1/sin(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
1/sin(x).

invSinFast1

public static double invSinFast1(double x,
                                 boolean degrees)
Optimised version of 1/sin(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
1/sin(x).

invCosFast0

public static double invCosFast0(double x,
                                 boolean degrees)
Optimised version of 1/cos(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
1/cos(x).

invCosFast1

public static double invCosFast1(double x,
                                 boolean degrees)
Optimised version of 1/cos(x).

Parameters:
x - Number to evaluate.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
1/cos(x).

invTanFast0

public static double invTanFast0(double x,
                                 boolean degrees)
Optimised version of 1/tan(x).

Parameters:
x - Variable x.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
1/tan(x).

invTanFast1

public static double invTanFast1(double x,
                                 boolean degrees)
Optimised version of 1/tan(x).

Parameters:
x - Variable x.
degrees - Specifies whether or not the returned value is in degrees or radians.
Returns:
1/tan(x).

invSqrtFast

public static final float invSqrtFast(float x)
Returns the fast approximation of the inverse square root of the specified value.

Parameters:
x - Value that inverse square root required.
Returns:
Inverse square root.

invSqrtFast

public static final double invSqrtFast(double x)
Optimised version of 1/sqrt(x).

Parameters:
x - Value.
Returns:
1/sqrt(x).

difference

public static double difference(double a,
                                double b,
                                int method)
                         throws java.lang.IllegalArgumentException
Returns the difference between the specified numbers.

Parameters:
a - Number 1.
b - Number 2.
method - Indicates the type of difference value returned:
                0: a-b
                1: |a-b|
                2: (a-b)/a
                3: (a-b)/b
                4: |(a-b)/a|
                5: |(a-b)/b|
                6: |(a-b)/a|x100%
                7: |(a-b)/b|*100%
 
Returns:
The difference according to the specified method.
Throws:
java.lang.IllegalArgumentException - Thrown if the specified method is invalid or if for a particular method a potential division by zero would occur.

withinAbsoluteDifference

public static boolean withinAbsoluteDifference(double a,
                                               double b,
                                               double absoluteTolerance)
Tests whether or not arguments a and b are within the specified absolute tolerance.

Parameters:
a - Number 1.
b - Number 2.
absoluteTolerance - Absolute tolerance.
Returns:
Logical-true if a and b are within the specified absolute tolerance, else logical-false.

withinPercentageDifference

public static boolean withinPercentageDifference(double a,
                                                 double b,
                                                 int percentageTolerance)
                                          throws java.lang.IllegalArgumentException
Tests whether or not arguments a and b are within the specified percentage tolerance.

Parameters:
a - Number 1.
b - Number 2.
percentageTolerance - Percentage tolerance.
Returns:
Logical-true if a and b are within the specified percentage tolerance, else logical-false.
Throws:
java.lang.IllegalArgumentException - Thrown is percentageTolerance is out of range [0:100].

fowlerAngle

public static double fowlerAngle(double dy,
                                 double dx)
If you require a monotonic function of the angle, rather than needing the value of the angle itself then you can avoid the arcsine and arctangent computation by using Fowler angles. These use dx and dy to return a value that can be used for angular comparison routines. Given dy and dx between 2 points A and B this method returns a number in [0.0, 8.0) which is a monotonic function of the direction from A to B:
 (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0) correspond to
   (  0,  45,  90, 135, 180, 225, 270, 315, 360) degrees, measured anticlockwise from the positive x-axis
 
For example fowlerAngle(2,4) = 0.5 and fowlerAngle(3,-4) = 3.25.

Parameters:
dy - Difference in y-direction.
dx - Difference in x-direction.
Returns:
Fowler angle.
See Also:
polarAngle(double, double, boolean)

polarAngle

public static double polarAngle(double x,
                                double y,
                                boolean degrees)
Returns the polar angle subtended by the specified x and y values. The angle is wrt the traditional set of x-y axes with x-axis acting to the right and the y-axis acting vertically upwards. The angle is measured from the x-axis in an anticlockwise manner.

Parameters:
x - Distance along the x-axis.
y - Distance along the y-axis.
degrees - Specifies whether or not the returned angle is in degrees, else radians.
Returns:
Polar angle (in [0:360] range) between x axis and point (x,y) measured in anticlockwise manner from x-axis.
See Also:
polarRadius(double, double), fowlerAngle(double, double)

polarRadius

public static double polarRadius(double x,
                                 double y)
Returns the polar radius; ie sqrt(x^2+y^2).

Parameters:
x - Distance along the x-axis.
y - Distance along the y-axis.
Returns:
Polar radial distance of the point defined by (x,y).
See Also:
polarAngle(double, double, boolean)

xyToPolar

public static PairDouble xyToPolar(double x,
                                   double y,
                                   boolean degrees)
Combines polarRadius() and polarAngle() by returning a pair.

Parameters:
x - Distance along the x-axis.
y - Distance along the y-axis.
degrees - Specifies whether or not the returned angle is in degrees, else radians.
Returns:
Polar pair of the speciifed (x,y).
See Also:
polarAngle(double, double, boolean), polarRadius(double, double), polarToXY(double, double, boolean)

polarToXY

public static PairDouble polarToXY(double r,
                                   double theta,
                                   boolean degrees)
Converts the specified (r,theta) polar pair to (x,y) rectangular coordinates.

Parameters:
r - Radial distance.
theta - Angle subtended from x-axis. The angle can be positive or negative and multiples of 360 degrees as rev() is used to mask the angle to the [0:360] range.
degrees - Specifies whether or not the specified nagle is in degrees or radians.
Returns:
Rectangular pair of the specified (r,theta).
See Also:
xyToPolar(double, double, boolean)

isSameValue

public static boolean isSameValue(double x,
                                  double y,
                                  double tolerance)
Tests whether or not x and y are the same, within the specified tolerance.

Parameters:
x - Value.
y - Other value.
tolerance - Required tolerance.
Returns:
Logical-true if x and y are the same value, within the specified tolerance.

equallySpacedPointsInInterval

public static java.util.Vector<HDouble> equallySpacedPointsInInterval(double a,
                                                                      double b,
                                                                      int n,
                                                                      boolean includeEndPoints)
                                                               throws java.lang.IllegalArgumentException
Returns a vector of n equally spaced points within the specified interval [a:b]. If includeEndPoints is true then a and b are included in the returned vector, else not.

Parameters:
a - Minimum of interval.
b - Maximum of interval.
n - Number of intermediate points.
includeEndPoints - If true then points a and b are included in the returned vector, else not.
Returns:
Vector of equally spaced values in the interval [a:b].
Throws:
java.lang.IllegalArgumentException - Thrown if n<=0 or a>=b.

maxAbs

public static double maxAbs(double a,
                            double b)
Returns the maximum absolute value of (a,b).

Parameters:
a - Value 1.
b - Value 2.
Returns:
Maximum absolute value of (a,b).

maxAbs

public static double maxAbs(double a,
                            double b,
                            double c)
Returns the maximum absolute value of (a,b,c).

Parameters:
a - Value 1.
b - Value 2.
c - Value 3.
Returns:
Maximum absolute value of (a,b,c).

maxSort

public static void maxSort(HDouble a,
                           HDouble b)
Sorts a and b so that a>=b.

Parameters:
a - Set upon return to the sorted value such that a>=b.
b - Set upon return to the sorted value such that a>=b.

minAbs

public static double minAbs(double a,
                            double b)
Returns the min absolute value of the specified 2 values.

Parameters:
a - Value 1.
b - Value 2.
Returns:
The min absolute value of the specified 2 values.

minAbs

public static double minAbs(double a,
                            double b,
                            double c)
Returns the min absolute value of the specified 3 values.

Parameters:
a - Value 1.
b - Value 2.
c - Value 3.
Returns:
The min absolute value of the specified 3 values.

minSort

public static void minSort(HDouble a,
                           HDouble b)
Sorts a and b so that a<=b.

Parameters:
a - Set upon return to the sorted value such that a<=b.
b - Set upon return to the sorted value such that a<=b.

maxIndex

public static int maxIndex(double[] doubles)
Returns index of maximum element in a given array of doubles. First maximum is returned.

Parameters:
doubles - The array of doubles
Returns:
The index of the maximum element

maxIndex

public static int maxIndex(int[] ints)
Returns index of maximum element in a given array of integers. First maximum is returned.

Parameters:
ints - The array of integers
Returns:
The index of the maximum element

minIndex

public static int minIndex(int[] ints)
Returns index of minimum element in a given array of integers. First minimum is returned.

Parameters:
ints - The array of integers.
Returns:
The index of the minimum element.

minIndex

public static int minIndex(double[] doubles)
Returns index of minimum element in a given array of doubles. First minimum is returned.

Parameters:
doubles - The array of doubles.
Returns:
The index of the minimum element.

oppositeSign

public static boolean oppositeSign(double x,
                                   double y)
Tests whether the arguments have opposite sign.

Parameters:
x - Value 1.
y - Value 2.
Returns:
Returns logical-true if x and y have opposite sign, else logical-false.

oppositeSignOrZero

public static boolean oppositeSignOrZero(double x,
                                         double y,
                                         double tolerance)
Returns logical-true if x and y have the same sign. If either x or y are zero then logical-true is also returned.

Parameters:
x - Value 1.
y - Value 2.
tolerance - Required tolerance.
Returns:
Logical-true if x and y both have opposite sign or either one of them is zero.

oppositeSignOrBothZero

public static boolean oppositeSignOrBothZero(double x,
                                             double y,
                                             double tolerance)
Returns logical-true if x and y have the same sign. If x and y are zero then logical-true is also returned.

Parameters:
x - Value 1.
y - Value 2.
tolerance - Required tolerance.
Returns:
Logical-true if x and y both have opposite sign or either one of them is zero.

sameSignOrZero

public static boolean sameSignOrZero(double x,
                                     double y,
                                     double tolerance)
Returns logical-true if x and y have the same sign. If either x or y are zero then logical-true is also returned.

Parameters:
x - Value 1.
y - Value 2.
tolerance - Required tolerance.
Returns:
Logical-true if x and y both have the same sign or either one of them is zero.

sameSignOrBothZero

public static boolean sameSignOrBothZero(double x,
                                         double y,
                                         double tolerance)
Returns logical-true if x and y have the same sign. If x and y are zero then logical-true is also returned.

Parameters:
x - Value 1.
y - Value 2.
tolerance - Required tolerance.
Returns:
Logical-true if x and y both have the same sign or either one of them is zero.

sign

public static double sign(double a,
                          double b)
If b>0 returns |a|, else returns -|a|.

Parameters:
a - Value one.
b - Value two.
Returns:
If b>0 returns |a|, else returns -|a|.

degreesToHours

public static double degreesToHours(double degrees)
Returns a number of degrees to hours by simply dividing by 15. For example, 26.6580 degrees is 26.6580/15=1.7772 hours.

Parameters:
degrees - The number of degrees.
Returns:
The number of hours.

degreesToDegreesMinutesSeconds

public static TripleDouble degreesToDegreesMinutesSeconds(double degrees)
Converts degrees to {degrees,minutes,seconds}. Caters for degrees being either positive or negative. If degrees is positive then a positive number is returned. If degrees is negative then a negative number is returned. The sign of both the minutes and seconds will always be positive. Catering for positive and negative numbers is necessary because westward longitudes are considered positive whereas eastward longitudes are considered negative.

Parameters:
degrees - The number of degrees.
Returns:
The {degrees,minutes,seconds} triple.

degreesMinutesSecondsToDegrees

public static double degreesMinutesSecondsToDegrees(double degrees,
                                                    double minutes,
                                                    double seconds)
Converts {degrees,minutes,seconds} to degrees. Caters for the degrees, minutes or seconds being eithr positive or negative. If positive then a positive number is returned, else a negative number is returned. Catering for positive and negative numbers is necessary because westward longitudes are considered positive whereas eastward longitudes are considered negative.

Parameters:
degrees - The number of degrees.
minutes - The number of minutes.
seconds - The number of seconds.
Returns:
The number of degrees equivalent to the specified {degrees,minutes,seconds}.

degreesMinutesSecondsToMinutes

public static double degreesMinutesSecondsToMinutes(double degrees,
                                                    double minutes,
                                                    double seconds)
Converts {degrees,minutes,seconds} to minutes.

Parameters:
degrees - The number of degrees.
minutes - The number of minutes.
seconds - The number of seconds.
Returns:
The number of minutes equivalent to the specified {degrees,minutes,seconds}.

degreesMinutesSecondsToSeconds

public static double degreesMinutesSecondsToSeconds(double degrees,
                                                    double minutes,
                                                    double seconds)
Converts {degrees,minutes,seconds} to seconds.

Parameters:
degrees - The number of degrees.
minutes - The number of minutes.
seconds - The number of seconds.
Returns:
The number of seconds equivalent to the specified {degrees,minutes,seconds}.

hoursToDegrees

public static double hoursToDegrees(double hours)
Converts hours to degrees; ie degrees=15*hours.

Parameters:
hours - The number of hours.
Returns:
The number of degrees.

isWholeNumber

public static boolean isWholeNumber(double n,
                                    double tolerance)
Tests whether or not the specified number is a whole number within the specified tolerance.

Parameters:
n - Number to test.
tolerance - Required tolerance.
Returns:
Logical-true if n is a whole number, within the specified tolerance.

nearestEven

public static int nearestEven(int n,
                              boolean increasing)
Returns the nearest even number to the specified integer in the specified direction.

Parameters:
n - Number to findest nearest even.
increasing - Specifies whether or not the greater or lesser number is required.
Returns:
Nearest even number to the specified number.

nearestOdd

public static int nearestOdd(int n,
                             boolean increasing)
Returns the nearest odd number to the specified integer in the specified direction.

Parameters:
n - Number to findest nearest odd.
increasing - Specifies whether or not the greater or lesser number is required.
Returns:
Nearest odd number to the specified number.

normalise

public static void normalise(double[] doubles)
Normalises the doubles in the specified array by their sum.

Parameters:
doubles - the array of double
Throws:
java.lang.IllegalArgumentException - if sum is Zero or NaN.

normalise

public static void normalise(double[] doubles,
                             double sum)
Normalises the doubles in the array using the given value.

Parameters:
doubles - the array of double
sum - the value by which the doubles are to be normalized
Throws:
java.lang.IllegalArgumentException - if sum is zero or NaN

numberDigits

public static int numberDigits(int number)
Returns the number of digits in the specified integer number. For example, inetger 12345 has 5 digits.

Parameters:
number - Integer number.
Returns:
Number of digits.

isNAN

public static boolean isNAN(double n)
Tests whether or not the specified number if equal to NAN(1e38).

Parameters:
n - Number to test.
Returns:
Logical-true if n is equal to NAN, else logical-false.

linearInterpolation

public static double linearInterpolation(java.util.Vector<HDouble> sample_points,
                                         double u)
                                  throws java.lang.IllegalArgumentException
Linear interpolation in 1-dimension. Refer to S. Hill, Graphics Gems IV, X.1

Parameters:
sample_points - The two sample points (i0,i1) and u(0<=u<=1) is the fractional distance from point 1 to point 2.
u - u(0<=u<=1) is the fractional distances along the parametric axis.
Returns:
Interpolated value.
Throws:
java.lang.IllegalArgumentException - Thrown if sample_points does not have 2 elements or if u is out of range.

linearInterpolation

public static double linearInterpolation(java.util.Vector<HDouble> sample_points,
                                         double u,
                                         double v)
                                  throws java.lang.IllegalArgumentException
Linear interpolation in 2-dimensions. Refer to S. Hill, Graphics Gems IV, X.1.

Parameters:
sample_points - The four sample points (i00,i10,i11,i01).
u - u(0<=u<=1) is the fractional distances along the parametric axis.
v - v(0<=u<=1) is the fractional distances along the parametric axis.
Returns:
Interpolated value.
Throws:
java.lang.IllegalArgumentException

linearInterpolation

public static double linearInterpolation(java.util.Vector<HDouble> sample_points,
                                         double u,
                                         double v,
                                         double w)
                                  throws java.lang.IllegalArgumentException
Linear interpolation in 3-dimensions. Refer to S. Hill, Graphics Gems IV, X.1.

Parameters:
sample_points - The eight sample points (i000,i100,i110,i010,i001,i101,i111,i011).
u - u(0<=u<=1) is the fractional distances along the parametric axis.
v - v(0<=u<=1) is the fractional distances along the parametric axis.
w - w(0<=u<=1) is the fractional distances along the parametric axis.
Returns:
Interpolated value.
Throws:
java.lang.IllegalArgumentException

wholePart

public static double wholePart(double n)
Returns the whole part of a number, stripping out the negative sign if present.

Parameters:
n - Number.
Returns:
Whole part of number n.

wholePartAndSign

public static double wholePartAndSign(double n)
Returns the whole part of a number, keeping negative sign if present.

Parameters:
n - Number.
Returns:
Whole part of number n, keeping sign if present.

bitCount1

public static int bitCount1(long a)
Counts the number of 1 bits in a 32-bit long integer (variant 1). Refer to A. W. Paeth and D. Schilling, Graphics Gems II, VIII.3.

Parameters:
a - Number to test.
Returns:
The number of 1 bits.

bitCount2

public static long bitCount2(long a)
Counts the number of 1 bits in a 32-bit long integer (variant 2). Refer to A. W. Paeth and D. Schilling, Graphics Gems II, VIII.3.

Parameters:
a - Number to test.
Returns:
The number of 1 bits.

bitCount3

public static long bitCount3(long a)
Counts the number of 1 bits in a 32-bit long integer (variant 3). Refer to A. W. Paeth and D. Schilling, Graphics Gems II, VIII.3.

Parameters:
a - Number to test.
Returns:
The number of 1 bits.

unitRandom

public static double unitRandom(int seed)
Generates a random number in the interval [0,1).

Parameters:
seed - Random number generator seed.
Returns:
Random number in interval [0:1].

symmetricRandom

public static double symmetricRandom(int seed)
Generates a random number in the interval [-1,1).

Parameters:
seed - Random number generator see.
Returns:
Random number.

intervalRandom

public static double intervalRandom(double min,
                                    double max,
                                    int seed)
Generates a random number in the interval [min,max).

Parameters:
min - Minimum number.
max - maximum number.
seed - Random number generator seed.
Returns:
Random number.

sort

public static TripleInteger sort(int a,
                                 int b,
                                 int c)
Sorts the specified integer values (a,b,c) in increasing order.

Parameters:
a - Value a.
b - Value b.
c - Value c.
Returns:
Sorted triple.

sort

public static TripleDouble sort(double a,
                                double b,
                                double c)
Sorts the specified double values (a,b,c) in increasing order.

Parameters:
a - Value a.
b - Value b.
c - Value c.
Returns:
Sorted triple.

sort

public static PairInteger sort(int a,
                               int b)
Sorts the specified integer values (a,b) in increasing order.

Parameters:
a - Value a.
b - Value b.
Returns:
Sorted pair.

sort

public static PairDouble sort(double a,
                              double b)
Sorts the specified double values (a,b) in increasing order.

Parameters:
a - Value a.
b - Value b.
Returns:
Sorted pair.

sort

public static PairInteger sort(int a,
                               int b,
                               boolean sortIncreasing)
Sorts the specified integer values (a,b) in increasing order.

Parameters:
a - Value a.
b - Value b.
sortIncreasing - Specifies whether or not the specified pair are sorted in increasing (true) order or decreasing (false) order.
Returns:
Sorted pair.

sort

public static PairDouble sort(double a,
                              double b,
                              boolean sortIncreasing)
Sorts the specified floating-point values (a,b) in increasing order.

Parameters:
a - Value a.
b - Value b.
sortIncreasing - Specifies whether or not the specified pair are sorted in increasing (true) order or decreasing (false) order.
Returns:
Sorted pair.

sort

public static TripleInteger sort(int a,
                                 int b,
                                 int c,
                                 boolean sortIncreasing)
Sorts the specified integer values (a,b,c) in increasing order.

Parameters:
a - Value a.
b - Value b.
c - Value c.
sortIncreasing - Specifies whether or not the specified triple are sorted in increasing (true) order or decreasing (false) order.
Returns:
Sorted triple.

sort

public static TripleDouble sort(double a,
                                double b,
                                double c,
                                boolean sortIncreasing)
Sorts the specified floating-point values (a,b,c) in increasing order.

Parameters:
a - Value a.
b - Value b.
c - Value c.
sortIncreasing - Specifies whether or not the specified triple are sorted in increasing (true) order or decreasing (false) order.
Returns:
Sorted triple.

mean

public static double mean(double[] vector)
Computes the mean of the specified array of doubles.

Parameters:
vector - Array of doubles.
Returns:
The mean of the specified array.

standardDeviation

public static double standardDeviation(double[] vector)
Returns the standard deviation of the specified array and returns INFINITE if the the vector is either empty of has only one element.

Parameters:
vector - 1D Java array of floating-point values.
Returns:
The standard deviation of this vector.

variance

public static double variance(double[] vector)
Computes the variance of the specified array of doubles.

Parameters:
vector - Array of doubles.
Returns:
The variance of the specified array.

covariance

public static double covariance(double[] v1,
                                double[] v2)
                         throws java.lang.IllegalArgumentException
Computes the covariance.

Parameters:
v1 - Array 1 of doubles.
v2 - Array 2 of doubles.
Returns:
The covariance.
Throws:
java.lang.IllegalArgumentException - Thrown if v1 and v2 not of the same length.

correlation

public static double correlation(double[] v1,
                                 double[] v2)
                          throws java.lang.IllegalArgumentException
Computes the linear correlation between the two specified arrays. Squaring the result and multiply by 100 gives the percentage of correlation.

Parameters:
v1 - Array 1 of doubles.
v2 - Array 2 of doubles.
Returns:
The covariance.
Throws:
java.lang.IllegalArgumentException - Thrown if v1 and v2 not of the same length.

sum

public static double sum(double[] doubles)
Returns the sum of the elements of an array of doubles.

Parameters:
doubles - The array of doubles.
Returns:
The sum of the elements.

sum

public static int sum(int[] ints)
Returns the sum of the elements of an array of integers.

Parameters:
ints - the array of integers
Returns:
the sum of the elements

average

public static double average(double[] doubles)
                      throws java.lang.IllegalArgumentException
Returns the average of the elements of an array of doubles.

Parameters:
doubles - The array of doubles.
Returns:
The average of the elements.
Throws:
java.lang.IllegalArgumentException - Thrown if doubles.length==0.

average

public static double average(int[] ints)
                      throws java.lang.IllegalArgumentException
Returns the average of the elements of an array of ints.

Parameters:
ints - The array of ints.
Returns:
The average of the elements.
Throws:
java.lang.IllegalArgumentException - Thrown if ints.length==0.

erf

public static double erf(double x)

Evaluates the error function, defined as:

                             x
                      2      |\
     erf(x)  =  ---------  | exp(-t*t)dt
                   sqrt(pi) \| 
                             0
 
erf() is frequently used to evaluate the Gauss/Normal distribution.

Parameters:
x - Value to erf().
Returns:
Value of erf(x).
See Also:
errorFunction(double), GaussDistribution

erfComplementary

public static double erfComplementary(double x)
Evaluates the complementary error function.

Parameters:
x - Value to erfc(x).
Returns:
Value of erfc(x).
See Also:
complementErrorFunction(double)