SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal
Class TridiagonalMatrix

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.diagonal.TridiagonalMatrix
All Implemented Interfaces:
DeepCopyable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Matrix, MatrixAccessor, MatrixRing, Densifiable, MatrixDimension

public class TridiagonalMatrix
extends MatrixStorageImpl<T>

This class represents a matrix with non-zero entries only on the super, main and sub diagonals.

See Also:
Wikipedia: Tridiagonal matrix

Constructor Summary
TridiagonalMatrix(double[][] data)
          Construct a tridiagonal matrix from a 2D double[][] array.
TridiagonalMatrix(int dim)
          Construct a tridiagonal matrix of dimension dim * dim.
TridiagonalMatrix(TridiagonalMatrix that)
          Copy constructor performing a deep copy.
 
Method Summary
 TridiagonalMatrix add(TridiagonalMatrix that)
           
 TridiagonalMatrix 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 diagonal()
          Get a copy of the diagonal of the matrix.
 boolean equals(java.lang.Object obj)
          Check if two matrices (of different implementations) are equal.
 DenseVector getColumn(int col)
          Get a specified column as a vector.
 DenseVector getRow(int row)
          Get a specified row as a vector.
 int hashCode()
           
 TridiagonalMatrix minus(TridiagonalMatrix that)
           
 TridiagonalMatrix ONE()
          Get an identity matrix that has the same dimension as this matrix.
 TridiagonalMatrix scaled(double scalar)
          scalar * this
 DenseVector subDiagonal()
          Get a copy of the sub-diagonal of the matrix.
 DenseVector superDiagonal()
          Get a copy of the super-diagonal of the matrix.
 TridiagonalMatrix 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.
 java.lang.String toString()
           
 TridiagonalMatrix 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
get, getMatrixData, 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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TridiagonalMatrix

public TridiagonalMatrix(int dim)
Construct a tridiagonal matrix of dimension dim * dim.

Parameters:
dim - dimension of the matrix

TridiagonalMatrix

public TridiagonalMatrix(double[][] data)
Construct a tridiagonal matrix from a 2D double[][] array.
  1. the first row is the super diagonal; (dim - 1) entries
  2. the second row is the main diagonal; (dim) entries
  3. the third row is the sub diagonal; (dim - 1) entries

For example,

         new double[][]{
              {2, 5, 8, 11},
              {1, 4, 7, 10, 13},
              {3, 6, 9, 12}
          }
 
is
              | 1  2  0  0   0  |
              | 3  4  5  0   0  |
              | 0  6  7  8   0  |
              | 0  0  9  10  11 |
              | 0  0  0  12  13 |
 

We allow 'null' input when a diagonal is 0s.

For example,

         new double[][]{
              {2, 5, 8, 11},
              {1, 4, 7, 10, 13},
              null
          }
 
is
              | 1  2  0  0   0  |
              | 0  4  5  0   0  |
              | 0  0  7  8   0  |
              | 0  0  0  10  11 |
              | 0  0  0  0   13 |
 

The following is not allowed because the dimension cannot be determined.

         new double[][]{
              null,
              null,
              null
          }
 

Parameters:
data - the 2D array input

TridiagonalMatrix

public TridiagonalMatrix(TridiagonalMatrix that)
Copy constructor performing a deep copy.

Parameters:
that - a TridiagonalMatrix
Method Detail

deepCopy

public TridiagonalMatrix 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.

Returns:
an independent copy of Matrix instance

add

public TridiagonalMatrix add(TridiagonalMatrix that)

minus

public TridiagonalMatrix minus(TridiagonalMatrix that)

ZERO

public TridiagonalMatrix 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.

Returns:
0

ONE

public TridiagonalMatrix 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).

Returns:
an identity matrix

t

public TridiagonalMatrix 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<TridiagonalMatrix>
Returns:
the transpose of this matrix, of the same type

scaled

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

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

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

diagonal

public DenseVector diagonal()
Get a copy of the diagonal of the matrix.

Returns:
a copy of the diagonal

superDiagonal

public DenseVector superDiagonal()
Get a copy of the super-diagonal of the matrix.

Returns:
a copy of the super-diagonal

subDiagonal

public DenseVector subDiagonal()
Get a copy of the sub-diagonal of the matrix.

Returns:
a copy of the sub-diagonal

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<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>
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<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>
Parameters:
col - the column index
Returns:
a vector A[, col]

toString

public java.lang.String toString()
Overrides:
toString in class MatrixMathImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>

equals

public boolean equals(java.lang.Object obj)
Description copied from class: MatrixStorageImpl
Check if two matrices (of different implementations) are equal. They are equal iff two matrices have the same values, entry-by-entry.

Overrides:
equals in class MatrixStorageImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>
Returns:
true iff two matrices have the same values, entry-by-entry

hashCode

public int hashCode()
Overrides:
hashCode in class MatrixStorageImpl<T extends com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.diagonal.DiagonalDataMatrix>

SuanShu, a Java numerical and statistical library

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