# SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype.dense.diagonal

## Class DiagonalMatrix

• java.lang.Object
• com.numericalmethod.suanshu.algebra.linear.matrix.doubles.matrixtype.dense.diagonal.DiagonalMatrix
• ### Constructor Summary

Constructors
Constructor and Description
DiagonalMatrix(DiagonalMatrix that)
Copy constructor.
DiagonalMatrix(double[] data)
Constructs a diagonal matrix from a double[].
DiagonalMatrix(double[] data, int nRows, int nCols)
DiagonalMatrix(int dim)
Constructs a 0 diagonal matrix of dimension dim * dim.
DiagonalMatrix(int nRows, int nCols)
• ### Method Summary

All Methods
Modifier and Type Method and Description
Matrix add(Matrix that)
Computes the sum of two diagonal matrices.
DiagonalMatrix 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)
Computes the difference between two diagonal matrices.
Matrix multiply(Matrix that)
Computes the product of two diagonal matrices.
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.
DiagonalMatrix ONE()
Get an identity matrix that has the same dimension as this matrix.
DiagonalMatrix opposite()
For each a in G, there exists an element b in G such that a + b = b + a = 0.
DiagonalMatrix 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.
DiagonalMatrix t()
The transpose of a diagonal matrix is the same as itself.
DenseMatrix toDense()
Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix, DenseMatrix.
String toString()
DiagonalMatrix 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 java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Constructor Detail

• #### DiagonalMatrix

public DiagonalMatrix(double[] data)
Constructs a diagonal matrix from a double[]. For example,

new double[][]{
{1, 2, 3, 4, 5},
}

gives $\begin{bmatrix} 1 & 0 & 0 & 0 & 0\\ 0 & 2 & 0 & 0 & 0\\ 0 & 0 & 3 & 0 & 0\\ 0 & 0 & 0 & 4 & 0\\ 0 & 0 & 0 & 0 & 5 \end{bmatrix}$
Parameters:
data - the 1D array input
• #### DiagonalMatrix

public DiagonalMatrix(double[] data,
int nRows,
int nCols)
• #### DiagonalMatrix

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

public DiagonalMatrix(int nRows,
int nCols)
• #### DiagonalMatrix

public DiagonalMatrix(DiagonalMatrix that)
Copy constructor.
Parameters:
that - a diagonal matrix
• ### Method Detail

• #### deepCopy

public DiagonalMatrix 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
• #### 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

public Matrix add(Matrix that)
Computes the sum of two diagonal matrices.
Specified by:
add in interface MatrixRing
Specified by:
add in interface AbelianGroup<Matrix>
Parameters:
that - a diagonal matrix
Returns:
this + that
• #### minus

public Matrix minus(Matrix that)
Computes the difference between two diagonal matrices.

Specified by:
minus in interface MatrixRing
Specified by:
minus in interface AbelianGroup<Matrix>
Parameters:
that - a diagonal matrix
Returns:
this - that
• #### multiply

public Matrix multiply(Matrix that)
Computes the product of two diagonal matrices.
Specified by:
multiply in interface MatrixRing
Specified by:
multiply in interface Monoid<Matrix>
Parameters:
that - a diagonal matrix
Returns:
this * 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
• #### scaled

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

public DiagonalMatrix opposite()
Description copied from interface: AbelianGroup
For each a in G, there exists an element b in G such that a + b = b + a = 0. That is, it is the object such as
this.add(this.opposite()) == this.ZERO
Returns:
• #### t

public DiagonalMatrix t()
The transpose of a diagonal matrix is the same as itself.
Returns:
a copy
• #### ZERO

public DiagonalMatrix 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:
• #### ONE

public DiagonalMatrix 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
• #### toString

public String toString()
• #### 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
• #### 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]
• #### equals

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

public int hashCode()
Overrides:
hashCode in class Object