# Class DenseMatrix

java.lang.Object
dev.nm.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix
All Implemented Interfaces:
Matrix, MatrixAccess, MatrixRing, MatrixTable, Densifiable, AbelianGroup<Matrix>, Monoid<Matrix>, Ring<Matrix>, Table, DeepCopyable
Direct Known Subclasses:
CongruentMatrix, CorrelationMatrix, GoldfeldQuandtTrotter, Inverse, Jacobian, KroneckerProduct, MAT, MatrixRootByDiagonalization, MatthewsDavies, OuterProduct, PositiveDefiniteMatrixByPositiveDiagonal, Pow, PseudoInverse, ReturnsMatrix, SampleCovariance, SimilarMatrix, SymmetricKronecker

public class DenseMatrix extends Object implements Matrix, Densifiable
This class implements the standard, dense, double based matrix representation. There are two additional methods to change the matrix content:
• ## Constructor Summary

Constructors
Modifier
Constructor
Description

DenseMatrix()
for serialization

DenseMatrix(double[][] data)
Constructs a matrix from a 2D double[][] array.

DenseMatrix(double[] data, int nRows, int nCols)
Constructs a matrix from a 1D double[].

DenseMatrix(int nRows, int nCols)
Constructs a 0 matrix of dimension nRows * nCols.

DenseMatrix(Matrix A)
Converts any matrix to the standard matrix representation.

DenseMatrix(DenseMatrix A)
Copy constructor performing a deep copy.
protected
DenseMatrix(DenseMatrix A, boolean copy)
This constructor is useful for subclass to pass in computed value.

DenseMatrix(Vector v)
Constructs a column matrix from a vector.
• ## Method Summary

Modifier and Type
Method
Description
Matrix
add(Matrix that)
this + that
DenseMatrix
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.
Vector
getColumn(int j, int beginRow, int endRow)
Gets a sub-column of the j-th column, from beginRow row to endRow row, inclusively.
Vector
getRow(int i)
Get the specified row in the matrix as a vector.
Vector
getRow(int i, int beginCol, int endCol)
Gets a sub-row of the i-th row, from beginCol column to endCol column, inclusively.
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.
DenseMatrix
ONE()
Get an identity matrix that has the same dimension as this matrix.
Matrix
opposite()
Get the opposite of this matrix.
DenseMatrix
scaled(double c)
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.
void
setColumn(int j, Vector v)
Changes the matrix column values to a vector value.
void
setRow(int i, Vector v)
Changes the matrix row values to a vector value.
DenseMatrix
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()

DenseMatrix
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

• ### DenseMatrix

public DenseMatrix()
for serialization
• ### DenseMatrix

public DenseMatrix(int nRows, int nCols)
Constructs a 0 matrix of dimension nRows * nCols.
Parameters:
nRows - the number of rows
nCols - the number of columns
• ### DenseMatrix

public DenseMatrix(double[][] data)
Constructs a matrix from a 2D double[][] array.
Parameters:
data - a 2D array input
Throws:
IllegalArgumentException - when data is a jagged array
• ### DenseMatrix

public DenseMatrix(double[] data, int nRows, int nCols)
Constructs a matrix from a 1D double[]. The array is a concatenation of the matrix rows. A sample usage is to convert a vector to a matrix. For example, to construct a column vector, we do

DenseMatrix V = new DenseMatrix(v.toArray(), v.length, 1);

To construct a row vector, we do

DenseMatrix V = new DenseMatrix(v.toArray(), 1, v.length);

Parameters:
data - the 1D array input
nRows - the number or rows
nCols - the number of columns
Throws:
IllegalArgumentException - when the length of data does not equal to nRows * nCols
• ### DenseMatrix

public DenseMatrix(Vector v)
Constructs a column matrix from a vector.
Parameters:
v - a vector
• ### DenseMatrix

public DenseMatrix(Matrix A)
Converts any matrix to the standard matrix representation. This method is the same as toDense() if A is Densifiable.
Parameters:
A - a matrix
• ### DenseMatrix

public DenseMatrix(DenseMatrix A)
Copy constructor performing a deep copy.
Parameters:
A - a DenseMatrix
• ### DenseMatrix

protected DenseMatrix(DenseMatrix A, boolean copy)
This constructor is useful for subclass to pass in computed value.
Parameters:
A - a DenseMatrix
copy - true if a deep copy of A is needed
• ## Method Details

• ### deepCopy

public DenseMatrix 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.
Specified by:
deepCopy in interface DeepCopyable
Specified by:
deepCopy in interface Matrix
Returns:
an independent (deep) copy of the instance
• ### 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
• ### 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
• ### setRow

public void setRow(int i, Vector v)
Changes the matrix row values to a vector value.
Parameters:
i - the i-th row to change
v - the values to change the row to
• ### setColumn

public void setColumn(int j, Vector v)
Changes the matrix column values to a vector value.
Parameters:
j - the j-th column to change
v - the values to change the column to
• ### 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, ]
• ### getRow

public Vector getRow(int i, int beginCol, int endCol)
Gets a sub-row of the i-th row, from beginCol column to endCol column, inclusively.
Parameters:
i - the row to extract
beginCol - the beginning column of the sub-row
endCol - the ending column of the sub-row
Returns:
a sub-row
• ### 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]
• ### getColumn

public Vector getColumn(int j, int beginRow, int endRow)
Gets a sub-column of the j-th column, from beginRow row to endRow row, inclusively.
Parameters:
j - the column to extract
beginRow - the beginning row of the sub-column
endRow - the ending row of the sub-column
Returns:
a sub-column

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
• ### 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
• ### ZERO

public DenseMatrix ZERO()
Description copied from interface: MatrixRing
Get a zero matrix that has the same dimension as this matrix.
Specified by:
ZERO in interface AbelianGroup<Matrix>
Specified by:
ZERO in interface MatrixRing
Returns:
the 0 matrix
• ### ONE

public DenseMatrix 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 MatrixRing
Specified by:
ONE in interface Monoid<Matrix>
Returns:
an identity matrix
• ### t

public DenseMatrix t()
Description copied from interface: MatrixRing
Get the transpose of this matrix. This is the involution on the matrix ring.
Specified by:
t in interface MatrixRing
Returns:
the transpose of this matrix
• ### 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 DenseMatrix scaled(double c)
Description copied from interface: Matrix
Scale this matrix, A, by a constant.
Specified by:
scaled in interface Matrix
Parameters:
c - a double
Returns:
cA
• ### opposite

public Matrix opposite()
Description copied from interface: MatrixRing
Get the opposite of this matrix.
Specified by:
opposite in interface AbelianGroup<Matrix>
Specified by:
opposite in interface MatrixRing
Returns:
-this
• ### 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