# Class TridiagonalMatrix

java.lang.Object
dev.nm.algebra.linear.matrix.doubles.matrixtype.dense.diagonal.TridiagonalMatrix
All Implemented Interfaces:
Matrix, MatrixAccess, MatrixRing, MatrixTable, Densifiable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Table, DeepCopyable

public class TridiagonalMatrix extends Object
A tri-diagonal matrix has non-zero entries only on the super, main and sub diagonals.
• ## Constructor Summary

Constructors
Constructor
Description
TridiagonalMatrix(double[][] data)
Constructs a tri-diagonal matrix from a 3-row 2D double[][] array such that: the first row is the super diagonal with (dim - 1) entries; the second row is the main diagonal with dim entries; the third row is the sub diagonal with (dim - 1) entries. For example,
TridiagonalMatrix(int dim)
Constructs a 0 tri-diagonal matrix of dimension dim * dim.
TridiagonalMatrix(Matrix A)
Casts a matrix to tridiagonal by copying the 3 diagonals (ignoring all other entries).
TridiagonalMatrix(TridiagonalMatrix that)
Copy constructor performing a deep copy.
• ## Method Summary

Modifier and Type
Method
Description
Matrix
add(Matrix that)
this + that
TridiagonalMatrix
deepCopy()
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
boolean
equals(Object obj)

double
get(int i, int j)
Get the matrix entry at [i,j].
Vector
getColumn(int j)
Get the specified column in the matrix as a vector.
DenseVector
getDiagonal()
Gets the main diagonal of the matrix.
Vector
getRow(int i)
Get the specified row in the matrix as a vector.
DenseVector
getSubDiagonal()
Gets the sub-diagonal of the matrix.
DenseVector
getSuperDiagonal()
Gets the super-diagonal of the matrix.
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()
Gets the number of columns.
int
nRows()
Gets the number of rows.
TridiagonalMatrix
ONE()
Get an identity matrix that has the same dimension as this matrix.
TridiagonalMatrix
opposite()
Get the opposite of this matrix.
TridiagonalMatrix
scaled(double scalar)
Scale this matrix, A, by a constant.
void
set(int i, int j, double value)
Set the matrix entry at [i,j] to a value.
TridiagonalMatrix
t()
Get the transpose of this matrix.
DenseMatrix
toDense()
Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix, DenseMatrix.
String
toString()

TridiagonalMatrix
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

### Methods inherited from interface dev.nm.algebra.linear.matrix.doubles.Matrix

toCSV
• ## Constructor Details

• ### TridiagonalMatrix

public TridiagonalMatrix(int dim)
Constructs a 0 tri-diagonal matrix of dimension dim * dim.
Parameters:
dim - the dimension of the matrix
• ### TridiagonalMatrix

public TridiagonalMatrix(double[][] data)
Constructs a tri-diagonal matrix from a 3-row 2D double[][] array such that:
1. the first row is the super diagonal with (dim - 1) entries;
2. the second row is the main diagonal with dim entries;
3. the third row is the sub diagonal with (dim - 1) entries.
For example,

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

gives $\begin{bmatrix} 1 invalid input: '&' 2 invalid input: '&' 0 invalid input: '&' 0 invalid input: '&' 0\\ 3 invalid input: '&' 4 invalid input: '&' 5 invalid input: '&' 0 invalid input: '&' 0\\ 0 invalid input: '&' 6 invalid input: '&' 7 invalid input: '&' 8 invalid input: '&' 0\\ 0 invalid input: '&' 0 invalid input: '&' 9 invalid input: '&' 10 invalid input: '&' 11\\ 0 invalid input: '&' 0 invalid input: '&' 0 invalid input: '&' 12 invalid input: '&' 13 \end{bmatrix}$ We allow null input when a diagonal is 0s. For example,

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

gives $\begin{bmatrix} 1 invalid input: '&' 2 invalid input: '&' 0 invalid input: '&' 0 invalid input: '&' 0\\ 0 invalid input: '&' 4 invalid input: '&' 5 invalid input: '&' 0 invalid input: '&' 0\\ 0 invalid input: '&' 0 invalid input: '&' 7 invalid input: '&' 8 invalid input: '&' 0\\ 0 invalid input: '&' 0 invalid input: '&' 0 invalid input: '&' 10 invalid input: '&' 11\\ 0 invalid input: '&' 0 invalid input: '&' 0 invalid input: '&' 0 invalid input: '&' 13 \end{bmatrix}$ 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(Matrix A)
Casts a matrix to tridiagonal by copying the 3 diagonals (ignoring all other entries).
Parameters:
A - the matrix
• ### TridiagonalMatrix

public TridiagonalMatrix(TridiagonalMatrix that)
Copy constructor performing a deep copy.
Parameters:
that - a tri-diagonal matrix
• ## Method Details

• ### deepCopy

public TridiagonalMatrix deepCopy()
Description copied from interface: DeepCopyable
The implementation returns an instance created from this by the copy constructor of the class, or just this if the instance itself is immutable.
Returns:
an independent (deep) copy of the instance

Description copied from interface: MatrixRing
this + that
Specified by:
add in interface AbelianGroup<Matrix>
Specified by:
add in interface MatrixRing
Parameters:
that - a 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 - a matrix
Returns:
the difference between this and that
• ### scaled

public TridiagonalMatrix scaled(double scalar)
Description copied from interface: Matrix
Scale this matrix, A, by a constant.
Parameters:
scalar - a double
Returns:
cA
• ### opposite

public TridiagonalMatrix opposite()
Description copied from interface: MatrixRing
Get the opposite of this matrix.
Returns:
-this
• ### t

public TridiagonalMatrix t()
Description copied from interface: MatrixRing
Get the transpose of this matrix. This is the involution on the matrix ring.
Returns:
the transpose of this matrix
• ### ZERO

public TridiagonalMatrix ZERO()
Description copied from interface: MatrixRing
Get a zero matrix that has the same dimension as this matrix.
Returns:
the 0 matrix
• ### 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
• ### toDense

public DenseMatrix toDense()
Description copied from interface: Densifiable
Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix, DenseMatrix.
Specified by:
toDense in interface Densifiable
Returns:
a matrix representation in DenseMatrix
• ### getDiagonal

public DenseVector getDiagonal()
Gets the main diagonal of the matrix.
Returns:
the main diagonal
• ### getSuperDiagonal

public DenseVector getSuperDiagonal()
Gets the super-diagonal of the matrix.
Returns:
the super-diagonal
• ### getSubDiagonal

public DenseVector getSubDiagonal()
Gets the sub-diagonal of the matrix.
Returns:
the sub-diagonal
• ### nRows

public int nRows()
Description copied from interface: Table
Gets the number of rows. Rows count from 1.
Specified by:
nRows in interface Table
Returns:
the number of rows
• ### nCols

public int nCols()
Description copied from interface: Table
Gets the number of columns. Columns count from 1.
Specified by:
nCols in interface Table
Returns:
the number of columns
• ### set

public void set(int i, int j, double value) throws MatrixAccessException
Description copied from interface: MatrixAccess
Set the matrix entry at [i,j] to a value. This is the only method that may change a matrix.
Specified by:
set in interface MatrixAccess
Parameters:
i - the row index
j - the column index
value - the value to set A[i,j] to
Throws:
MatrixAccessException - if i or j is out of range
• ### get

public double get(int i, int j) throws MatrixAccessException
Description copied from interface: MatrixAccess
Get the matrix entry at [i,j].
Specified by:
get in interface MatrixAccess
Parameters:
i - the row index
j - the column index
Returns:
A[i,j]
Throws:
MatrixAccessException - if i or j is out of range
• ### getRow

public Vector getRow(int i)
Description copied from interface: Matrix
Get the specified row in the matrix as a vector.
Specified by:
getRow in interface Matrix
Parameters:
i - the row index
Returns:
the vector A[i, ]
• ### getColumn

public Vector getColumn(int j)
Description copied from interface: Matrix
Get the specified column in the matrix as a vector.
Specified by:
getColumn in interface Matrix
Parameters:
j - the column index
Returns:
a vector A[, j]
• ### multiply

public Matrix multiply(Matrix that)
Description copied from interface: MatrixRing
this * that
Specified by:
multiply in interface MatrixRing
Specified by:
multiply in interface Monoid<Matrix>
Parameters:
that - a matrix
Returns:
the product ofthis 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:
Av, a vector
• ### toString

public String toString()
Overrides:
toString in class Object
• ### equals

public boolean equals(Object obj)
Overrides:
equals in class Object
• ### hashCode

public int hashCode()
Overrides:
hashCode in class Object