SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles
Class ImmutableMatrix

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

public class ImmutableMatrix
extends java.lang.Object
implements Matrix

A read-only view of a Matrix instance. The only constructor of this class takes a Matrix instance. This class keeps the reference to the instance, delegates all operations to the instance except for set(int, int, double) which will always result in an MatrixAccessException.

That means, a user can still modify the matrix via the reference of the original Matrix. If an independent copy is required, a copy of the instance can be passed into the constructor of this class, e.g.,

ImmutableMatrix immutable = new ImmutableMatrix(m.deepCopy());

Note that the returned values of all operations (e.g., add()) remain the same types as the original ones.

The usage of this class could be, e.g., the final member of a class, the return value of a method.


Constructor Summary
ImmutableMatrix(Matrix A)
          Construct a read-only version of matrix.
 
Method Summary
 Matrix add(Matrix that)
          this + that
 ImmutableMatrix deepCopy()
          Return 'this' as this Matrix is immutable.
 boolean equals(java.lang.Object obj)
           
 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.
 int hashCode()
           
 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)
          Set the matrix entry at [row, col] to value.
 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, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ImmutableMatrix

public ImmutableMatrix(Matrix A)
Construct a read-only version of matrix.

Note that changing the original matrix changes the "immutable" version as well.

Parameters:
A - a matrix
Method Detail

set

public void set(int row,
                int col,
                double value)
         throws MatrixAccessException
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

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)
           throws MatrixAccessException
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]
Throws:
MatrixAccessException - if row or col is out of range

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

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

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

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

toString

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

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

deepCopy

public ImmutableMatrix 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'

SuanShu, a Java numerical and statistical library

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