SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense
Class DenseMatrix

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl<T>
      extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixStorageImpl<DenseMatrix>
          extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.DenseMatrix
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, Densifiable, MatrixDimension
Direct Known Subclasses:
CovarianceMatrix, GoldfeldQuandtTrotter, Gradient, Inverse, Jacobian, JordanExchange, KroneckerProduct, MatthewsDavies, Pow, PseudoInverse

public class DenseMatrix
extends MatrixStorageImpl<DenseMatrix>
implements Densifiable

This class implements the standard double dense matrix representation.


Constructor Summary
DenseMatrix(DenseMatrix A)
          Copy constructor performing a deep copy.
DenseMatrix(double[][] data)
          Construct a matrix from a 2D double[][] array.
DenseMatrix(double[] data, int nRows, int nCols)
          Construct a matrix from a 1D double[] array.
DenseMatrix(int nRows, int nCols)
          Construct a matrix of dimension nRows * nCols.
DenseMatrix(Matrix A)
          Copy constructor performing a deep copy.
DenseMatrix(Vector v)
          Construct a column matrix from a vector.
 
Method Summary
 DenseMatrix add(DenseMatrix that)
           
 DenseMatrix deepCopy()
          The implementation can return an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
 Vector getColumn(int col)
          Get a specified column as a vector.
 Vector getColumn(int col, int beginRow, int endRow)
          Return a sub-column of the col-th column, from beginRow row to endRow row, inclusively.
protected  DenseData getMatrixData()
           
 Vector getRow(int row)
          Get a specified row as a vector.
 Vector getRow(int row, int beginCol, int endCol)
          Return a sub-row of the row-th row, from beginCol column to endCol column, inclusively.
protected  DenseMatrix getSample()
           
 DenseMatrix minus(DenseMatrix that)
           
 DenseMatrix multiply(DenseMatrix that)
           
 Vector multiply(Vector v)
          Compute A %*% v, where v is a vector.
 DenseMatrix ONE()
          Get an identity matrix that has the same dimension as this matrix.
protected  void overwrite(Matrix A)
          This method is provided so that extended classes can overwrite itself efficiently.
 DenseMatrix scaled(double scalar)
          scalar * this
 DenseMatrix t()
          t(this) Compute the transpose of this matrix.
 DenseMatrix toDense()
          Densify a matrix, i.e., convert a matrix implementation to a standard dense matrix, DenseMatrix.
 DenseMatrix ZERO()
          The additive element 0 in the group, such that for all elements a in the group, the equation 0 + a = a + 0 = a holds.
 
Methods inherited from class com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixStorageImpl
equals, get, hashCode, set, setMatrixData
 
Methods inherited from class com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl
add, call, minus, multiply, nCols, nRows, opposite, setColumn, setColumn, setRow, setRow, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DenseMatrix

public DenseMatrix(int nRows,
                   int nCols)
Construct a matrix of dimension nRows * nCols. Space for the matrix data is not yet allocated.

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

DenseMatrix

public DenseMatrix(double[][] data)
Construct a matrix from a 2D double[][] array.

Parameters:
data - the 2D array input
Throws:
java.lang.IllegalArgumentException - when data is a jagged array

DenseMatrix

public DenseMatrix(double[] data,
                   int nRows,
                   int nCols)
Construct a matrix from a 1D double[] array. The array is a concatenation of the matrix rows.

This can be used to convert a vector to a matrix.

E.g., to form a column vector, we do

DenseMatrix V = new DenseMatrix(v.toArray(), v.length, 1)

E.g., to form a row vector, we do

DenseMatrix V = new DenseMatrix(v.toArray(), 1, v.length)

Parameters:
data - the 1D array input
nRows - number or rows
nCols - number of columns
Throws:
java.lang.IllegalArgumentException - when the length of data is not equal to nRows * nCols

DenseMatrix

public DenseMatrix(Vector v)
Construct a column matrix from a vector.

Parameters:
v - a vector
Throws:
java.lang.IllegalArgumentException - when row < 1, or when row > number of rows

DenseMatrix

public DenseMatrix(Matrix A)
Copy constructor performing a deep copy. It does so by first converting the input to the standard matrix representation.

This method is in general very inefficient especially for large sparse matrices. Use toDense() whenever available.

Parameters:
A - a matrix

DenseMatrix

public DenseMatrix(DenseMatrix A)
Copy constructor performing a deep copy.

Parameters:
A - a DenseMatrix
Method Detail

getMatrixData

protected DenseData getMatrixData()
Overrides:
getMatrixData in class MatrixStorageImpl<DenseMatrix>

overwrite

protected void overwrite(Matrix A)
This method is provided so that extended classes can overwrite itself efficiently. This method is more efficient than calling MatrixStorageImpl.set(int, int, double) repeatedly.

Parameters:
A - a matrix

deepCopy

public DenseMatrix deepCopy()
Description copied from interface: Matrix
The implementation can return an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.

Override the return type.

Specified by:
deepCopy in interface DeepCopyable
Specified by:
deepCopy in interface Matrix
Returns:
an independent copy of Matrix instance

toDense

public DenseMatrix toDense()
Description copied from interface: Densifiable
Densify a matrix, i.e., convert a matrix implementation to a standard dense matrix, DenseMatrix.

This is equivalent to

new DenseMatrix(Matrix A)

Individual matrix implementation may optimize the conversion by taking advantage of having access to the class' private members. In other words, toDense() is in general more efficient than explicit copy construction.

As an example, for some long matrix computations, we may not be sure about the return type. We can always cast it to the standard matrix. That is,

DenseMatrix A = B.multiply(C).add(D).minus(E).toDense();

This statement always works regardless of the types of B, C, D, and E.

Specified by:
toDense in interface Densifiable
Returns:
a matrix representation in DenseMatrix

getSample

protected DenseMatrix getSample()
Overrides:
getSample in class MatrixMathImpl<DenseMatrix>

getRow

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

Specified by:
getRow in interface MatrixAccessor
Overrides:
getRow in class MatrixMathImpl<DenseMatrix>
Parameters:
row - the row index
Returns:
a vector A[row, ]

getRow

public Vector getRow(int row,
                     int beginCol,
                     int endCol)
Return a sub-row of the row-th row, from beginCol column to endCol column, inclusively.

Parameters:
row - the row to extract
beginCol - the beginning column of the sub-row
endCol - the ending column of the sub-row
Returns:
a sub-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
Overrides:
getColumn in class MatrixMathImpl<DenseMatrix>
Parameters:
col - the column index
Returns:
a vector A[, col]

getColumn

public Vector getColumn(int col,
                        int beginRow,
                        int endRow)
Return a sub-column of the col-th column, from beginRow row to endRow row, inclusively.

Parameters:
col - the column to extract
beginRow - the beginning row of the sub-column
endRow - the ending row of the sub-column
Returns:
a sub-column

add

public DenseMatrix add(DenseMatrix that)

minus

public DenseMatrix minus(DenseMatrix that)

multiply

public DenseMatrix multiply(DenseMatrix that)

ZERO

public DenseMatrix ZERO()
Description copied from interface: AbelianGroup
The additive element 0 in the group, such that for all elements a in the group, the equation
0 + a = a + 0 = a
holds.

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

ONE

public DenseMatrix 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 DenseMatrix 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
Overrides:
t in class MatrixMathImpl<DenseMatrix>
Returns:
the transpose of this matrix, of the same type

multiply

public Vector multiply(Vector v)
Compute A %*% v, where v is a vector.

Specified by:
multiply in interface Matrix
Overrides:
multiply in class MatrixMathImpl<DenseMatrix>
Parameters:
v - a vector
Returns:
A %*% v

scaled

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

Specified by:
scaled in interface Matrix
Overrides:
scaled in class MatrixMathImpl<DenseMatrix>
Parameters:
scalar - a double
Returns:
scalar * this

SuanShu, a Java numerical and statistical library

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