gov.sandia.cognition.math.matrix.mtj
Class DenseMatrix

java.lang.Object
  extended by gov.sandia.cognition.util.AbstractCloneableSerializable
      extended by gov.sandia.cognition.math.AbstractRing<Matrix>
          extended by gov.sandia.cognition.math.matrix.AbstractMatrix
              extended by gov.sandia.cognition.math.matrix.mtj.AbstractMTJMatrix
                  extended by gov.sandia.cognition.math.matrix.mtj.DenseMatrix
All Implemented Interfaces:
Matrix, Vectorizable, Ring<Matrix>, CloneableSerializable, Serializable, Cloneable, Iterable<MatrixEntry>

@CodeReview(reviewer="Jonathan McClain",
            date="2006-05-19",
            changesNeeded=true,
            comments="Comments indicated with / / / ",
            response=@CodeReviewResponse(respondent="Kevin R. Dixon",date="2006-05-22",moreChangesNeeded=false,comments="Fixed comments for writeObject and readObject"))
@PublicationReference(author="Bjorn-Ove Heimsund",
                      title="Matrix Toolkits for Java DenseMatrix",
                      type=WebPage,
                      year=2006,
                      url="http://ressim.berlios.de/doc/no/uib/cipr/matrix/DenseMatrix.html",
                      notes="This class wraps the DenseMatrix class from Heimsund\'s MTJ package")
public class DenseMatrix
extends AbstractMTJMatrix
implements Serializable

Matrix that represents all its entries using a fixed-size storage scheme, based on MTJ's DenseMatrix storage class. Fast iteration, access, and arithmetic, but storage size scale with the size of the number of rows and columns.

To create a new DenseMatrix, please use the DenseMatrixFactoryMTJ static calls, or the DefaultRingFactory class. This is there to provide a layer of abstraction and make it easier for people to drop in new matrix packages

Since:
1.0
Author:
Kevin R. Dixon
See Also:
Serialized Form

Constructor Summary
protected DenseMatrix(no.uib.cipr.matrix.DenseMatrix internalMatrix)
          Creates a new instance of DenseMatrix
protected DenseMatrix(DenseMatrix matrix)
          Creates a new instance of DenseMatrix
protected DenseMatrix(int numRows, int numColumns)
          Creates a new instance of DenseMatrix
protected DenseMatrix(Matrix matrix)
          Creates a new instance of DenseMatrix
protected DenseMatrix(MatrixReader reader)
          Creates a new instance of DenseMatrix
 
Method Summary
 DenseMatrix clone()
          This makes public the clone method on the Object class and removes the exception that it throws.
 void convertFromVector(DenseVector parameters)
          Incorporates the parameters in the given vector back into the object.
 void convertFromVector(Vector parameters)
          uploads a matrix from a column-stacked vector of parameters, so that v(k) = A(i,j) = A( k%M, k/M )
 DenseVector convertToVector()
          Creates a column-stacked version of this, so that v(k) = A(i,j) = v(j*M+i)
protected  double[] getArray()
          Gets the underlying double array from the MTJ matrix
 DenseVector getColumn(int columnIndex)
          Gets the specified column from the zero-based index and returns a vector that corresponds to that column.
 DenseVector getRow(int rowIndex)
          Gets the specified row from the zero-based index and returns a vector that corresponds to that column
 DenseMatrix getSubMatrix(int minRow, int maxRow, int minColumn, int maxColumn)
          Gets the embedded submatrix inside of the Matrix, specified by the inclusive, zero-based indices such that the result matrix will have size (maxRow-minRow+1) x (maxColum-minCcolumn+1)
 boolean isSparse()
          Returns true if this matrix has a potentially sparse underlying structure.
 DenseMatrix pseudoInverse(double effectiveZero)
          Computes the effective pseudo-inverse of this, using a rather expensive procedure (SVD)
protected  void solveInto(DenseMatrix B, DenseMatrix X)
          Solve for "X" in the equation: this*X = B
protected  void solveInto(DenseVector b, DenseVector x)
          Solve for "x" in the equation: this*x = b
 DenseMatrix times(AbstractMTJMatrix matrix)
          Matrix multiplication of this and matrix, operates like the "*" operator in Matlab
 DenseVector times(AbstractMTJVector vector)
          Returns the column vector from the equation return = this * vector
 String toString()
          Writes the DenseMatrix out as a matrix of values (no indices are printed)
 String toString(NumberFormat format)
          Converts the vector to a String, using the given formatter.
 DenseMatrix transpose()
          Returns the transpose of this
 
Methods inherited from class gov.sandia.cognition.math.matrix.mtj.AbstractMTJMatrix
dotTimesEquals, dotTimesEquals, equals, getElement, getInternalMatrix, getNumColumns, getNumRows, getSubMatrixInto, identity, inverse, isSquare, isSymmetric, iterator, logDeterminant, minusEquals, minusEquals, normFrobenius, plusEquals, plusEquals, rank, scaledMinusEquals, scaledPlusEquals, scaledPlusEquals, scaleEquals, setElement, setInternalMatrix, solve, solve, solve, solve, times, times, timesInto, timesInto, transposeInto, zero
 
Methods inherited from class gov.sandia.cognition.math.matrix.AbstractMatrix
assertSameDimensions, checkMultiplicationDimensions, checkSameDimensions, dotDivide, dotDivideEquals, equals, equals, getColumnInto, getRowInto, hashCode, isSymmetric, isZero, pseudoInverse, rank, setColumn, setRow, setSubMatrix, sumOfColumns, sumOfRows, toArray, trace
 
Methods inherited from class gov.sandia.cognition.math.AbstractRing
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface gov.sandia.cognition.math.Ring
dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus
 

Constructor Detail

DenseMatrix

protected DenseMatrix(int numRows,
                      int numColumns)
Creates a new instance of DenseMatrix

Parameters:
numRows - number of rows in the matrix
numColumns - number of columns in the matrix

DenseMatrix

protected DenseMatrix(no.uib.cipr.matrix.DenseMatrix internalMatrix)
Creates a new instance of DenseMatrix

Parameters:
internalMatrix - Internal MTJ-based matrix

DenseMatrix

protected DenseMatrix(Matrix matrix)
Creates a new instance of DenseMatrix

Parameters:
matrix - Matrix from which to pull data, will not be modified

DenseMatrix

protected DenseMatrix(DenseMatrix matrix)
Creates a new instance of DenseMatrix

Parameters:
matrix - DenseMatrix from which to pull data, will not be modified

DenseMatrix

protected DenseMatrix(MatrixReader reader)
               throws IOException
Creates a new instance of DenseMatrix

Parameters:
reader - takes in information from a java stream
Throws:
IOException - if the stream is invalid
Method Detail

clone

public DenseMatrix clone()
Description copied from class: AbstractCloneableSerializable
This makes public the clone method on the Object class and removes the exception that it throws. Its default behavior is to automatically create a clone of the exact type of object that the clone is called on and to copy all primitives but to keep all references, which means it is a shallow copy. Extensions of this class may want to override this method (but call super.clone() to implement a "smart copy". That is, to target the most common use case for creating a copy of the object. Because of the default behavior being a shallow copy, extending classes only need to handle fields that need to have a deeper copy (or those that need to be reset). Some of the methods in ObjectUtil may be helpful in implementing a custom clone method. Note: The contract of this method is that you must use super.clone() as the basis for your implementation.

Specified by:
clone in interface Matrix
Specified by:
clone in interface Vectorizable
Specified by:
clone in interface Ring<Matrix>
Specified by:
clone in interface CloneableSerializable
Overrides:
clone in class AbstractMTJMatrix
Returns:
A clone of this object.

times

public DenseMatrix times(AbstractMTJMatrix matrix)
Description copied from class: AbstractMTJMatrix
Matrix multiplication of this and matrix, operates like the "*" operator in Matlab

Specified by:
times in class AbstractMTJMatrix
Parameters:
matrix - this.getNumColumns()==matrix.getNumRows()
Returns:
Matrix multiplication of this and matrix, will this.getNumRows() rows and matrix.getNumColumns() columns

times

public DenseVector times(AbstractMTJVector vector)
Description copied from class: AbstractMTJMatrix
Returns the column vector from the equation return = this * vector

Specified by:
times in class AbstractMTJMatrix
Parameters:
vector - Vector by which to post-multiply this, must have the same number of rows as this
Returns:
Vector with the same dimensionality as the number of rows as this and vector

solveInto

protected void solveInto(DenseMatrix B,
                         DenseMatrix X)
Solve for "X" in the equation: this*X = B

Parameters:
B - must satisfy this.getNumRows() == B.numRows()
X - must satisfy this.getNumColumns() == X.getNumRows()

solveInto

protected void solveInto(DenseVector b,
                         DenseVector x)
Solve for "x" in the equation: this*x = b

Parameters:
b - must satisfy this.getNumRows() == b.getDimensionality()
x - must satisfy this.getNumColumns() == x.getDimensionality()

pseudoInverse

public DenseMatrix pseudoInverse(double effectiveZero)
Description copied from interface: Matrix
Computes the effective pseudo-inverse of this, using a rather expensive procedure (SVD)

Specified by:
pseudoInverse in interface Matrix
Parameters:
effectiveZero - effective zero to pass along to the SVD
Returns:
effective pseudo-inverse of this

getSubMatrix

public DenseMatrix getSubMatrix(int minRow,
                                int maxRow,
                                int minColumn,
                                int maxColumn)
Description copied from interface: Matrix
Gets the embedded submatrix inside of the Matrix, specified by the inclusive, zero-based indices such that the result matrix will have size (maxRow-minRow+1) x (maxColum-minCcolumn+1)

Specified by:
getSubMatrix in interface Matrix
Parameters:
minRow - Zero-based index into the rows of the Matrix, must be less than or equal to maxRow
maxRow - Zero-based index into the rows of the Matrix, must be greater than or equal to minRow
minColumn - Zero-based index into the rows of the Matrix, must be less than or equal to maxColumn
maxColumn - Zero-based index into the rows of the Matrix, must be greater than or equal to minColumn
Returns:
the Matrix of dimension (maxRow-minRow+1)x(maxColumn-minColumn+1)

transpose

public DenseMatrix transpose()
Description copied from interface: Matrix
Returns the transpose of this

Specified by:
transpose in interface Matrix
Returns:
Matrix whose elements are equivalent to: this.getElement(i, j) == this.transpose().getElement(j, i) for any valid i, j.

toString

public String toString()
Writes the DenseMatrix out as a matrix of values (no indices are printed)

Specified by:
toString in interface Matrix
Overrides:
toString in class Object
Returns:
String representing the DenseMatrix

toString

public String toString(NumberFormat format)
Description copied from interface: Matrix
Converts the vector to a String, using the given formatter.

Specified by:
toString in interface Matrix
Parameters:
format - The number format to use.
Returns:
The String representation of the Matrix.

getColumn

public DenseVector getColumn(int columnIndex)
Description copied from interface: Matrix
Gets the specified column from the zero-based index and returns a vector that corresponds to that column.

Specified by:
getColumn in interface Matrix
Parameters:
columnIndex - zero-based index into the matrix
Returns:
Vector with elements equal to the number of rows in this

getRow

public DenseVector getRow(int rowIndex)
Description copied from interface: Matrix
Gets the specified row from the zero-based index and returns a vector that corresponds to that column

Specified by:
getRow in interface Matrix
Parameters:
rowIndex - zero-based index into the matrix
Returns:
Vector with elements equal to the number of columns in this

isSparse

public boolean isSparse()
Description copied from interface: Matrix
Returns true if this matrix has a potentially sparse underlying structure. This can indicate that it is faster to only process the non-zero elements rather than to do dense operations on it. Of course, even with a sparse structure, there may be no zero elements or conversely even with a non-sparse (dense) structure there may be many zero elements.

Specified by:
isSparse in interface Matrix
Returns:
True if the matrix has a potentially sparse structure. Otherwise, false.

getArray

protected double[] getArray()
Gets the underlying double array from the MTJ matrix

Returns:
double array from the underlying MTJ matrix

convertToVector

public DenseVector convertToVector()
Description copied from interface: Matrix
Creates a column-stacked version of this, so that v(k) = A(i,j) = v(j*M+i)

Specified by:
convertToVector in interface Matrix
Specified by:
convertToVector in interface Vectorizable
Overrides:
convertToVector in class AbstractMTJMatrix
Returns:
column-stacked Vector representing this

convertFromVector

public void convertFromVector(Vector parameters)
Description copied from interface: Matrix
uploads a matrix from a column-stacked vector of parameters, so that v(k) = A(i,j) = A( k%M, k/M )

Specified by:
convertFromVector in interface Matrix
Specified by:
convertFromVector in interface Vectorizable
Overrides:
convertFromVector in class AbstractMTJMatrix
Parameters:
parameters - column-stacked version of this

convertFromVector

public void convertFromVector(DenseVector parameters)
Incorporates the parameters in the given vector back into the object.

Parameters:
parameters - The parameters to incorporate.