Class MathEx
- scalar functions: sqr, factorial, lfactorial, choose, lchoose, log2 are provided.
- vector functions: min, max, mean, sum, var, sd, cov, L1 norm, L2 norm, L∞ norm, normalize, unitize, cor, Spearman correlation, Kendall correlation, distance, dot product, histogram, vector (element-wise) copy, equal, plus, minus, times, and divide.
- matrix functions: min, max, rowSums, colSums, rowMeans, colMeans, transpose, cov, cor, matrix copy, equals.
- random functions: random, randomInt, and permutate.
- Find the root of a univariate function with or without derivative.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The number of digits (in radix base) in the mantissa.static final double
The machine precision for the double type, which is the difference between 1 and the smallest value greater than 1 that is representable for the double type.static final int
The number of digits (in radix base) in the mantissa.static final float
The machine precision for the float type, which is the difference between 1 and the smallest value greater than 1 that is representable for the float type.static final int
The largest negative integer such that 1.0 + RADIXMACHEP ≠ 1.0, except that machep is bounded below by -(DIGITS+3)static final int
The largest negative integer such that 1.0 - RADIXNEGEP ≠ 1.0, except that negeps is bounded below by -(DIGITS+3)static final int
The largest negative integer such that 1.0 + RADIXMACHEP ≠ 1.0, except that machep is bounded below by -(DIGITS+3)static final int
The largest negative integer such that 1.0 - RADIXNEGEP ≠ 1.0, except that negeps is bounded below by -(DIGITS+3)static final int
The base of the exponent of the double type.static final int
Rounding style. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
add
(double[] y, double[] x) Element-wise sum of two arrays y = x + y.static double[]
axpy
(double a, double[] x, double[] y) Update an array by adding a multiple of another array y = a * x + y.static double[]
c
(double... x) Combines the arguments to form a vector.static double[]
c
(double[]... list) Concatenates multiple vectors into one.static float[]
c
(float... x) Combines the arguments to form a vector.static float[]
c
(float[]... list) Concatenates multiple vectors into one.static int[]
c
(int... x) Combines the arguments to form a vector.static int[]
c
(int[]... list) Concatenates multiple vectors into one.static String[]
Combines the arguments to form a vector.static String[]
Concatenates multiple vectors into one array of strings.static double[]
cbind
(double[]... x) Concatenates vectors by columns.static float[]
cbind
(float[]... x) Concatenates vectors by columns.static int[]
cbind
(int[]... x) Concatenates vectors by columns.static String[]
Concatenates vectors by columns.static double
choose
(int n, int k) The n choose k.static double[][]
clone
(double[][] x) Deep clone a two-dimensional array.static float[][]
clone
(float[][] x) Deep clone a two-dimensional array.static int[][]
clone
(int[][] x) Deep clone a two-dimensional array.static double[]
colMax
(double[][] matrix) Returns the column maximum of a matrix.static int[]
colMax
(int[][] matrix) Returns the column maximum of a matrix.static double[]
colMeans
(double[][] matrix) Returns the column means of a matrix.static double[]
colMin
(double[][] matrix) Returns the column minimum of a matrix.static int[]
colMin
(int[][] matrix) Returns the column minimum of a matrix.static double[]
colSds
(double[][] matrix) Returns the column standard deviations of a matrix.static double[]
colSums
(double[][] matrix) Returns the column sums of a matrix.static long[]
colSums
(int[][] matrix) Returns the column sums of a matrix.static boolean
contains
(double[][] polygon, double[] point) Determines if the polygon contains the point.static boolean
contains
(double[][] polygon, double x, double y) Determines if the polygon contains the point.static void
copy
(double[][] x, double[][] y) Deep copy x into y.static void
copy
(float[][] x, float[][] y) Deep copy x into y.static void
copy
(int[][] x, int[][] y) Copy x into y.static double[][]
cor
(double[][] data) Returns the sample correlation matrix.static double[][]
cor
(double[][] data, double[] mu) Returns the sample correlation matrix.static double
cor
(double[] x, double[] y) Returns the correlation coefficient between two vectors.static double
cor
(float[] x, float[] y) Returns the correlation coefficient between two vectors.static double
cor
(int[] x, int[] y) Returns the correlation coefficient between two vectors.static double
cos
(double[] a, double[] b) Returns the cosine similarity.static float
cos
(float[] a, float[] b) Returns the cosine similarity.static double[][]
cov
(double[][] data) Returns the sample covariance matrix.static double[][]
cov
(double[][] data, double[] mu) Returns the sample covariance matrix.static double
cov
(double[] x, double[] y) Returns the covariance between two vectors.static double
cov
(float[] x, float[] y) Returns the covariance between two vectors.static double
cov
(int[] x, int[] y) Returns the covariance between two vectors.static double
distance
(double[] a, double[] b) The Euclidean distance.static double
distance
(float[] a, float[] b) The Euclidean distance.static double
distance
(int[] a, int[] b) The Euclidean distance on binary sparse arrays, which are the indices of nonzero elements in ascending order.static double
distance
(SparseArray a, SparseArray b) The Euclidean distance.static double
dot
(double[] a, double[] b) Returns the dot product between two vectors.static float
dot
(float[] a, float[] b) Returns the dot product between two vectors.static int
dot
(int[] a, int[] b) Returns the dot product between two binary sparse arrays, which are the indices of nonzero elements in ascending order.static double
dot
(SparseArray x, SparseArray y) Returns the dot product between two sparse arrays.static double
entropy
(double[] p) Shannon's entropy.static boolean
equals
(double[][] x, double[][] y) Check if x element-wisely equals y with default epsilon 1E-10.static boolean
equals
(double[][] x, double[][] y, double epsilon) Check if x element-wisely equals y in given precision.static boolean
equals
(double[] x, double[] y) Check if x element-wisely equals y with default epsilon 1E-10.static boolean
equals
(double[] x, double[] y, double epsilon) Check if x element-wisely equals y in given precision.static boolean
equals
(double a, double b) Returns true if two double values equals to each other in the system precision.static boolean
equals
(float[][] x, float[][] y) Check if x element-wisely equals y with default epsilon 1E-7.static boolean
equals
(float[][] x, float[][] y, float epsilon) Check if x element-wisely equals y in given precision.static boolean
equals
(float[] x, float[] y) Check if x element-wisely equals y with default epsilon 1E-7.static boolean
equals
(float[] x, float[] y, float epsilon) Check if x element-wisely equals y in given precision.static double
factorial
(int n) The factorial of n.static long
Returns a random number to seed other random number generators.static byte[]
generateSeed
(int numBytes) Returns the given number of random bytes to seed other random number generators.static boolean
isInt
(double x) Returns true if x is an integer.static boolean
isInt
(float x) Returns true if x is an integer.static boolean
isPower2
(int x) Returns true if x is a power of 2.static boolean
isProbablePrime
(long n, int k) Returns true if n is probably prime, false if it's definitely composite.static boolean
isZero
(double x) Tests if a floating number is zero in machine precision.static boolean
isZero
(double x, double epsilon) Tests if a floating number is zero in given precision.static boolean
isZero
(float x) Tests if a floating number is zero in machine precision.static boolean
isZero
(float x, float epsilon) Tests if a floating number is zero in given precision.static double
JensenShannonDivergence
(double[] p, double[] q) Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.static double
JensenShannonDivergence
(double[] p, SparseArray q) Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.static double
JensenShannonDivergence
(SparseArray p, double[] q) Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.static double
Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.static double
kendall
(double[] x, double[] y) The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant.static double
kendall
(float[] x, float[] y) The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant.static double
kendall
(int[] x, int[] y) The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant.static double
KullbackLeiblerDivergence
(double[] p, double[] q) Kullback-Leibler divergence.static double
KullbackLeiblerDivergence
(double[] p, SparseArray q) Kullback-Leibler divergence.static double
KullbackLeiblerDivergence
(SparseArray p, double[] q) Kullback-Leibler divergence.static double
Kullback-Leibler divergence.static double
lchoose
(int n, int k) The log of n choose k.static double
lfactorial
(int n) The log of factorial of n.static double
log
(double x) Returns natural log without underflow.static double
log1pe
(double x) Returns natural log(1+exp(x)) without overflow.static double
log2
(double x) Log of base 2.static double
mad
(double[] array) Returns the median absolute deviation (MAD).static double
mad
(float[] array) Returns the median absolute deviation (MAD).static double
mad
(int[] array) Returns the median absolute deviation (MAD).static double
max
(double[] array) Returns the maximum value of an array.static double
max
(double[][] matrix) Returns the maximum of a matrix.static double
max
(double a, double b, double c) Returns the maximum of 4 double numbers.static double
max
(double a, double b, double c, double d) Returns the maximum of 4 double numbers.static float
max
(float[] array) Returns the maximum value of an array.static float
max
(float a, float b, float c) Returns the maximum of 4 float numbers.static float
max
(float a, float b, float c, float d) Returns the maximum of 4 float numbers.static int
max
(int[] array) Returns the maximum value of an array.static int
max
(int[][] matrix) Returns the maximum of a matrix.static int
max
(int a, int b, int c) Returns the maximum of 3 integer numbers.static int
max
(int a, int b, int c, int d) Returns the maximum of 4 integer numbers.static double
mean
(double[] array) Returns the mean of an array.static double
mean
(float[] array) Returns the mean of an array.static double
mean
(int[] array) Returns the mean of an array.static double
median
(double[] x) Find the median of an array of type double.static float
median
(float[] x) Find the median of an array of type float.static int
median
(int[] x) Find the median of an array of type int.static <T extends Comparable<? super T>>
Tmedian
(T[] x) Find the median of an array of type double.static double
min
(double[] array) Returns the minimum value of an array.static double
min
(double[][] matrix) Returns the minimum of a matrix.static double
min
(double a, double b, double c) Returns the minimum of 3 double numbers.static double
min
(double a, double b, double c, double d) Returns the minimum of 4 double numbers.static float
min
(float[] array) Returns the minimum value of an array.static float
min
(float a, float b, float c) Returns the minimum of 3 float numbers.static float
min
(float a, float b, float c, float d) Returns the minimum of 4 float numbers.static int
min
(int[] array) Returns the minimum value of an array.static int
min
(int[][] matrix) Returns the minimum of a matrix.static int
min
(int a, int b, int c) Returns the minimum of 3 integer numbers.static int
min
(int a, int b, int c, int d) Returns the minimum of 4 integer numbers.static int
mode
(int[] a) Returns the mode of the array, which is the most frequent element.static double
norm
(double[] x) L2 vector norm.static float
norm
(float[] x) L2 vector norm.static double
norm1
(double[] x) L1 vector norm.static float
norm1
(float[] x) L1 vector norm.static double
norm2
(double[] x) L2 vector norm.static float
norm2
(float[] x) L2 vector norm.static void
normalize
(double[][] matrix) Unitizes each column of a matrix to unit length (L_2 norm).static void
normalize
(double[][] matrix, boolean centerizing) Unitizes each column of a matrix to unit length (L_2 norm).static double
normInf
(double[] x) L∞ vector norm.static float
normInf
(float[] x) L∞ vector norm that is the maximum absolute value.static double[]
omit
(double[] a, double value) Returns a new array without the specified value.static float[]
omit
(float[] a, float value) Returns a new array without the specified value.static int[]
omit
(int[] a, int value) Returns a new array without the specified value.static double[]
omitNaN
(double[] a) Returns a new array without NaN values.static float[]
omitNaN
(float[] a) Returns a new array without NaN values.static Matrix
pdist
(double[][] x) Returns the pairwise distance matrix of multiple vectors.static Matrix
pdist
(double[][] x, boolean squared) Returns the pairwise distance matrix of multiple vectors.static Matrix
pdist
(float[][] x) Returns the pairwise distance matrix of multiple vectors.static Matrix
pdist
(float[][] x, boolean squared) Returns the pairwise distance matrix of multiple vectors.static Matrix
pdist
(int[][] x) Returns the pairwise distance matrix of multiple binary sparse vectors.static Matrix
pdist
(int[][] x, boolean squared) Returns the pairwise distance matrix of multiple binary sparse vectors.static Matrix
pdist
(SparseArray[] x) Returns the pairwise distance matrix of multiple vectors.static Matrix
pdist
(SparseArray[] x, boolean squared) Returns the pairwise distance matrix of multiple vectors.static <T> void
Computes the pairwise distance matrix of multiple vectors.static Matrix
pdot
(double[][] x) Returns the pairwise dot product matrix of double vectors.static Matrix
pdot
(float[][] x) Returns the pairwise dot product matrix of float vectors.static Matrix
pdot
(int[][] x) Returns the pairwise dot product matrix of binary sparse vectors.static Matrix
pdot
(SparseArray[] x) Returns the pairwise dot product matrix of multiple vectors.static void
permutate
(double[] x) Permutates an array.static void
permutate
(float[] x) Permutates an array.static int[]
permutate
(int n) Returns a permutation of(0, 1, 2, ..., n-1)
.static void
permutate
(int[] x) Permutates an array.static void
Permutates an array.static double[]
pow
(double[] x, double n) Raise each element of an array to a scalar power.static double
pow2
(double x) Returns x * x.static long
probablePrime
(long n, int k) Returns a probably prime number greater than n.static double
q1
(double[] x) Find the first quantile (p = 1/4) of an array of type double.static float
q1
(float[] x) Find the first quantile (p = 1/4) of an array of type float.static int
q1
(int[] x) Find the first quantile (p = 1/4) of an array of type int.static <T extends Comparable<? super T>>
Tq1
(T[] x) Find the first quantile (p = 1/4) of an array of type double.static double
q3
(double[] x) Find the third quantile (p = 3/4) of an array of type double.static float
q3
(float[] x) Find the third quantile (p = 3/4) of an array of type float.static int
q3
(int[] x) Find the third quantile (p = 3/4) of an array of type int.static <T extends Comparable<? super T>>
Tq3
(T[] x) Find the third quantile (p = 3/4) of an array of type double.static double
random()
Generate a random number in [0, 1).static int
random
(double[] prob) Given a set of n probabilities, generate a random number in [0, n).static int[]
random
(double[] prob, int n) Given a set of m probabilities, draw with replacement a set of n random number in [0, m).static double
random
(double lo, double hi) Generate a uniform random number in the range [lo, hi).static double[]
random
(double lo, double hi, int n) Generate uniform random numbers in the range [lo, hi).static double[]
random
(int n) Generate n random numbers in [0, 1).static int
randomInt
(int n) Returns a random integer in [0, n).static int
randomInt
(int lo, int hi) Returns a random integer in [lo, hi).static long
Returns a random long integer.static double[][]
rbind
(double[]... x) Concatenates vectors by rows.static float[][]
rbind
(float[]... x) Concatenates vectors by rows.static int[][]
rbind
(int[]... x) Concatenates vectors by rows.static String[][]
Concatenates vectors by rows.static void
reverse
(double[] a) Reverses the order of the elements in the specified array.static void
reverse
(float[] a) Reverses the order of the elements in the specified array.static void
reverse
(int[] a) Reverses the order of the elements in the specified array.static <T> void
reverse
(T[] a) Reverses the order of the elements in the specified array.static double
round
(double x, int decimal) Round a double vale to given digits such as 10^n, where n is a positive or negative integer.static double[]
rowMax
(double[][] matrix) Returns the row maximum of a matrix.static int[]
rowMax
(int[][] matrix) Returns the row maximum of a matrix.static double[]
rowMeans
(double[][] matrix) Returns the row means of a matrix.static double[]
rowMin
(double[][] matrix) Returns the row minimum of a matrix.static int[]
rowMin
(int[][] matrix) Returns the row minimum of a matrix.static double[]
rowSds
(double[][] matrix) Returns the row standard deviations of a matrix.static double[]
rowSums
(double[][] matrix) Returns the row sums of a matrix.static long[]
rowSums
(int[][] matrix) Returns the row sums of a matrix.static void
scale
(double[][] matrix) Scales each column of a matrix to range [0, 1].static void
scale
(double a, double[] x) Scale each element of an array by a constant x = a * x.static void
scale
(double a, double[] x, double[] y) Scale each element of an array by a constant y = a * x.static double
sd
(double[] array) Returns the standard deviation of an array.static double
sd
(float[] array) Returns the standard deviation of an array.static double
sd
(int[] array) Returns the standard deviation of an array.static LongStream
seeds()
Returns a stream of random numbers to be used as RNG seeds.static void
setSeed
(long seed) Initialize the random number generator with a seed.static double
sigmoid
(double x) Logistic sigmoid function1 / (1 + exp(-x))
.static double[]
slice
(double[] data, int[] index) Returns a slice of data for given indices.static float[]
slice
(float[] data, int[] index) Returns a slice of data for given indices.static int[]
slice
(int[] data, int[] index) Returns a slice of data for given indices.static <E> E[]
slice
(E[] data, int[] index) Returns a slice of data for given indices.static int
softmax
(double[] posteriori) The softmax function without overflow.static int
softmax
(double[] x, int k) The softmax function without overflow.static double[]
solve
(double[] a, double[] b, double[] c, double[] r) Solve the tridiagonal linear set which is of diagonal dominance |bi|>
|ai| + |ci|.static int[][]
sort
(double[][] matrix) Sorts each variable and returns the index of values in ascending order.static double
spearman
(double[] x, double[] y) The Spearman Rank Correlation Coefficient is a form of the Pearson coefficient with the data converted to rankings (i.e.static double
spearman
(float[] x, float[] y) The Spearman Rank Correlation Coefficient is a form of the Pearson coefficient with the data converted to rankings (i.e.static double
spearman
(int[] x, int[] y) The Spearman Rank Correlation Coefficient is a form of the Pearson coefficient with the data converted to rankings (i.e.static double
squaredDistance
(double[] a, double[] b) The squared Euclidean distance.static double
squaredDistance
(float[] a, float[] b) The squared Euclidean distance.static double
squaredDistance
(int[] a, int[] b) The squared Euclidean distance on binary sparse arrays, which are the indices of nonzero elements in ascending order.static double
The Euclidean distance on sparse arrays.static double
squaredDistanceWithMissingValues
(double[] x, double[] y) The squared Euclidean distance with handling missing values (represented as NaN).static void
standardize
(double[] array) Standardizes an array to mean 0 and variance 1.static void
standardize
(double[][] matrix) Standardizes each column of a matrix to 0 mean and unit variance.static void
sub
(double[] a, double[] b) Element-wise subtraction of two arrays a -= b.static int
sum
(byte[] x) Returns the sum of an array.static double
sum
(double[] x) Returns the sum of an array.static double
sum
(float[] x) Returns the sum of an array.static long
sum
(int[] x) Returns the sum of an array.static void
swap
(double[] x, double[] y) Swap two arrays.static void
swap
(double[] x, int i, int j) Swap two elements of an array.static void
swap
(float[] x, float[] y) Swap two arrays.static void
swap
(float[] x, int i, int j) Swap two elements of an array.static void
swap
(int[] x, int[] y) Swap two arrays.static void
swap
(int[] x, int i, int j) Swap two elements of an array.static <E> void
swap
(E[] x, E[] y) Swap two arrays.static void
Swap two elements of an array.static double[][]
transpose
(double[][] matrix) Returns the matrix transpose.static int[]
unique
(int[] array) Find unique elements of vector.static String[]
Find unique elements of vector.static void
unitize
(double[] array) Unitize an array so that L2 norm of array = 1.static void
unitize1
(double[] array) Unitize an array so that L1 norm of array is 1.static void
unitize2
(double[] array) Unitize an array so that L2 norm of array = 1.static double
var
(double[] array) Returns the variance of an array.static double
var
(float[] array) Returns the variance of an array.static double
var
(int[] array) Returns the variance of an array.static int
whichMax
(double[] array) Returns the index of maximum value of an array.static IntPair
whichMax
(double[][] matrix) Returns the index of maximum value of a matrix.static int
whichMax
(float[] array) Returns the index of maximum value of an array.static int
whichMax
(int[] array) Returns the index of maximum value of an array.static int
whichMin
(double[] array) Returns the index of minimum value of an array.static IntPair
whichMin
(double[][] matrix) Returns the index of minimum value of a matrix.static int
whichMin
(float[] array) Returns the index of minimum value of an array.static int
whichMin
(int[] array) Returns the index of minimum value of an array.
-
Field Details
-
EPSILON
public static final double EPSILONThe machine precision for the double type, which is the difference between 1 and the smallest value greater than 1 that is representable for the double type. -
FLOAT_EPSILON
public static final float FLOAT_EPSILONThe machine precision for the float type, which is the difference between 1 and the smallest value greater than 1 that is representable for the float type. -
RADIX
public static final int RADIXThe base of the exponent of the double type. -
DIGITS
public static final int DIGITSThe number of digits (in radix base) in the mantissa. -
FLOAT_DIGITS
public static final int FLOAT_DIGITSThe number of digits (in radix base) in the mantissa. -
ROUND_STYLE
public static final int ROUND_STYLERounding style.- 0 if floating-point addition chops
- 1 if floating-point addition rounds, but not in the ieee style
- 2 if floating-point addition rounds in the ieee style
- 3 if floating-point addition chops, and there is partial underflow
- 4 if floating-point addition rounds, but not in the ieee style, and there is partial underflow
- 5 if floating-point addition rounds in the IEEEE style, and there is partial underflow
-
MACHEP
public static final int MACHEPThe largest negative integer such that 1.0 + RADIXMACHEP ≠ 1.0, except that machep is bounded below by -(DIGITS+3) -
FLOAT_MACHEP
public static final int FLOAT_MACHEPThe largest negative integer such that 1.0 + RADIXMACHEP ≠ 1.0, except that machep is bounded below by -(DIGITS+3) -
NEGEP
public static final int NEGEPThe largest negative integer such that 1.0 - RADIXNEGEP ≠ 1.0, except that negeps is bounded below by -(DIGITS+3) -
FLOAT_NEGEP
public static final int FLOAT_NEGEPThe largest negative integer such that 1.0 - RADIXNEGEP ≠ 1.0, except that negeps is bounded below by -(DIGITS+3)
-
-
Method Details
-
log2
public static double log2(double x) Log of base 2.- Parameters:
x
- a real number.- Returns:
- the value
log2(x)
.
-
log
public static double log(double x) Returns natural log without underflow.- Parameters:
x
- a real number.- Returns:
- the value
log(x)
.
-
log1pe
public static double log1pe(double x) Returns natural log(1+exp(x)) without overflow.- Parameters:
x
- a real number.- Returns:
- the value
log(1+exp(x))
.
-
isInt
public static boolean isInt(float x) Returns true if x is an integer.- Parameters:
x
- a real number.- Returns:
- true if x is an integer.
-
isInt
public static boolean isInt(double x) Returns true if x is an integer.- Parameters:
x
- a real number.- Returns:
- true if x is an integer.
-
equals
public static boolean equals(double a, double b) Returns true if two double values equals to each other in the system precision.- Parameters:
a
- a double value.b
- a double value.- Returns:
- true if two double values equals to each other in the system precision
-
sigmoid
public static double sigmoid(double x) Logistic sigmoid function1 / (1 + exp(-x))
.- Parameters:
x
- a real number.- Returns:
- the sigmoid function.
-
pow2
public static double pow2(double x) Returns x * x.- Parameters:
x
- a real number.- Returns:
- the square of x.
-
isPower2
public static boolean isPower2(int x) Returns true if x is a power of 2.- Parameters:
x
- a real number.- Returns:
- true if x is a power of 2.
-
isProbablePrime
public static boolean isProbablePrime(long n, int k) Returns true if n is probably prime, false if it's definitely composite. This implements Miller-Rabin primality test.- Parameters:
n
- an odd integer to be tested for primalityk
- the number of rounds of primality test to perform. With larger number of rounds, the test is more accurate but also takes longer time.- Returns:
- true if n is probably prime, false if it's definitely composite.
-
round
public static double round(double x, int decimal) Round a double vale to given digits such as 10^n, where n is a positive or negative integer.- Parameters:
x
- a real number.decimal
- the number of digits to round to.- Returns:
- the rounded value.
-
factorial
public static double factorial(int n) The factorial of n.- Parameters:
n
- a positive integer.- Returns:
- the factorial returned as double but is, numerically, an integer. Numerical rounding may make this an approximation after n = 21.
-
lfactorial
public static double lfactorial(int n) The log of factorial of n.- Parameters:
n
- a positive integer.- Returns:
- the log of factorial .
-
choose
public static double choose(int n, int k) The n choose k. Returns 0 if n is less than k.- Parameters:
n
- the total number of objects in the set.k
- the number of choosing objects from the set.- Returns:
- the number of combinations.
-
lchoose
public static double lchoose(int n, int k) The log of n choose k.- Parameters:
n
- the total number of objects in the set.k
- the number of choosing objects from the set.- Returns:
- the log of the number of combinations.
-
generateSeed
public static long generateSeed()Returns a random number to seed other random number generators.- Returns:
- a random number to seed other random number generators.
-
generateSeed
public static byte[] generateSeed(int numBytes) Returns the given number of random bytes to seed other random number generators.- Parameters:
numBytes
- the number of seed bytes to generate.- Returns:
- the seed bytes.
-
seeds
Returns a stream of random numbers to be used as RNG seeds.- Returns:
- a stream of random numbers to be used as RNG seeds.
-
setSeed
public static void setSeed(long seed) Initialize the random number generator with a seed.- Parameters:
seed
- the RNG seed.
-
probablePrime
public static long probablePrime(long n, int k) Returns a probably prime number greater than n.- Parameters:
n
- the returned value should be greater than n.k
- the number of rounds of primality test to perform. With larger number of rounds, the test is more accurate but also takes longer time.- Returns:
- a probably prime number greater than n.
-
random
public static int random(double[] prob) Given a set of n probabilities, generate a random number in [0, n).- Parameters:
prob
- probabilities of size n. The prob argument can be used to give a vector of weights for obtaining the elements of the vector being sampled. They need not sum to one, but they should be non-negative and not all zero.- Returns:
- a random integer in [0, n).
-
random
public static int[] random(double[] prob, int n) Given a set of m probabilities, draw with replacement a set of n random number in [0, m).- Parameters:
prob
- probabilities of size n. The prob argument can be used to give a vector of weights for obtaining the elements of the vector being sampled. They need not sum to one, but they should be non-negative and not all zero.n
- the number of random numbers.- Returns:
- random numbers in range of [0, m).
-
random
public static double random()Generate a random number in [0, 1).- Returns:
- a random number.
-
random
public static double[] random(int n) Generate n random numbers in [0, 1).- Parameters:
n
- the number of random numbers.- Returns:
- the random numbers.
-
random
public static double random(double lo, double hi) Generate a uniform random number in the range [lo, hi).- Parameters:
lo
- lower limit of rangehi
- upper limit of range- Returns:
- a uniform random number in the range [lo, hi)
-
random
public static double[] random(double lo, double hi, int n) Generate uniform random numbers in the range [lo, hi).- Parameters:
lo
- lower limit of rangehi
- upper limit of rangen
- the number of random numbers.- Returns:
- uniform random numbers in the range [lo, hi)
-
randomLong
public static long randomLong()Returns a random long integer.- Returns:
- a random long integer.
-
randomInt
public static int randomInt(int n) Returns a random integer in [0, n).- Parameters:
n
- the upper bound of random number.- Returns:
- a random integer.
-
randomInt
public static int randomInt(int lo, int hi) Returns a random integer in [lo, hi).- Parameters:
lo
- lower limit of rangehi
- upper limit of range- Returns:
- a uniform random number in the range [lo, hi)
-
permutate
public static int[] permutate(int n) Returns a permutation of(0, 1, 2, ..., n-1)
.- Parameters:
n
- the upper bound.- Returns:
- the permutation of
(0, 1, 2, ..., n-1)
.
-
permutate
public static void permutate(int[] x) Permutates an array.- Parameters:
x
- the array.
-
permutate
public static void permutate(float[] x) Permutates an array.- Parameters:
x
- the array.
-
permutate
public static void permutate(double[] x) Permutates an array.- Parameters:
x
- the array.
-
permutate
Permutates an array.- Parameters:
x
- the array.
-
softmax
public static int softmax(double[] posteriori) The softmax function without overflow. The function takes as an input vector of K real numbers, and normalizes it into a probability distribution consisting of K probabilities proportional to the exponentials of the input numbers. That is, prior to applying softmax, some vector components could be negative, or greater than one; and might not sum to 1; but after applying softmax, each component will be in the interval (0,1), and the components will add up to 1, so that they can be interpreted as probabilities. Furthermore, the larger input components will correspond to larger probabilities.- Parameters:
posteriori
- the input/output vector.- Returns:
- the index of largest posteriori probability.
-
softmax
public static int softmax(double[] x, int k) The softmax function without overflow. The function takes as an input vector of K real numbers, and normalizes it into a probability distribution consisting of K probabilities proportional to the exponentials of the input numbers. That is, prior to applying softmax, some vector components could be negative, or greater than one; and might not sum to 1; but after applying softmax, each component will be in the interval (0,1), and the components will add up to 1, so that they can be interpreted as probabilities. Furthermore, the larger input components will correspond to larger probabilities.- Parameters:
x
- the input/output vector.k
- uses only first k components of input vector.- Returns:
- the index of largest posteriori probability.
-
c
public static int[] c(int... x) Combines the arguments to form a vector.- Parameters:
x
- the vector elements.- Returns:
- the vector.
-
c
public static float[] c(float... x) Combines the arguments to form a vector.- Parameters:
x
- the vector elements.- Returns:
- the vector.
-
c
public static double[] c(double... x) Combines the arguments to form a vector.- Parameters:
x
- the vector elements.- Returns:
- the vector.
-
c
Combines the arguments to form a vector.- Parameters:
x
- the vector elements.- Returns:
- the vector.
-
c
public static int[] c(int[]... list) Concatenates multiple vectors into one.- Parameters:
list
- the vectors.- Returns:
- the concatenated vector.
-
c
public static float[] c(float[]... list) Concatenates multiple vectors into one.- Parameters:
list
- the vectors.- Returns:
- the concatenated vector.
-
c
public static double[] c(double[]... list) Concatenates multiple vectors into one.- Parameters:
list
- the vectors.- Returns:
- the concatenated vector.
-
c
Concatenates multiple vectors into one array of strings.- Parameters:
list
- the vectors.- Returns:
- the concatenated vector.
-
cbind
public static int[] cbind(int[]... x) Concatenates vectors by columns.- Parameters:
x
- the vectors.- Returns:
- the concatenated vector.
-
cbind
public static float[] cbind(float[]... x) Concatenates vectors by columns.- Parameters:
x
- the vectors.- Returns:
- the concatenated vector.
-
cbind
public static double[] cbind(double[]... x) Concatenates vectors by columns.- Parameters:
x
- the vectors.- Returns:
- the concatenated vector.
-
cbind
Concatenates vectors by columns.- Parameters:
x
- the vectors.- Returns:
- the concatenated vector.
-
rbind
public static int[][] rbind(int[]... x) Concatenates vectors by rows.- Parameters:
x
- the vectors.- Returns:
- the matrix.
-
rbind
public static float[][] rbind(float[]... x) Concatenates vectors by rows.- Parameters:
x
- the vectors.- Returns:
- the matrix.
-
rbind
public static double[][] rbind(double[]... x) Concatenates vectors by rows.- Parameters:
x
- the vectors.- Returns:
- the matrix.
-
rbind
Concatenates vectors by rows.- Parameters:
x
- the vectors.- Returns:
- the matrix.
-
slice
public static <E> E[] slice(E[] data, int[] index) Returns a slice of data for given indices.- Type Parameters:
E
- the data type of elements.- Parameters:
data
- the array.index
- the indices of selected elements.- Returns:
- the selected elements.
-
slice
public static int[] slice(int[] data, int[] index) Returns a slice of data for given indices.- Parameters:
data
- the array.index
- the indices of selected elements.- Returns:
- the selected elements.
-
slice
public static float[] slice(float[] data, int[] index) Returns a slice of data for given indices.- Parameters:
data
- the array.index
- the indices of selected elements.- Returns:
- the selected elements.
-
slice
public static double[] slice(double[] data, int[] index) Returns a slice of data for given indices.- Parameters:
data
- the array.index
- the indices of selected elements.- Returns:
- the selected elements.
-
contains
public static boolean contains(double[][] polygon, double[] point) Determines if the polygon contains the point.- Parameters:
polygon
- the vertices of polygon.point
- the point.- Returns:
- true if the Polygon contains the point.
-
contains
public static boolean contains(double[][] polygon, double x, double y) Determines if the polygon contains the point.- Parameters:
polygon
- the vertices of polygon.x
- the x coordinate of point.y
- the y coordinate of point.- Returns:
- true if the Polygon contains the point.
-
omit
public static int[] omit(int[] a, int value) Returns a new array without the specified value.- Parameters:
a
- an input array.value
- the value to omit.- Returns:
- a new array without the specified value.
-
omit
public static float[] omit(float[] a, float value) Returns a new array without the specified value.- Parameters:
a
- an input array.value
- the value to omit.- Returns:
- a new array without the specified value.
-
omit
public static double[] omit(double[] a, double value) Returns a new array without the specified value.- Parameters:
a
- an input array.value
- the value to omit.- Returns:
- a new array without the specified value.
-
omitNaN
public static float[] omitNaN(float[] a) Returns a new array without NaN values.- Parameters:
a
- an input array that may contain NaN.- Returns:
- a new array without NaN values.
-
omitNaN
public static double[] omitNaN(double[] a) Returns a new array without NaN values.- Parameters:
a
- an input array that may contain NaN.- Returns:
- a new array without NaN values.
-
reverse
public static void reverse(int[] a) Reverses the order of the elements in the specified array.- Parameters:
a
- an array to reverse.
-
reverse
public static void reverse(float[] a) Reverses the order of the elements in the specified array.- Parameters:
a
- the array to reverse.
-
reverse
public static void reverse(double[] a) Reverses the order of the elements in the specified array.- Parameters:
a
- the array to reverse.
-
reverse
public static <T> void reverse(T[] a) Reverses the order of the elements in the specified array.- Type Parameters:
T
- the data type of array elements.- Parameters:
a
- the array to reverse.
-
mode
public static int mode(int[] a) Returns the mode of the array, which is the most frequent element. If there are multiple modes, one of them will be returned.- Parameters:
a
- the array. The order of elements will be changed on output.- Returns:
- the mode.
-
min
public static int min(int a, int b, int c) Returns the minimum of 3 integer numbers.- Parameters:
a
- a number.b
- a number.c
- a number.- Returns:
- the minimum.
-
min
public static float min(float a, float b, float c) Returns the minimum of 3 float numbers.- Parameters:
a
- a number.b
- a number.c
- a number.- Returns:
- the minimum.
-
min
public static double min(double a, double b, double c) Returns the minimum of 3 double numbers.- Parameters:
a
- a number.b
- a number.c
- a number.- Returns:
- the minimum.
-
min
public static int min(int a, int b, int c, int d) Returns the minimum of 4 integer numbers.- Parameters:
a
- a number.b
- a number.c
- a number.d
- a number.- Returns:
- the minimum.
-
min
public static float min(float a, float b, float c, float d) Returns the minimum of 4 float numbers.- Parameters:
a
- a number.b
- a number.c
- a number.d
- a number.- Returns:
- the minimum.
-
min
public static double min(double a, double b, double c, double d) Returns the minimum of 4 double numbers.- Parameters:
a
- a number.b
- a number.c
- a number.d
- a number.- Returns:
- the minimum.
-
max
public static int max(int a, int b, int c) Returns the maximum of 3 integer numbers.- Parameters:
a
- a number.b
- a number.c
- a number.- Returns:
- the maximum.
-
max
public static float max(float a, float b, float c) Returns the maximum of 4 float numbers.- Parameters:
a
- a number.b
- a number.c
- a number.- Returns:
- the maximum.
-
max
public static double max(double a, double b, double c) Returns the maximum of 4 double numbers.- Parameters:
a
- a number.b
- a number.c
- a number.- Returns:
- the maximum.
-
max
public static int max(int a, int b, int c, int d) Returns the maximum of 4 integer numbers.- Parameters:
a
- a number.b
- a number.c
- a number.d
- a number.- Returns:
- the maximum.
-
max
public static float max(float a, float b, float c, float d) Returns the maximum of 4 float numbers.- Parameters:
a
- a number.b
- a number.c
- a number.d
- a number.- Returns:
- the maximum.
-
max
public static double max(double a, double b, double c, double d) Returns the maximum of 4 double numbers.- Parameters:
a
- a number.b
- a number.c
- a number.d
- a number.- Returns:
- the maximum.
-
min
public static int min(int[] array) Returns the minimum value of an array.- Parameters:
array
- the array.- Returns:
- the minimum.
-
min
public static float min(float[] array) Returns the minimum value of an array.- Parameters:
array
- the array.- Returns:
- the minimum.
-
min
public static double min(double[] array) Returns the minimum value of an array.- Parameters:
array
- the array.- Returns:
- the minimum.
-
whichMin
public static int whichMin(int[] array) Returns the index of minimum value of an array.- Parameters:
array
- the array.- Returns:
- the index of minimum.
-
whichMin
public static int whichMin(float[] array) Returns the index of minimum value of an array.- Parameters:
array
- the array.- Returns:
- the index of minimum.
-
whichMin
public static int whichMin(double[] array) Returns the index of minimum value of an array.- Parameters:
array
- the array.- Returns:
- the index of minimum.
-
max
public static int max(int[] array) Returns the maximum value of an array.- Parameters:
array
- the array.- Returns:
- the index of maximum.
-
max
public static float max(float[] array) Returns the maximum value of an array.- Parameters:
array
- the array.- Returns:
- the index of maximum.
-
max
public static double max(double[] array) Returns the maximum value of an array.- Parameters:
array
- the array.- Returns:
- the index of maximum.
-
whichMax
public static int whichMax(int[] array) Returns the index of maximum value of an array.- Parameters:
array
- the array.- Returns:
- the index of maximum.
-
whichMax
public static int whichMax(float[] array) Returns the index of maximum value of an array.- Parameters:
array
- the array.- Returns:
- the index of maximum.
-
whichMax
public static int whichMax(double[] array) Returns the index of maximum value of an array.- Parameters:
array
- the array.- Returns:
- the index of maximum.
-
min
public static int min(int[][] matrix) Returns the minimum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the minimum.
-
min
public static double min(double[][] matrix) Returns the minimum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the minimum.
-
max
public static int max(int[][] matrix) Returns the maximum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the maximum.
-
max
public static double max(double[][] matrix) Returns the maximum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the maximum.
-
whichMin
Returns the index of minimum value of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the index of minimum.
-
whichMax
Returns the index of maximum value of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the index of maximum.
-
transpose
public static double[][] transpose(double[][] matrix) Returns the matrix transpose.- Parameters:
matrix
- the matrix.- Returns:
- the transpose.
-
rowMin
public static int[] rowMin(int[][] matrix) Returns the row minimum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row minimums.
-
rowMax
public static int[] rowMax(int[][] matrix) Returns the row maximum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row maximums.
-
rowSums
public static long[] rowSums(int[][] matrix) Returns the row sums of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row sums.
-
rowMin
public static double[] rowMin(double[][] matrix) Returns the row minimum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row minimums.
-
rowMax
public static double[] rowMax(double[][] matrix) Returns the row maximum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row maximums.
-
rowSums
public static double[] rowSums(double[][] matrix) Returns the row sums of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row sums.
-
rowMeans
public static double[] rowMeans(double[][] matrix) Returns the row means of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row means.
-
rowSds
public static double[] rowSds(double[][] matrix) Returns the row standard deviations of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the row standard deviations.
-
colMin
public static int[] colMin(int[][] matrix) Returns the column minimum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column minimums.
-
colMax
public static int[] colMax(int[][] matrix) Returns the column maximum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column maximums.
-
colSums
public static long[] colSums(int[][] matrix) Returns the column sums of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column sums.
-
colMin
public static double[] colMin(double[][] matrix) Returns the column minimum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column minimums.
-
colMax
public static double[] colMax(double[][] matrix) Returns the column maximum of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column maximums.
-
colSums
public static double[] colSums(double[][] matrix) Returns the column sums of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column sums.
-
colMeans
public static double[] colMeans(double[][] matrix) Returns the column means of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column means.
-
colSds
public static double[] colSds(double[][] matrix) Returns the column standard deviations of a matrix.- Parameters:
matrix
- the matrix.- Returns:
- the column standard deviations.
-
sum
public static int sum(byte[] x) Returns the sum of an array.- Parameters:
x
- the array.- Returns:
- the sum.
-
sum
public static long sum(int[] x) Returns the sum of an array.- Parameters:
x
- the array.- Returns:
- the sum.
-
sum
public static double sum(float[] x) Returns the sum of an array.- Parameters:
x
- the array.- Returns:
- the sum.
-
sum
public static double sum(double[] x) Returns the sum of an array.- Parameters:
x
- the array.- Returns:
- the sum.
-
median
public static int median(int[] x) Find the median of an array of type int. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the median.
-
median
public static float median(float[] x) Find the median of an array of type float. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the median.
-
median
public static double median(double[] x) Find the median of an array of type double. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the median.
-
median
Find the median of an array of type double. The input array will be rearranged.- Type Parameters:
T
- the data type of array elements.- Parameters:
x
- the array.- Returns:
- the median.
-
q1
public static int q1(int[] x) Find the first quantile (p = 1/4) of an array of type int. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the first quantile.
-
q1
public static float q1(float[] x) Find the first quantile (p = 1/4) of an array of type float. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the first quantile.
-
q1
public static double q1(double[] x) Find the first quantile (p = 1/4) of an array of type double. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the first quantile.
-
q1
Find the first quantile (p = 1/4) of an array of type double. The input array will be rearranged.- Type Parameters:
T
- the data type of array elements.- Parameters:
x
- the array.- Returns:
- the first quantile.
-
q3
public static int q3(int[] x) Find the third quantile (p = 3/4) of an array of type int. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the third quantile.
-
q3
public static float q3(float[] x) Find the third quantile (p = 3/4) of an array of type float. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the third quantile.
-
q3
public static double q3(double[] x) Find the third quantile (p = 3/4) of an array of type double. The input array will be rearranged.- Parameters:
x
- the array.- Returns:
- the third quantile.
-
q3
Find the third quantile (p = 3/4) of an array of type double. The input array will be rearranged.- Type Parameters:
T
- the data type of array elements.- Parameters:
x
- the array.- Returns:
- the third quantile.
-
mean
public static double mean(int[] array) Returns the mean of an array.- Parameters:
array
- the array.- Returns:
- the mean.
-
mean
public static double mean(float[] array) Returns the mean of an array.- Parameters:
array
- the array.- Returns:
- the mean.
-
mean
public static double mean(double[] array) Returns the mean of an array.- Parameters:
array
- the array.- Returns:
- the mean.
-
var
public static double var(int[] array) Returns the variance of an array.- Parameters:
array
- the array.- Returns:
- the variance.
-
var
public static double var(float[] array) Returns the variance of an array.- Parameters:
array
- the array.- Returns:
- the variance.
-
var
public static double var(double[] array) Returns the variance of an array.- Parameters:
array
- the array.- Returns:
- the variance.
-
sd
public static double sd(int[] array) Returns the standard deviation of an array.- Parameters:
array
- the array.- Returns:
- the standard deviation.
-
sd
public static double sd(float[] array) Returns the standard deviation of an array.- Parameters:
array
- the array.- Returns:
- the standard deviation.
-
sd
public static double sd(double[] array) Returns the standard deviation of an array.- Parameters:
array
- the array.- Returns:
- the standard deviation.
-
mad
public static double mad(int[] array) Returns the median absolute deviation (MAD). Note that input array will be altered after the computation. MAD is a robust measure of the variability of a univariate sample of quantitative data. For a univariate data set X1, X2, ..., Xn, the MAD is defined as the median of the absolute deviations from the data's median:MAD(X) = median(|Xi - median(Xi)|)
that is, starting with the residuals (deviations) from the data's median, the MAD is the median of their absolute values.
MAD is a more robust estimator of scale than the sample variance or standard deviation. For instance, MAD is more resilient to outliers in a data set than the standard deviation. It thus behaves better with distributions without a mean or variance, such as the Cauchy distribution.
In order to use the MAD as a consistent estimator for the estimation of the standard deviation σ, one takes σ = K * MAD, where K is a constant scale factor, which depends on the distribution. For normally distributed data K is taken to be 1.4826. Other distributions behave differently: for example for large samples from a uniform continuous distribution, this factor is about 1.1547.
- Parameters:
array
- the array.- Returns:
- the median absolute deviation.
-
mad
public static double mad(float[] array) Returns the median absolute deviation (MAD). Note that input array will be altered after the computation. MAD is a robust measure of the variability of a univariate sample of quantitative data. For a univariate data set X1, X2, ..., Xn, the MAD is defined as the median of the absolute deviations from the data's median:MAD(X) = median(|Xi - median(Xi)|)
that is, starting with the residuals (deviations) from the data's median, the MAD is the median of their absolute values.
MAD is a more robust estimator of scale than the sample variance or standard deviation. For instance, MAD is more resilient to outliers in a data set than the standard deviation. It thus behaves better with distributions without a mean or variance, such as the Cauchy distribution.
In order to use the MAD as a consistent estimator for the estimation of the standard deviation σ, one takes σ = K * MAD, where K is a constant scale factor, which depends on the distribution. For normally distributed data K is taken to be 1.4826. Other distributions behave differently: for example for large samples from a uniform continuous distribution, this factor is about 1.1547.
- Parameters:
array
- the array.- Returns:
- the median abolute deviation.
-
mad
public static double mad(double[] array) Returns the median absolute deviation (MAD). Note that input array will be altered after the computation. MAD is a robust measure of the variability of a univariate sample of quantitative data. For a univariate data set X1, X2, ..., Xn, the MAD is defined as the median of the absolute deviations from the data's median:MAD(X) = median(|Xi - median(Xi)|)
that is, starting with the residuals (deviations) from the data's median, the MAD is the median of their absolute values.
MAD is a more robust estimator of scale than the sample variance or standard deviation. For instance, MAD is more resilient to outliers in a data set than the standard deviation. It thus behaves better with distributions without a mean or variance, such as the Cauchy distribution.
In order to use the MAD as a consistent estimator for the estimation of the standard deviation σ, one takes σ = K * MAD, where K is a constant scale factor, which depends on the distribution. For normally distributed data K is taken to be 1.4826. Other distributions behave differently: for example for large samples from a uniform continuous distribution, this factor is about 1.1547.
- Parameters:
array
- the array.- Returns:
- the median abolute deviation.
-
distance
public static double distance(int[] a, int[] b) The Euclidean distance on binary sparse arrays, which are the indices of nonzero elements in ascending order.- Parameters:
a
- a binary sparse vector.b
- a binary sparse vector.- Returns:
- the Euclidean distance.
-
distance
public static double distance(float[] a, float[] b) The Euclidean distance.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the Euclidean distance.
-
distance
public static double distance(double[] a, double[] b) The Euclidean distance.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the Euclidean distance.
-
distance
The Euclidean distance.- Parameters:
a
- a sparse vector.b
- a sparse vector.- Returns:
- the Euclidean distance.
-
squaredDistance
public static double squaredDistance(int[] a, int[] b) The squared Euclidean distance on binary sparse arrays, which are the indices of nonzero elements in ascending order.- Parameters:
a
- a binary sparse vector.b
- a binary sparse vector.- Returns:
- the square of Euclidean distance.
-
squaredDistance
public static double squaredDistance(float[] a, float[] b) The squared Euclidean distance.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the square of Euclidean distance.
-
squaredDistance
public static double squaredDistance(double[] a, double[] b) The squared Euclidean distance.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the square of Euclidean distance.
-
squaredDistance
The Euclidean distance on sparse arrays.- Parameters:
a
- a sparse vector.b
- a sparse vector.- Returns:
- the square of Euclidean distance.
-
squaredDistanceWithMissingValues
public static double squaredDistanceWithMissingValues(double[] x, double[] y) The squared Euclidean distance with handling missing values (represented as NaN). NaN will be treated as missing values and will be excluded from the calculation. Let m be the number nonmissing values, and n be the number of all values. The returned distance is (n * d / m), where d is the square of distance between nonmissing values.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the square of Euclidean distance.
-
pdist
Returns the pairwise distance matrix of multiple binary sparse vectors.- Parameters:
x
- binary sparse vectors, which are the indices of nonzero elements in ascending order.- Returns:
- the full pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple binary sparse vectors.- Parameters:
x
- binary sparse vectors, which are the indices of nonzero elements in ascending order.squared
- If true, compute the squared Euclidean distance.- Returns:
- the pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple vectors.- Parameters:
x
- the vectors.- Returns:
- the full pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple vectors.- Parameters:
x
- the vectors.squared
- If true, compute the squared Euclidean distance.- Returns:
- the pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple vectors.- Parameters:
x
- the vectors.- Returns:
- the full pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple vectors.- Parameters:
x
- the vectors.squared
- If true, compute the squared Euclidean distance.- Returns:
- the pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple vectors.- Parameters:
x
- the vectors.- Returns:
- the full pairwise distance matrix.
-
pdist
Returns the pairwise distance matrix of multiple vectors.- Parameters:
x
- the vectors.squared
- If true, compute the squared Euclidean distance.- Returns:
- the pairwise distance matrix.
-
pdist
Computes the pairwise distance matrix of multiple vectors.- Type Parameters:
T
- the data type of vectors.- Parameters:
x
- the vectors.d
- The output distance matrix. It may be only the lower half.distance
- the distance lambda.
-
entropy
public static double entropy(double[] p) Shannon's entropy.- Parameters:
p
- the probabilities.- Returns:
- Shannon's entropy.
-
KullbackLeiblerDivergence
public static double KullbackLeiblerDivergence(double[] p, double[] q) Kullback-Leibler divergence. The Kullback-Leibler divergence (also information divergence, information gain, relative entropy, or KLIC) is a non-symmetric measure of the difference between two probability distributions P and Q. KL measures the expected number of extra bits required to code samples from P when using a code based on Q, rather than using a code based on P. Typically P represents the "true" distribution of data, observations, or a precise calculated theoretical distribution. The measure Q typically represents a theory, model, description, or approximation of P.Although it is often intuited as a distance metric, the KL divergence is not a true metric - for example, the KL from P to Q is not necessarily the same as the KL from Q to P.
- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Kullback-Leibler divergence
-
KullbackLeiblerDivergence
Kullback-Leibler divergence. The Kullback-Leibler divergence (also information divergence, information gain, relative entropy, or KLIC) is a non-symmetric measure of the difference between two probability distributions P and Q. KL measures the expected number of extra bits required to code samples from P when using a code based on Q, rather than using a code based on P. Typically P represents the "true" distribution of data, observations, or a precise calculated theoretical distribution. The measure Q typically represents a theory, model, description, or approximation of P.Although it is often intuited as a distance metric, the KL divergence is not a true metric - for example, the KL from P to Q is not necessarily the same as the KL from Q to P.
- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Kullback-Leibler divergence
-
KullbackLeiblerDivergence
Kullback-Leibler divergence. The Kullback-Leibler divergence (also information divergence, information gain, relative entropy, or KLIC) is a non-symmetric measure of the difference between two probability distributions P and Q. KL measures the expected number of extra bits required to code samples from P when using a code based on Q, rather than using a code based on P. Typically P represents the "true" distribution of data, observations, or a precise calculated theoretical distribution. The measure Q typically represents a theory, model, description, or approximation of P.Although it is often intuited as a distance metric, the KL divergence is not a true metric - for example, the KL from P to Q is not necessarily the same as the KL from Q to P.
- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Kullback-Leibler divergence
-
KullbackLeiblerDivergence
Kullback-Leibler divergence. The Kullback-Leibler divergence (also information divergence, information gain, relative entropy, or KLIC) is a non-symmetric measure of the difference between two probability distributions P and Q. KL measures the expected number of extra bits required to code samples from P when using a code based on Q, rather than using a code based on P. Typically P represents the "true" distribution of data, observations, or a precise calculated theoretical distribution. The measure Q typically represents a theory, model, description, or approximation of P.Although it is often intuited as a distance metric, the KL divergence is not a true metric - for example, the KL from P to Q is not necessarily the same as the KL from Q to P.
- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Kullback-Leibler divergence
-
JensenShannonDivergence
public static double JensenShannonDivergence(double[] p, double[] q) Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2. The Jensen-Shannon divergence is a popular method of measuring the similarity between two probability distributions. It is also known as information radius or total divergence to the average. It is based on the Kullback-Leibler divergence, with the difference that it is always a finite value. The square root of the Jensen-Shannon divergence is a metric.- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Jensen-Shannon divergence
-
JensenShannonDivergence
Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2. The Jensen-Shannon divergence is a popular method of measuring the similarity between two probability distributions. It is also known as information radius or total divergence to the average. It is based on the Kullback-Leibler divergence, with the difference that it is always a finite value. The square root of the Jensen-Shannon divergence is a metric.- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Jensen-Shannon divergence
-
JensenShannonDivergence
Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2. The Jensen-Shannon divergence is a popular method of measuring the similarity between two probability distributions. It is also known as information radius or total divergence to the average. It is based on the Kullback-Leibler divergence, with the difference that it is always a finite value. The square root of the Jensen-Shannon divergence is a metric.- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Jensen-Shannon divergence
-
JensenShannonDivergence
Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2. The Jensen-Shannon divergence is a popular method of measuring the similarity between two probability distributions. It is also known as information radius or total divergence to the average. It is based on the Kullback-Leibler divergence, with the difference that it is always a finite value. The square root of the Jensen-Shannon divergence is a metric.- Parameters:
p
- a probability distribution.q
- a probability distribution.- Returns:
- Jensen-Shannon divergence
-
dot
public static int dot(int[] a, int[] b) Returns the dot product between two binary sparse arrays, which are the indices of nonzero elements in ascending order.- Parameters:
a
- a binary sparse vector.b
- a binary sparse vector.- Returns:
- the dot product.
-
dot
public static float dot(float[] a, float[] b) Returns the dot product between two vectors.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the dot product.
-
dot
public static double dot(double[] a, double[] b) Returns the dot product between two vectors.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the dot product.
-
dot
Returns the dot product between two sparse arrays.- Parameters:
x
- a sparse vector.y
- a sparse vector.- Returns:
- the dot product.
-
pdot
Returns the pairwise dot product matrix of binary sparse vectors.- Parameters:
x
- the binary sparse vectors, which are the indices of nonzero elements in ascending order.- Returns:
- the dot product matrix.
-
pdot
Returns the pairwise dot product matrix of float vectors.- Parameters:
x
- the vectors.- Returns:
- the dot product matrix.
-
pdot
Returns the pairwise dot product matrix of double vectors.- Parameters:
x
- the vectors.- Returns:
- the dot product matrix.
-
pdot
Returns the pairwise dot product matrix of multiple vectors.- Parameters:
x
- the sparse vectors.- Returns:
- the dot product matrix.
-
cov
public static double cov(int[] x, int[] y) Returns the covariance between two vectors.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the covariance.
-
cov
public static double cov(float[] x, float[] y) Returns the covariance between two vectors.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the covariance.
-
cov
public static double cov(double[] x, double[] y) Returns the covariance between two vectors.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the covariance.
-
cov
public static double[][] cov(double[][] data) Returns the sample covariance matrix.- Parameters:
data
- the samples- Returns:
- the covariance matrix.
-
cov
public static double[][] cov(double[][] data, double[] mu) Returns the sample covariance matrix.- Parameters:
data
- the samplesmu
- the known mean of data.- Returns:
- the covariance matrix.
-
cor
public static double cor(int[] x, int[] y) Returns the correlation coefficient between two vectors.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the correlation coefficient.
-
cor
public static double cor(float[] x, float[] y) Returns the correlation coefficient between two vectors.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the correlation coefficient.
-
cor
public static double cor(double[] x, double[] y) Returns the correlation coefficient between two vectors.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the correlation coefficient.
-
cor
public static double[][] cor(double[][] data) Returns the sample correlation matrix.- Parameters:
data
- the samples- Returns:
- the correlation matrix.
-
cor
public static double[][] cor(double[][] data, double[] mu) Returns the sample correlation matrix.- Parameters:
data
- the samplesmu
- the known mean of data.- Returns:
- the correlation matrix.
-
spearman
public static double spearman(int[] x, int[] y) The Spearman Rank Correlation Coefficient is a form of the Pearson coefficient with the data converted to rankings (i.e. when variables are ordinal). It can be used when there is non-parametric data and hence Pearson cannot be used.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the Spearman rank correlation coefficient.
-
spearman
public static double spearman(float[] x, float[] y) The Spearman Rank Correlation Coefficient is a form of the Pearson coefficient with the data converted to rankings (i.e. when variables are ordinal). It can be used when there is non-parametric data and hence Pearson cannot be used.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the Spearman rank correlation coefficient.
-
spearman
public static double spearman(double[] x, double[] y) The Spearman Rank Correlation Coefficient is a form of the Pearson coefficient with the data converted to rankings (i.e. when variables are ordinal). It can be used when there is non-parametric data and hence Pearson cannot be used.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the Spearman rank correlation coefficient.
-
kendall
public static double kendall(int[] x, int[] y) The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant. It is used with non-parametric data.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the Kendall rank correlation coefficient.
-
kendall
public static double kendall(float[] x, float[] y) The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant. It is used with non-parametric data.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the Kendall rank correlation coefficient.
-
kendall
public static double kendall(double[] x, double[] y) The Kendall Tau Rank Correlation Coefficient is used to measure the degree of correspondence between sets of rankings where the measures are not equidistant. It is used with non-parametric data.- Parameters:
x
- a vector.y
- a vector.- Returns:
- the Kendall rank correlation coefficient.
-
norm1
public static float norm1(float[] x) L1 vector norm.- Parameters:
x
- a vector.- Returns:
- L1 norm.
-
norm1
public static double norm1(double[] x) L1 vector norm.- Parameters:
x
- a vector.- Returns:
- L1 norm.
-
norm2
public static float norm2(float[] x) L2 vector norm.- Parameters:
x
- a vector.- Returns:
- L2 norm.
-
norm2
public static double norm2(double[] x) L2 vector norm.- Parameters:
x
- a vector.- Returns:
- L2 norm.
-
normInf
public static float normInf(float[] x) L∞ vector norm that is the maximum absolute value.- Parameters:
x
- a vector.- Returns:
- L∞ norm.
-
normInf
public static double normInf(double[] x) L∞ vector norm. Maximum absolute value.- Parameters:
x
- a vector.- Returns:
- L∞ norm.
-
norm
public static float norm(float[] x) L2 vector norm.- Parameters:
x
- a vector.- Returns:
- L2 norm.
-
norm
public static double norm(double[] x) L2 vector norm.- Parameters:
x
- a vector.- Returns:
- L2 norm.
-
cos
public static float cos(float[] a, float[] b) Returns the cosine similarity.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the cosine similarity.
-
cos
public static double cos(double[] a, double[] b) Returns the cosine similarity.- Parameters:
a
- a vector.b
- a vector.- Returns:
- the cosine similarity.
-
standardize
public static void standardize(double[] array) Standardizes an array to mean 0 and variance 1.- Parameters:
array
- the array.
-
scale
public static void scale(double[][] matrix) Scales each column of a matrix to range [0, 1].- Parameters:
matrix
- the matrix.
-
standardize
public static void standardize(double[][] matrix) Standardizes each column of a matrix to 0 mean and unit variance.- Parameters:
matrix
- the matrix.
-
normalize
public static void normalize(double[][] matrix) Unitizes each column of a matrix to unit length (L_2 norm).- Parameters:
matrix
- the matrix.
-
normalize
public static void normalize(double[][] matrix, boolean centerizing) Unitizes each column of a matrix to unit length (L_2 norm).- Parameters:
matrix
- the matrix.centerizing
- If true, centerize each column to 0 mean.
-
unitize
public static void unitize(double[] array) Unitize an array so that L2 norm of array = 1.- Parameters:
array
- the vector.
-
unitize1
public static void unitize1(double[] array) Unitize an array so that L1 norm of array is 1.- Parameters:
array
- the vector.
-
unitize2
public static void unitize2(double[] array) Unitize an array so that L2 norm of array = 1.- Parameters:
array
- the vector.
-
equals
public static boolean equals(float[] x, float[] y) Check if x element-wisely equals y with default epsilon 1E-7.- Parameters:
x
- an array.y
- an array.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(float[] x, float[] y, float epsilon) Check if x element-wisely equals y in given precision.- Parameters:
x
- an array.y
- an array.epsilon
- a number close to zero.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(double[] x, double[] y) Check if x element-wisely equals y with default epsilon 1E-10.- Parameters:
x
- an array.y
- an array.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(double[] x, double[] y, double epsilon) Check if x element-wisely equals y in given precision.- Parameters:
x
- an array.y
- an array.epsilon
- a number close to zero.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(float[][] x, float[][] y) Check if x element-wisely equals y with default epsilon 1E-7.- Parameters:
x
- a two-dimensional array.y
- a two-dimensional array.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(float[][] x, float[][] y, float epsilon) Check if x element-wisely equals y in given precision.- Parameters:
x
- a two-dimensional array.y
- a two-dimensional array.epsilon
- a number close to zero.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(double[][] x, double[][] y) Check if x element-wisely equals y with default epsilon 1E-10.- Parameters:
x
- a two-dimensional array.y
- a two-dimensional array.- Returns:
- true if x element-wisely equals y.
-
equals
public static boolean equals(double[][] x, double[][] y, double epsilon) Check if x element-wisely equals y in given precision.- Parameters:
x
- a two-dimensional array.y
- a two-dimensional array.epsilon
- a number close to zero.- Returns:
- true if x element-wisely equals y.
-
isZero
public static boolean isZero(float x) Tests if a floating number is zero in machine precision.- Parameters:
x
- a real number.- Returns:
- true if x is zero in machine precision.
-
isZero
public static boolean isZero(float x, float epsilon) Tests if a floating number is zero in given precision.- Parameters:
x
- a real number.epsilon
- a number close to zero.- Returns:
- true if x is zero in
epsilon
precision.
-
isZero
public static boolean isZero(double x) Tests if a floating number is zero in machine precision.- Parameters:
x
- a real number.- Returns:
- true if x is zero in machine precision.
-
isZero
public static boolean isZero(double x, double epsilon) Tests if a floating number is zero in given precision.- Parameters:
x
- a real number.epsilon
- a number close to zero.- Returns:
- true if x is zero in
epsilon
precision.
-
clone
public static int[][] clone(int[][] x) Deep clone a two-dimensional array.- Parameters:
x
- a two-dimensional array.- Returns:
- the deep clone.
-
clone
public static float[][] clone(float[][] x) Deep clone a two-dimensional array.- Parameters:
x
- a two-dimensional array.- Returns:
- the deep clone.
-
clone
public static double[][] clone(double[][] x) Deep clone a two-dimensional array.- Parameters:
x
- a two-dimensional array.- Returns:
- the deep clone.
-
swap
public static void swap(int[] x, int i, int j) Swap two elements of an array.- Parameters:
x
- an array.i
- the index of first element.j
- the index of second element.
-
swap
public static void swap(float[] x, int i, int j) Swap two elements of an array.- Parameters:
x
- an array.i
- the index of first element.j
- the index of second element.
-
swap
public static void swap(double[] x, int i, int j) Swap two elements of an array.- Parameters:
x
- an array.i
- the index of first element.j
- the index of second element.
-
swap
Swap two elements of an array.- Parameters:
x
- an array.i
- the index of first element.j
- the index of second element.
-
swap
public static void swap(int[] x, int[] y) Swap two arrays.- Parameters:
x
- an array.y
- the other array.
-
swap
public static void swap(float[] x, float[] y) Swap two arrays.- Parameters:
x
- an array.y
- the other array.
-
swap
public static void swap(double[] x, double[] y) Swap two arrays.- Parameters:
x
- an array.y
- the other array.
-
swap
public static <E> void swap(E[] x, E[] y) Swap two arrays.- Type Parameters:
E
- the data type of array elements.- Parameters:
x
- an array.y
- the other array.
-
copy
public static void copy(int[][] x, int[][] y) Copy x into y.- Parameters:
x
- the input matrix.y
- the output matrix.
-
copy
public static void copy(float[][] x, float[][] y) Deep copy x into y.- Parameters:
x
- the input matrix.y
- the output matrix.
-
copy
public static void copy(double[][] x, double[][] y) Deep copy x into y.- Parameters:
x
- the input matrix.y
- the output matrix.
-
add
public static void add(double[] y, double[] x) Element-wise sum of two arrays y = x + y.- Parameters:
y
- avector.x
- a vector.
-
sub
public static void sub(double[] a, double[] b) Element-wise subtraction of two arrays a -= b.- Parameters:
a
- the minuend array.b
- the subtrahend array.
-
scale
public static void scale(double a, double[] x) Scale each element of an array by a constant x = a * x.- Parameters:
a
- the scale factor.x
- the input and output vector.
-
scale
public static void scale(double a, double[] x, double[] y) Scale each element of an array by a constant y = a * x.- Parameters:
a
- the scale factor.x
- a vector.y
- the output vector.
-
axpy
public static double[] axpy(double a, double[] x, double[] y) Update an array by adding a multiple of another array y = a * x + y.- Parameters:
a
- the scale factor.x
- a vector.y
- the input and output vector.- Returns:
- the vector y.
-
pow
public static double[] pow(double[] x, double n) Raise each element of an array to a scalar power.- Parameters:
x
- the base array.n
- the scalar exponent.- Returns:
- xn
-
unique
public static int[] unique(int[] array) Find unique elements of vector.- Parameters:
array
- an integer array.- Returns:
- the same values as in x but with no repetitions.
-
unique
Find unique elements of vector.- Parameters:
array
- an array of strings.- Returns:
- the same values as in x but with no repetitions.
-
sort
public static int[][] sort(double[][] matrix) Sorts each variable and returns the index of values in ascending order. Note that the order of original array is NOT altered.- Parameters:
matrix
- a set of variables to be sorted. Each row is an instance. Each column is a variable.- Returns:
- the index of values in ascending order
-
solve
public static double[] solve(double[] a, double[] b, double[] c, double[] r) Solve the tridiagonal linear set which is of diagonal dominance |bi|>
|ai| + |ci|.| b0 c0 0 0 0 ... | | a1 b1 c1 0 0 ... | | 0 a2 b2 c2 0 ... | | ... | | ... a(n-2) b(n-2) c(n-2) | | ... 0 a(n-1) b(n-1) |
- Parameters:
a
- the lower part of tridiagonal matrix. a[0] is undefined and not referenced by the method.b
- the diagonal of tridiagonal matrix.c
- the upper of tridiagonal matrix. c[n-1] is undefined and not referenced by the method.r
- the right-hand side of linear equations.- Returns:
- the solution.
-