public class ArrayMath
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
ArrayMath.InvalidElementException |
Modifier and Type | Method and Description |
---|---|
static double[] |
add(double[] a,
double c) |
static float[] |
add(float[] a,
double c) |
static void |
addInPlace(double[][] m1,
double[][] m2)
Add the two 2d arrays and write the answer in place of
m1 . |
static void |
addInPlace(double[] a,
double b)
Increases the values in the first array a by b.
|
static void |
addInPlace(float[] a,
double b)
Increases the values in this array by b.
|
static void |
addMultInPlace(double[] a,
double[] b,
double c)
Add c times the array b to array a.
|
static void |
addMultInto(double[] a,
double[] b,
double[] c,
double d) |
static int |
argmax_tieLast(double[] a) |
static int |
argmax(double[] a) |
static int |
argmax(float[] a) |
static int |
argmax(int[] a) |
static int |
argmin(double[] a) |
static int |
argmin(float[] a) |
static int |
argmin(int[] a) |
static void |
assertFinite(double[] vector,
java.lang.String vectorName) |
static double |
average(double[] a) |
static java.lang.Double[] |
box(double[] assignment) |
static java.lang.Integer[] |
box(int[] assignment) |
static int[][] |
castToInt(double[][] doubleCounts) |
static boolean |
contains(int[] a,
int i) |
static boolean |
containsInSubarray(int[] a,
int begin,
int end,
int i) |
static int |
countCloseToZero(double[] v,
double epsilon) |
static int |
countInfinite(double[] v) |
static int |
countNaN(double[] v) |
static int |
countNegative(double[] v) |
static int |
countNonZero(double[] v) |
static int |
countPositive(double[] v) |
static int[][] |
deepCopy(int[][] counts) |
static int[] |
diag(int[][] a)
Returns diagonal elements of the given (square) matrix.
|
static void |
divideInPlace(double[] a,
double b)
Divides the values in this array by b.
|
static double |
dotProduct(double[] a,
double[] b)
Assumes that both arrays have same length.
|
static double |
dotProduct(double[] a,
float[] b)
Dot product of a and b.
|
static float |
dotProduct(float[] a,
float[] b)
Dot product of a and b.
|
static float[] |
doubleArrayToFloatArray(double[] a) |
static float[][] |
doubleArrayToFloatArray(double[][] a) |
static double |
entropy(double[] probs) |
static double[] |
exp(double[] a) |
static void |
expInPlace(double[] a) |
static double[] |
filterInfinite(double[] v) |
static double[] |
filterNaN(double[] v) |
static double[] |
filterNaNAndInfinite(double[] v) |
static double[] |
floatArrayToDoubleArray(float[] a) |
static double[][] |
floatArrayToDoubleArray(float[][] a) |
static boolean |
hasInfinite(double[] a) |
static boolean |
hasNaN(double[] a) |
static boolean |
hasNaN(float[] a) |
static int |
indexOf(int n,
int[] a) |
static double |
innerProduct(double[] a,
double[] b) |
static double |
innerProduct(float[] a,
float[] b) |
static double |
iterativeAverage(double[] a)
This version avoids certain types of overflow.
|
static double |
jensenShannonDivergence(double[] a,
double[] b)
Returns the Jensen Shannon divergence (information radius) between
a and b, defined as the average of the kl divergences from a to b
and from b to a.
|
static double |
klDivergence(double[] from,
double[] to) |
static double |
L1Norm(double[] a) |
static void |
L1normalize(double[] a) |
static double |
L2Norm(double[] a) |
static float |
L2Norm(float[] a) |
static void |
L2normalize(double[] a) |
static void |
L2normalize(float[] a) |
static double[][] |
load2DMatrixFromFile(java.lang.String filename) |
static double[] |
log(double[] a) |
static void |
logInPlace(double[] a) |
static void |
logNormalize(double[] a)
Makes the values in this array sum to 1.0.
|
static double |
logSum(double... logInputs)
Returns the log of the sum of an array of numbers, which are
themselves input in log form.
|
static double |
logSum(double[] logInputs,
int fromIndex,
int toIndex)
Returns the log of the portion between
fromIndex , inclusive, and
toIndex , exclusive, of an array of numbers, which are
themselves input in log form. |
static double |
logSum(double[] logInputs,
int fromIndex,
int afterIndex,
int stride)
Returns the log of the portion between
fromIndex , inclusive, and
toIndex , exclusive, of an array of numbers, which are
themselves input in log form. |
static float |
logSum(float[] logInputs)
Returns the log of the sum of an array of numbers, which are
themselves input in log form.
|
static double |
logSum(java.util.List<java.lang.Double> logInputs) |
static double |
logSum(java.util.List<java.lang.Double> logInputs,
int fromIndex,
int toIndex) |
static void |
main(java.lang.String[] args)
For testing only.
|
static double |
max(java.util.Collection<java.lang.Double> a) |
static double |
max(double[] a) |
static float |
max(float[] a) |
static int |
max(int... vector) |
static int |
max(int[][] matrix)
Returns the smallest element of the matrix
|
static double |
mean(double[] a) |
static double |
mean(int[] a)
Return the mean of an array of int.
|
static double |
median(double[] a) |
static double |
min(double... vector) |
static float |
min(float[] a) |
static int |
min(int... vector) |
static int |
min(int[][] matrix)
Returns the smallest element of the matrix
|
static double[] |
multiply(double[] a,
double c)
Scales the values in this array by c.
|
static float[] |
multiply(float[] a,
float c)
Scales the values in this array by c.
|
static void |
multiplyInPlace(double[] a,
double b)
Scales the values in this array by b.
|
static void |
multiplyInPlace(float[] a,
double b)
Scales the values in this array by b.
|
static void |
multiplyInto(double[] a,
double[] b,
double c) |
static double |
norm_1(double[] a)
Computes 1-norm of vector.
|
static double |
norm_1(float[] a)
Computes 1-norm of vector.
|
static double |
norm_inf(double[] a)
Computes inf-norm of vector.
|
static double |
norm_inf(float[] a)
Computes inf-norm of vector.
|
static double |
norm(double[] a)
Computes 2-norm of vector.
|
static double |
norm(float[] a)
Computes 2-norm of vector.
|
static void |
normalize(double[] a)
Makes the values in this array sum to 1.0.
|
static void |
normalize(float[] a)
Makes the values in this array sum to 1.0.
|
static int |
numRows(double[] v) |
static double[] |
pairwiseAdd(double[] a,
double[] b) |
static float[] |
pairwiseAdd(float[] a,
float[] b) |
static int[] |
pairwiseAdd(int[] a,
int[] b) |
static void |
pairwiseAddInPlace(double[] to,
double[] from)
Add the two 1d arrays in place of
to . |
static void |
pairwiseAddInPlace(double[] to,
float[] from)
Add the two 1d arrays in place of
to . |
static void |
pairwiseAddInPlace(double[] to,
int[] from) |
static void |
pairwiseAddInPlace(double[] to,
short[] from) |
static void |
pairwiseAddInPlace(float[] to,
float[] from) |
static void |
pairwiseDivideInPlace(double[] a,
double[] b)
Divide the first array by the second elementwise,
and store results in place.
|
static double[] |
pairwiseMultiply(double[] a,
double[] b)
Assumes that both arrays have same length.
|
static void |
pairwiseMultiply(double[] a,
double[] b,
double[] result)
Puts the result in the result array.
|
static float[] |
pairwiseMultiply(float[] a,
float[] b)
Assumes that both arrays have same length.
|
static void |
pairwiseMultiply(float[] a,
float[] b,
float[] result)
Puts the result in the result array.
|
static double[] |
pairwiseScaleAdd(double[] a,
double[] b,
double bScale) |
static void |
pairwiseScaleAddInPlace(double[] to,
double[] from,
double fromScale) |
static double[] |
pairwiseSubtract(double[] a,
double[] b) |
static float[] |
pairwiseSubtract(float[] a,
float[] b) |
static void |
pairwiseSubtractInPlace(double[] to,
double[] from) |
static double |
pearsonCorrelation(double[] x,
double[] y)
Direct computation of Pearson product-moment correlation coefficient.
|
static double[] |
pow(double[] a,
double c)
raises each entry in array a by power c
|
static float[] |
pow(float[] a,
float c)
raises each entry in array a by power c
|
static void |
powInPlace(double[] a,
double c)
Scales the values in this array by c.
|
static void |
powInPlace(float[] a,
float c)
Sets the values in this array by to their value taken to cth power.
|
static int[] |
range(int end)
Generate a range of integers from start (inclusive) to end (exclusive).
|
static int[] |
range(int start,
int end)
Generate a range of integers from start (inclusive) to end (exclusive).
|
static void |
reverse(int[] a) |
static double |
safeMax(double[] v)
Returns the largest value in a vector of doubles.
|
static double |
safeMean(double[] v)
Returns the mean of a vector of doubles.
|
static double |
safeMin(double[] v)
Returns the smallest value in a vector of doubles.
|
static double |
safeStdev(double[] v)
Returns the standard deviation of a vector of doubles.
|
static int |
sampleFromDistribution(double[] d)
Samples from the distribution over values 0 through d.length given by d.
|
static int |
sampleFromDistribution(double[] d,
java.util.Random random)
Samples from the distribution over values 0 through d.length given by d.
|
static int |
sampleFromDistribution(float[] d,
java.util.Random random)
Samples from the distribution over values 0 through d.length given by d.
|
static void |
sampleWithoutReplacement(int[] array,
int numArgClasses)
Fills the array with sample from 0 to numArgClasses-1 without replacement.
|
static void |
sampleWithoutReplacement(int[] array,
int numArgClasses,
java.util.Random rand)
Fills the array with sample from 0 to numArgClasses-1 without replacement.
|
static void |
setToLogDeterministic(double[] a,
int i) |
static void |
setToLogDeterministic(float[] a,
int i) |
static void |
shuffle(int[] a) |
static void |
shuffle(int[] a,
java.util.Random rand) |
static double |
sigLevelByApproxRand(boolean[] A,
boolean[] B) |
static double |
sigLevelByApproxRand(boolean[] A,
boolean[] B,
int iterations) |
static double |
sigLevelByApproxRand(double[] A,
double[] B)
Computes the significance level by approximate randomization, using a
default value of 1000 iterations.
|
static double |
sigLevelByApproxRand(double[] A,
double[] B,
int iterations)
Takes a pair of arrays, A and B, which represent corresponding
outcomes of a pair of random variables: say, results for two different
classifiers on a sequence of inputs.
|
static double |
sigLevelByApproxRand(int[] A,
int[] B) |
static double |
sigLevelByApproxRand(int[] A,
int[] B,
int iterations) |
static double[] |
softmax(double[] scales) |
static double |
standardErrorOfMean(double[] a) |
static void |
standardize(double[] a)
Standardize values in this array, i.e., subtract the mean and divide by the standard deviation.
|
static double |
stdev(double[] a) |
static double |
sum(double[] a)
Returns the sum of an array of doubles.
|
static double |
sum(double[] a,
int fromIndex,
int toIndex)
Returns the sum of the portion of an array of numbers between
fromIndex , inclusive, and toIndex , exclusive. |
static float |
sum(float[] a) |
static int |
sum(int[] a) |
static int |
sum(int[][] a) |
static double |
sumSquared(double[] a) |
static double |
sumSquaredError(double[] a) |
static java.lang.String |
toBinaryString(byte[] b) |
static java.lang.String |
toString(byte[] a) |
static java.lang.String |
toString(byte[] a,
java.text.NumberFormat nf) |
static java.lang.String |
toString(double[] a) |
static java.lang.String |
toString(double[][] counts) |
static java.lang.String |
toString(double[][] counts,
int cellSize,
java.lang.Object[] rowLabels,
java.lang.Object[] colLabels,
int rowLabelSize,
java.text.NumberFormat nf,
boolean printRowTotals,
boolean printColumnTotals,
java.lang.Object topLeft) |
static java.lang.String |
toString(double[][] counts,
int cellSize,
java.lang.Object[] rowLabels,
java.lang.Object[] colLabels,
java.text.NumberFormat nf,
boolean printTotals) |
static java.lang.String |
toString(double[] a,
java.text.NumberFormat nf) |
static java.lang.String |
toString(float[] a) |
static java.lang.String |
toString(float[][] counts) |
static java.lang.String |
toString(float[][] counts,
int cellSize,
java.lang.Object[] rowLabels,
java.lang.Object[] colLabels,
java.text.NumberFormat nf,
boolean printTotals) |
static java.lang.String |
toString(float[] a,
java.text.NumberFormat nf) |
static java.lang.String |
toString(int[] a) |
static java.lang.String |
toString(int[][] counts) |
static java.lang.String |
toString(int[][] counts,
java.lang.Object[] rowLabels,
java.lang.Object[] colLabels,
int labelSize,
int cellSize,
java.text.NumberFormat nf,
boolean printTotals) |
static java.lang.String |
toString(int[] a,
java.text.NumberFormat nf) |
static double[] |
unbox(java.util.Collection<java.lang.Double> list) |
static int[] |
unboxToInt(java.util.Collection<java.lang.Integer> list) |
static double |
variance(double[] a) |
public static int numRows(double[] v)
public static int[] range(int end)
end
- Ending number (exclusive)public static int[] range(int start, int end)
start
- Beginning number (inclusive)end
- Ending number (exclusive)public static float[] doubleArrayToFloatArray(double[] a)
public static double[] floatArrayToDoubleArray(float[] a)
public static double[][] floatArrayToDoubleArray(float[][] a)
public static float[][] doubleArrayToFloatArray(double[][] a)
public static double[] exp(double[] a)
public static double[] log(double[] a)
public static void expInPlace(double[] a)
public static void logInPlace(double[] a)
public static double[] softmax(double[] scales)
public static void addInPlace(double[] a, double b)
a
- The arrayb
- The amount by which to increase each itempublic static void addInPlace(float[] a, double b)
a
- The arrayb
- The amount by which to increase each itempublic static void addMultInPlace(double[] a, double[] b, double c)
public static void multiplyInPlace(double[] a, double b)
public static void multiplyInPlace(float[] a, double b)
public static void divideInPlace(double[] a, double b)
public static void powInPlace(double[] a, double c)
public static void powInPlace(float[] a, float c)
public static double[] add(double[] a, double c)
public static float[] add(float[] a, double c)
public static double[] multiply(double[] a, double c)
public static float[] multiply(float[] a, float c)
public static double[] pow(double[] a, double c)
public static float[] pow(float[] a, float c)
public static void pairwiseAddInPlace(float[] to, float[] from)
public static void pairwiseAddInPlace(double[] to, double[] from)
to
.java.lang.IllegalArgumentException
- If to
and from
are not of the same dimensionspublic static void pairwiseAddInPlace(double[] to, float[] from)
to
.java.lang.IllegalArgumentException
- If to
and from
are not of the same dimensionspublic static void pairwiseAddInPlace(double[] to, int[] from)
public static void pairwiseAddInPlace(double[] to, short[] from)
public static void addInPlace(double[][] m1, double[][] m2)
m1
.java.lang.IllegalArgumentException
- If m1
and m2
are not of the same dimensionspublic static void pairwiseSubtractInPlace(double[] to, double[] from)
public static void pairwiseScaleAddInPlace(double[] to, double[] from, double fromScale)
public static int[] pairwiseAdd(int[] a, int[] b)
public static double[] pairwiseAdd(double[] a, double[] b)
public static float[] pairwiseAdd(float[] a, float[] b)
public static double[] pairwiseScaleAdd(double[] a, double[] b, double bScale)
public static double[] pairwiseSubtract(double[] a, double[] b)
public static float[] pairwiseSubtract(float[] a, float[] b)
public static double dotProduct(double[] a, double[] b)
public static float dotProduct(float[] a, float[] b)
a
- float vectorb
- float vectorpublic static double dotProduct(double[] a, float[] b)
a
- double vectorb
- float vectorpublic static double[] pairwiseMultiply(double[] a, double[] b)
public static float[] pairwiseMultiply(float[] a, float[] b)
public static void pairwiseMultiply(double[] a, double[] b, double[] result)
public static void pairwiseMultiply(float[] a, float[] b, float[] result)
public static void pairwiseDivideInPlace(double[] a, double[] b)
public static boolean hasNaN(double[] a)
public static boolean hasInfinite(double[] a)
public static boolean hasNaN(float[] a)
public static int countNaN(double[] v)
public static double[] filterNaN(double[] v)
public static int countInfinite(double[] v)
public static int countNonZero(double[] v)
public static int countCloseToZero(double[] v, double epsilon)
public static int countPositive(double[] v)
public static int countNegative(double[] v)
public static double[] filterInfinite(double[] v)
public static double[] filterNaNAndInfinite(double[] v)
public static double sum(double[] a)
public static double sum(double[] a, int fromIndex, int toIndex)
fromIndex
, inclusive, and toIndex
, exclusive.
Returns 0 if fromIndex >= toIndex
.public static int sum(int[] a)
public static float sum(float[] a)
public static int sum(int[][] a)
public static int[] diag(int[][] a)
public static double average(double[] a)
public static double iterativeAverage(double[] a)
public static double norm_inf(double[] a)
a
- Array of doublepublic static double norm_inf(float[] a)
public static double norm_1(double[] a)
a
- A vector of doublepublic static double norm_1(float[] a)
a
- A vector of floatspublic static double norm(double[] a)
a
- A vector of doublepublic static double norm(float[] a)
a
- A vector of floatspublic static int argmax(double[] a)
public static int argmax_tieLast(double[] a)
public static double max(double[] a)
public static double max(java.util.Collection<java.lang.Double> a)
public static int argmax(float[] a)
public static float max(float[] a)
public static int argmin(double[] a)
public static double min(double... vector)
public static double safeMin(double[] v)
public static int argmin(float[] a)
public static float min(float[] a)
public static int argmin(int[] a)
public static int min(int... vector)
public static int argmax(int[] a)
public static int max(int... vector)
public static int min(int[][] matrix)
public static int max(int[][] matrix)
public static double safeMax(double[] v)
public static double logSum(double... logInputs)
logInputs
- An array of numbers [log(x1), ..., log(xn)]public static double logSum(double[] logInputs, int fromIndex, int toIndex)
fromIndex
, inclusive, and
toIndex
, exclusive, of an array of numbers, which are
themselves input in log form. This is all natural logarithms.
Reasonable care is taken to do this as efficiently as possible
(under the assumption that the numbers might differ greatly in
magnitude), with high accuracy, and without numerical overflow. Throws an
IllegalArgumentException
if logInputs
is of length zero.
Otherwise, returns Double.NEGATIVE_INFINITY if fromIndex
>=
toIndex
.logInputs
- An array of numbers [log(x1), ..., log(xn)]fromIndex
- The array index to start the sum fromtoIndex
- The array index after the last element to be summedpublic static double logSum(double[] logInputs, int fromIndex, int afterIndex, int stride)
fromIndex
, inclusive, and
toIndex
, exclusive, of an array of numbers, which are
themselves input in log form. This is all natural logarithms.
This version incorporates a stride, so you can sum only select numbers.
Reasonable care is taken to do this as efficiently as possible
(under the assumption that the numbers might differ greatly in
magnitude), with high accuracy, and without numerical overflow. Throws an
IllegalArgumentException
if logInputs
is of length zero.
Otherwise, returns Double.NEGATIVE_INFINITY if fromIndex
>=
toIndex
.logInputs
- An array of numbers [log(x1), ..., log(xn)]fromIndex
- The array index to start the sum fromafterIndex
- The array index after the last element to be summedpublic static double logSum(java.util.List<java.lang.Double> logInputs)
public static double logSum(java.util.List<java.lang.Double> logInputs, int fromIndex, int toIndex)
public static float logSum(float[] logInputs)
logInputs
- An array of numbers [log(x1), ..., log(xn)]public static double innerProduct(double[] a, double[] b)
public static double innerProduct(float[] a, float[] b)
public static double[][] load2DMatrixFromFile(java.lang.String filename) throws java.io.IOException
java.io.IOException
public static java.lang.Integer[] box(int[] assignment)
public static int[] unboxToInt(java.util.Collection<java.lang.Integer> list)
public static java.lang.Double[] box(double[] assignment)
public static double[] unbox(java.util.Collection<java.lang.Double> list)
public static int indexOf(int n, int[] a)
public static int[][] castToInt(double[][] doubleCounts)
public static void normalize(double[] a)
public static void L1normalize(double[] a)
public static void L2normalize(double[] a)
public static void normalize(float[] a)
public static void L2normalize(float[] a)
public static void standardize(double[] a)
public static double L2Norm(double[] a)
public static float L2Norm(float[] a)
public static double L1Norm(double[] a)
public static void logNormalize(double[] a)
public static int sampleFromDistribution(double[] d)
d
- the distribution to sample frompublic static int sampleFromDistribution(double[] d, java.util.Random random)
d
- the distribution to sample frompublic static int sampleFromDistribution(float[] d, java.util.Random random)
d
- the distribution to sample frompublic static double klDivergence(double[] from, double[] to)
public static double jensenShannonDivergence(double[] a, double[] b)
public static void setToLogDeterministic(float[] a, int i)
public static void setToLogDeterministic(double[] a, int i)
public static double mean(double[] a)
public static double mean(int[] a)
public static double median(double[] a)
public static double safeMean(double[] v)
public static double sumSquaredError(double[] a)
public static double sumSquared(double[] a)
public static double variance(double[] a)
public static double stdev(double[] a)
public static double safeStdev(double[] v)
public static double standardErrorOfMean(double[] a)
public static void sampleWithoutReplacement(int[] array, int numArgClasses)
public static void sampleWithoutReplacement(int[] array, int numArgClasses, java.util.Random rand)
public static void shuffle(int[] a)
public static void shuffle(int[] a, java.util.Random rand)
public static void reverse(int[] a)
public static boolean contains(int[] a, int i)
public static boolean containsInSubarray(int[] a, int begin, int end, int i)
public static double pearsonCorrelation(double[] x, double[] y)
public static double sigLevelByApproxRand(double[] A, double[] B)
public static double sigLevelByApproxRand(double[] A, double[] B, int iterations)
A
- Outcome of one r.v.B
- Outcome of another r.v.public static double sigLevelByApproxRand(int[] A, int[] B)
public static double sigLevelByApproxRand(int[] A, int[] B, int iterations)
public static double sigLevelByApproxRand(boolean[] A, boolean[] B)
public static double sigLevelByApproxRand(boolean[] A, boolean[] B, int iterations)
public static java.lang.String toBinaryString(byte[] b)
public static java.lang.String toString(double[] a)
public static java.lang.String toString(double[] a, java.text.NumberFormat nf)
public static java.lang.String toString(float[] a)
public static java.lang.String toString(float[] a, java.text.NumberFormat nf)
public static java.lang.String toString(int[] a)
public static java.lang.String toString(int[] a, java.text.NumberFormat nf)
public static java.lang.String toString(byte[] a)
public static java.lang.String toString(byte[] a, java.text.NumberFormat nf)
public static java.lang.String toString(int[][] counts)
public static java.lang.String toString(int[][] counts, java.lang.Object[] rowLabels, java.lang.Object[] colLabels, int labelSize, int cellSize, java.text.NumberFormat nf, boolean printTotals)
public static java.lang.String toString(double[][] counts)
public static java.lang.String toString(double[][] counts, int cellSize, java.lang.Object[] rowLabels, java.lang.Object[] colLabels, java.text.NumberFormat nf, boolean printTotals)
public static java.lang.String toString(double[][] counts, int cellSize, java.lang.Object[] rowLabels, java.lang.Object[] colLabels, int rowLabelSize, java.text.NumberFormat nf, boolean printRowTotals, boolean printColumnTotals, java.lang.Object topLeft)
public static java.lang.String toString(float[][] counts)
public static java.lang.String toString(float[][] counts, int cellSize, java.lang.Object[] rowLabels, java.lang.Object[] colLabels, java.text.NumberFormat nf, boolean printTotals)
public static void main(java.lang.String[] args)
args
- Ignoredpublic static int[][] deepCopy(int[][] counts)
public static void addMultInto(double[] a, double[] b, double[] c, double d)
public static void multiplyInto(double[] a, double[] b, double c)
public static double entropy(double[] probs)
public static void assertFinite(double[] vector, java.lang.String vectorName) throws ArrayMath.InvalidElementException