Class SparseMatrix
-
- All Implemented Interfaces:
-
java.io.Serializable
public abstract class SparseMatrix extends BaseMatrix
Base class for sparse matrix implementations, containing the core data structure and methods that directly manipulate the underlying sparse matrix representation.
This class encapsulates all interactions with EJML's DMatrixSparseCSC and CommonOps_DSCC classes, providing a clean abstraction layer for sparse matrix operations. It serves as the foundation for concrete sparse matrix implementations like
Matrix
.The class provides:
- Basic matrix operations (get, set, dimensions)
- Encapsulated CommonOps_DSCC method calls
- Memory management for sparse matrix data structures
- Utility methods for matrix manipulation
- Since:
1.0
QORE Lab, Imperial College London
-
-
Field Summary
Fields Modifier and Type Field Description public DMatrixSparseCSC
data
-
Constructor Summary
Constructors Constructor Description SparseMatrix(int numRows, int numCols, int arrayLength)
Constructs a sparse matrix with specified dimensions and initial capacity. SparseMatrix(int numRows, int numCols)
Constructs an empty sparse matrix with specified dimensions. SparseMatrix(DMatrix matrix)
Constructs a sparse matrix by copying an existing EJML sparse matrix. SparseMatrix(SparseMatrix matrix)
Copy constructor for creating a new sparse matrix from an existing one. SparseMatrix()
Protected constructor for delayed initialization.
-
Method Summary
Modifier and Type Method Description DMatrixSparseCSC
getData()
Returns the underlying EJML sparse matrix data structure. void
setData(DMatrixSparseCSC newData)
Sets the underlying EJML sparse matrix data structure. static void
addMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
static DMatrixSparseCSC
addMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
static void
changeSignStatic(DMatrix input, DMatrix output)
static void
concatColumnsInPlaceStatic(DMatrix left, DMatrix right, DMatrix output)
static DMatrixSparseCSC
concatColumnsStatic(DMatrix left, DMatrix right, DMatrix output)
static void
concatRowsInPlaceStatic(DMatrix top, DMatrix bottom, DMatrix output)
static DMatrixSparseCSC
concatRowsStatic(DMatrix top, DMatrix bottom, DMatrix output)
static double
determinantStatic(DMatrix matrix)
static DMatrixSparseCSC
diagStatic(Array<double> values)
static DMatrixSparseCSC
diagWithMatrixStatic(DMatrixSparseCSC A, Array<double> values, int offset, int length)
static DMatrixSparseCSC
diagWithRetStatic(DMatrixSparseCSC ret, Array<double> values, int offset, int length)
static void
divideInPlaceStatic(DMatrix matrix, double scalar)
static void
divideMatrixStatic(DMatrix input, double scalar, DMatrix output)
static void
divideRowsByArrayStatic(Array<double> diag, int offset, DMatrix matrix)
static void
divideScalarByMatrixStatic(double scalar, DMatrix input, DMatrix output)
static DMatrixSparseCSC
elementMultStatic(DMatrix A, DMatrix B, DMatrix output)
static void
extractColumnInPlaceStatic(DMatrix input, int column, DMatrix output)
static DMatrixSparseCSC
extractColumnStatic(DMatrix input, int column, DMatrix output)
static void
extractDiagStatic(DMatrix input, DMatrix output)
static void
extractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
static void
extractRowsInPlaceStatic(DMatrix input, int row0, int row1, DMatrix output)
static DMatrixSparseCSC
extractRowsStatic(DMatrix input, int row0, int row1, DMatrix output)
static void
fillMatrixStatic(DMatrix matrix, double value)
static DMatrixSparseCSC
identityStatic(int length)
static void
invertStatic(DMatrix input, DMatrixRMaj output)
static DMatrixSparseCSC
multMatrixStatic(DMatrix A, DMatrix B, DMatrix output)
static void
removeZerosWithTolStatic(DMatrix matrix, double tolerance)
static void
scaleMatrixStatic(double scalar, DMatrix input, DMatrix output)
static boolean
solveStatic(DMatrix a, DMatrix b, DMatrix x)
static DMatrixRMaj
sumColsStatic(DMatrix matrix)
static DMatrixRMaj
sumRowsStatic(DMatrix matrix)
static void
transposeMatrixStatic(DMatrix input, DMatrix output)
void
absEq()
Replaces each value in the matrix with its absolute value, in-place. void
add(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
Performs the operation: output = alpha*A + beta*B. DMatrix
addMatrices(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
Performs the operation: output = alpha*A + beta*B. void
addMatricesInPlace(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
boolean
any()
Checks if the current matrix contains any non-zero elements. void
applyConditionalTransform(double source, double target, double tol, String operation)
Applies a transformation to matrix elements based on conditions. void
changeSign()
abstract BaseMatrix
copy()
Creates and returns a deep copy of this sparse matrix. void
colIncrease(int col, double a)
Adds a scalar value to each element in the specified column. DMatrixSparseCSC
concatColumns(DMatrix left, DMatrix right, DMatrix output)
void
concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)
Concatenates matrices column-wise. DMatrixSparseCSC
concatRows(DMatrix top, DMatrix bottom, DMatrix output)
void
concatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output)
Concatenates matrices row-wise. BaseMatrix
countEachRow(double val)
Counts occurrences of each unique value in each row. abstract BaseMatrix
createNewInstance(int rows, int cols, int nzLength)
Creates a new instance of the concrete sparse matrix implementation. double
determinant()
void
divideInPlace(double scalar)
void
divideMatrix(double scalar, DMatrix output)
void
divideRowsByArray(Array<double> diag, int offset)
void
divideScalarByMatrix(double scalar, DMatrix output)
double
elementMax()
double
elementMaxAbs()
double
elementMin()
DMatrix
elementMult(DMatrix A, DMatrix B, DMatrix output)
double
elementSum()
abstract boolean
equals(Object obj)
Indicates whether some other object is "equal to" this sparse matrix. DMatrixSparseCSC
extractColumn(DMatrix input, int column, DMatrix output)
void
extractColumnInPlace(DMatrix input, int column, DMatrix output)
void
extractDiag(DMatrix input, DMatrix output)
void
extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
DMatrixSparseCSC
extractRows(DMatrix input, int row0, int row1, DMatrix output)
void
extractRowsInPlace(DMatrix input, int row0, int row1, DMatrix output)
void
fillMatrix(double value)
Fills the matrix with the specified value. BaseMatrix
findNonNegative()
Returns a matrix containing indices of all non-negative elements. double
get(int row, int col)
Returns the value at the specified matrix position. int
getColumnIndex(int col)
Array<int>
getColumnIndices()
Array<int>
getColumnIndicesArray()
void
setData(Object newData)
Sets the underlying data structure for BaseMatrix compatibility. int
getNonZeroLength()
Returns the number of stored non-zero elements. void
setNonZeroLength(int length)
int
getNonZeroRow(int index)
Array<int>
getNonZeroRows()
Returns array of row indices of non-zero entries. double
getNonZeroValue(int index)
Array<double>
getNonZeroValues()
Returns array of non-zero values. int
getNonZeros()
Returns the number of non-zero elements in the matrix. int
getNumCols()
Returns the number of columns in the matrix. int
getNumColsInternal()
int
getNumNonZeros()
Returns the number of non-zero elements in the matrix. int
getNumRows()
Returns the number of rows in the matrix. int
getNumRowsInternal()
boolean
hasDuplicates()
Checks if the matrix contains duplicate values. boolean
hasFinite()
Checks if the matrix contains any finite (non-infinite, non-NaN) values. boolean
hasInfinite()
Checks if the matrix contains any infinite values. boolean
hasMultipleFinite()
Checks if more than one finite value exists. boolean
hasNaN()
Checks if the matrix contains any NaN values. void
mult(DMatrix A, DMatrix B, DMatrix output)
Performs matrix multiplication: output = A * B. DMatrix
multMatrix(DMatrix B, DMatrix output)
Performs matrix multiplication with sparse matrices. void
removeZeros()
Removes zero-valued elements from the sparse matrix with default tolerance. void
removeZerosWithTol(double tolerance)
Removes zero-valued elements from the matrix with specified tolerance. void
reshape(int numRows, int numCols)
Reshapes the matrix to the specified dimensions. void
scaleInPlace(double alpha)
Scales the matrix in-place by the specified factor. void
scaleMatrix(double scalar, DMatrix output)
void
set(int row, int col, double value)
Sets the value at the specified matrix position. void
setColumnIndex(int col, int value)
void
setNonZeroRow(int index, int row)
void
setNonZeroValue(int index, double value)
void
shrinkNumCols(int newmax)
Reduce the maximum number of columns by setting the internal column count. void
shrinkNumRows(int newmax)
Reduce the maximum number of rows by setting the internal row count. DMatrix
sumColsRaw()
DMatrix
sumRowsRaw()
String
toString()
Returns a string representation of the matrix. void
transpose(DMatrix input, DMatrix output)
Computes the transpose of the input matrix. void
transposeMatrix(DMatrix output)
Computes the transpose of this matrix. void
zero()
Sets all elements in the matrix to zero. -
-
Constructor Detail
-
SparseMatrix
SparseMatrix(int numRows, int numCols, int arrayLength)
Constructs a sparse matrix with specified dimensions and initial capacity.- Parameters:
numRows
- the number of rows in the matrixnumCols
- the number of columns in the matrixarrayLength
- the initial capacity for non-zero elements
-
SparseMatrix
SparseMatrix(int numRows, int numCols)
Constructs an empty sparse matrix with specified dimensions.- Parameters:
numRows
- the number of rows in the matrixnumCols
- the number of columns in the matrix
-
SparseMatrix
SparseMatrix(DMatrix matrix)
Constructs a sparse matrix by copying an existing EJML sparse matrix.- Parameters:
matrix
- the EJML sparse matrix to copy
-
SparseMatrix
SparseMatrix(SparseMatrix matrix)
Copy constructor for creating a new sparse matrix from an existing one.- Parameters:
matrix
- the sparse matrix to copy
-
SparseMatrix
SparseMatrix()
Protected constructor for delayed initialization.
-
-
Method Detail
-
getData
DMatrixSparseCSC getData()
Returns the underlying EJML sparse matrix data structure. This method provides direct access to the internal data for subclasses.
- Returns:
the underlying DMatrixSparseCSC instance
-
setData
void setData(DMatrixSparseCSC newData)
Sets the underlying EJML sparse matrix data structure. This method allows subclasses to replace the internal data.
- Parameters:
newData
- the new DMatrixSparseCSC instance to use
-
addMatricesInPlaceStatic
static void addMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
-
addMatricesStatic
static DMatrixSparseCSC addMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
-
changeSignStatic
static void changeSignStatic(DMatrix input, DMatrix output)
-
concatColumnsInPlaceStatic
static void concatColumnsInPlaceStatic(DMatrix left, DMatrix right, DMatrix output)
-
concatColumnsStatic
static DMatrixSparseCSC concatColumnsStatic(DMatrix left, DMatrix right, DMatrix output)
-
concatRowsInPlaceStatic
static void concatRowsInPlaceStatic(DMatrix top, DMatrix bottom, DMatrix output)
-
concatRowsStatic
static DMatrixSparseCSC concatRowsStatic(DMatrix top, DMatrix bottom, DMatrix output)
-
determinantStatic
static double determinantStatic(DMatrix matrix)
-
diagStatic
static DMatrixSparseCSC diagStatic(Array<double> values)
-
diagWithMatrixStatic
static DMatrixSparseCSC diagWithMatrixStatic(DMatrixSparseCSC A, Array<double> values, int offset, int length)
-
diagWithRetStatic
static DMatrixSparseCSC diagWithRetStatic(DMatrixSparseCSC ret, Array<double> values, int offset, int length)
-
divideInPlaceStatic
static void divideInPlaceStatic(DMatrix matrix, double scalar)
-
divideMatrixStatic
static void divideMatrixStatic(DMatrix input, double scalar, DMatrix output)
-
divideRowsByArrayStatic
static void divideRowsByArrayStatic(Array<double> diag, int offset, DMatrix matrix)
-
divideScalarByMatrixStatic
static void divideScalarByMatrixStatic(double scalar, DMatrix input, DMatrix output)
-
elementMultStatic
static DMatrixSparseCSC elementMultStatic(DMatrix A, DMatrix B, DMatrix output)
-
extractColumnInPlaceStatic
static void extractColumnInPlaceStatic(DMatrix input, int column, DMatrix output)
-
extractColumnStatic
static DMatrixSparseCSC extractColumnStatic(DMatrix input, int column, DMatrix output)
-
extractDiagStatic
static void extractDiagStatic(DMatrix input, DMatrix output)
-
extractMatrixStatic
static void extractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
-
extractRowsInPlaceStatic
static void extractRowsInPlaceStatic(DMatrix input, int row0, int row1, DMatrix output)
-
extractRowsStatic
static DMatrixSparseCSC extractRowsStatic(DMatrix input, int row0, int row1, DMatrix output)
-
fillMatrixStatic
static void fillMatrixStatic(DMatrix matrix, double value)
-
identityStatic
static DMatrixSparseCSC identityStatic(int length)
-
invertStatic
static void invertStatic(DMatrix input, DMatrixRMaj output)
-
multMatrixStatic
static DMatrixSparseCSC multMatrixStatic(DMatrix A, DMatrix B, DMatrix output)
-
removeZerosWithTolStatic
static void removeZerosWithTolStatic(DMatrix matrix, double tolerance)
-
scaleMatrixStatic
static void scaleMatrixStatic(double scalar, DMatrix input, DMatrix output)
-
solveStatic
static boolean solveStatic(DMatrix a, DMatrix b, DMatrix x)
-
sumColsStatic
static DMatrixRMaj sumColsStatic(DMatrix matrix)
-
sumRowsStatic
static DMatrixRMaj sumRowsStatic(DMatrix matrix)
-
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:
true
if at least one non-zero element exists;false
otherwise.
-
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 sparse matrix.
- Returns:
a new sparse 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.
-
concatColumns
DMatrixSparseCSC concatColumns(DMatrix left, DMatrix right, DMatrix output)
-
concatColumnsInPlace
void concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)
Concatenates matrices column-wise.
- Parameters:
left
- the left matrixright
- the right matrixoutput
- the result matrix
-
concatRows
DMatrixSparseCSC concatRows(DMatrix top, DMatrix bottom, DMatrix output)
-
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 sparse 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 matrixnzLength
- the initial capacity for non-zero elements- Returns:
a new instance of the concrete sparse 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 sparse matrix.
- Parameters:
obj
- the reference object with which to compare- Returns:
true
if this object is the same as the obj argument;false
otherwise
-
extractColumn
DMatrixSparseCSC extractColumn(DMatrix input, int column, DMatrix output)
-
extractColumnInPlace
void extractColumnInPlace(DMatrix input, int column, DMatrix output)
-
extractDiag
void extractDiag(DMatrix input, DMatrix output)
-
extractMatrix
void extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
-
extractRows
DMatrixSparseCSC extractRows(DMatrix input, int row0, int row1, DMatrix output)
-
extractRowsInPlace
void extractRowsInPlace(DMatrix input, int row0, int row1, DMatrix output)
-
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)
-
getColumnIndices
Array<int> getColumnIndices()
-
getColumnIndicesArray
Array<int> getColumnIndicesArray()
-
setData
void setData(Object newData)
Sets the underlying data structure for BaseMatrix compatibility.
- Parameters:
newData
- the new data structure (must be DMatrixSparseCSC)
-
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.
- Returns:
the number of non-zero elements
-
getNumCols
int getNumCols()
Returns the number of columns in the matrix.
- Returns:
the number of columns
-
getNumColsInternal
int getNumColsInternal()
-
getNumNonZeros
int getNumNonZeros()
Returns the number of non-zero elements in the matrix. This is an alias for getNonZeros.
- Returns:
the number of non-zero elements
-
getNumRows
int getNumRows()
Returns the number of rows in the matrix.
- Returns:
the number of rows
-
getNumRowsInternal
int getNumRowsInternal()
-
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 sparse matrix with default tolerance.
-
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.
-
-
-
-