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 DMatrixSparseCSCdata
-
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 DMatrixSparseCSCgetData()Returns the underlying EJML sparse matrix data structure. voidsetData(DMatrixSparseCSC newData)Sets the underlying EJML sparse matrix data structure. static voidaddMatricesInPlaceStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)static DMatrixSparseCSCaddMatricesStatic(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)static voidchangeSignStatic(DMatrix input, DMatrix output)static voidconcatColumnsInPlaceStatic(DMatrix left, DMatrix right, DMatrix output)static DMatrixSparseCSCconcatColumnsStatic(DMatrix left, DMatrix right, DMatrix output)static voidconcatRowsInPlaceStatic(DMatrix top, DMatrix bottom, DMatrix output)static DMatrixSparseCSCconcatRowsStatic(DMatrix top, DMatrix bottom, DMatrix output)static doubledeterminantStatic(DMatrix matrix)static DMatrixSparseCSCdiagStatic(Array<double> values)static DMatrixSparseCSCdiagWithMatrixStatic(DMatrixSparseCSC A, Array<double> values, int offset, int length)static DMatrixSparseCSCdiagWithRetStatic(DMatrixSparseCSC ret, Array<double> values, int offset, int length)static voiddivideInPlaceStatic(DMatrix matrix, double scalar)static voiddivideMatrixStatic(DMatrix input, double scalar, DMatrix output)static voiddivideRowsByArrayStatic(Array<double> diag, int offset, DMatrix matrix)static voiddivideScalarByMatrixStatic(double scalar, DMatrix input, DMatrix output)static DMatrixSparseCSCelementMultStatic(DMatrix A, DMatrix B, DMatrix output)static voidextractColumnInPlaceStatic(DMatrix input, int column, DMatrix output)static DMatrixSparseCSCextractColumnStatic(DMatrix input, int column, DMatrix output)static voidextractDiagStatic(DMatrix input, DMatrix output)static voidextractMatrixStatic(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)static voidextractRowsInPlaceStatic(DMatrix input, int row0, int row1, DMatrix output)static DMatrixSparseCSCextractRowsStatic(DMatrix input, int row0, int row1, DMatrix output)static voidfillMatrixStatic(DMatrix matrix, double value)static DMatrixSparseCSCidentityStatic(int length)static voidinvertStatic(DMatrix input, DMatrixRMaj output)static DMatrixSparseCSCmultMatrixStatic(DMatrix A, DMatrix B, DMatrix output)static voidremoveZerosWithTolStatic(DMatrix matrix, double tolerance)static voidscaleMatrixStatic(double scalar, DMatrix input, DMatrix output)static booleansolveStatic(DMatrix a, DMatrix b, DMatrix x)static DMatrixRMajsumColsStatic(DMatrix matrix)static DMatrixRMajsumRowsStatic(DMatrix matrix)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 sparse matrix. voidcolIncrease(int col, double a)Adds a scalar value to each element in the specified column. DMatrixSparseCSCconcatColumns(DMatrix left, DMatrix right, DMatrix output)voidconcatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)Concatenates matrices column-wise. DMatrixSparseCSCconcatRows(DMatrix top, DMatrix bottom, DMatrix output)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 sparse 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 sparse matrix. DMatrixSparseCSCextractColumn(DMatrix input, int column, DMatrix output)voidextractColumnInPlace(DMatrix input, int column, DMatrix output)voidextractDiag(DMatrix input, DMatrix output)voidextractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)DMatrixSparseCSCextractRows(DMatrix input, int row0, int row1, DMatrix output)voidextractRowsInPlace(DMatrix input, int row0, int row1, DMatrix output)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>getColumnIndices()Array<int>getColumnIndicesArray()voidsetData(Object newData)Sets the underlying data structure for BaseMatrix compatibility. 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. intgetNumColsInternal()intgetNumNonZeros()Returns the number of non-zero elements in the matrix. intgetNumRows()Returns the number of rows in the matrix. intgetNumRowsInternal()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 sparse 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
-
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:
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 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:
trueif this object is the same as the obj argument;falseotherwise
-
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.
-
-
-
-