Class DenseMatrix
-
- All Implemented Interfaces:
-
java.io.Serializable
public abstract class DenseMatrix extends BaseMatrix
Base class for dense matrix implementations, containing the core data structure and methods that directly manipulate the underlying dense matrix representation.
This class encapsulates all interactions with EJML's DMatrixRMaj and CommonOps_DDRM classes, providing a clean abstraction layer for dense matrix operations. It serves as the foundation for concrete dense matrix implementations.
The class provides:
- Basic matrix operations (get, set, dimensions)
- Encapsulated CommonOps_DDRM method calls
- Memory management for dense matrix data structures
- Utility methods for matrix manipulation
Dense matrices store all elements (including zeros) explicitly, making them suitable for matrices with a high percentage of non-zero elements or when dense linear algebra operations are required.
- Since:
1.0
QORE Lab, Imperial College London
-
-
Field Summary
Fields Modifier and Type Field Description protected DMatrixRMajdata
-
Constructor Summary
Constructors Constructor Description DenseMatrix(int numRows, int numCols)Constructs a dense matrix with specified dimensions. DenseMatrix(DMatrixRMaj matrix)Constructs a dense matrix by copying an existing EJML dense matrix. DenseMatrix(DMatrix matrix)Constructs a dense matrix by copying an existing DMatrix. DenseMatrix(DenseMatrix matrix)Copy constructor for creating a new dense matrix from an existing one. DenseMatrix()Protected constructor for delayed initialization.
-
Method Summary
Modifier and Type Method Description DMatrixRMajgetData()Returns the underlying EJML dense matrix data structure. voidsetData(DMatrixRMaj newData)Sets the underlying EJML dense matrix data structure. static voidaddMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)static DMatrixRMajaddMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)static DMatrixRMajaddStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)Performs element-wise addition of two dense matrices: output = A + B. static voidchangeSignStatic(DMatrix input, DMatrix output)static doubledeterminantStatic(DMatrix matrix)static DMatrixRMajelementMultStatic(DMatrix A, DMatrix B, DMatrix output)static voidextractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)static voidfillMatrixStatic(DMatrix matrix, double value)static DMatrixRMajidentityStatic(int size)Creates an identity matrix of the specified size. static voidinvertStatic(DMatrix input, DMatrix output)static DMatrixRMajkronStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)Computes the Kronecker product of two dense matrices: A ⊗ B. static DMatrixRMajmultMatrixStatic(DMatrix A, DMatrix B, DMatrix output)static voidscaleMatrixStatic(double scalar, DMatrix input, DMatrix output)static voidtransposeMatrixStatic(DMatrix input, DMatrix output)voidabsEq()Replaces each value in the matrix with its absolute value, in-place. voidadd(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)Performs the operation: output = alpha*A + beta*B. DMatrixaddMatrices(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)Performs the operation: output = alpha*A + beta*B. voidaddMatricesInPlace(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)booleanany()Checks if the current matrix contains any non-zero elements. voidapplyConditionalTransform(double source, double target, double tol, String operation)Applies a transformation to matrix elements based on conditions. voidchangeSign()abstract BaseMatrixcopy()Creates and returns a deep copy of this dense matrix. voidcolIncrease(int col, double a)Adds a scalar value to each element in the specified column. voidconcatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)Concatenates matrices column-wise. voidconcatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output)Concatenates matrices row-wise. BaseMatrixcountEachRow(double val)Counts occurrences of each unique value in each row. abstract BaseMatrixcreateNewInstance(int rows, int cols, int nzLength)Creates a new instance of the concrete dense matrix implementation. doubledeterminant()voiddivideInPlace(double scalar)voiddivideMatrix(double scalar, DMatrix output)voiddivideRowsByArray(Array<double> diag, int offset)voiddivideScalarByMatrix(double scalar, DMatrix output)doubleelementMax()doubleelementMaxAbs()doubleelementMin()DMatrixelementMult(DMatrix A, DMatrix B, DMatrix output)doubleelementSum()abstract booleanequals(Object obj)Indicates whether some other object is "equal to" this dense matrix. voidextractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)voidfillMatrix(double value)Fills the matrix with the specified value. BaseMatrixfindNonNegative()Returns a matrix containing indices of all non-negative elements. doubleget(int row, int col)Returns the value at the specified matrix position. intgetColumnIndex(int col)Array<int>getColumnIndicesArray()voidsetData(DMatrix newData)Sets the underlying EJML dense matrix data structure. voidsetData(Object newData)Sets the underlying data structure. intgetNonZeroLength()Returns the number of stored non-zero elements. voidsetNonZeroLength(int length)intgetNonZeroRow(int index)Array<int>getNonZeroRows()Returns array of row indices of non-zero entries. doublegetNonZeroValue(int index)Array<double>getNonZeroValues()Returns array of non-zero values. intgetNonZeros()Returns the number of non-zero elements in the matrix. intgetNumCols()Returns the number of columns in the matrix. intgetNumRows()Returns the number of rows in the matrix. booleanhasDuplicates()Checks if the matrix contains duplicate values. booleanhasFinite()Checks if the matrix contains any finite (non-infinite, non-NaN) values. booleanhasInfinite()Checks if the matrix contains any infinite values. booleanhasMultipleFinite()Checks if more than one finite value exists. booleanhasNaN()Checks if the matrix contains any NaN values. voidmult(DMatrix A, DMatrix B, DMatrix output)Performs matrix multiplication: output = A * B. DMatrixmultMatrix(DMatrix B, DMatrix output)Performs matrix multiplication with sparse matrices. voidremoveZeros()Removes zero-valued elements from the matrix with default tolerance. voidremoveZerosWithTol(double tolerance)Removes zero-valued elements from the matrix with specified tolerance. voidreshape(int numRows, int numCols)Reshapes the matrix to the specified dimensions. voidscaleInPlace(double alpha)Scales the matrix in-place by the specified factor. voidscaleMatrix(double scalar, DMatrix output)voidset(int row, int col, double value)Sets the value at the specified matrix position. voidsetColumnIndex(int col, int value)voidsetNonZeroRow(int index, int row)voidsetNonZeroValue(int index, double value)voidshrinkNumCols(int newmax)Reduce the maximum number of columns by setting the internal column count. voidshrinkNumRows(int newmax)Reduce the maximum number of rows by setting the internal row count. DMatrixsumColsRaw()DMatrixsumRowsRaw()StringtoString()Returns a string representation of the matrix. voidtranspose(DMatrix input, DMatrix output)Computes the transpose of the input matrix. voidtransposeMatrix(DMatrix output)Computes the transpose of this matrix. voidzero()Sets all elements in the matrix to zero. -
-
Constructor Detail
-
DenseMatrix
DenseMatrix(int numRows, int numCols)
Constructs a dense matrix with specified dimensions.- Parameters:
numRows- the number of rows in the matrixnumCols- the number of columns in the matrix
-
DenseMatrix
DenseMatrix(DMatrixRMaj matrix)
Constructs a dense matrix by copying an existing EJML dense matrix.- Parameters:
matrix- the EJML dense matrix to copy
-
DenseMatrix
DenseMatrix(DMatrix matrix)
Constructs a dense matrix by copying an existing DMatrix.- Parameters:
matrix- the DMatrix to copy
-
DenseMatrix
DenseMatrix(DenseMatrix matrix)
Copy constructor for creating a new dense matrix from an existing one.- Parameters:
matrix- the dense matrix to copy
-
DenseMatrix
DenseMatrix()
Protected constructor for delayed initialization.
-
-
Method Detail
-
getData
DMatrixRMaj getData()
Returns the underlying EJML dense matrix data structure. This method provides direct access to the internal data for subclasses.
- Returns:
the underlying DMatrixRMaj instance
-
setData
void setData(DMatrixRMaj newData)
Sets the underlying EJML dense matrix data structure. This method allows subclasses to replace the internal data.
- Parameters:
newData- the new DMatrixRMaj instance to use
-
addMatricesInPlaceStatic
static void addMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
-
addMatricesStatic
static DMatrixRMaj addMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
-
addStatic
static DMatrixRMaj addStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)
Performs element-wise addition of two dense matrices: output = A + B.
- Parameters:
A- the first matrixB- the second matrixoutput- the result matrix (can be null to create a new matrix)- Returns:
the sum matrix
-
changeSignStatic
static void changeSignStatic(DMatrix input, DMatrix output)
-
determinantStatic
static double determinantStatic(DMatrix matrix)
-
elementMultStatic
static DMatrixRMaj elementMultStatic(DMatrix A, DMatrix B, DMatrix output)
-
extractMatrixStatic
static void extractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
-
fillMatrixStatic
static void fillMatrixStatic(DMatrix matrix, double value)
-
identityStatic
static DMatrixRMaj identityStatic(int size)
Creates an identity matrix of the specified size. An identity matrix has ones on the main diagonal and zeros elsewhere.
- Parameters:
size- the number of rows and columns in the identity matrix- Returns:
a new identity matrix
-
invertStatic
static void invertStatic(DMatrix input, DMatrix output)
-
kronStatic
static DMatrixRMaj kronStatic(DMatrixRMaj A, DMatrixRMaj B, DMatrixRMaj output)
Computes the Kronecker product of two dense matrices: A ⊗ B. The Kronecker product creates a larger matrix where each element of A is multiplied by the entire matrix B.
- Parameters:
A- the left matrixB- the right matrixoutput- the result matrix (can be null to create a new matrix)- Returns:
the Kronecker product matrix
-
multMatrixStatic
static DMatrixRMaj multMatrixStatic(DMatrix A, DMatrix B, DMatrix output)
-
scaleMatrixStatic
static void scaleMatrixStatic(double scalar, DMatrix input, DMatrix output)
-
transposeMatrixStatic
static void transposeMatrixStatic(DMatrix input, DMatrix output)
-
absEq
void absEq()
Replaces each value in the matrix with its absolute value, in-place.
-
add
void add(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
Performs the operation: output = alpha*A + beta*B.
- Parameters:
alpha- scalar coefficient for matrix AA- first input matrixbeta- scalar coefficient for matrix BB- second input matrixoutput- the result matrix
-
addMatrices
DMatrix addMatrices(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
Performs the operation: output = alpha*A + beta*B.
- Parameters:
alpha- scalar coefficient for matrix AA- first input matrixbeta- scalar coefficient for matrix BB- second input matrixoutput- the result matrix
-
addMatricesInPlace
void addMatricesInPlace(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
-
any
boolean any()
Checks if the current matrix contains any non-zero elements.
- Returns:
trueif at least one non-zero element exists;falseotherwise.
-
applyConditionalTransform
void applyConditionalTransform(double source, double target, double tol, String operation)
Applies a transformation to matrix elements based on conditions. Replaces elements matching the condition with the target value.
-
changeSign
void changeSign()
-
copy
abstract BaseMatrix copy()
Creates and returns a deep copy of this dense matrix.
- Returns:
a new dense matrix that is a copy of this instance
-
colIncrease
void colIncrease(int col, double a)
Adds a scalar value to each element in the specified column.
- Parameters:
col- Column index to update.a- Scalar value to add to each element in the column.
-
concatColumnsInPlace
void concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)
Concatenates matrices column-wise.
- Parameters:
left- the left matrixright- the right matrixoutput- the result matrix
-
concatRowsInPlace
void concatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output)
Concatenates matrices row-wise.
- Parameters:
top- the top matrixbottom- the bottom matrixoutput- the result matrix
-
countEachRow
BaseMatrix countEachRow(double val)
Counts occurrences of each unique value in each row.
-
createNewInstance
abstract BaseMatrix createNewInstance(int rows, int cols, int nzLength)
Creates a new instance of the concrete dense matrix implementation. This factory method allows the base class to create instances of the correct subtype.
- Parameters:
rows- the number of rows for the new matrixcols- the number of columns for the new matrix- Returns:
a new instance of the concrete dense matrix type
-
determinant
double determinant()
-
divideInPlace
void divideInPlace(double scalar)
-
divideMatrix
void divideMatrix(double scalar, DMatrix output)
-
divideRowsByArray
void divideRowsByArray(Array<double> diag, int offset)
-
divideScalarByMatrix
void divideScalarByMatrix(double scalar, DMatrix output)
-
elementMax
double elementMax()
-
elementMaxAbs
double elementMaxAbs()
-
elementMin
double elementMin()
-
elementMult
DMatrix elementMult(DMatrix A, DMatrix B, DMatrix output)
-
elementSum
double elementSum()
-
equals
abstract boolean equals(Object obj)
Indicates whether some other object is "equal to" this dense matrix.
- Parameters:
obj- the reference object with which to compare- Returns:
trueif this object is the same as the obj argument;falseotherwise
-
extractMatrix
void extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
-
fillMatrix
void fillMatrix(double value)
Fills the matrix with the specified value.
- Parameters:
value- the value to fill the matrix with
-
findNonNegative
BaseMatrix findNonNegative()
Returns a matrix containing indices of all non-negative elements.
-
get
double get(int row, int col)
Returns the value at the specified matrix position.
- Parameters:
row- the row indexcol- the column index- Returns:
the value at position (row, col)
-
getColumnIndex
int getColumnIndex(int col)
-
getColumnIndicesArray
Array<int> getColumnIndicesArray()
-
setData
void setData(DMatrix newData)
Sets the underlying EJML dense matrix data structure. This method allows subclasses to replace the internal data.
- Parameters:
newData- the new DMatrix instance to use
-
setData
void setData(Object newData)
Sets the underlying data structure. Implementation depends on the concrete matrix type.
- Parameters:
newData- the new data structure
-
getNonZeroLength
int getNonZeroLength()
Returns the number of stored non-zero elements. This may be different from actual non-zero count for dense matrices.
- Returns:
number of stored non-zero elements
-
setNonZeroLength
void setNonZeroLength(int length)
-
getNonZeroRow
int getNonZeroRow(int index)
-
getNonZeroRows
Array<int> getNonZeroRows()
Returns array of row indices of non-zero entries. For dense matrices, this may compute row indices dynamically. For sparse matrices, this returns the compressed row index array.
- Returns:
array of row indices
-
getNonZeroValue
double getNonZeroValue(int index)
-
getNonZeroValues
Array<double> getNonZeroValues()
Returns array of non-zero values. For dense matrices, this may return all values or only actual non-zeros. For sparse matrices, this returns the compressed storage array.
- Returns:
array of non-zero values
-
getNonZeros
int getNonZeros()
Returns the number of non-zero elements in the matrix. For dense matrices, this counts actual non-zero values. For sparse matrices, this returns the stored non-zero count.
- Returns:
the number of non-zero elements
-
getNumCols
int getNumCols()
Returns the number of columns in the matrix.
- Returns:
the number of columns
-
getNumRows
int getNumRows()
Returns the number of rows in the matrix.
- Returns:
the number of rows
-
hasDuplicates
boolean hasDuplicates()
Checks if the matrix contains duplicate values.
- Returns:
true if duplicates exist
-
hasFinite
boolean hasFinite()
Checks if the matrix contains any finite (non-infinite, non-NaN) values.
- Returns:
true if at least one finite value exists
-
hasInfinite
boolean hasInfinite()
Checks if the matrix contains any infinite values.
- Returns:
true if at least one infinite value exists
-
hasMultipleFinite
boolean hasMultipleFinite()
Checks if more than one finite value exists.
- Returns:
true if more than one finite value exists
-
hasNaN
boolean hasNaN()
Checks if the matrix contains any NaN values.
- Returns:
true if at least one NaN exists
-
mult
void mult(DMatrix A, DMatrix B, DMatrix output)
Performs matrix multiplication: output = A * B.
- Parameters:
A- the left matrixB- the right matrixoutput- the result matrix
-
multMatrix
DMatrix multMatrix(DMatrix B, DMatrix output)
Performs matrix multiplication with sparse matrices.
- Parameters:
B- the right matrixoutput- the result matrix
-
removeZeros
void removeZeros()
Removes zero-valued elements from the matrix with default tolerance. For dense matrices, this may be a no-op or convert to sparse representation.
-
removeZerosWithTol
void removeZerosWithTol(double tolerance)
Removes zero-valued elements from the matrix with specified tolerance.
- Parameters:
tolerance- the tolerance for considering values as zero
-
reshape
void reshape(int numRows, int numCols)
Reshapes the matrix to the specified dimensions.
- Parameters:
numRows- the new number of rowsnumCols- the new number of columns
-
scaleInPlace
void scaleInPlace(double alpha)
Scales the matrix in-place by the specified factor.
- Parameters:
alpha- the scaling factor
-
scaleMatrix
void scaleMatrix(double scalar, DMatrix output)
-
set
void set(int row, int col, double value)
Sets the value at the specified matrix position.
- Parameters:
row- the row indexcol- the column indexvalue- the value to set
-
setColumnIndex
void setColumnIndex(int col, int value)
-
setNonZeroRow
void setNonZeroRow(int index, int row)
-
setNonZeroValue
void setNonZeroValue(int index, double value)
-
shrinkNumCols
void shrinkNumCols(int newmax)
Reduce the maximum number of columns by setting the internal column count.
- Parameters:
newmax- the new maximum number of columns
-
shrinkNumRows
void shrinkNumRows(int newmax)
Reduce the maximum number of rows by setting the internal row count.
- Parameters:
newmax- the new maximum number of rows
-
sumColsRaw
DMatrix sumColsRaw()
-
sumRowsRaw
DMatrix sumRowsRaw()
-
toString
String toString()
Returns a string representation of the matrix. Elements are formatted to 4 decimal places in a grid layout.
- Returns:
a formatted string representation of the matrix
-
transpose
void transpose(DMatrix input, DMatrix output)
Computes the transpose of the input matrix.
- Parameters:
input- the matrix to transposeoutput- the transposed matrix
-
transposeMatrix
void transposeMatrix(DMatrix output)
Computes the transpose of this matrix.
- Parameters:
output- the transposed matrix
-
zero
void zero()
Sets all elements in the matrix to zero.
-
-
-
-