The Encog Project

org.encog.matrix
Class Matrix

java.lang.Object
  extended by org.encog.matrix.Matrix
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, EncogPersistedObject

public class Matrix
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable, EncogPersistedObject

This class implements a mathematical matrix. Matrix math is very important to neural network processing. Many of the neural network classes make use of the matrix classes in this package.

See Also:
Serialized Form

Constructor Summary
Matrix(boolean[][] sourceMatrix)
          Construct a bipolar matrix from an array of booleans.
Matrix(double[][] sourceMatrix)
          Create a matrix from an array of doubles.
Matrix(int rows, int cols)
          Create a blank array with the specified number of rows and columns.
 
Method Summary
 void add(int row, int col, double value)
          Add a value to one cell in the matrix.
 void add(Matrix matrix)
          Add the specified matrix to this matrix.
 void clear()
          Set all rows and columns to zero.
 Matrix clone()
          Create a copy of the matrix.
static Matrix createColumnMatrix(double[] input)
          Turn an array of doubles into a column matrix.
 Persistor createPersistor()
          Create a Persistor for this object.
static Matrix createRowMatrix(double[] input)
          Turn an array of doubles into a row matrix.
 boolean equals(Matrix matrix)
          Check to see if this matrix equals another, using default precision.
 boolean equals(Matrix matrix, int precision)
          Compare to matrixes with the specified level of precision.
 int fromPackedArray(java.lang.Double[] array, int index)
          Create a matrix from a packed array.
 double get(int row, int col)
          Read the specified cell in the matrix.
 Matrix getCol(int col)
          Read one entire column from the matrix as a sub-matrix.
 int getCols()
          Get the columns in the matrix.
 java.lang.String getDescription()
           
 java.lang.String getName()
           
 Matrix getRow(int row)
          Get the specified row as a sub-matrix.
 int getRows()
          Get the number of rows in the matrix.
 int hashCode()
          Compute a hash code for this matrix.
 boolean isVector()
          Determine if the matrix is a vector.
 boolean isZero()
          Return true if every value in the matrix is zero.
 void multiply(double value)
          Multiply every value in the matrix by the specified value.
 void set(double value)
          Set every value in the matrix to the specified value.
 void set(int row, int col, double value)
          Set an individual cell in the matrix to the specified value.
 void setDescription(java.lang.String description)
          Set the description for this object.
 void setName(java.lang.String name)
          Set the name of this object.
 int size()
          Get the size of the array.
 double sum()
          Sum all of the values in the matrix.
 java.lang.Double[] toPackedArray()
          Convert the matrix into a packed array.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Matrix

public Matrix(boolean[][] sourceMatrix)
Construct a bipolar matrix from an array of booleans.

Parameters:
sourceMatrix - The booleans to create the matrix from.

Matrix

public Matrix(double[][] sourceMatrix)
Create a matrix from an array of doubles.

Parameters:
sourceMatrix - An array of doubles.

Matrix

public Matrix(int rows,
              int cols)
Create a blank array with the specified number of rows and columns.

Parameters:
rows - How many rows in the matrix.
cols - How many columns in the matrix.
Method Detail

createColumnMatrix

public static Matrix createColumnMatrix(double[] input)
Turn an array of doubles into a column matrix.

Parameters:
input - A double array.
Returns:
A column matrix.

createRowMatrix

public static Matrix createRowMatrix(double[] input)
Turn an array of doubles into a row matrix.

Parameters:
input - A double array.
Returns:
A row matrix.

add

public void add(int row,
                int col,
                double value)
Add a value to one cell in the matrix.

Parameters:
row - The row to add to.
col - The column to add to.
value - The value to add to the matrix.

add

public void add(Matrix matrix)
Add the specified matrix to this matrix. This will modify the matrix to hold the result of the addition.

Parameters:
matrix - The matrix to add.

clear

public void clear()
Set all rows and columns to zero.


clone

public Matrix clone()
Create a copy of the matrix.

Specified by:
clone in interface EncogPersistedObject
Overrides:
clone in class java.lang.Object
Returns:
A colne of the matrix.

createPersistor

public Persistor createPersistor()
Create a Persistor for this object.

Specified by:
createPersistor in interface EncogPersistedObject
Returns:
The new persistor.

equals

public boolean equals(Matrix matrix)
Check to see if this matrix equals another, using default precision.

Parameters:
matrix - The other matrix to compare.
Returns:
True if the two matrixes are equal.

equals

public boolean equals(Matrix matrix,
                      int precision)
Compare to matrixes with the specified level of precision.

Parameters:
matrix - The other matrix to compare to.
precision - How much precision to use.
Returns:
True if the two matrixes are equal.

fromPackedArray

public int fromPackedArray(java.lang.Double[] array,
                           int index)
Create a matrix from a packed array.

Parameters:
array - The packed array.
index - Where to start in the packed array.
Returns:
The new index after this matrix has been read.

get

public double get(int row,
                  int col)
Read the specified cell in the matrix.

Parameters:
row - The row to read.
col - The column to read.
Returns:
The value at the specified row and column.

getCol

public Matrix getCol(int col)
Read one entire column from the matrix as a sub-matrix.

Parameters:
col - The column to read.
Returns:
The column as a sub-matrix.

getCols

public int getCols()
Get the columns in the matrix.

Returns:
The number of columns in the matrix.

getDescription

public java.lang.String getDescription()
Specified by:
getDescription in interface EncogPersistedObject
Returns:
the description

getName

public java.lang.String getName()
Specified by:
getName in interface EncogPersistedObject
Returns:
the name

getRow

public Matrix getRow(int row)
Get the specified row as a sub-matrix.

Parameters:
row - The row to get.
Returns:
A matrix.

getRows

public int getRows()
Get the number of rows in the matrix.

Returns:
The number of rows in the matrix.

hashCode

public int hashCode()
Compute a hash code for this matrix.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code.

isVector

public boolean isVector()
Determine if the matrix is a vector. A vector is has either a single number of rows or columns.

Returns:
True if this matrix is a vector.

isZero

public boolean isZero()
Return true if every value in the matrix is zero.

Returns:
True if the matrix is all zeros.

multiply

public void multiply(double value)
Multiply every value in the matrix by the specified value.

Parameters:
value - The value to multiply the matrix by.

set

public void set(double value)
Set every value in the matrix to the specified value.

Parameters:
value - The value to set the matrix to.

set

public void set(int row,
                int col,
                double value)
Set an individual cell in the matrix to the specified value.

Parameters:
row - The row to set.
col - The column to set.
value - The value to be set.

setDescription

public void setDescription(java.lang.String description)
Set the description for this object.

Specified by:
setDescription in interface EncogPersistedObject
Parameters:
description - the description to set

setName

public void setName(java.lang.String name)
Description copied from interface: EncogPersistedObject
Set the name of this object.

Specified by:
setName in interface EncogPersistedObject
Parameters:
name - the name to set

size

public int size()
Get the size of the array. This is the number of elements it would take to store the matrix as a packed array.

Returns:
The size of the matrix.

sum

public double sum()
Sum all of the values in the matrix.

Returns:
The sum of the matrix.

toPackedArray

public java.lang.Double[] toPackedArray()
Convert the matrix into a packed array.

Returns:
The matrix as a packed array.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
Convert the matrix to a string.

The Encog Project