org.retro.neural
Class Matrix

java.lang.Object
  extended byorg.retro.neural.Matrix

public class Matrix
extends java.lang.Object


Field Summary
protected  FieldElement[][] entries
           
protected  int numOfCols
           
protected  int numOfRows
           
 
Constructor Summary
Matrix(FieldElement[][] theEntries)
          Constructs a Matrix out of a two dimensional array of FieldElements.
Matrix(FieldElement[][] theEntries, int rows, int cols)
          Constructs a Matrix out of a two dimensional array of FieldElements.
Matrix(FieldElement[] theEntries, int numberOfRows)
           
Matrix(int numberOfRows, int numberOfCols)
           
Matrix(Vector[] rowVectors)
          Constructs a Matrix out of an array of row vectors.
 
Method Summary
 Matrix add(FieldElement scalar)
          Returns the matrix that is the sum of this Matrix and a scalar.
 Matrix add(Matrix matrix)
          Returns the matrix that is the sum of this Matrix and another matrix.
 void addReplace(FieldElement scalar)
          Adds a scalar to this Matrix.
 void addReplace(Matrix anotherMatrix)
          Adds another matrix to this Matrix.
 Matrix and(Matrix anotherMatrix)
          Returns the logical AND of this Matrix with another.
 Matrix apply(FieldElement scalar, DyadicOperator fun)
          Returns the result of applying a specified function to the elements of a this Matrix a scalar.
 Matrix apply(Matrix anotherMatrix, DyadicOperator fun)
          Returns the result of applying a specified function to the elements of this Matrix and another.
 Matrix apply(MonadicOperator fun)
          Returns the result of applying a specified function to every element of this Matrix.
 void applyReplace(FieldElement scalar, DyadicOperator fun)
          Sets this Matrix to the result of applying a specified function to elements of this Matrix and a scalar.
 void applyReplace(Matrix anotherMatrix, DyadicOperator fun)
          Sets this Matrix to the result of applying a specified function to elements of this Matrix and another's.
 void applyReplace(MonadicOperator fun)
          Sets this Matrix to the result of applying a specified function to every element of this Matrix.
 Matrix arrayMultiply(Matrix anotherMatrix)
          Returns the element-wise product of this Matrix and another.
 Matrix copy()
          Returns a deep copy of this Matrix.
 FieldElement det()
          Returns the determinant of this Matrix.
 Matrix divide(FieldElement scalar)
          Returns a Matrix that is this Matrix divided by a scalar.
 void divideReplace(FieldElement scalar)
          Divides this Matrix by a scalar.
 void divideReplace(Matrix anotherMatrix)
          Divides this Matrix by another.
 Matrix eq(FieldElement scalar)
          Returns a Matrix containing ones where this Matrix's elements are equal to a scalar, and zeros elsewhere.
 Matrix eq(Matrix anotherMatrix)
          Returns a Matrix containing ones where this Matrix's elements are equal to those of another Matrices, and zeros elsewhere.
 boolean equals(Matrix anotherMatrix)
          Tests two matrices for equality.
 Matrix gausselim()
          Returns a matrix that is this Matrix with Gauss-elimination executed on.
 Matrix gaussjord()
          Returns a matrix that is this Matrix with the Gauss-Jordan algorithm executed on.
 Matrix ge(FieldElement scalar)
          Returns a Matrix containing ones where this Matrix's elements are greater than or equal to a scalar, and zeros elsewhere.
 Matrix ge(Matrix anotherMatrix)
          Returns a Matrix containing ones where this Matrix's elements are greater than or equal to those of another Matrices, and zeros elsewhere.
 Vector getCol(int colIndex)
          Gets the column vector at a certain column index.
 int getCols()
          Gets the number of columns of this Matrix.
 FieldElement getEntry(int rowIndex, int colIndex)
          Gets the entry of this Matrix at a certain row - and col index.
 Vector getRow(int rowIndex)
          Gets the row vector at a certain row index.
 int getRows()
          Gets the number of rows of this Matrix.
 Matrix gt(FieldElement scalar)
          Returns a Matrix containing ones where this Matrix's elements are greater than a scalar, and zeros elsewhere.
 Matrix gt(Matrix anotherMatrix)
          Returns a Matrix containing ones where this Matrix's elements are greater than those of another Matrices, and zeros elsewhere.
 Matrix hermitian()
          Returns a matrix that is this Matrix hermitianly transposed.
 Matrix insertCol(int colIndex, Vector vector)
          Returns a Matrix with a Vector inserted at specified index as a column.
 Matrix insertRow(int rowIndex, Vector vector)
          Returns a Matrix with a Vector inserted at specified index as a row.
 Matrix inverse()
          Returns the inverse of this Matrix.
 boolean isZeroCol(int colIndex)
          Returns whether the column at the specified column index is a zero column or not.
 boolean isZeroRow(int rowIndex)
          Returns whether the row at the specified row index is a zero row or not.
 Matrix le(FieldElement scalar)
          Returns a Matrix containing ones where this Matrix's elements are less than or equal to a scalar, and zeros elsewhere.
 Matrix le(Matrix anotherMatrix)
          Returns a Matrix containing ones where this Matrix's elements are less than or equal to those of another Matrices, and zeros elsewhere.
 Matrix lt(FieldElement scalar)
          Returns a Matrix containing ones where this Matrix's elements are less than a scalar, and zeros elsewhere.
 Matrix lt(Matrix anotherMatrix)
          Returns a Matrix containing ones where this Matrix's elements are less than those of another Matrices, and zeros elsewhere.
 FieldElement max()
          Computes the largest value of any element in this Matrix.
 FieldElement mean()
          Computes the mean over all elements of this Matrix.
 Vector meanCols()
          Computes the mean over the columns of this Matrix.
 Vector meanRows()
          Computes the mean over the rows of this Matrix.
 FieldElement min()
          Computes the smallest value of any element in this Matrix.
 Matrix multiply(FieldElement scalar)
          Returns a Matrix that is this Matrix multiplied with a scalar.
 Matrix multiply(Matrix anotherMatrix)
          Returns the matrix that is the product of this Matrix and another matrix.
 Vector multiply(Vector vector)
          Returns the vector that is the product of this Matrix and a given vector.
 void multiplyReplace(FieldElement scalar)
          Multiplies this Matrix by a scalar.
 void multiplyReplace(Matrix anotherMatrix)
          Multiplies this Matrix element-wise by another.
 Matrix ne(FieldElement scalar)
          Returns a Matrix containing ones where this Matrix's elements are not equal to a scalar, and zeros elsewhere.
 Matrix ne(Matrix anotherMatrix)
          Returns a Matrix containing ones where this Matrix's elements are not equal to those of another Matrices, and zeros elsewhere.
 Matrix not()
          Returns the logical negation of this Matrix.
 Matrix or(Matrix anotherMatrix)
          Returns the logical OR of this Matrix with another.
 int rank()
          Returns the rank of this Matrix.
 void setAll(FieldElement newEntry)
          Sets all entries to a FieldElement.
 void setCol(int colIndex, Vector vector)
          Sets the column vector at a certain column index of the matrix.
 void setEntry(int rowIndex, int colIndex, FieldElement newEntry)
          Sets the entry of this Matrix at a certain row - and col index.
 void setRow(int rowIndex, Vector vector)
          Sets the row vector at a certain index of the matrix.
 Matrix subtract(FieldElement scalar)
          Returns the matrix that is this this Matrix minus a scalar.
 Matrix subtract(Matrix matrix)
          Returns the matrix that is this this Matrix minus another one.
 void subtractReplace(FieldElement scalar)
          Subtracts a scalar from this Matrix.
 void subtractReplace(Matrix anotherMatrix)
          Subtracts another Matrix from this.
 FieldElement sum()
          Computes the sum over all elements of this Matrix.
 Vector sumCols()
          Computes the sum over the columns of this matrix.
 Vector sumRows()
          Computes the sum over the rows of this matrix.
 void swapCols(int colIndex1, int colIndex2)
          Swaps two columns of this Matrix.
 void swapRows(int rowIndex1, int rowIndex2)
          Swaps two rows of this Matrix.
 java.lang.String toString()
          Returns a String representation of this Matrix.
 Vector toVector()
          Returns a new 1xN Vector made from the N elements of this Matrix.
 Matrix transpose()
          Returns a matrix that is this Matrix transposed.
 Matrix withoutCol(int colIndex)
          Returns this Matrix without the column at a certain column index.
 Matrix withoutRow(int rowIndex)
          Returns this Matrix without the row at a certain row index.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

numOfRows

protected int numOfRows

numOfCols

protected int numOfCols

entries

protected FieldElement[][] entries
Constructor Detail

Matrix

public Matrix(int numberOfRows,
              int numberOfCols)

Matrix

public Matrix(FieldElement[] theEntries,
              int numberOfRows)
       throws InvalidOperationException

Matrix

public Matrix(Vector[] rowVectors)
       throws InvalidOperationException
Constructs a Matrix out of an array of row vectors.

Parameters:
rowVectors - as an array of Vectors
Throws:
InvalidOperationException - if rowVectors is null
InvalidOperationException - if rowVectors contains a null Vector
InvalidOperationException - if rowVectors contains Vectors of unequal lengths

Matrix

public Matrix(FieldElement[][] theEntries)
       throws InvalidOperationException
Constructs a Matrix out of a two dimensional array of FieldElements.

Parameters:
theEntries - as a two dimensional FieldElement array.
Throws:
InvalidOperationException - if theEntries is null

Matrix

public Matrix(FieldElement[][] theEntries,
              int rows,
              int cols)
Constructs a Matrix out of a two dimensional array of FieldElements. But with the dimensions given it is faster than the constructor above because it doesnt check any integrity of the entries-array.

Parameters:
theEntries -
rows -
cols -
Method Detail

getRows

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

Returns:
number of rows

getCols

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

Returns:
number of columns

getEntry

public FieldElement getEntry(int rowIndex,
                             int colIndex)
Gets the entry of this Matrix at a certain row - and col index.

Parameters:
rowIndex -
colIndex -
Returns:
the FieldElement at this row - and column index

setEntry

public void setEntry(int rowIndex,
                     int colIndex,
                     FieldElement newEntry)
              throws InvalidOperationException
Sets the entry of this Matrix at a certain row - and col index.

Parameters:
rowIndex -
colIndex -
newEntry -
Throws:
InvalidOperationException - if rowIndex or colIndex is invalid

getRow

public Vector getRow(int rowIndex)
Gets the row vector at a certain row index.

Parameters:
rowIndex -

getCol

public Vector getCol(int colIndex)
Gets the column vector at a certain column index.

Parameters:
colIndex -

setRow

public void setRow(int rowIndex,
                   Vector vector)
            throws InvalidOperationException
Sets the row vector at a certain index of the matrix.

Parameters:
rowIndex -
vector -
Throws:
InvalidOperationException - if index out of bounds

setCol

public void setCol(int colIndex,
                   Vector vector)
Sets the column vector at a certain column index of the matrix.

Parameters:
colIndex -
vector -

withoutRow

public Matrix withoutRow(int rowIndex)
Returns this Matrix without the row at a certain row index.

Parameters:
rowIndex -
Returns:
the matrix without the row at the row index.

withoutCol

public Matrix withoutCol(int colIndex)
Returns this Matrix without the column at a certain column index.

Parameters:
colIndex -
Returns:
the matrix without the column at the column index.

insertCol

public Matrix insertCol(int colIndex,
                        Vector vector)
                 throws InvalidOperationException
Returns a Matrix with a Vector inserted at specified index as a column. Index 1 will it put at the beginning and index numOfCols+1 will attach it to the end.

Parameters:
vector -
colIndex -
Returns:
matrix with vector inserted.
Throws:
InvalidOperationException - if index out of bounds

insertRow

public Matrix insertRow(int rowIndex,
                        Vector vector)
                 throws InvalidOperationException
Returns a Matrix with a Vector inserted at specified index as a row. Index 1 will put at the beginning and index numOfRows+1 will attach it to the end.

Parameters:
vector -
rowIndex -
Returns:
matrix with vector inserted.
Throws:
InvalidOperationException - if index out of bounds

toString

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

Returns:
String representation

add

public Matrix add(Matrix matrix)
           throws InvalidOperationException
Returns the matrix that is the sum of this Matrix and another matrix.

Returns:
this + anotherMatrix
Throws:
InvalidOperationException - if matrices differ in size

add

public Matrix add(FieldElement scalar)
Returns the matrix that is the sum of this Matrix and a scalar.

Parameters:
scalar -
Returns:
this + scalar

subtract

public Matrix subtract(Matrix matrix)
                throws InvalidOperationException
Returns the matrix that is this this Matrix minus another one.

Returns:
this - anotherMatrix
Throws:
InvalidOperationException - if matrices differ in size

subtract

public Matrix subtract(FieldElement scalar)
Returns the matrix that is this this Matrix minus a scalar.

Parameters:
scalar -
Returns:
this - scalar

multiply

public Matrix multiply(FieldElement scalar)
Returns a Matrix that is this Matrix multiplied with a scalar.

Parameters:
scalar -
Returns:
multiplied Matrix

divide

public Matrix divide(FieldElement scalar)
Returns a Matrix that is this Matrix divided by a scalar.

Parameters:
scalar -
Returns:
divided Matrix

multiply

public Vector multiply(Vector vector)
                throws InvalidOperationException
Returns the vector that is the product of this Matrix and a given vector.

Parameters:
vector -
Returns:
product vector
Throws:
InvalidOperationException - if number of columns of this matrix does not equal number of elements of vector

multiply

public Matrix multiply(Matrix anotherMatrix)
                throws InvalidOperationException
Returns the matrix that is the product of this Matrix and another matrix.

Parameters:
anotherMatrix -
Returns:
product matrix
Throws:
InvalidOperationException - if number of columns of this matrix does not equal number of rows of the other matrix

copy

public Matrix copy()
Returns a deep copy of this Matrix.

Returns:
matrix copy

det

public FieldElement det()
                 throws InvalidOperationException
Returns the determinant of this Matrix.

Returns:
determinant
Throws:
InvalidOperationException - if matrix is not square

swapRows

public void swapRows(int rowIndex1,
                     int rowIndex2)
Swaps two rows of this Matrix.

Parameters:
rowIndex1 - index of first swap partner.
rowIndex2 - index of second swap partner.

swapCols

public void swapCols(int colIndex1,
                     int colIndex2)
Swaps two columns of this Matrix.

Parameters:
colIndex1 - index of first swap partner.
colIndex2 - index of second swap partner.

gaussjord

public Matrix gaussjord()
Returns a matrix that is this Matrix with the Gauss-Jordan algorithm executed on. In other words: It returns the reduced row echelon form of this Matrix.

Returns:
matrix in reduced row-echelon form

gausselim

public Matrix gausselim()
Returns a matrix that is this Matrix with Gauss-elimination executed on. In other words: It returns a row echelon form of this Matrix.

Returns:
matrix in row-echelon form

isZeroRow

public boolean isZeroRow(int rowIndex)
Returns whether the row at the specified row index is a zero row or not.

Parameters:
rowIndex - index of the row to be tested
Returns:
true if there are only zero elements in the row.

isZeroCol

public boolean isZeroCol(int colIndex)
Returns whether the column at the specified column index is a zero column or not.

Parameters:
colIndex - of the column to be tested
Returns:
true if there are only zero elements in the column.

rank

public int rank()
Returns the rank of this Matrix.

Returns:
rank

transpose

public Matrix transpose()
Returns a matrix that is this Matrix transposed.

Returns:
transposed matrix

hermitian

public Matrix hermitian()
Returns a matrix that is this Matrix hermitianly transposed. For almost all matrices this method is equivalent to transpose. But in case of complex number entries the matrix will be transposed and all entries will be conjugated as well.

Returns:
hermitianly transposed matrix

equals

public boolean equals(Matrix anotherMatrix)
Tests two matrices for equality.

Parameters:
anotherMatrix -
Returns:
true if and only if the two matrices equal in all entries.

inverse

public Matrix inverse()
Returns the inverse of this Matrix.

Returns:
inverse Matrix

divideReplace

public void divideReplace(FieldElement scalar)
Divides this Matrix by a scalar.

Parameters:
scalar -

divideReplace

public void divideReplace(Matrix anotherMatrix)
                   throws InvalidOperationException
Divides this Matrix by another.

Throws:
InvalidOperationException - if the matrices have different sizes

multiplyReplace

public void multiplyReplace(FieldElement scalar)
Multiplies this Matrix by a scalar.

Parameters:
scalar -

multiplyReplace

public void multiplyReplace(Matrix anotherMatrix)
                     throws InvalidOperationException
Multiplies this Matrix element-wise by another.

Throws:
InvalidOperationException - if the matrices have different sizes

addReplace

public void addReplace(FieldElement scalar)
Adds a scalar to this Matrix.

Parameters:
scalar -

addReplace

public void addReplace(Matrix anotherMatrix)
Adds another matrix to this Matrix.

Throws:
InvalidOperationException - if the matrices have different sizes

subtractReplace

public void subtractReplace(FieldElement scalar)
Subtracts a scalar from this Matrix.

Parameters:
scalar -

subtractReplace

public void subtractReplace(Matrix anotherMatrix)
Subtracts another Matrix from this.

Throws:
InvalidOperationException - if the matrices have different sizes

and

public Matrix and(Matrix anotherMatrix)
Returns the logical AND of this Matrix with another. Elements of the result are 1 where both matrices are non-zero, and zero elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of 1's and 0's
Throws:
InvalidOperationException - if the matrices have different sizes

or

public Matrix or(Matrix anotherMatrix)
Returns the logical OR of this Matrix with another. Elements of the result are 1 where both matrices are non-zero, and zero elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of 1's and 0's
Throws:
InvalidOperationException - if the matrices have different sizes

not

public Matrix not()
Returns the logical negation of this Matrix. Elements of the result are 1 where the matrix is zero, and one elsewhere.

Returns:
Matrix of 1's and 0's

lt

public Matrix lt(Matrix anotherMatrix)
Returns a Matrix containing ones where this Matrix's elements are less than those of another Matrices, and zeros elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of ones and zeros
Throws:
InvalidOperationException - if the matrices have different sizes

lt

public Matrix lt(FieldElement scalar)
Returns a Matrix containing ones where this Matrix's elements are less than a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Matrix of ones and zeros

le

public Matrix le(Matrix anotherMatrix)
Returns a Matrix containing ones where this Matrix's elements are less than or equal to those of another Matrices, and zeros elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of ones and zeros
Throws:
InvalidOperationException - if the matrices have different sizes

le

public Matrix le(FieldElement scalar)
Returns a Matrix containing ones where this Matrix's elements are less than or equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Matrix of ones and zeros

gt

public Matrix gt(Matrix anotherMatrix)
Returns a Matrix containing ones where this Matrix's elements are greater than those of another Matrices, and zeros elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of ones and zeros
Throws:
InvalidOperationException - if the matrices have different sizes

gt

public Matrix gt(FieldElement scalar)
Returns a Matrix containing ones where this Matrix's elements are greater than a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Matrix of ones and zeros

ge

public Matrix ge(Matrix anotherMatrix)
Returns a Matrix containing ones where this Matrix's elements are greater than or equal to those of another Matrices, and zeros elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of ones and zeros
Throws:
InvalidOperationException - if the matrices have different sizes

ge

public Matrix ge(FieldElement scalar)
Returns a Matrix containing ones where this Matrix's elements are greater than or equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Matrix of ones and zeros

eq

public Matrix eq(Matrix anotherMatrix)
Returns a Matrix containing ones where this Matrix's elements are equal to those of another Matrices, and zeros elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of ones and zeros
Throws:
InvalidOperationException - if the matrices have different sizes

eq

public Matrix eq(FieldElement scalar)
Returns a Matrix containing ones where this Matrix's elements are equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Matrix of ones and zeros

ne

public Matrix ne(Matrix anotherMatrix)
Returns a Matrix containing ones where this Matrix's elements are not equal to those of another Matrices, and zeros elsewhere.

Parameters:
anotherMatrix -
Returns:
Matrix of ones and zeros
Throws:
InvalidOperationException - if the matrices have different sizes

ne

public Matrix ne(FieldElement scalar)
Returns a Matrix containing ones where this Matrix's elements are not equal to a scalar, and zeros elsewhere.

Parameters:
scalar -
Returns:
Matrix of ones and zeros

applyReplace

public void applyReplace(MonadicOperator fun)
Sets this Matrix to the result of applying a specified function to every element of this Matrix. New functions can be applied to a Matrix by subclassing the abstract MonadicOperator class.

Parameters:
fun - the function to apply
Returns:
result of applying fun to this Matrix

apply

public Matrix apply(MonadicOperator fun)
Returns the result of applying a specified function to every element of this Matrix. New functions can be applied to a Matrix by subclassing the abstract MonadicOperator class.

Parameters:
fun - the function to apply
Returns:
result of applying fun to this Matrix

applyReplace

public void applyReplace(Matrix anotherMatrix,
                         DyadicOperator fun)
Sets this Matrix to the result of applying a specified function to elements of this Matrix and another's. New functions can be applied to a Matrix by subclassing the abstract DyadicOperator class.

Parameters:
anotherMatrix -
fun - the function to apply
Returns:
result of applying fun to the two Matrices

apply

public Matrix apply(Matrix anotherMatrix,
                    DyadicOperator fun)
Returns the result of applying a specified function to the elements of this Matrix and another. New functions can be applied to a Matrix by subclassing the abstract DyadicOperator class.

Parameters:
anotherMatrix -
fun - the function to apply
Returns:
result of applying fun to the two Matrices

applyReplace

public void applyReplace(FieldElement scalar,
                         DyadicOperator fun)
Sets this Matrix to the result of applying a specified function to elements of this Matrix and a scalar. New functions can be applied to a Matrix by subclassing the abstract DyadicOperator class.

Parameters:
scalar -
fun - the function to apply
Returns:
result of applying fun to this Matrix and the scalar

apply

public Matrix apply(FieldElement scalar,
                    DyadicOperator fun)
Returns the result of applying a specified function to the elements of a this Matrix a scalar. New functions can be applied to a Matrix by subclassing the abstract DyadicOperator class.

Parameters:
scalar -
fun - the function to apply
Returns:
result of applying fun to the Matrix and scalar

arrayMultiply

public Matrix arrayMultiply(Matrix anotherMatrix)
                     throws InvalidOperationException
Returns the element-wise product of this Matrix and another.

Parameters:
anotherMatrix -
Returns:
this .* anotherMatrix
Throws:
InvalidOperationException - if the matrices have different sizes

setAll

public void setAll(FieldElement newEntry)
Sets all entries to a FieldElement.

Parameters:
newEntry - the FieldElement

sum

public FieldElement sum()
Computes the sum over all elements of this Matrix.

Returns:
the sum

mean

public FieldElement mean()
Computes the mean over all elements of this Matrix.

Returns:
the mean

min

public FieldElement min()
Computes the smallest value of any element in this Matrix.

Returns:
the smallest value

max

public FieldElement max()
Computes the largest value of any element in this Matrix.

Returns:
the largest value

sumRows

public Vector sumRows()
Computes the sum over the rows of this matrix.

Returns:
the sum

sumCols

public Vector sumCols()
Computes the sum over the columns of this matrix.

Returns:
the sum

meanRows

public Vector meanRows()
Computes the mean over the rows of this Matrix.

Returns:
the mean

meanCols

public Vector meanCols()
Computes the mean over the columns of this Matrix.

Returns:
the mean

toVector

public Vector toVector()
                throws InvalidOperationException
Returns a new 1xN Vector made from the N elements of this Matrix. Matrix should have 1 row.

Returns:
the Matrix
Throws:
InvalidOperationException - if number of rows not equal to one