mark.core.math
Class Array

java.lang.Object
  |
  +--mark.core.math.Array

public class Array
extends java.lang.Object

Maintains a (dense) count array and totals for a one dimensional random variable. That is, this class should be renamed to Vector! Assume a sampling process, where each sample determines a discrete random variable X. The range of X is the integers in [0, l(X) - 1], where l(X) is the number of values X can assume. An Array maintains counts of X. Counts may be doubles, meaning that a count of 0.5, for example, is legal.


Constructor Summary
Array(double[] a)
          Initializes all counts with the values in the given array.
Array(int Xlength)
          Initializes all counts to zero.
Array(int Xlength, boolean laplace)
          Initializes all counts to zero if not laplace and to one if laplace.
 
Method Summary
 void add(Array v)
          Performs vector addition.
static Array add(Array v1, Array v2)
          Performs vector addition.
 int argmax()
          Returns the X value of the largest count in the Array.
 int argmin()
          Returns the X value of the smallest value in the Array.
 java.lang.Object clone()
          Clones the Array.
 double dot(Array v)
          Performs a dot product.
static double dot(Array v1, Array v2)
          Performs a dot product.
 boolean equals(java.lang.Object rhs)
          Returns whether the Array is equivalent to the given Array.
 void fromLog()
          Subtracts the smallest value from every element, converts from log space, and then normalizes counts.
 void inc(int x)
          Increments #(X = x).
 void inc(int x, double c)
          Increments #(X = x) by the given count.
 int length()
          Returns the number of values X can assume.
 double magnitude()
          Returns the magnitude of the vector.
static void main(java.lang.String[] a)
          Tests the class.
 double max()
          Returns the largest count in the Array.
 double min()
          Returns the smallest count in the Array.
static Array multiply(Array v, double s)
          Performs scalar multiplication.
 void multiply(double s)
          Performs scalar multiplication.
static Array multiply(double s, Array v)
          Performs scalar multiplication.
 void normalizeCounts()
          Divides each count by the sum of the counts.
 void normalizeMagnitude()
          Normalizes the vector to have magnitude 1.
static Array normalizeMagnitude(Array v)
          Normalizes the vector to have magnitude 1.
 double num()
          Returns the total number of samples.
 double numX(int x)
          Returns #(X = x).
 double prob()
          Returns 1.0.
 double probX(int x)
          Returns p (X = x).
 void reset(boolean laplace)
          Resets all counts to zero if not laplace and to one if laplace.
 void set(int x, double c)
          Sets #(X = x).
 void subtract(Array v)
          Performs vector subtraction.
static Array subtract(Array v1, Array v2)
          Performs vector subtraction.
 double[] toDoubleArray()
          Returns a representation as a double[].
 void toLog()
          Normalizes counts, and then converts into log space.
 java.lang.String toString()
          Returns a string representation of the vector.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Array

public Array(int Xlength)
Initializes all counts to zero.


Array

public Array(int Xlength,
             boolean laplace)
Initializes all counts to zero if not laplace and to one if laplace.

Parameters:
laplace - whether to use laplace smoothing.

Array

public Array(double[] a)
Initializes all counts with the values in the given array.

Parameters:
a - the array.
Method Detail

clone

public java.lang.Object clone()
Clones the Array.

Overrides:
clone in class java.lang.Object
Returns:
the copy.

equals

public boolean equals(java.lang.Object rhs)
Returns whether the Array is equivalent to the given Array.

Overrides:
equals in class java.lang.Object
Parameters:
rhs - the given Array.
Returns:
whether the two Arrays are equivalent.

toString

public java.lang.String toString()
Returns a string representation of the vector.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the vector.

toDoubleArray

public double[] toDoubleArray()
Returns a representation as a double[].

Returns:
the double[].

reset

public void reset(boolean laplace)
Resets all counts to zero if not laplace and to one if laplace.

Parameters:
laplace - whether to use laplace smoothing.

length

public int length()
Returns the number of values X can assume.

Returns:
l(X).

numX

public double numX(int x)
Returns #(X = x).

Parameters:
x - the X value.
Returns:
the count.

num

public double num()
Returns the total number of samples.

Returns:
the count.

probX

public double probX(int x)
Returns p (X = x).

Parameters:
x - the X value.
Returns:
the probability.

prob

public double prob()
Returns 1.0.

Returns:
the probability.

set

public void set(int x,
                double c)
Sets #(X = x).

Parameters:
x - the X value.
c - the count.

inc

public void inc(int x)
Increments #(X = x).

Parameters:
x - the X value.

inc

public void inc(int x,
                double c)
Increments #(X = x) by the given count.

Parameters:
x - the X value.
c - the count.

normalizeCounts

public void normalizeCounts()
Divides each count by the sum of the counts.


main

public static void main(java.lang.String[] a)
                 throws java.lang.Exception
Tests the class. This code assumes that toDoubleArray works.

Parameters:
a - ignored.
java.lang.Exception

max

public double max()
Returns the largest count in the Array.

Returns:
the largest count in the Array.

min

public double min()
Returns the smallest count in the Array.

Returns:
the smallest count in the Array.

argmax

public int argmax()
Returns the X value of the largest count in the Array.

Returns:
the X value of the largest count in the Array.

argmin

public int argmin()
Returns the X value of the smallest value in the Array.

Returns:
the X value of the smallest value in the Array.

toLog

public void toLog()
Normalizes counts, and then converts into log space.


fromLog

public void fromLog()
Subtracts the smallest value from every element, converts from log space, and then normalizes counts.


add

public void add(Array v)
Performs vector addition.

Parameters:
v - the rhs.

subtract

public void subtract(Array v)
Performs vector subtraction.

Parameters:
v - the rhs.

multiply

public void multiply(double s)
Performs scalar multiplication.

Parameters:
s - the scalar.

dot

public double dot(Array v)
Performs a dot product.

Parameters:
v - the rhs.
Returns:
the dot product.

magnitude

public double magnitude()
Returns the magnitude of the vector.

Returns:
the magnitude of the vector.

normalizeMagnitude

public void normalizeMagnitude()
Normalizes the vector to have magnitude 1.


add

public static Array add(Array v1,
                        Array v2)
Performs vector addition.

Parameters:
v1 - the lhs.
v2 - the rhs.
Returns:
the sum.

subtract

public static Array subtract(Array v1,
                             Array v2)
Performs vector subtraction.

Parameters:
v1 - the lhs.
v2 - the rhs.
Returns:
the difference.

multiply

public static Array multiply(Array v,
                             double s)
Performs scalar multiplication.

Parameters:
v - the lhs.
s - the rhs.
Returns:
the result.

multiply

public static Array multiply(double s,
                             Array v)
Performs scalar multiplication.

Parameters:
s - the lhs.
v - the rhs.
Returns:
the result.

dot

public static double dot(Array v1,
                         Array v2)
Performs a dot product.

Parameters:
v1 - the lhs.
v2 - the rhs.
Returns:
the result.

normalizeMagnitude

public static Array normalizeMagnitude(Array v)
Normalizes the vector to have magnitude 1.

Parameters:
v - the vector to normalize.
Returns:
the result.