Class DiagonalMatrix
- java.lang.Object
-
- dev.nm.algebra.linear.matrix.doubles.matrixtype.dense.diagonal.DiagonalMatrix
-
- All Implemented Interfaces:
Matrix,MatrixAccess,MatrixRing,MatrixTable,Densifiable,AbelianGroup<Matrix>,Monoid<Matrix>,Ring<Matrix>,Table,DeepCopyable
public class DiagonalMatrix extends Object
A diagonal matrix has non-zero entries only on the main diagonal.- See Also:
- Wikipedia: Diagonal matrix
-
-
Constructor Summary
Constructors Constructor Description DiagonalMatrix(double[] data)Constructs a diagonal matrix from adouble[].DiagonalMatrix(double[] data, int nRows, int nCols)DiagonalMatrix(int dim)Constructs a 0 diagonal matrix of dimension dim * dim.DiagonalMatrix(int nRows, int nCols)DiagonalMatrix(DiagonalMatrix that)Copy constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrixadd(Matrix that)Computes the sum of two diagonal matrices.DiagonalMatrixdeepCopy()The implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.booleanequals(Object obj)doubleget(int i, int j)Get the matrix entry at [i,j].VectorgetColumn(int j)Get the specified column in the matrix as a vector.DenseVectorgetDiagonal()Gets the main diagonal of the matrix.VectorgetRow(int i)Get the specified row in the matrix as a vector.DenseVectorgetSubDiagonal()Gets the sub-diagonal of the matrix.DenseVectorgetSuperDiagonal()Gets the super-diagonal of the matrix.inthashCode()Matrixminus(Matrix that)Computes the difference between two diagonal matrices.Matrixmultiply(Matrix that)Computes the product of two diagonal matrices.Vectormultiply(Vector v)Right multiply this matrix, A, by a vector.intnCols()Gets the number of columns.intnRows()Gets the number of rows.DiagonalMatrixONE()Get an identity matrix that has the same dimension as this matrix.DiagonalMatrixopposite()Get the opposite of this matrix.DiagonalMatrixscaled(double scalar)Scale this matrix, A, by a constant.voidset(int i, int j, double value)Set the matrix entry at [i,j] to a value.DiagonalMatrixt()The transpose of a diagonal matrix is the same as itself.DenseMatrixtoDense()Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix,DenseMatrix.StringtoString()DiagonalMatrixZERO()Get a zero matrix that has the same dimension as this matrix.
-
-
-
Constructor Detail
-
DiagonalMatrix
public DiagonalMatrix(double[] data)
Constructs a diagonal matrix from adouble[]. For example,
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} \]new double[][]{ {1, 2, 3, 4, 5}, }- 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:DeepCopyableThe implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.- Returns:
- an independent (deep) copy of the instance
-
nRows
public int nRows()
Description copied from interface:TableGets the number of rows. Rows count from 1.
-
nCols
public int nCols()
Description copied from interface:TableGets the number of columns. Columns count from 1.
-
add
public Matrix add(Matrix that)
Computes the sum of two diagonal matrices.- Specified by:
addin interfaceAbelianGroup<Matrix>- Specified by:
addin interfaceMatrixRing- Parameters:
that- a diagonal matrix- Returns:
this+that
-
minus
public Matrix minus(Matrix that)
Computes the difference between two diagonal matrices.- Specified by:
minusin interfaceAbelianGroup<Matrix>- Specified by:
minusin interfaceMatrixRing- Parameters:
that- a diagonal matrix- Returns:
this-that
-
multiply
public Matrix multiply(Matrix that)
Computes the product of two diagonal matrices.- Specified by:
multiplyin interfaceMatrixRing- Specified by:
multiplyin interfaceMonoid<Matrix>- Parameters:
that- a diagonal matrix- Returns:
this*that
-
multiply
public Vector multiply(Vector v)
Description copied from interface:MatrixRight multiply this matrix, A, by a vector.
-
scaled
public DiagonalMatrix scaled(double scalar)
Description copied from interface:MatrixScale this matrix, A, by a constant.- Parameters:
scalar- a double- Returns:
- cA
-
opposite
public DiagonalMatrix opposite()
Description copied from interface:MatrixRingGet the opposite of this matrix.- Returns:
- -this
- See Also:
- Wikipedia: Additive inverse
-
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:MatrixRingGet a zero matrix that has the same dimension as this matrix.- Returns:
- the 0 matrix
-
ONE
public DiagonalMatrix ONE()
Description copied from interface:MatrixRingGet 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:DensifiableDensify a matrix, i.e., convert a matrix implementation to the standard dense matrix,DenseMatrix.- Specified by:
toDensein interfaceDensifiable- 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 MatrixAccessExceptionDescription copied from interface:MatrixAccessSet the matrix entry at [i,j] to a value. This is the only method that may change a matrix.- Specified by:
setin interfaceMatrixAccess- Parameters:
i- the row indexj- the column indexvalue- 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 MatrixAccessExceptionDescription copied from interface:MatrixAccessGet the matrix entry at [i,j].- Specified by:
getin interfaceMatrixAccess- Parameters:
i- the row indexj- 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:MatrixGet the specified row in the matrix as a vector.
-
getColumn
public Vector getColumn(int j)
Description copied from interface:MatrixGet the specified column in the matrix as a vector.
-
-