SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.triangle
Class UpperTriangularMatrix

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixMathImpl<T>
      extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.MatrixStorageImpl<T>
          extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.triangle.TriangularMatrix<UpperTriangularMatrix>
              extended by com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.triangle.UpperTriangularMatrix
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, Densifiable, MatrixDimension

public class UpperTriangularMatrix
extends TriangularMatrix<UpperTriangularMatrix>
implements Densifiable

This class implements upper triangular matrix, which has 0 entries whenever row index > column index.

This implementation saves about half of the data cost (except for the diagonal) when compared to the implementation of DenseMatrix. Users may use this class instead of DenseMatrix when they know that the matrix is upper triangular.

Upper triangular matrices are always square.


Constructor Summary
UpperTriangularMatrix(double[][] data)
          Construct an upper triangular matrix from a 2D double[][] array.
UpperTriangularMatrix(int dim)
          Construct an upper triangular matrix of dimension dim * dim.
UpperTriangularMatrix(Matrix U)
          Copy constructor performing a deep copy of a matrix.
UpperTriangularMatrix(UpperTriangularMatrix U)
          Copy constructor performing a deep copy.
 
Method Summary
 UpperTriangularMatrix add(UpperTriangularMatrix that)
           
 UpperTriangularMatrix 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.
 DenseVector getColumn(int col)
          Get a specified column as a vector.
 DenseVector getRow(int row)
          Get a specified row as a vector.
 UpperTriangularMatrix minus(UpperTriangularMatrix that)
           
 UpperTriangularMatrix multiply(UpperTriangularMatrix that)
           
 UpperTriangularMatrix ONE()
          Get an identity matrix that has the same dimension as this matrix.
 UpperTriangularMatrix scaled(double scalar)
          scalar * this
 LowerTriangularMatrix t()
          t(A)
 DenseMatrix toDense()
          Densify a matrix, i.e., convert a matrix implementation to a standard dense matrix, DenseMatrix.
 UpperTriangularMatrix 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.dense.triangle.TriangularMatrix
dim, getMatrixData
 
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, getSample, minus, multiply, 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

UpperTriangularMatrix

public UpperTriangularMatrix(int dim)
Construct an upper triangular matrix of dimension dim * dim. Space for the matrix data is not yet allocated.

Parameters:
dim - dimension; for a square matrix, it is the number of rows or columns

UpperTriangularMatrix

public UpperTriangularMatrix(double[][] data)
Construct an upper triangular matrix from a 2D double[][] array.

Parameters:
data - the 2D array input
Throws:
java.lang.IllegalArgumentException - if data is not upper triangular

UpperTriangularMatrix

public UpperTriangularMatrix(Matrix U)
Copy constructor performing a deep copy of a matrix.

Parameters:
U - a matrix
Throws:
java.lang.IllegalArgumentException - when A is not square, or when A is not upper triangular

UpperTriangularMatrix

public UpperTriangularMatrix(UpperTriangularMatrix U)
Copy constructor performing a deep copy.

Parameters:
U - an UpperTriangularMatrix
Method Detail

deepCopy

public UpperTriangularMatrix 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

getRow

public DenseVector 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<UpperTriangularMatrix>
Parameters:
row - the row index
Returns:
a vector A[row, ]

getColumn

public DenseVector 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<UpperTriangularMatrix>
Parameters:
col - the column index
Returns:
a vector A[, col]

add

public UpperTriangularMatrix add(UpperTriangularMatrix that)

minus

public UpperTriangularMatrix minus(UpperTriangularMatrix that)

multiply

public UpperTriangularMatrix multiply(UpperTriangularMatrix that)

ZERO

public UpperTriangularMatrix 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 UpperTriangularMatrix 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 LowerTriangularMatrix t()
t(A)

Specified by:
t in interface MatrixRing
Overrides:
t in class MatrixMathImpl<UpperTriangularMatrix>
Returns:
a lower triangular matrix which is the transpose of this

scaled

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

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

SuanShu, a Java numerical and statistical library

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