edu.stanford.nlp.math
Class ArrayMath

java.lang.Object
  extended by edu.stanford.nlp.math.ArrayMath

public class ArrayMath
extends java.lang.Object

Class ArrayMath


Constructor Summary
ArrayMath()
           
 
Method Summary
static double[] add(double[] a, double c)
           
static double[] add(double[] a, double[] b)
           
static float[] add(float[] a, float[] b)
           
static void addInPlace(double[] a, double b)
           
static void addInPlace(double[] a, double[] b)
           
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 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 float[] doubleArrayToFloatArray(double[] a)
           
static float[][] doubleArrayToFloatArray(double[][] a)
           
static double[] exp(double[] a)
           
static void expInPlace(double[] a)
           
static double[] floatArrayToDoubleArray(float[] a)
           
static double[][] floatArrayToDoubleArray(float[][] a)
           
static boolean hasInfinite(double[] a)
           
static boolean hasNaN(double[] a)
           
static int indexOf(int n, int[] a)
           
static double innerProduct(double[] a, double[] b)
           
static double innerProduct(float[] a, float[] b)
           
static double klDivergence(double[] from, double[] to)
           
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 float logSum(float[] logInputs)
          Returns the log of the sum of an array of numbers, which are themselves input in log form.
static double max(double[] a)
           
static float max(float[] a)
           
static int max(int[] a)
           
static double mean(double[] a)
           
static double min(double[] a)
           
static float min(float[] a)
           
static int min(int[] a)
           
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 c)
          Scales in place the values in this array by 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 double[] pairwiseAdd(double[] a, double[] b)
           
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[] pow(double[] a, double c)
          Scales the values in this array by c.
static float[] pow(float[] a, float c)
          Scales the values in this array by c.
static void powInPlace(double[] a, double c)
          Scales the values in this array by c.
static void powInPlace(float[] a, float c)
          Scales the values in this array by c.
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 scale(double[] a, double b)
          Scales the values in this array by b.
static void scale(float[] a, double b)
          Scales the values in this array by b.
static void setToLogDeterministic(double[] a, int i)
           
static void setToLogDeterministic(float[] a, int i)
           
static void shift(double[] a, double b)
          Shifts the values in this array by b.
static void shuffle(int[] a)
           
static void shuffle(int[] a, java.util.Random rand)
           
static double standardErrorOfMean(double[] a)
           
static double stdev(double[] a)
           
static int[] subArray(int[] a, int from, int to)
           
static double[] subtract(double[] a, double[] b)
           
static float[] subtract(float[] a, float[] b)
           
static double sum(double[] a)
           
static float sum(float[] a)
           
static int sum(int[] a)
           
static int sum(int[][] a)
           
static double sumSquaredError(double[] a)
           
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, 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 variance(double[] a)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayMath

public ArrayMath()
Method Detail

doubleArrayToFloatArray

public static float[] doubleArrayToFloatArray(double[] a)

floatArrayToDoubleArray

public static double[] floatArrayToDoubleArray(float[] a)

floatArrayToDoubleArray

public static double[][] floatArrayToDoubleArray(float[][] a)

doubleArrayToFloatArray

public static float[][] doubleArrayToFloatArray(double[][] a)

add

public static double[] add(double[] a,
                           double c)

addInPlace

public static void addInPlace(double[] a,
                              double b)

addInPlace

public static void addInPlace(double[] a,
                              double[] b)

add

public static double[] add(double[] a,
                           double[] b)

add

public static float[] add(float[] a,
                          float[] b)

subtract

public static double[] subtract(double[] a,
                                double[] b)

subtract

public static float[] subtract(float[] a,
                               float[] b)

multiply

public static double[] multiply(double[] a,
                                double c)
Scales the values in this array by c.


multiplyInPlace

public static void multiplyInPlace(double[] a,
                                   double c)
Scales in place the values in this array by c.


multiply

public static float[] multiply(float[] a,
                               float c)
Scales the values in this array by c.


average

public static double average(double[] a)

pow

public static float[] pow(float[] a,
                          float c)
Scales the values in this array by c.


powInPlace

public static void powInPlace(float[] a,
                              float c)
Scales the values in this array by c.


powInPlace

public static void powInPlace(double[] a,
                              double c)
Scales the values in this array by c.


pow

public static double[] pow(double[] a,
                           double c)
Scales the values in this array by c.


hasNaN

public static boolean hasNaN(double[] a)

hasInfinite

public static boolean hasInfinite(double[] a)

norm_inf

public static double norm_inf(double[] a)
Computes inf-norm of vector

Parameters:
a -
Returns:
inf-norm of a

norm_inf

public static double norm_inf(float[] a)
Computes inf-norm of vector

Parameters:
a -
Returns:
inf-norm of a

norm_1

public static double norm_1(double[] a)
Computes 1-norm of vector

Parameters:
a -
Returns:
1-norm of a

norm_1

public static double norm_1(float[] a)
Computes 1-norm of vector

Parameters:
a -
Returns:
1-norm of a

norm

public static double norm(double[] a)
Computes 2-norm of vector

Parameters:
a -
Returns:
Euclidean norm of a

norm

public static double norm(float[] a)
Computes 2-norm of vector

Parameters:
a -
Returns:
Euclidean norm of a

scale

public static void scale(double[] a,
                         double b)
Scales the values in this array by b. Does it in place.


scale

public static void scale(float[] a,
                         double b)
Scales the values in this array by b. Does it in place.


shift

public static void shift(double[] a,
                         double b)
Shifts the values in this array by b. Does it in place.


pairwiseMultiply

public static double[] pairwiseMultiply(double[] a,
                                        double[] b)
Assumes that both arrays have same length.


pairwiseMultiply

public static float[] pairwiseMultiply(float[] a,
                                       float[] b)
Assumes that both arrays have same length.


pairwiseMultiply

public static void pairwiseMultiply(double[] a,
                                    double[] b,
                                    double[] result)
Puts the result in the result array. Assumes that all arrays have same length.


pairwiseMultiply

public static void pairwiseMultiply(float[] a,
                                    float[] b,
                                    float[] result)
Puts the result in the result array. Assumes that all arrays have same length.


pairwiseAdd

public static double[] pairwiseAdd(double[] a,
                                   double[] b)

sum

public static double sum(double[] a)

sum

public static int sum(int[] a)

sum

public static float sum(float[] a)

sum

public static int sum(int[][] a)

argmax

public static int argmax(double[] a)
Returns:
the index of the max value; if max is a tie, returns the first one.

max

public static double max(double[] a)

argmax

public static int argmax(float[] a)
Returns:
the index of the max value; if max is a tie, returns the first one.

max

public static float max(float[] a)

argmin

public static int argmin(double[] a)
Returns:
the index of the max value; if max is a tie, returns the first one.

min

public static double min(double[] a)

argmin

public static int argmin(float[] a)
Returns:
the index of the max value; if max is a tie, returns the first one.

min

public static float min(float[] a)

argmin

public static int argmin(int[] a)
Returns:
the index of the max value; if max is a tie, returns the first one.

min

public static int min(int[] a)

argmax

public static int argmax(int[] a)
Returns:
the index of the max value; if max is a tie, returns the first one.

max

public static int max(int[] a)

exp

public static double[] exp(double[] a)

log

public static double[] log(double[] a)

expInPlace

public static void expInPlace(double[] a)

logInPlace

public static void logInPlace(double[] a)

innerProduct

public static double innerProduct(double[] a,
                                  double[] b)

innerProduct

public static double innerProduct(float[] a,
                                  float[] b)

subArray

public static int[] subArray(int[] a,
                             int from,
                             int to)

toString

public static java.lang.String toString(double[] a)

toString

public static java.lang.String toString(double[] a,
                                        java.text.NumberFormat nf)

toString

public static java.lang.String toString(float[] a)

toString

public static java.lang.String toString(float[] a,
                                        java.text.NumberFormat nf)

toString

public static java.lang.String toString(int[] a)

toString

public static java.lang.String toString(int[] a,
                                        java.text.NumberFormat nf)

toString

public static java.lang.String toString(byte[] a)

toString

public static java.lang.String toString(byte[] a,
                                        java.text.NumberFormat nf)

normalize

public static void normalize(double[] a)
Makes the values in this array sum to 1.0. Does it in place. If the total is 0.0, sets a to the uniform distribution.


normalize

public static void normalize(float[] a)
Makes the values in this array sum to 1.0. Does it in place. If the total is 0.0, sets a to the uniform distribution.


logNormalize

public static void logNormalize(double[] a)
Makes the values in this array sum to 1.0. Does it in place. If the total is 0.0, sets a to the uniform distribution.


sampleFromDistribution

public static int sampleFromDistribution(double[] d)
Samples from the distribution over values 0 through d.length given by d. Assumes that the distribution sums to 1.0.

Parameters:
d - the distribution to sample from
Returns:
a value from 0 to d.length

sampleFromDistribution

public static int sampleFromDistribution(double[] d,
                                         java.util.Random random)
Samples from the distribution over values 0 through d.length given by d. Assumes that the distribution sums to 1.0.

Parameters:
d - the distribution to sample from
Returns:
a value from 0 to d.length

sampleFromDistribution

public static int sampleFromDistribution(float[] d,
                                         java.util.Random random)
Samples from the distribution over values 0 through d.length given by d. Assumes that the distribution sums to 1.0.

Parameters:
d - the distribution to sample from
Returns:
a value from 0 to d.length

klDivergence

public static double klDivergence(double[] from,
                                  double[] to)

setToLogDeterministic

public static void setToLogDeterministic(float[] a,
                                         int i)

setToLogDeterministic

public static void setToLogDeterministic(double[] a,
                                         int i)

mean

public static double mean(double[] a)

sumSquaredError

public static double sumSquaredError(double[] a)

variance

public static double variance(double[] a)

stdev

public static double stdev(double[] a)

standardErrorOfMean

public static double standardErrorOfMean(double[] a)

toString

public static java.lang.String toString(int[][] counts)

toString

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)

toString

public static java.lang.String toString(double[][] counts)

toString

public static java.lang.String toString(double[][] counts,
                                        int cellSize,
                                        java.lang.Object[] rowLabels,
                                        java.lang.Object[] colLabels,
                                        java.text.NumberFormat nf,
                                        boolean printTotals)

toString

public static java.lang.String toString(float[][] counts)

toString

public static java.lang.String toString(float[][] counts,
                                        int cellSize,
                                        java.lang.Object[] rowLabels,
                                        java.lang.Object[] colLabels,
                                        java.text.NumberFormat nf,
                                        boolean printTotals)

logSum

public static double logSum(double[] logInputs)
Returns the log of the sum 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.

Parameters:
logInputs - An array of numbers [log(x1), ..., log(xn)]
Returns:
log(x1 + ... + xn)

logSum

public static float logSum(float[] logInputs)
Returns the log of the sum 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.

Parameters:
logInputs - An array of numbers [log(x1), ..., log(xn)]
Returns:
log(x1 + ... + xn)

load2DMatrixFromFile

public static double[][] load2DMatrixFromFile(java.lang.String filename)
                                       throws java.io.IOException
Throws:
java.io.IOException

sampleWithoutReplacement

public static void sampleWithoutReplacement(int[] array,
                                            int numArgClasses)
Fills the array with sample from 0 to numArgClasses-1 without replacement.

Parameters:
array -
numArgClasses -

sampleWithoutReplacement

public static void sampleWithoutReplacement(int[] array,
                                            int numArgClasses,
                                            java.util.Random rand)
Fills the array with sample from 0 to numArgClasses-1 without replacement.

Parameters:
array -
numArgClasses -

shuffle

public static void shuffle(int[] a)

shuffle

public static void shuffle(int[] a,
                           java.util.Random rand)

contains

public static boolean contains(int[] a,
                               int i)

box

public static java.lang.Integer[] box(int[] assignment)

box

public static java.lang.Double[] box(double[] assignment)

indexOf

public static int indexOf(int n,
                          int[] a)

castToInt

public static int[][] castToInt(double[][] doubleCounts)