gov.sandia.cognition.math.matrix
Class AbstractMatrix

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
All Implemented Interfaces:
Matrix, Vectorizable, Ring<Matrix>, CloneableSerializable, Serializable, Cloneable, Iterable<MatrixEntry>
Direct Known Subclasses:
AbstractMTJMatrix

@CodeReviews(reviews={@CodeReview(reviewer="Kevin R. Dixon",date="2008-02-26",changesNeeded=false,comments={"Minor changes to formatting and documentation for equals().","Otherwise, looks good."}),@CodeReview(reviewer="Jonathan McClain",date="2006-05-16",changesNeeded=true,comments="A few minor changes.",response=@CodeReviewResponse(respondent="Justin Basilico",date="2006-05-16",moreChangesNeeded=false,comments="Added a line to the class documentation that says that setElement is used extensively in the abstract implementation."))})
public abstract class AbstractMatrix
extends AbstractRing<Matrix>
implements Matrix

Abstract implementation of some low-hanging functions in the Matrix interface. It contains default implementations of these functions that make extensive use of the setElement method, which it assumes to be a fast lookup function.

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

Constructor Summary
AbstractMatrix()
          Creates a new instance of AbstractMatrix.
 
Method Summary
 void assertSameDimensions(Matrix other)
          Throws a DimensionalityMismatchException if dimensions between this and otherMatrix aren't the same
 boolean checkMultiplicationDimensions(Matrix postMultiplicationMatrix)
          Checks to see if the dimensions are appropriate for: this.times( postMultiplicationMatrix )
 boolean checkSameDimensions(Matrix otherMatrix)
          Checks to see if the dimensions are the same between this and otherMatrix
 Matrix dotDivide(Matrix other)
          Element-wise division of this by other.
 void dotDivideEquals(Matrix other)
          Inline element-wise division of this by other.
 boolean equals(Matrix other, double effectiveZero)
          Determines if two RingType objects are effectively equal
 boolean equals(Object other)
          Determines if two RingType objects are equal
protected  void getColumnInto(int columnIndex, Vector destinationVector)
          Internal function that writes the column onto the destinationVector.
protected  void getRowInto(int rowIndex, Vector destinationVector)
          Internal function that writes the row onto the destinationVector.
 int hashCode()
           
 boolean isSymmetric()
          Determines if the matrix is symmetric.
 boolean isZero(double effectiveZero)
          Determines if this ring is equal to zero using the element-wise effective zero value.
 Matrix pseudoInverse()
          Computes the effective pseudo-inverse of this, using a rather expensive procedure (SVD)
 int rank()
          Computes the rank of this, which is the number of linearly independent rows and columns in this.
 void setColumn(int columnIndex, Vector columnVector)
          Sets the specified column from the given columnVector
 void setRow(int rowIndex, Vector rowVector)
          Sets the specified row from the given rowVector
 void setSubMatrix(int minRow, int minColumn, Matrix submatrix)
          Sets the submatrix inside of the Matrix, specified by the zero-based indices.
 Vector sumOfColumns()
          Returns a new vector containing the sum across the columns.
 Vector sumOfRows()
          Returns a new vector containing the sum across the rows.
 double[][] toArray()
          Converts this matrix to a new array of array of doubles, in the same order as they are in the matrix.
 double trace()
          Computes the trace of this, which is the sum of the eigenvalues and if equivalent to the sum of the diagonal element (which is probably the most interesting result in all of algebra!!)
 
Methods inherited from class gov.sandia.cognition.math.AbstractRing
clone, dotTimes, isZero, minus, negative, negativeEquals, plus, scale, scaledMinus, scaledMinusEquals, scaledPlus, zero
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface gov.sandia.cognition.math.matrix.Matrix
clone, convertFromVector, convertToVector, getColumn, getElement, getNumColumns, getNumRows, getRow, getSubMatrix, identity, inverse, isSparse, isSquare, isSymmetric, logDeterminant, normFrobenius, pseudoInverse, rank, setElement, solve, solve, times, times, toString, toString, transpose
 
Methods inherited from interface java.lang.Iterable
iterator
 
Methods inherited from interface gov.sandia.cognition.math.Ring
dotTimes, dotTimesEquals, isZero, minus, minusEquals, negative, negativeEquals, plus, plusEquals, scale, scaledMinus, scaledMinusEquals, scaledPlus, scaledPlusEquals, scaleEquals, zero
 

Constructor Detail

AbstractMatrix

public AbstractMatrix()
Creates a new instance of AbstractMatrix.

Method Detail

equals

public boolean equals(Object other)
Description copied from interface: Ring
Determines if two RingType objects are equal

Specified by:
equals in interface Ring<Matrix>
Overrides:
equals in class Object
Parameters:
other - RingType to compare against this
Returns:
True if the two objects are equal, false otherwise

equals

public boolean equals(Matrix other,
                      double effectiveZero)
Description copied from interface: Ring
Determines if two RingType objects are effectively equal

Specified by:
equals in interface Ring<Matrix>
Parameters:
other - RingType to compare against this
effectiveZero - tolerance threshold for element-wise equality
Returns:
True if the two objects are equal, false otherwise

hashCode

public int hashCode()
Overrides:
hashCode in class Object

isSymmetric

public boolean isSymmetric()
Description copied from interface: Matrix
Determines if the matrix is symmetric.

Specified by:
isSymmetric in interface Matrix
Returns:
true if the matrix is symmetric, false otherwise

checkSameDimensions

public boolean checkSameDimensions(Matrix otherMatrix)
Description copied from interface: Matrix
Checks to see if the dimensions are the same between this and otherMatrix

Specified by:
checkSameDimensions in interface Matrix
Parameters:
otherMatrix - matrix against which to check
Returns:
true if this.getNumRows() == otherMatrix.getNumRows() and this.getNumColumns() == otherMatrix.getNumColumns()

assertSameDimensions

public void assertSameDimensions(Matrix other)
Description copied from interface: Matrix
Throws a DimensionalityMismatchException if dimensions between this and otherMatrix aren't the same

Specified by:
assertSameDimensions in interface Matrix
Parameters:
other - Matrix dimensions to compare to this

checkMultiplicationDimensions

public boolean checkMultiplicationDimensions(Matrix postMultiplicationMatrix)
Description copied from interface: Matrix
Checks to see if the dimensions are appropriate for: this.times( postMultiplicationMatrix )

Specified by:
checkMultiplicationDimensions in interface Matrix
Parameters:
postMultiplicationMatrix - matrix by which this is to be multiplied
Returns:
true if this.getNumColumns() == postMultlicationMatrix.getNumColumns(), false otherwise

dotDivide

public Matrix dotDivide(Matrix other)
Description copied from interface: Matrix
Element-wise division of this by other. Note that if other has zero elements the result will contain NaN values.

Specified by:
dotDivide in interface Matrix
Parameters:
other - The other ring whose elements will divide into this one.
Returns:
A new ring of equal size whose elements are equal to the corresponding element in this divided by the element in other.

dotDivideEquals

public void dotDivideEquals(Matrix other)
Description copied from interface: Matrix
Inline element-wise division of this by other. Note that if other has zero elements this will contain NaN values.

Specified by:
dotDivideEquals in interface Matrix
Parameters:
other - The other vector whose elements will divide into this one.

trace

public double trace()
Description copied from interface: Matrix
Computes the trace of this, which is the sum of the eigenvalues and if equivalent to the sum of the diagonal element (which is probably the most interesting result in all of algebra!!)

Specified by:
trace in interface Matrix
Returns:
trace of this

setSubMatrix

public void setSubMatrix(int minRow,
                         int minColumn,
                         Matrix submatrix)
Description copied from interface: Matrix
Sets the submatrix inside of the Matrix, specified by the zero-based indices.

Specified by:
setSubMatrix in interface Matrix
Parameters:
minRow - Zero-based index into the rows of the Matrix, must be less than or equal to maxRow
minColumn - Zero-based index into the rows of the Matrix, must be less than or equal to maxColumn
submatrix - Matrix containing the values to set at the specified indices

rank

public int rank()
Description copied from interface: Matrix
Computes the rank of this, which is the number of linearly independent rows and columns in this. Rank is typically based on the SVD, which is a fairly computationally expensive procedure and should be used carefully

Specified by:
rank in interface Matrix
Returns:
rank of this, equivalent to the number of linearly independent rows and columns in this

pseudoInverse

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

Specified by:
pseudoInverse in interface Matrix
Returns:
full singular-value pseudo-inverse of this

setColumn

public void setColumn(int columnIndex,
                      Vector columnVector)
Description copied from interface: Matrix
Sets the specified column from the given columnVector

Specified by:
setColumn in interface Matrix
Parameters:
columnIndex - zero-based index into the matrix
columnVector - vector to replace in this, must have same number of elements as this has rows

setRow

public void setRow(int rowIndex,
                   Vector rowVector)
Description copied from interface: Matrix
Sets the specified row from the given rowVector

Specified by:
setRow in interface Matrix
Parameters:
rowIndex - zero-based index into the matrix
rowVector - vector to replace in this, must have the same number of elements as this has columns

getColumnInto

protected void getColumnInto(int columnIndex,
                             Vector destinationVector)
Internal function that writes the column onto the destinationVector. Gets the specified column from the zero-based index and returns a vector that corresponds to that column.

Parameters:
columnIndex - zero-based index into the matrix
destinationVector - Vector with elements equal to the number of rows in this

getRowInto

protected void getRowInto(int rowIndex,
                          Vector destinationVector)
Internal function that writes the row onto the destinationVector. Gets the specified row from the zero-based index and returns a vector that corresponds to that column

Parameters:
rowIndex - zero-based index into the matrix
destinationVector - Vector with elements equal to the number of columns in this

sumOfRows

public Vector sumOfRows()
Description copied from interface: Matrix
Returns a new vector containing the sum across the rows.

Specified by:
sumOfRows in interface Matrix
Returns:
A new vector containing the sum of the rows. Its dimensionality is equal to the number of columns.

sumOfColumns

public Vector sumOfColumns()
Description copied from interface: Matrix
Returns a new vector containing the sum across the columns.

Specified by:
sumOfColumns in interface Matrix
Returns:
A new vector containing the sum of the columns. Its dimensionality is equal to the number of rows.

isZero

public boolean isZero(double effectiveZero)
Description copied from interface: Ring
Determines if this ring is equal to zero using the element-wise effective zero value.

Specified by:
isZero in interface Ring<Matrix>
Parameters:
effectiveZero - Tolerance threshold for element-wise equality
Returns:
True if all of the elements of this ring are effectively zero.

toArray

public double[][] toArray()
Description copied from interface: Matrix
Converts this matrix to a new array of array of doubles, in the same order as they are in the matrix. The returned will be safe in that no references are maintained by this class.

Specified by:
toArray in interface Matrix
Returns:
This matrix as a dense array. The length of the first layer of the array will be equal to the number of rows and the second layer will be equal to the number of columns.