Package smile.math

Class Math


  • public class Math
    extends java.lang.Object
    A collection of useful mathematical functions. The following functions are included:
    • scalar functions: Besides all methods in java.lang.Math are included for convenience, sqr, factorial, logFractorial, choose, logChoose, 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.
    Author:
    Haifeng Li
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DIGITS
      The number of digits (in radix base) in the mantissa.
      static double E
      The base of the natural logarithms.
      static double EPSILON
      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 int MACHEP
      The largest negative integer such that 1.0 + RADIXMACHEP ≠ 1.0, except that machep is bounded below by -(DIGITS+3)
      static int NEGEP
      The largest negative integer such that 1.0 - RADIXNEGEP ≠ 1.0, except that negeps is bounded below by -(DIGITS+3)
      static double PI
      The ratio of the circumference of a circle to its diameter.
      static int RADIX
      The base of the exponent of the double type.
      static int ROUND_STYLE
      Rounding style.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double abs​(double a)
      Returns the absolute value of a double value.
      static float abs​(float a)
      Returns the absolute value of a float value.
      static int abs​(int a)
      Returns the absolute value of an int value.
      static long abs​(long a)
      Returns the absolute value of a long value.
      static double acos​(double a)
      Returns the arc cosine of an angle, in the range of 0.0 through pi.
      static boolean all​(boolean[] x)
      Given a set of boolean values, are all of the values true?
      static boolean any​(boolean[] x)
      Given a set of boolean values, is at least one of the values true?
      static double asin​(double a)
      Returns the arc sine of an angle, in the range of -pi/2 through pi/2.
      static double atan​(double a)
      Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
      static double atan2​(double y, double x)
      Converts rectangular coordinates (x, y) to polar (r, theta).
      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[]... x)
      Merges multiple vectors into one.
      static float[] c​(float... x)
      Combines the arguments to form a vector.
      static float[] c​(float[]... x)
      Merges multiple vectors into one.
      static int[] c​(int... x)
      Combines the arguments to form a vector.
      static int[] c​(int[]... x)
      Merges multiple vectors into one.
      static java.lang.String[] c​(java.lang.String... x)
      Combines the arguments to form a vector.
      static java.lang.String[] c​(java.lang.String[]... x)
      Merges multiple vectors into one.
      static double[] cbind​(double[]... x)
      Take a sequence of vector arguments and combine by columns.
      static float[] cbind​(float[]... x)
      Take a sequence of vector arguments and combine by columns.
      static int[] cbind​(int[]... x)
      Take a sequence of vector arguments and combine by columns.
      static java.lang.String[] cbind​(java.lang.String[]... x)
      Take a sequence of vector arguments and combine by columns.
      static double cbrt​(double a)
      Returns the cube root of a double value.
      static double ceil​(double a)
      Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
      static double choose​(int n, int k)
      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[][] data)
      Returns the column maximum for a matrix.
      static double[] colMeans​(double[][] data)
      Returns the column means for a matrix.
      static double[] colMin​(double[][] data)
      Returns the column minimum for a matrix.
      static double[] colSds​(double[][] data)
      Returns the column deviations for a matrix.
      static double[] colSums​(double[][] data)
      Returns the column sums for a matrix.
      static boolean contains​(double[][] polygon, double[] point)
      Determines if the polygon contains the specified coordinates.
      static boolean contains​(double[][] polygon, double x, double y)
      Determines if the polygon contains the specified coordinates.
      static void copy​(double[][] x, double[][] y)
      Copy x into y.
      static void copy​(double[] x, double[] y)
      Copy x into y.
      static void copy​(float[][] x, float[][] y)
      Copy x into y.
      static void copy​(float[] x, float[] y)
      Copy x into y.
      static void copy​(int[][] x, int[][] y)
      Copy x into y.
      static void copy​(int[] x, int[] y)
      Copy x into y.
      static double copySign​(double magnitude, double sign)
      Returns the first floating-point argument with the sign of the second floating-point argument.
      static float copySign​(float magnitude, float sign)
      Returns the first floating-point argument with the sign of the second floating-point argument.
      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)
      Returns the trigonometric cosine of an angle.
      static double cosh​(double x)
      Returns the hyperbolic cosine of a double value.
      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[] x, double[] y)
      The Euclidean distance.
      static double distance​(float[] x, float[] y)
      The Euclidean distance.
      static double distance​(int[] x, int[] y)
      The Euclidean distance.
      static double distance​(SparseArray x, SparseArray y)
      The Euclidean distance.
      static double dot​(double[] x, double[] y)
      Returns the dot product between two vectors.
      static double dot​(float[] x, float[] y)
      Returns the dot product between two vectors.
      static double dot​(int[] x, int[] y)
      Returns the dot product between two vectors.
      static double dot​(SparseArray x, SparseArray y)
      Returns the dot product between two sparse arrays.
      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 eps)
      Check if x element-wisely equals y.
      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 eps)
      Check if x element-wisely equals y.
      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.
      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.
      static boolean equals​(int[][] x, int[][] y)
      Check if x element-wisely equals y.
      static boolean equals​(int[] x, int[] y)
      Check if x element-wisely equals y.
      static <T extends java.lang.Comparable<? super T>>
      boolean
      equals​(T[][] x, T[][] y)
      Check if x element-wisely equals y.
      static <T extends java.lang.Comparable<? super T>>
      boolean
      equals​(T[] x, T[] y)
      Check if x element-wisely equals y.
      static double exp​(double a)
      Returns Euler's number e raised to the power of a double value.
      static double expm1​(double x)
      Returns ex-1.
      static double factorial​(int n)
      factorial of n
      static double floor​(double a)
      Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
      static int getExponent​(double d)
      Returns the unbiased exponent used in the representation of a double.
      static int getExponent​(float f)
      Returns the unbiased exponent used in the representation of a float.
      static double GoodTuring​(int[] r, int[] Nr, double[] p)
      Takes a set of (frequency, frequency-of-frequency) pairs, and applies the "Simple Good-Turing" technique for estimating the probabilities corresponding to the observed frequencies, and P0, the joint probability of all unobserved species.
      static double hypot​(double x, double y)
      Returns sqrt(x2 +y2) without intermediate overflow or underflow.
      static double IEEEremainder​(double f1, double f2)
      Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
      static boolean isPower2​(int x)
      Returns true if x is a power of 2.
      static boolean isZero​(double x)
      Tests if a floating number is zero.
      static boolean isZero​(double x, double epsilon)
      Tests if a floating number is zero with given epsilon.
      static boolean isZero​(float x)
      Tests if a floating number is zero.
      static boolean isZero​(float x, float epsilon)
      Tests if a floating number is zero with given epsilon.
      static double JensenShannonDivergence​(double[] x, double[] y)
      Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.
      static double JensenShannonDivergence​(double[] x, SparseArray y)
      Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.
      static double JensenShannonDivergence​(SparseArray x, double[] y)
      Jensen-Shannon divergence JS(P||Q) = (KL(P||M) + KL(Q||M)) / 2, where M = (P+Q)/2.
      static double JensenShannonDivergence​(SparseArray x, SparseArray y)
      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[] x, double[] y)
      Kullback-Leibler divergence.
      static double KullbackLeiblerDivergence​(double[] x, SparseArray y)
      Kullback-Leibler divergence.
      static double KullbackLeiblerDivergence​(SparseArray x, double[] y)
      Kullback-Leibler divergence.
      static double KullbackLeiblerDivergence​(SparseArray x, SparseArray y)
      Kullback-Leibler divergence.
      static double log​(double a)
      Returns the natural logarithm (base e) of a double value.
      static double log10​(double a)
      Returns the base 10 logarithm of a double value.
      static double log1p​(double x)
      Returns the natural logarithm of the sum of the argument and 1.
      static double log2​(double x)
      Log of base 2.
      static double logChoose​(int n, int k)
      log of n choose k
      static double logFactorial​(int n)
      log of factorial of n
      static double logistic​(double x)
      Logistic sigmoid function.
      static double mad​(double[] x)
      Returns the median absolute deviation (MAD).
      static double mad​(float[] x)
      Returns the median absolute deviation (MAD).
      static double mad​(int[] x)
      Returns the median absolute deviation (MAD).
      static double max​(double[] x)
      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)
      Returns the greater of two double values.
      static double max​(double a, double b, double c)
      maximum of 3 doubles
      static float max​(float[] x)
      Returns the maximum value of an array.
      static float max​(float a, float b)
      Returns the greater of two float values.
      static float max​(float a, float b, float c)
      maximum of 3 floats
      static int max​(int[] x)
      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)
      Returns the greater of two int values.
      static int max​(int a, int b, int c)
      maximum of 3 integers
      static long max​(long a, long b)
      Returns the greater of two long values.
      static double mean​(double[] x)
      Returns the mean of an array.
      static double mean​(float[] x)
      Returns the mean of an array.
      static double mean​(int[] x)
      Returns the mean of an array.
      static double median​(double[] a)
      Find the median of an array of type double.
      static float median​(float[] a)
      Find the median of an array of type float.
      static int median​(int[] a)
      Find the median of an array of type int.
      static <T extends java.lang.Comparable<? super T>>
      T
      median​(T[] a)
      Find the median of an array of type double.
      static double min​(double[] x)
      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)
      Returns the smaller of two double values.
      static double min​(double a, double b, double c)
      minimum of 3 doubles
      static float min​(float[] x)
      Returns the minimum value of an array.
      static float min​(float a, float b)
      Returns the smaller of two float values.
      static double min​(float a, float b, float c)
      minimum of 3 floats
      static int min​(int[] x)
      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)
      Returns the smaller of two int values.
      static int min​(int a, int b, int c)
      minimum of 3 integers
      static long min​(long a, long b)
      Returns the smaller of two long values.
      static double min​(DifferentiableMultivariateFunction func, double[] x, double gtol)
      This method solves the unconstrained minimization problem
      static double min​(DifferentiableMultivariateFunction func, double[] x, double gtol, int maxIter)
      This method solves the unconstrained minimization problem
      static double min​(DifferentiableMultivariateFunction func, int m, double[] x, double gtol)
      This method solves the unconstrained minimization problem
      static double min​(DifferentiableMultivariateFunction func, int m, double[] x, double gtol, int maxIter)
      This method solves the unconstrained minimization problem
      static void minus​(double[] y, double[] x)
      Element-wise subtraction of two arrays y = y - x.
      static double nextAfter​(double start, double direction)
      Returns the floating-point number adjacent to the first argument in the direction of the second argument.
      static float nextAfter​(float start, double direction)
      Returns the floating-point number adjacent to the first argument in the direction of the second argument.
      static double nextUp​(double d)
      Returns the floating-point value adjacent to d in the direction of positive infinity.
      static float nextUp​(float f)
      Returns the floating-point value adjacent to f in the direction of positive infinity.
      static double norm​(double[] x)
      L2 vector norm.
      static double norm1​(double[] x)
      L1 vector norm.
      static double norm2​(double[] x)
      L2 vector norm.
      static void normalize​(double[][] x)
      Unitizes each column of a matrix to unit length (L_2 norm).
      static void normalize​(double[][] x, boolean centerizing)
      Unitizes each column of a matrix to unit length (L_2 norm).
      static double normInf​(double[] x)
      L-infinity vector norm.
      static double[][] pdist​(double[][] x)
      Pairwise distance between pairs of objects.
      static void pdist​(double[][] x, double[][] dist, boolean squared, boolean half)
      Pairwise distance between pairs of objects.
      static void permutate​(double[] x)
      Generates a permutation of given array.
      static void permutate​(float[] x)
      Generates a permutation of given array.
      static int[] permutate​(int n)
      Generates a permutation of 0, 1, 2, ..., n-1, which is useful for sampling without replacement.
      static void permutate​(int[] x)
      Generates a permutation of given array.
      static void permutate​(java.lang.Object[] x)
      Generates a permutation of given array.
      static void plus​(double[] y, double[] x)
      Element-wise sum of two arrays y = x + y.
      static double[] pow​(double[] x, double n)
      Raise each element of an array to a scalar power.
      static double pow​(double a, double b)
      Returns the value of the first argument raised to the power of the second argument.
      static double q1​(double[] a)
      Find the first quantile (p = 1/4) of an array of type double.
      static float q1​(float[] a)
      Find the first quantile (p = 1/4) of an array of type float.
      static int q1​(int[] a)
      Find the first quantile (p = 1/4) of an array of type int.
      static <T extends java.lang.Comparable<? super T>>
      T
      q1​(T[] a)
      Find the first quantile (p = 1/4) of an array of type double.
      static double q3​(double[] a)
      Find the third quantile (p = 3/4) of an array of type double.
      static float q3​(float[] a)
      Find the third quantile (p = 3/4) of an array of type float.
      static int q3​(int[] a)
      Find the third quantile (p = 3/4) of an array of type int.
      static <T extends java.lang.Comparable<? super T>>
      T
      q3​(T[] a)
      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 n 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 double[][] rbind​(double[]... x)
      Take a sequence of vector arguments and combine by rows.
      static float[][] rbind​(float[]... x)
      Take a sequence of vector arguments and combine by rows.
      static int[][] rbind​(int[]... x)
      Take a sequence of vector arguments and combine by rows.
      static java.lang.String[][] rbind​(java.lang.String[]... x)
      Take a sequence of vector arguments and combine 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 rint​(double a)
      Returns the double value that is closest in value to the argument and is equal to a mathematical integer.
      static double root​(DifferentiableFunction func, double x1, double x2, double tol)
      Returns the root of a function whose derivative is available known to lie between x1 and x2 by Newton-Raphson method.
      static double root​(DifferentiableFunction func, double x1, double x2, double tol, int maxIter)
      Returns the root of a function whose derivative is available known to lie between x1 and x2 by Newton-Raphson method.
      static double root​(Function func, double x1, double x2, double tol)
      Returns the root of a function known to lie between x1 and x2 by Brent's method.
      static double root​(Function func, double x1, double x2, double tol, int maxIter)
      Returns the root of a function known to lie between x1 and x2 by Brent's method.
      static long round​(double a)
      Returns the closest long to the argument.
      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 int round​(float a)
      Returns the closest int to the argument.
      static double[] rowMax​(double[][] data)
      Returns the row maximum for a matrix.
      static double[] rowMeans​(double[][] data)
      Returns the row means for a matrix.
      static double[] rowMin​(double[][] data)
      Returns the row minimum for a matrix.
      static double[] rowSds​(double[][] data)
      Returns the row standard deviations for a matrix.
      static double[] rowSums​(double[][] data)
      Returns the row sums for a matrix.
      static double scalb​(double d, int scaleFactor)
      Returns d x 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
      static float scalb​(float f, int scaleFactor)
      Returns f x 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
      static void scale​(double[][] x)
      Scales each column of a matrix to range [0, 1].
      static void scale​(double[][] x, double lo, double hi)
      Scales each column of a matrix to range [lo, hi].
      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[] x)
      Returns the standard deviation of an array.
      static double sd​(float[] x)
      Returns the standard deviation of an array.
      static double sd​(int[] x)
      Returns the standard deviation of an array.
      static void setSeed​(long seed)
      Initialize the random generator with a seed.
      static double signum​(double d)
      Returns the signum of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
      static float signum​(float f)
      Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.
      static double sin​(double a)
      Returns the trigonometric sine of an angle.
      static double sinh​(double x)
      Returns the hyperbolic sine of a double value.
      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 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[][] x)
      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 (ie.
      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 (ie.
      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 (ie.
      static double sqr​(double x)
      Returns x * x.
      static double sqrt​(double a)
      Returns the correctly rounded positive square root of a double value.
      static double squaredDistance​(double[] x, double[] y)
      The squared Euclidean distance.
      static double squaredDistance​(float[] x, float[] y)
      The squared Euclidean distance.
      static double squaredDistance​(int[] x, int[] y)
      The squared Euclidean distance.
      static double squaredDistance​(SparseArray x, SparseArray y)
      The Euclidean distance.
      static void standardize​(double[] x)
      Standardizes an array to mean 0 and variance 1.
      static void standardize​(double[][] x)
      Standardizes each column of a matrix to 0 mean and unit variance.
      static double sum​(double[] x)
      Returns the sum of an array.
      static double sum​(float[] x)
      Returns the sum of an array.
      static int 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​(java.lang.Object[] x, int i, int j)
      Swap two elements of an array.
      static double tan​(double a)
      Returns the trigonometric tangent of an angle.
      static double tanh​(double x)
      Returns the hyperbolic tangent of a double value.
      static double toDegrees​(double angrad)
      Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
      static double toRadians​(double angdeg)
      Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
      static double[][] transpose​(double[][] A)
      Returns the matrix transpose.
      static double ulp​(double d)
      Returns the size of an ulp of the argument.
      static float ulp​(float f)
      * Returns the size of an ulp of the argument.
      static int[] unique​(int[] x)
      Find unique elements of vector.
      static java.lang.String[] unique​(java.lang.String[] x)
      Find unique elements of vector.
      static void unitize​(double[] x)
      Unitize an array so that L2 norm of x = 1.
      static void unitize1​(double[] x)
      Unitize an array so that L1 norm of x is 1.
      static void unitize2​(double[] x)
      Unitize an array so that L2 norm of x = 1.
      static double var​(double[] x)
      Returns the variance of an array.
      static double var​(float[] x)
      Returns the variance of an array.
      static double var​(int[] x)
      Returns the variance of an array.
      static int whichMax​(double[] x)
      Returns the index of maximum value of an array.
      static int whichMax​(float[] x)
      Returns the index of maximum value of an array.
      static int whichMax​(int[] x)
      Returns the index of maximum value of an array.
      static int whichMin​(double[] x)
      Returns the index of minimum value of an array.
      static int whichMin​(float[] x)
      Returns the index of minimum value of an array.
      static int whichMin​(int[] x)
      Returns the index of minimum value of an array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PI

        public static final double PI
        The ratio of the circumference of a circle to its diameter.
        See Also:
        Constant Field Values
      • EPSILON

        public static double EPSILON
        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.
      • RADIX

        public static int RADIX
        The base of the exponent of the double type.
      • DIGITS

        public static int DIGITS
        The number of digits (in radix base) in the mantissa.
      • ROUND_STYLE

        public static int ROUND_STYLE
        Rounding 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 int MACHEP
        The largest negative integer such that 1.0 + RADIXMACHEP ≠ 1.0, except that machep is bounded below by -(DIGITS+3)
      • NEGEP

        public static int NEGEP
        The largest negative integer such that 1.0 - RADIXNEGEP ≠ 1.0, except that negeps is bounded below by -(DIGITS+3)
    • Method Detail

      • abs

        public static double abs​(double a)
        Returns the absolute value of a double value.
      • abs

        public static float abs​(float a)
        Returns the absolute value of a float value.
      • abs

        public static int abs​(int a)
        Returns the absolute value of an int value.
      • abs

        public static long abs​(long a)
        Returns the absolute value of a long value.
      • acos

        public static double acos​(double a)
        Returns the arc cosine of an angle, in the range of 0.0 through pi.
      • asin

        public static double asin​(double a)
        Returns the arc sine of an angle, in the range of -pi/2 through pi/2.
      • atan

        public static double atan​(double a)
        Returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
      • atan2

        public static double atan2​(double y,
                                   double x)
        Converts rectangular coordinates (x, y) to polar (r, theta).
      • cbrt

        public static double cbrt​(double a)
        Returns the cube root of a double value.
      • ceil

        public static double ceil​(double a)
        Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.
      • copySign

        public static double copySign​(double magnitude,
                                      double sign)
        Returns the first floating-point argument with the sign of the second floating-point argument.
      • copySign

        public static float copySign​(float magnitude,
                                     float sign)
        Returns the first floating-point argument with the sign of the second floating-point argument.
      • cos

        public static double cos​(double a)
        Returns the trigonometric cosine of an angle.
      • cosh

        public static double cosh​(double x)
        Returns the hyperbolic cosine of a double value.
      • exp

        public static double exp​(double a)
        Returns Euler's number e raised to the power of a double value.
      • expm1

        public static double expm1​(double x)
        Returns ex-1.
      • floor

        public static double floor​(double a)
        Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
      • getExponent

        public static int getExponent​(double d)
        Returns the unbiased exponent used in the representation of a double.
      • getExponent

        public static int getExponent​(float f)
        Returns the unbiased exponent used in the representation of a float.
      • hypot

        public static double hypot​(double x,
                                   double y)
        Returns sqrt(x2 +y2) without intermediate overflow or underflow.
      • IEEEremainder

        public static double IEEEremainder​(double f1,
                                           double f2)
        Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
      • log

        public static double log​(double a)
        Returns the natural logarithm (base e) of a double value.
      • log10

        public static double log10​(double a)
        Returns the base 10 logarithm of a double value.
      • log1p

        public static double log1p​(double x)
        Returns the natural logarithm of the sum of the argument and 1.
      • max

        public static double max​(double a,
                                 double b)
        Returns the greater of two double values.
      • max

        public static float max​(float a,
                                float b)
        Returns the greater of two float values.
      • max

        public static int max​(int a,
                              int b)
        Returns the greater of two int values.
      • max

        public static long max​(long a,
                               long b)
        Returns the greater of two long values.
      • min

        public static double min​(double a,
                                 double b)
        Returns the smaller of two double values.
      • min

        public static float min​(float a,
                                float b)
        Returns the smaller of two float values.
      • min

        public static int min​(int a,
                              int b)
        Returns the smaller of two int values.
      • min

        public static long min​(long a,
                               long b)
        Returns the smaller of two long values.
      • nextAfter

        public static double nextAfter​(double start,
                                       double direction)
        Returns the floating-point number adjacent to the first argument in the direction of the second argument.
      • nextAfter

        public static float nextAfter​(float start,
                                      double direction)
        Returns the floating-point number adjacent to the first argument in the direction of the second argument.
      • nextUp

        public static double nextUp​(double d)
        Returns the floating-point value adjacent to d in the direction of positive infinity.
      • nextUp

        public static float nextUp​(float f)
        Returns the floating-point value adjacent to f in the direction of positive infinity.
      • pow

        public static double pow​(double a,
                                 double b)
        Returns the value of the first argument raised to the power of the second argument.
      • rint

        public static double rint​(double a)
        Returns the double value that is closest in value to the argument and is equal to a mathematical integer.
      • round

        public static long round​(double a)
        Returns the closest long to the argument.
      • round

        public static int round​(float a)
        Returns the closest int to the argument.
      • scalb

        public static double scalb​(double d,
                                   int scaleFactor)
        Returns d x 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.
      • scalb

        public static float scalb​(float f,
                                  int scaleFactor)
        Returns f x 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.
      • signum

        public static double signum​(double d)
        Returns the signum of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
      • signum

        public static float signum​(float f)
        Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.
      • sin

        public static double sin​(double a)
        Returns the trigonometric sine of an angle.
      • sinh

        public static double sinh​(double x)
        Returns the hyperbolic sine of a double value.
      • sqrt

        public static double sqrt​(double a)
        Returns the correctly rounded positive square root of a double value.
      • tan

        public static double tan​(double a)
        Returns the trigonometric tangent of an angle.
      • tanh

        public static double tanh​(double x)
        Returns the hyperbolic tangent of a double value.
      • toDegrees

        public static double toDegrees​(double angrad)
        Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
      • toRadians

        public static double toRadians​(double angdeg)
        Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
      • ulp

        public static double ulp​(double d)
        Returns the size of an ulp of the argument.
      • ulp

        public static float ulp​(float f)
        * Returns the size of an ulp of the argument.
      • log2

        public static double log2​(double x)
        Log of base 2.
      • 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
      • logistic

        public static double logistic​(double x)
        Logistic sigmoid function.
      • sqr

        public static double sqr​(double x)
        Returns x * x.
      • isPower2

        public static boolean isPower2​(int x)
        Returns true if x is a power of 2.
      • 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.
      • factorial

        public static double factorial​(int n)
        factorial of n
        Returns:
        factorial returned as double but is, numerically, an integer. Numerical rounding may makes this an approximation after n = 21
      • logFactorial

        public static double logFactorial​(int n)
        log of factorial of n
      • choose

        public static double choose​(int n,
                                    int k)
        n choose k. Returns 0 if n is less than k.
      • logChoose

        public static double logChoose​(int n,
                                       int k)
        log of n choose k
      • setSeed

        public static void setSeed​(long seed)
        Initialize the random generator with a seed.
      • 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 nonnegative 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 nonnegative and not all zero.
        Returns:
        an random array of length n in range of [0, m).
      • random

        public static double random()
        Generate a random number in [0, 1).
      • random

        public static double[] random​(int n)
        Generate n random numbers in [0, 1).
      • random

        public static double random​(double lo,
                                    double hi)
        Generate a uniform random number in the range [lo, hi).
        Parameters:
        lo - lower limit of range
        hi - upper limit of range
        Returns:
        a uniform random real in the range [lo, hi)
      • random

        public static double[] random​(double lo,
                                      double hi,
                                      int n)
        Generate n uniform random numbers in the range [lo, hi).
        Parameters:
        n - size of the array
        lo - lower limit of range
        hi - upper limit of range
        Returns:
        a uniform random real in the range [lo, hi)
      • randomInt

        public static int randomInt​(int n)
        Returns a random integer in [0, n).
      • randomInt

        public static int randomInt​(int lo,
                                    int hi)
        Returns a random integer in [lo, hi).
      • permutate

        public static int[] permutate​(int n)
        Generates a permutation of 0, 1, 2, ..., n-1, which is useful for sampling without replacement.
      • permutate

        public static void permutate​(int[] x)
        Generates a permutation of given array.
      • permutate

        public static void permutate​(float[] x)
        Generates a permutation of given array.
      • permutate

        public static void permutate​(double[] x)
        Generates a permutation of given array.
      • permutate

        public static void permutate​(java.lang.Object[] x)
        Generates a permutation of given array.
      • c

        public static int[] c​(int... x)
        Combines the arguments to form a vector.
      • c

        public static float[] c​(float... x)
        Combines the arguments to form a vector.
      • c

        public static double[] c​(double... x)
        Combines the arguments to form a vector.
      • c

        public static java.lang.String[] c​(java.lang.String... x)
        Combines the arguments to form a vector.
      • c

        public static int[] c​(int[]... x)
        Merges multiple vectors into one.
      • c

        public static float[] c​(float[]... x)
        Merges multiple vectors into one.
      • c

        public static double[] c​(double[]... x)
        Merges multiple vectors into one.
      • c

        public static java.lang.String[] c​(java.lang.String[]... x)
        Merges multiple vectors into one.
      • cbind

        public static int[] cbind​(int[]... x)
        Take a sequence of vector arguments and combine by columns.
      • cbind

        public static float[] cbind​(float[]... x)
        Take a sequence of vector arguments and combine by columns.
      • cbind

        public static double[] cbind​(double[]... x)
        Take a sequence of vector arguments and combine by columns.
      • cbind

        public static java.lang.String[] cbind​(java.lang.String[]... x)
        Take a sequence of vector arguments and combine by columns.
      • rbind

        public static int[][] rbind​(int[]... x)
        Take a sequence of vector arguments and combine by rows.
      • rbind

        public static float[][] rbind​(float[]... x)
        Take a sequence of vector arguments and combine by rows.
      • rbind

        public static double[][] rbind​(double[]... x)
        Take a sequence of vector arguments and combine by rows.
      • rbind

        public static java.lang.String[][] rbind​(java.lang.String[]... x)
        Take a sequence of vector arguments and combine by rows.
      • slice

        public static <E> E[] slice​(E[] data,
                                    int[] index)
        Returns a slice of data for given indices.
      • slice

        public static int[] slice​(int[] data,
                                  int[] index)
        Returns a slice of data for given indices.
      • slice

        public static float[] slice​(float[] data,
                                    int[] index)
        Returns a slice of data for given indices.
      • slice

        public static double[] slice​(double[] data,
                                     int[] index)
        Returns a slice of data for given indices.
      • contains

        public static boolean contains​(double[][] polygon,
                                       double[] point)
        Determines if the polygon contains the specified coordinates.
        Parameters:
        point - the coordinates of specified point to be tested.
        Returns:
        true if the Polygon contains the specified coordinates; false otherwise.
      • contains

        public static boolean contains​(double[][] polygon,
                                       double x,
                                       double y)
        Determines if the polygon contains the specified coordinates.
        Parameters:
        x - the specified x coordinate.
        y - the specified y coordinate.
        Returns:
        true if the Polygon contains the specified coordinates; false otherwise.
      • 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 - an array to reverse.
      • reverse

        public static void reverse​(double[] a)
        Reverses the order of the elements in the specified array.
        Parameters:
        a - an array to reverse.
      • reverse

        public static <T> void reverse​(T[] a)
        Reverses the order of the elements in the specified array.
        Parameters:
        a - an array to reverse.
      • min

        public static int min​(int a,
                              int b,
                              int c)
        minimum of 3 integers
      • min

        public static double min​(float a,
                                 float b,
                                 float c)
        minimum of 3 floats
      • min

        public static double min​(double a,
                                 double b,
                                 double c)
        minimum of 3 doubles
      • max

        public static int max​(int a,
                              int b,
                              int c)
        maximum of 3 integers
      • max

        public static float max​(float a,
                                float b,
                                float c)
        maximum of 3 floats
      • max

        public static double max​(double a,
                                 double b,
                                 double c)
        maximum of 3 doubles
      • min

        public static int min​(int[] x)
        Returns the minimum value of an array.
      • min

        public static float min​(float[] x)
        Returns the minimum value of an array.
      • min

        public static double min​(double[] x)
        Returns the minimum value of an array.
      • whichMin

        public static int whichMin​(int[] x)
        Returns the index of minimum value of an array.
      • whichMin

        public static int whichMin​(float[] x)
        Returns the index of minimum value of an array.
      • whichMin

        public static int whichMin​(double[] x)
        Returns the index of minimum value of an array.
      • max

        public static int max​(int[] x)
        Returns the maximum value of an array.
      • max

        public static float max​(float[] x)
        Returns the maximum value of an array.
      • max

        public static double max​(double[] x)
        Returns the maximum value of an array.
      • whichMax

        public static int whichMax​(int[] x)
        Returns the index of maximum value of an array.
      • whichMax

        public static int whichMax​(float[] x)
        Returns the index of maximum value of an array.
      • whichMax

        public static int whichMax​(double[] x)
        Returns the index of maximum value of an array.
      • min

        public static int min​(int[][] matrix)
        Returns the minimum of a matrix.
      • min

        public static double min​(double[][] matrix)
        Returns the minimum of a matrix.
      • max

        public static int max​(int[][] matrix)
        Returns the maximum of a matrix.
      • max

        public static double max​(double[][] matrix)
        Returns the maximum of a matrix.
      • transpose

        public static double[][] transpose​(double[][] A)
        Returns the matrix transpose.
      • rowMin

        public static double[] rowMin​(double[][] data)
        Returns the row minimum for a matrix.
      • rowMax

        public static double[] rowMax​(double[][] data)
        Returns the row maximum for a matrix.
      • rowSums

        public static double[] rowSums​(double[][] data)
        Returns the row sums for a matrix.
      • rowMeans

        public static double[] rowMeans​(double[][] data)
        Returns the row means for a matrix.
      • rowSds

        public static double[] rowSds​(double[][] data)
        Returns the row standard deviations for a matrix.
      • colMin

        public static double[] colMin​(double[][] data)
        Returns the column minimum for a matrix.
      • colMax

        public static double[] colMax​(double[][] data)
        Returns the column maximum for a matrix.
      • colSums

        public static double[] colSums​(double[][] data)
        Returns the column sums for a matrix.
      • colMeans

        public static double[] colMeans​(double[][] data)
        Returns the column means for a matrix.
      • colSds

        public static double[] colSds​(double[][] data)
        Returns the column deviations for a matrix.
      • sum

        public static int sum​(int[] x)
        Returns the sum of an array.
      • sum

        public static double sum​(float[] x)
        Returns the sum of an array.
      • sum

        public static double sum​(double[] x)
        Returns the sum of an array.
      • median

        public static int median​(int[] a)
        Find the median of an array of type int. The input array will be rearranged.
      • median

        public static float median​(float[] a)
        Find the median of an array of type float. The input array will be rearranged.
      • median

        public static double median​(double[] a)
        Find the median of an array of type double. The input array will be rearranged.
      • median

        public static <T extends java.lang.Comparable<? super T>> T median​(T[] a)
        Find the median of an array of type double. The input array will be rearranged.
      • q1

        public static int q1​(int[] a)
        Find the first quantile (p = 1/4) of an array of type int. The input array will be rearranged.
      • q1

        public static float q1​(float[] a)
        Find the first quantile (p = 1/4) of an array of type float. The input array will be rearranged.
      • q1

        public static double q1​(double[] a)
        Find the first quantile (p = 1/4) of an array of type double. The input array will be rearranged.
      • q1

        public static <T extends java.lang.Comparable<? super T>> T q1​(T[] a)
        Find the first quantile (p = 1/4) of an array of type double. The input array will be rearranged.
      • q3

        public static int q3​(int[] a)
        Find the third quantile (p = 3/4) of an array of type int. The input array will be rearranged.
      • q3

        public static float q3​(float[] a)
        Find the third quantile (p = 3/4) of an array of type float. The input array will be rearranged.
      • q3

        public static double q3​(double[] a)
        Find the third quantile (p = 3/4) of an array of type double. The input array will be rearranged.
      • q3

        public static <T extends java.lang.Comparable<? super T>> T q3​(T[] a)
        Find the third quantile (p = 3/4) of an array of type double. The input array will be rearranged.
      • mean

        public static double mean​(int[] x)
        Returns the mean of an array.
      • mean

        public static double mean​(float[] x)
        Returns the mean of an array.
      • mean

        public static double mean​(double[] x)
        Returns the mean of an array.
      • var

        public static double var​(int[] x)
        Returns the variance of an array.
      • var

        public static double var​(float[] x)
        Returns the variance of an array.
      • var

        public static double var​(double[] x)
        Returns the variance of an array.
      • sd

        public static double sd​(int[] x)
        Returns the standard deviation of an array.
      • sd

        public static double sd​(float[] x)
        Returns the standard deviation of an array.
      • sd

        public static double sd​(double[] x)
        Returns the standard deviation of an array.
      • mad

        public static double mad​(int[] x)
        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.

      • mad

        public static double mad​(float[] x)
        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.

      • mad

        public static double mad​(double[] x)
        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.

      • all

        public static boolean all​(boolean[] x)
        Given a set of boolean values, are all of the values true?
      • any

        public static boolean any​(boolean[] x)
        Given a set of boolean values, is at least one of the values true?
      • distance

        public static double distance​(int[] x,
                                      int[] y)
        The Euclidean distance.
      • distance

        public static double distance​(float[] x,
                                      float[] y)
        The Euclidean distance.
      • distance

        public static double distance​(double[] x,
                                      double[] y)
        The Euclidean distance.
      • pdist

        public static double[][] pdist​(double[][] x)
        Pairwise distance between pairs of objects.
        Parameters:
        x - Rows of x correspond to observations, and columns correspond to variables.
        Returns:
        a full pairwise distance matrix.
      • pdist

        public static void pdist​(double[][] x,
                                 double[][] dist,
                                 boolean squared,
                                 boolean half)
        Pairwise distance between pairs of objects.
        Parameters:
        x - Rows of x correspond to observations, and columns correspond to variables.
        squared - If true, compute the squared Euclidean distance.
        half - If true, only the lower half of dist will be referenced.
        dist - The distance matrix.
      • squaredDistance

        public static double squaredDistance​(int[] x,
                                             int[] y)
        The squared Euclidean distance.
      • squaredDistance

        public static double squaredDistance​(float[] x,
                                             float[] y)
        The squared Euclidean distance.
      • squaredDistance

        public static double squaredDistance​(double[] x,
                                             double[] y)
        The squared Euclidean distance.
      • squaredDistance

        public static double squaredDistance​(SparseArray x,
                                             SparseArray y)
        The Euclidean distance.
      • KullbackLeiblerDivergence

        public static double KullbackLeiblerDivergence​(double[] x,
                                                       double[] y)
        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.

      • KullbackLeiblerDivergence

        public static double KullbackLeiblerDivergence​(SparseArray x,
                                                       SparseArray y)
        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.

      • KullbackLeiblerDivergence

        public static double KullbackLeiblerDivergence​(double[] x,
                                                       SparseArray y)
        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.

      • KullbackLeiblerDivergence

        public static double KullbackLeiblerDivergence​(SparseArray x,
                                                       double[] y)
        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.

      • JensenShannonDivergence

        public static double JensenShannonDivergence​(double[] x,
                                                     double[] y)
        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.
      • JensenShannonDivergence

        public static double JensenShannonDivergence​(SparseArray x,
                                                     SparseArray y)
        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.
      • JensenShannonDivergence

        public static double JensenShannonDivergence​(double[] x,
                                                     SparseArray y)
        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.
      • JensenShannonDivergence

        public static double JensenShannonDivergence​(SparseArray x,
                                                     double[] y)
        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.
      • dot

        public static double dot​(int[] x,
                                 int[] y)
        Returns the dot product between two vectors.
      • dot

        public static double dot​(float[] x,
                                 float[] y)
        Returns the dot product between two vectors.
      • dot

        public static double dot​(double[] x,
                                 double[] y)
        Returns the dot product between two vectors.
      • dot

        public static double dot​(SparseArray x,
                                 SparseArray y)
        Returns the dot product between two sparse arrays.
      • cov

        public static double cov​(int[] x,
                                 int[] y)
        Returns the covariance between two vectors.
      • cov

        public static double cov​(float[] x,
                                 float[] y)
        Returns the covariance between two vectors.
      • cov

        public static double cov​(double[] x,
                                 double[] y)
        Returns the covariance between two vectors.
      • cov

        public static double[][] cov​(double[][] data)
        Returns the sample covariance matrix.
      • cov

        public static double[][] cov​(double[][] data,
                                     double[] mu)
        Returns the sample covariance matrix.
        Parameters:
        mu - the known mean of data.
      • cor

        public static double cor​(int[] x,
                                 int[] y)
        Returns the correlation coefficient between two vectors.
      • cor

        public static double cor​(float[] x,
                                 float[] y)
        Returns the correlation coefficient between two vectors.
      • cor

        public static double cor​(double[] x,
                                 double[] y)
        Returns the correlation coefficient between two vectors.
      • cor

        public static double[][] cor​(double[][] data)
        Returns the sample correlation matrix.
      • cor

        public static double[][] cor​(double[][] data,
                                     double[] mu)
        Returns the sample correlation matrix.
        Parameters:
        mu - the known mean of data.
      • 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 (ie. when variables are ordinal). It can be used when there is non-parametric data and hence Pearson cannot be used.
      • 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 (ie. when variables are ordinal). It can be used when there is non-parametric data and hence Pearson cannot be used.
      • 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 (ie. when variables are ordinal). It can be used when there is non-parametric data and hence Pearson cannot be used.
      • 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.
      • 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.
      • 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.
      • norm1

        public static double norm1​(double[] x)
        L1 vector norm.
      • norm2

        public static double norm2​(double[] x)
        L2 vector norm.
      • normInf

        public static double normInf​(double[] x)
        L-infinity vector norm. Maximum absolute value.
      • norm

        public static double norm​(double[] x)
        L2 vector norm.
      • standardize

        public static void standardize​(double[] x)
        Standardizes an array to mean 0 and variance 1.
      • scale

        public static void scale​(double[][] x)
        Scales each column of a matrix to range [0, 1].
      • scale

        public static void scale​(double[][] x,
                                 double lo,
                                 double hi)
        Scales each column of a matrix to range [lo, hi].
        Parameters:
        lo - lower limit of range
        hi - upper limit of range
      • standardize

        public static void standardize​(double[][] x)
        Standardizes each column of a matrix to 0 mean and unit variance.
      • normalize

        public static void normalize​(double[][] x)
        Unitizes each column of a matrix to unit length (L_2 norm).
      • normalize

        public static void normalize​(double[][] x,
                                     boolean centerizing)
        Unitizes each column of a matrix to unit length (L_2 norm).
        Parameters:
        centerizing - If true, centerize each column to 0 mean.
      • unitize

        public static void unitize​(double[] x)
        Unitize an array so that L2 norm of x = 1.
        Parameters:
        x - the array of double
      • unitize1

        public static void unitize1​(double[] x)
        Unitize an array so that L1 norm of x is 1.
        Parameters:
        x - an array of nonnegative double
      • unitize2

        public static void unitize2​(double[] x)
        Unitize an array so that L2 norm of x = 1.
        Parameters:
        x - the array of double
      • GoodTuring

        public static double GoodTuring​(int[] r,
                                        int[] Nr,
                                        double[] p)
        Takes a set of (frequency, frequency-of-frequency) pairs, and applies the "Simple Good-Turing" technique for estimating the probabilities corresponding to the observed frequencies, and P0, the joint probability of all unobserved species.
        Parameters:
        r - the frequency in ascending order.
        Nr - the frequency of frequencies.
        p - on output, it is the estimated probabilities.
        Returns:
        P0 for all unobserved species.
      • equals

        public static boolean equals​(int[] x,
                                     int[] y)
        Check 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.
      • equals

        public static boolean equals​(float[] x,
                                     float[] y,
                                     float epsilon)
        Check 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.
      • equals

        public static boolean equals​(double[] x,
                                     double[] y,
                                     double eps)
        Check if x element-wisely equals y.
      • equals

        public static <T extends java.lang.Comparable<? super T>> boolean equals​(T[] x,
                                                                                 T[] y)
        Check if x element-wisely equals y.
      • equals

        public static boolean equals​(int[][] x,
                                     int[][] y)
        Check 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.
      • equals

        public static boolean equals​(float[][] x,
                                     float[][] y,
                                     float epsilon)
        Check 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.
      • equals

        public static boolean equals​(double[][] x,
                                     double[][] y,
                                     double eps)
        Check if x element-wisely equals y.
      • isZero

        public static boolean isZero​(float x)
        Tests if a floating number is zero.
      • isZero

        public static boolean isZero​(float x,
                                     float epsilon)
        Tests if a floating number is zero with given epsilon.
      • isZero

        public static boolean isZero​(double x)
        Tests if a floating number is zero.
      • isZero

        public static boolean isZero​(double x,
                                     double epsilon)
        Tests if a floating number is zero with given epsilon.
      • equals

        public static <T extends java.lang.Comparable<? super T>> boolean equals​(T[][] x,
                                                                                 T[][] y)
        Check if x element-wisely equals y.
      • clone

        public static int[][] clone​(int[][] x)
        Deep clone a two-dimensional array.
      • clone

        public static float[][] clone​(float[][] x)
        Deep clone a two-dimensional array.
      • clone

        public static double[][] clone​(double[][] x)
        Deep clone a two-dimensional array.
      • swap

        public static void swap​(int[] x,
                                int i,
                                int j)
        Swap two elements of an array.
      • swap

        public static void swap​(float[] x,
                                int i,
                                int j)
        Swap two elements of an array.
      • swap

        public static void swap​(double[] x,
                                int i,
                                int j)
        Swap two elements of an array.
      • swap

        public static void swap​(java.lang.Object[] x,
                                int i,
                                int j)
        Swap two elements of an array.
      • swap

        public static void swap​(int[] x,
                                int[] y)
        Swap two arrays.
      • swap

        public static void swap​(float[] x,
                                float[] y)
        Swap two arrays.
      • swap

        public static void swap​(double[] x,
                                double[] y)
        Swap two arrays.
      • swap

        public static <E> void swap​(E[] x,
                                    E[] y)
        Swap two arrays.
      • copy

        public static void copy​(int[] x,
                                int[] y)
        Copy x into y.
      • copy

        public static void copy​(float[] x,
                                float[] y)
        Copy x into y.
      • copy

        public static void copy​(double[] x,
                                double[] y)
        Copy x into y.
      • copy

        public static void copy​(int[][] x,
                                int[][] y)
        Copy x into y.
      • copy

        public static void copy​(float[][] x,
                                float[][] y)
        Copy x into y.
      • copy

        public static void copy​(double[][] x,
                                double[][] y)
        Copy x into y.
      • plus

        public static void plus​(double[] y,
                                double[] x)
        Element-wise sum of two arrays y = x + y.
      • minus

        public static void minus​(double[] y,
                                 double[] x)
        Element-wise subtraction of two arrays y = y - x.
        Parameters:
        y - minuend matrix
        x - subtrahend matrix
      • scale

        public static void scale​(double a,
                                 double[] x)
        Scale each element of an array by a constant x = a * x.
      • scale

        public static void scale​(double a,
                                 double[] x,
                                 double[] y)
        Scale each element of an array by a constant y = a * x.
      • 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.
      • pow

        public static double[] pow​(double[] x,
                                   double n)
        Raise each element of an array to a scalar power.
        Parameters:
        x - array
        n - scalar exponent
        Returns:
        xn
      • unique

        public static int[] unique​(int[] x)
        Find unique elements of vector.
        Parameters:
        x - an integer array.
        Returns:
        the same values as in x but with no repetitions.
      • unique

        public static java.lang.String[] unique​(java.lang.String[] x)
        Find unique elements of vector.
        Parameters:
        x - an array of strings.
        Returns:
        the same values as in x but with no repetitions.
      • sort

        public static int[][] sort​(double[][] x)
        Sorts each variable and returns the index of values in ascending order. Note that the order of original array is NOT altered.
        Parameters:
        x - 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.
      • root

        public static double root​(Function func,
                                  double x1,
                                  double x2,
                                  double tol)
        Returns the root of a function known to lie between x1 and x2 by Brent's method. The root will be refined until its accuracy is tol. The method is guaranteed to converge as long as the function can be evaluated within the initial interval known to contain a root.
        Parameters:
        func - the function to be evaluated.
        x1 - the left end of search interval.
        x2 - the right end of search interval.
        tol - the accuracy tolerance.
        Returns:
        the root.
      • root

        public static double root​(Function func,
                                  double x1,
                                  double x2,
                                  double tol,
                                  int maxIter)
        Returns the root of a function known to lie between x1 and x2 by Brent's method. The root will be refined until its accuracy is tol. The method is guaranteed to converge as long as the function can be evaluated within the initial interval known to contain a root.
        Parameters:
        func - the function to be evaluated.
        x1 - the left end of search interval.
        x2 - the right end of search interval.
        tol - the accuracy tolerance.
        maxIter - the maximum number of allowed iterations.
        Returns:
        the root.
      • root

        public static double root​(DifferentiableFunction func,
                                  double x1,
                                  double x2,
                                  double tol)
        Returns the root of a function whose derivative is available known to lie between x1 and x2 by Newton-Raphson method. The root will be refined until its accuracy is within xacc.
        Parameters:
        func - the function to be evaluated.
        x1 - the left end of search interval.
        x2 - the right end of search interval.
        tol - the accuracy tolerance.
        Returns:
        the root.
      • root

        public static double root​(DifferentiableFunction func,
                                  double x1,
                                  double x2,
                                  double tol,
                                  int maxIter)
        Returns the root of a function whose derivative is available known to lie between x1 and x2 by Newton-Raphson method. The root will be refined until its accuracy is within xacc.
        Parameters:
        func - the function to be evaluated.
        x1 - the left end of search interval.
        x2 - the right end of search interval.
        tol - the accuracy tolerance.
        maxIter - the maximum number of allowed iterations.
        Returns:
        the root.
      • min

        public static double min​(DifferentiableMultivariateFunction func,
                                 int m,
                                 double[] x,
                                 double gtol)
        This method solves the unconstrained minimization problem
             min f(x),    x = (x1,x2,...,x_n),
         
        using the limited-memory BFGS method. The method is especially effective on problems involving a large number of variables. In a typical iteration of this method an approximation Hk to the inverse of the Hessian is obtained by applying m BFGS updates to a diagonal matrix Hk0, using information from the previous M steps. The user specifies the number m, which determines the amount of storage required by the routine. The algorithm is described in "On the limited memory BFGS method for large scale optimization", by D. Liu and J. Nocedal, Mathematical Programming B 45 (1989) 503-528.
        Parameters:
        func - the function to be minimized.
        m - the number of corrections used in the L-BFGS update. Values of m less than 3 are not recommended; large values of m will result in excessive computing time. 3 <= m <= 7 is recommended.
        x - on initial entry this must be set by the user to the values of the initial estimate of the solution vector. On exit with iflag = 0, it contains the values of the variables at the best point found (usually a solution).
        gtol - the convergence requirement on zeroing the gradient.
        Returns:
        the minimum value of the function.
      • min

        public static double min​(DifferentiableMultivariateFunction func,
                                 int m,
                                 double[] x,
                                 double gtol,
                                 int maxIter)
        This method solves the unconstrained minimization problem
             min f(x),    x = (x1,x2,...,x_n),
         
        using the limited-memory BFGS method. The method is especially effective on problems involving a large number of variables. In a typical iteration of this method an approximation Hk to the inverse of the Hessian is obtained by applying m BFGS updates to a diagonal matrix Hk0, using information from the previous M steps. The user specifies the number m, which determines the amount of storage required by the routine. The algorithm is described in "On the limited memory BFGS method for large scale optimization", by D. Liu and J. Nocedal, Mathematical Programming B 45 (1989) 503-528.
        Parameters:
        func - the function to be minimized.
        m - the number of corrections used in the L-BFGS update. Values of m less than 3 are not recommended; large values of m will result in excessive computing time. 3 <= m <= 7 is recommended. A common choice for m is m = 5.
        x - on initial entry this must be set by the user to the values of the initial estimate of the solution vector. On exit with iflag = 0, it contains the values of the variables at the best point found (usually a solution).
        gtol - the convergence requirement on zeroing the gradient.
        maxIter - the maximum allowed number of iterations.
        Returns:
        the minimum value of the function.
      • min

        public static double min​(DifferentiableMultivariateFunction func,
                                 double[] x,
                                 double gtol)
        This method solves the unconstrained minimization problem
             min f(x),    x = (x1,x2,...,x_n),
         
        using the BFGS method.
        Parameters:
        func - the function to be minimized.
        x - on initial entry this must be set by the user to the values of the initial estimate of the solution vector. On exit, it contains the values of the variables at the best point found (usually a solution).
        gtol - the convergence requirement on zeroing the gradient.
        Returns:
        the minimum value of the function.
      • min

        public static double min​(DifferentiableMultivariateFunction func,
                                 double[] x,
                                 double gtol,
                                 int maxIter)
        This method solves the unconstrained minimization problem
             min f(x),    x = (x1,x2,...,x_n),
         
        using the BFGS method.
        Parameters:
        func - the function to be minimized.
        x - on initial entry this must be set by the user to the values of the initial estimate of the solution vector. On exit, it contains the values of the variables at the best point found (usually a solution).
        gtol - the convergence requirement on zeroing the gradient.
        maxIter - the maximum allowed number of iterations.
        Returns:
        the minimum value of the function.