SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.operation
Class SubMatrixRef

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.operation.SubMatrixRef
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, MatrixDimension

public class SubMatrixRef
extends java.lang.Object
implements Matrix

This class creates a 'reference' to a sub-part of a large matrix without copying it. Often, we only need to work on a part of a matrix. We do not want to make copies of a matrix for performance reason.

Example applications include printing a sub-matrix.

The reference sub-matrix is immutable.


Constructor Summary
SubMatrixRef(Matrix A)
          Construct a sub-matrix reference.
SubMatrixRef(Matrix A, int rowFrom, int rowTo, int colFrom, int colTo)
          Construct a sub-matrix reference.
 
Method Summary
 Matrix add(Matrix that)
          this + that
 SubMatrixRef deepCopy()
          Return 'this' as this Matrix is immutable.
 double get(int row, int col)
          Get the matrix entry at [row, col].
 Vector getColumn(int col)
          Get a specified column as a vector.
 Vector getRow(int row)
          Get a specified row as a vector.
 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 ONE()
          Get an identity matrix that has the same dimension as this matrix.
 Matrix opposite()
          Get the opposite of this matrix.
 Matrix scaled(double scalar)
          scalar * this
 void set(int row, int col, double value)
          Deprecated. SubMatrixRef is immutable
 Matrix t()
          t(this) Compute the transpose of this matrix.
 java.lang.String toString()
           
 Matrix ZERO()
          Get a zero matrix that has the same dimension as this matrix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SubMatrixRef

public SubMatrixRef(Matrix A,
                    int rowFrom,
                    int rowTo,
                    int colFrom,
                    int colTo)
Construct a sub-matrix reference.

Parameters:
A - the matrix to be taken a sub-snapshot from without copying
rowFrom - the beginning row index
rowTo - the ending row index
colFrom - the beginning column index
colTo - the ending column index
Throws:
java.lang.IndexOutOfBoundsException - if rowFrom, rowTo, colFrom, colTo are invalid

SubMatrixRef

public SubMatrixRef(Matrix A)
Construct a sub-matrix reference. This references the whole matrix without copying it.

Parameters:
A - the matrix to be referenced
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

get

public double get(int row,
                  int col)
Description copied from interface: MatrixAccessor
Get the matrix entry at [row, col].

Specified by:
get in interface MatrixAccessor
Parameters:
row - the row index
col - the column index
Returns:
A[row, col]

getRow

public Vector getRow(int row)
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, ]

getColumn

public Vector getColumn(int col)
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]

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

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

ZERO

public Matrix ZERO()
Description copied from interface: MatrixRing
Get a zero matrix that has the same dimension as this matrix.

Specified by:
ZERO in interface AbelianGroup<Matrix>
Specified by:
ZERO in interface MatrixRing
Returns:
the 0 matrix

ONE

public Matrix ONE()
Description copied from interface: MatrixRing
Get an identity matrix that has the same dimension as this matrix.

For a non-square matrix, it zeros out the rows (columns) with index > nCols (nRows).

Specified by:
ONE in interface Monoid<Matrix>
Specified by:
ONE in interface MatrixRing
Returns:
an identity matrix

toString

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

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

deepCopy

public SubMatrixRef deepCopy()
Return 'this' as this Matrix is immutable. To produce a mutable copy, a deep copy of the referenced matrix is needed.

Specified by:
deepCopy in interface DeepCopyable
Specified by:
deepCopy in interface Matrix
Returns:
'this'

set

@Deprecated
public void set(int row,
                           int col,
                           double value)
         throws MatrixAccessException
Deprecated. SubMatrixRef is immutable

Description copied from interface: MatrixAccessor
Set the matrix entry at [row, col] to value.

This is the only method that may change the entries of a matrix.

Specified by:
set in interface MatrixAccessor
Parameters:
row - the row index
col - the column index
value - the value to set A[row, col] to
Throws:
MatrixAccessException - if row or col is out of range

SuanShu, a Java numerical and statistical library

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