SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype
Class MatrixMathImpl<T extends Matrix>

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl<T>
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, MatrixDimension
Direct Known Subclasses:
GivensMatrix, MatrixStorageImpl, PermutationMatrix

public abstract class MatrixMathImpl<T extends Matrix>
extends java.lang.Object
implements Matrix

This is one implementation of Matrix. It provides default implementation for common methods used in the double based Matrix. It assumes no knowledge of how a Matrix subclass is implemented and the data structure. Subclasses can (and should) override these methods for performance whenever possible.


Constructor Summary
MatrixMathImpl(int nRows, int nCols)
          Construct a MatrixImpl using SimpleMatrixMathOperation.
MatrixMathImpl(int nRows, int nCols, MatrixMathOperation math)
          Construct a MatrixImpl, allowing overriding the default implementation.
 
Method Summary
 Matrix add(Matrix that)
          this + that
 T call(java.lang.String op, Matrix that)
           
 Vector getColumn(int col)
          Get a specified column as a vector.
 Vector getRow(int row)
          Get a specified row as a vector.
protected  T getSample()
           
 Matrix minus(Matrix that)
          this - that
 Matrix multiply(Matrix that)
          this %*% that
 Vector multiply(Vector v)
          Right multiply this matrix, A by a vector.
 int nCols()
          Get the number of columns.
 int nRows()
          Get the number of rows.
 Matrix opposite()
          Get the opposite of this matrix.
 Matrix scaled(double scalar)
          scalar * this
 void setColumn(int col, double... values)
          Set the column entries in the matrix, i.e., [*, column].
 void setColumn(int col, Vector v)
          Set the column entries in the matrix, i.e., [*, column].
 void setRow(int row, double... values)
          Set the row entries in the matrix, i.e., [row, *].
 void setRow(int row, Vector v)
          Set the row entries in the matrix, i.e., [row, *].
 Matrix t()
          t(this) Compute the transpose of this matrix.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.numericalmethod.suanshu.matrix.doubles.Matrix
deepCopy
 
Methods inherited from interface com.numericalmethod.suanshu.matrix.doubles.MatrixAccessor
get, set
 
Methods inherited from interface com.numericalmethod.suanshu.matrix.doubles.MatrixRing
ONE, ZERO
 

Constructor Detail

MatrixMathImpl

public MatrixMathImpl(int nRows,
                      int nCols,
                      MatrixMathOperation math)
Construct a MatrixImpl, allowing overriding the default implementation.

Parameters:
nRows - number of rows
nCols - number of columns
math - an implementation of matrix math operations

MatrixMathImpl

public MatrixMathImpl(int nRows,
                      int nCols)
Construct a MatrixImpl using SimpleMatrixMathOperation.

Parameters:
nRows - number of rows
nCols - number of columns
Method Detail

nRows

public int nRows()
Description copied from interface: MatrixDimension
Get the number of rows. Rows count from 1.

Specified by:
nRows in interface MatrixDimension
Returns:
the number of rows

nCols

public int nCols()
Description copied from interface: MatrixDimension
Get the number of columns. Columns count from 1.

Specified by:
nCols in interface MatrixDimension
Returns:
the number of columns

getSample

protected T getSample()

setRow

public void setRow(int row,
                   double... values)
Set the row entries in the matrix, i.e., [row, *].

Parameters:
row - the row index, counting from 1
values - the values to change the row entries to
Throws:
MatrixAccessException - if the number of values does not match the column size

setRow

public void setRow(int row,
                   Vector v)
Set the row entries in the matrix, i.e., [row, *].

Parameters:
row - the row index, counting from 1
v - the vector to change the row entries to
Throws:
MatrixAccessException - if the number of values does not match the column size

getRow

public Vector getRow(int row)
              throws MatrixAccessException
Description copied from interface: MatrixAccessor
Get a specified row as a vector.

Specified by:
getRow in interface MatrixAccessor
Parameters:
row - the row index
Returns:
a vector A[row, ]
Throws:
MatrixAccessException - when row < 1, or when row > number of rows

setColumn

public void setColumn(int col,
                      double... values)
Set the column entries in the matrix, i.e., [*, column].

Parameters:
col - the column index, counting from 1
values - the values to change the column entries to
Throws:
MatrixAccessException - if the number of values does not match the row size

setColumn

public void setColumn(int col,
                      Vector v)
Set the column entries in the matrix, i.e., [*, column].

Parameters:
col - the column index, counting from 1
v - the vector to change the column entries to
Throws:
MatrixAccessException - if the number of values does not match the row size

getColumn

public Vector getColumn(int col)
                 throws MatrixAccessException
Description copied from interface: MatrixAccessor
Get a specified column as a vector.

Specified by:
getColumn in interface MatrixAccessor
Parameters:
col - the column index
Returns:
a vector A[, col]
Throws:
MatrixAccessException - when col < 1, or when col > number of columns

call

public T call(java.lang.String op,
              Matrix that)
                      throws java.lang.NoSuchMethodException,
                             java.lang.IllegalAccessException
Throws:
java.lang.NoSuchMethodException
java.lang.IllegalAccessException

add

public Matrix add(Matrix that)
Description copied from interface: MatrixRing
this + that

Specified by:
add in interface AbelianGroup<Matrix>
Specified by:
add in interface MatrixRing
Parameters:
that - another matrix
Returns:
the sum of this and that

minus

public Matrix minus(Matrix that)
Description copied from interface: MatrixRing
this - that

Specified by:
minus in interface AbelianGroup<Matrix>
Specified by:
minus in interface MatrixRing
Parameters:
that - another matrix
Returns:
the difference between this and that

multiply

public Matrix multiply(Matrix that)
Description copied from interface: MatrixRing
this %*% that

Specified by:
multiply in interface Monoid<Matrix>
Specified by:
multiply in interface MatrixRing
Parameters:
that - another matrix
Returns:
the product of this and that

multiply

public Vector multiply(Vector v)
Description copied from interface: Matrix
Right multiply this matrix, A by a vector.

Specified by:
multiply in interface Matrix
Parameters:
v - a vector
Returns:
A %*% v

scaled

public Matrix scaled(double scalar)
Description copied from interface: Matrix
scalar * this

Specified by:
scaled in interface Matrix
Parameters:
scalar - a double
Returns:
scalar * this

t

public Matrix t()
Description copied from interface: MatrixRing
t(this)

Compute the transpose of this matrix. The original matrix does not change. The returned value is independent and can be modified anyhow.

This is the involution on the matrix ring.

Specified by:
t in interface MatrixRing
Returns:
the transpose of this matrix, of the same type

opposite

public Matrix opposite()
Description copied from interface: MatrixRing
Get the opposite of this matrix.

Specified by:
opposite in interface AbelianGroup<Matrix>
Specified by:
opposite in interface MatrixRing
Returns:
-this
See Also:
Wikipedia: Additive inverse

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.