edu.wlu.cs.levy.SNARLI
Class SNMatrix

java.lang.Object
  |
  +--edu.wlu.cs.levy.SNARLI.SNMatrix

public class SNMatrix
extends java.lang.Object

This class provides glue code for matrix operations in SNARLI. You do not need to call any of these methods to use SNARLI. This class is provided to show you the methods you need to implement if you want to use a different implementation for matrices.


Constructor Summary
SNMatrix(double[][] x)
          Constructs a matrix using a 2D array of double-precision floating-point values.
SNMatrix(int m, int n)
          Constructs a matrix of all zeros.
 
Method Summary
 void add(edu.wlu.cs.levy.SNARLI.SNMatrix a)
          Adds a matrix to this matrix.
static edu.wlu.cs.levy.SNARLI.SNMatrix arrayMultiply(edu.wlu.cs.levy.SNARLI.SNMatrix a, edu.wlu.cs.levy.SNARLI.SNMatrix b)
          Returns a matrix containing the products of the elements of two matrices.
 edu.wlu.cs.levy.SNARLI.SNMatrix copy()
          Returns a deep copy of this matrix.
 void divide(double s)
          Divides this matrix by a scalar.
static edu.wlu.cs.levy.SNARLI.SNMatrix divide(edu.wlu.cs.levy.SNARLI.SNMatrix a, double s)
          Returns the quotient of a matrix and a scalar.
static edu.wlu.cs.levy.SNARLI.SNMatrix gaussianNoise(int m, int n, java.util.Random r)
          Returns a matrix of normally distributed random values.
 int getCols()
          Returns the number of columns of this Matrix.
 edu.wlu.cs.levy.SNARLI.SNVector getRow(int i)
          Gets the row vector at a certain index of this matrix.
 int getRows()
          Returns the number of rows of this Matrix.
 double[][] getValues()
          Returns a 2D array of the values contained in this matrix.
 double max()
          Returns the value of the largest element of this matrix.
 edu.wlu.cs.levy.SNARLI.SNVector meanRows()
          Returns a vector repsenting the mean over the rows of this Matrix.
static edu.wlu.cs.levy.SNARLI.SNMatrix multiply(edu.wlu.cs.levy.SNARLI.SNMatrix a, double s)
          Returns the product of a matrix and a scalar.
 edu.wlu.cs.levy.SNARLI.SNVector multiply(edu.wlu.cs.levy.SNARLI.SNVector x)
          Returns the product of this matrix and a vector.
 edu.wlu.cs.levy.SNARLI.SNMatrix ne(double s)
          Returns a matrix containing ones where this matrix's elements are not equal to a scalar, and zeros elsewhere.
 void setRow(int i, edu.wlu.cs.levy.SNARLI.SNVector x)
          Sets the row vector at a certain index of this matrix.
static edu.wlu.cs.levy.SNARLI.SNMatrix subtract(edu.wlu.cs.levy.SNARLI.SNMatrix a, edu.wlu.cs.levy.SNARLI.SNMatrix b)
          Returns the difference between two matrices.
 double sum()
          Returns the sum over all elements of this Matrix.
 edu.wlu.cs.levy.SNARLI.SNVector sumRows()
          Returns a vector representing the sum over the rows of this Matrix.
 java.lang.String toString()
          Returns a String representation of this matrix.
 edu.wlu.cs.levy.SNARLI.SNMatrix transpose()
          Returns the transpose of this matrix.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SNMatrix

public SNMatrix(int m,
                int n)
Constructs a matrix of all zeros.

Parameters:
m - number of rows
n - number of columns

SNMatrix

public SNMatrix(double[][] x)
Constructs a matrix using a 2D array of double-precision floating-point values.

Method Detail

meanRows

public edu.wlu.cs.levy.SNARLI.SNVector meanRows()
Returns a vector repsenting the mean over the rows of this Matrix.

Returns:
the mean

sumRows

public edu.wlu.cs.levy.SNARLI.SNVector sumRows()
Returns a vector representing the sum over the rows of this Matrix.

Returns:
the sum

subtract

public static edu.wlu.cs.levy.SNARLI.SNMatrix subtract(edu.wlu.cs.levy.SNARLI.SNMatrix a,
                                                       edu.wlu.cs.levy.SNARLI.SNMatrix b)
Returns the difference between two matrices.

Parameters:
a - one matrix
b - the other matrix
Returns:
a - b
Throws:
java.lang.RuntimeException - if the matrices have unequal sizes

arrayMultiply

public static edu.wlu.cs.levy.SNARLI.SNMatrix arrayMultiply(edu.wlu.cs.levy.SNARLI.SNMatrix a,
                                                            edu.wlu.cs.levy.SNARLI.SNMatrix b)
Returns a matrix containing the products of the elements of two matrices.

Parameters:
a - one matrix
b - the other matrix
Returns:
a .* b
Throws:
java.lang.RuntimeException - if the matrices have unequal sizes

getRows

public int getRows()
Returns the number of rows of this Matrix.

Returns:
number of rows

getCols

public int getCols()
Returns the number of columns of this Matrix.

Returns:
number of columns

getValues

public double[][] getValues()
Returns a 2D array of the values contained in this matrix.

Returns:
values

setRow

public void setRow(int i,
                   edu.wlu.cs.levy.SNARLI.SNVector x)
            throws java.lang.RuntimeException
Sets the row vector at a certain index of this matrix. First row has index 0.

Parameters:
i - index
x - vector
Throws:
java.lang.RuntimeException - if index is out of range

getRow

public edu.wlu.cs.levy.SNARLI.SNVector getRow(int i)
Gets the row vector at a certain index of this matrix. First row has index 0.

Parameters:
i - index
Returns:
vector at row i
Throws:
java.lang.RuntimeException - if index is out of range

gaussianNoise

public static edu.wlu.cs.levy.SNARLI.SNMatrix gaussianNoise(int m,
                                                            int n,
                                                            java.util.Random r)
Returns a matrix of normally distributed random values. Values are taken from a Gaussian distribution with zero mean and standard deviation one.

Parameters:
m - number of rows
n - number of columns
Returns:
matrix of normally distributed random values

add

public void add(edu.wlu.cs.levy.SNARLI.SNMatrix a)
         throws java.lang.RuntimeException
Adds a matrix to this matrix.

Parameters:
a - the other matrix
Throws:
java.lang.RuntimeException - if the matrices have unequal sizes

divide

public static edu.wlu.cs.levy.SNARLI.SNMatrix divide(edu.wlu.cs.levy.SNARLI.SNMatrix a,
                                                     double s)
Returns the quotient of a matrix and a scalar.

Parameters:
a - the matrix
s - the scalar
Returns:
a / s

divide

public void divide(double s)
Divides this matrix by a scalar.

Parameters:
s - the scalar

transpose

public edu.wlu.cs.levy.SNARLI.SNMatrix transpose()
Returns the transpose of this matrix.

Returns:
this^T

multiply

public static edu.wlu.cs.levy.SNARLI.SNMatrix multiply(edu.wlu.cs.levy.SNARLI.SNMatrix a,
                                                       double s)
Returns the product of a matrix and a scalar.

Parameters:
a - the matrix
s - the scalar
Returns:
a * s

multiply

public edu.wlu.cs.levy.SNARLI.SNVector multiply(edu.wlu.cs.levy.SNARLI.SNVector x)
Returns the product of this matrix and a vector.

Parameters:
x - the vector
Returns:
this * x
Throws:
java.lang.RuntimeException - if number of columns of this matrix does not equal number of elements in x

sum

public double sum()
Returns the sum over all elements of this Matrix.

Returns:
the sum

max

public double max()
Returns the value of the largest element of this matrix.

Returns:
maximum value

copy

public edu.wlu.cs.levy.SNARLI.SNMatrix copy()
Returns a deep copy of this matrix.

Returns:
matrix copy

ne

public edu.wlu.cs.levy.SNARLI.SNMatrix ne(double s)
Returns a matrix containing ones where this matrix's elements are not equal to a scalar, and zeros elsewhere.

Parameters:
s - the scalar
Returns:
matrix of ones and zeros

toString

public java.lang.String toString()
Returns a String representation of this matrix.

Overrides:
toString in class java.lang.Object
Returns:
string representation