Class BaseMatrix
-
- All Implemented Interfaces:
-
java.io.Serializable
public abstract class BaseMatrix implements Serializable
Common base class for matrix implementations, providing a unified interface for both dense and sparse matrix operations.
This abstract class defines the common contract that all matrix implementations must support, enabling the delegation pattern in Matrix.java to work with either dense or sparse underlying representations.
The class provides:
- Basic matrix operations (get, set, dimensions)
- Common utility methods for matrix manipulation
- Abstract methods that must be implemented by concrete subclasses
- Factory methods for creating new instances
- Since:
1.0
QORE Lab, Imperial College London
-
-
Constructor Summary
Constructors Constructor Description BaseMatrix()
-
Method Summary
Modifier and Type Method Description abstract void
absEq()
Replaces each value in the matrix with its absolute value, in-place. abstract DMatrix
addMatrices(double alpha, DMatrix A, double beta, DMatrix B, DMatrix output)
Performs the operation: output = alpha*A + beta*B. abstract boolean
any()
Checks if the current matrix contains any non-zero elements. abstract void
applyConditionalTransform(double source, double target, double tol, String operation)
Applies a transformation to matrix elements based on conditions. abstract void
changeSign()
abstract BaseMatrix
copy()
Creates and returns a deep copy of this matrix. abstract void
colIncrease(int col, double a)
Adds a scalar value to each element in the specified column. abstract void
concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)
Concatenates matrices column-wise. abstract void
concatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output)
Concatenates matrices row-wise. abstract 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 matrix implementation. abstract double
determinant()
abstract void
divideInPlace(double scalar)
abstract void
divideMatrix(double scalar, DMatrix output)
abstract void
divideRowsByArray(Array<double> diag, int offset)
abstract void
divideScalarByMatrix(double scalar, DMatrix output)
abstract double
elementMax()
abstract double
elementMaxAbs()
abstract double
elementMin()
abstract DMatrix
elementMult(DMatrix A, DMatrix B, DMatrix output)
abstract double
elementSum()
abstract boolean
equals(Object obj)
Indicates whether some other object is "equal to" this matrix. abstract void
extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
abstract void
fillMatrix(double value)
Fills the matrix with the specified value. abstract BaseMatrix
findNonNegative()
Returns a matrix containing indices of all non-negative elements. abstract double
get(int row, int col)
Returns the value at the specified matrix position. abstract int
getColumnIndex(int col)
abstract Array<int>
getColumnIndicesArray()
abstract Object
getData()
Returns the underlying data structure. abstract void
setData(Object newData)
Sets the underlying data structure. abstract int
getNonZeroLength()
Returns the number of stored non-zero elements. abstract void
setNonZeroLength(int length)
abstract int
getNonZeroRow(int index)
abstract Array<int>
getNonZeroRows()
Returns array of row indices of non-zero entries. abstract double
getNonZeroValue(int index)
abstract Array<double>
getNonZeroValues()
Returns array of non-zero values. abstract int
getNonZeros()
Returns the number of non-zero elements in the matrix. abstract int
getNumCols()
Returns the number of columns in the matrix. int
getNumNonZeros()
Returns the number of non-zero elements in the matrix. abstract int
getNumRows()
Returns the number of rows in the matrix. abstract boolean
hasDuplicates()
Checks if the matrix contains duplicate values. abstract boolean
hasFinite()
Checks if the matrix contains any finite (non-infinite, non-NaN) values. abstract boolean
hasInfinite()
Checks if the matrix contains any infinite values. abstract boolean
hasMultipleFinite()
Checks if more than one finite value exists. abstract boolean
hasNaN()
Checks if the matrix contains any NaN values. abstract DMatrix
multMatrix(DMatrix B, DMatrix output)
Performs matrix multiplication with sparse matrices. abstract void
removeZeros()
Removes zero-valued elements from the matrix with default tolerance. abstract void
removeZerosWithTol(double tolerance)
Removes zero-valued elements from the matrix with specified tolerance. abstract void
reshape(int numRows, int numCols)
Reshapes the matrix to the specified dimensions. abstract void
scaleInPlace(double alpha)
Scales the matrix in-place by the specified factor. abstract void
scaleMatrix(double scalar, DMatrix output)
abstract void
set(int row, int col, double value)
Sets the value at the specified matrix position. abstract void
setColumnIndex(int col, int value)
abstract void
setNonZeroRow(int index, int row)
abstract void
setNonZeroValue(int index, double value)
abstract void
shrinkNumCols(int newmax)
Reduce the maximum number of columns by setting the internal column count. abstract void
shrinkNumRows(int newmax)
Reduce the maximum number of rows by setting the internal row count. abstract DMatrix
sumColsRaw()
abstract DMatrix
sumRowsRaw()
String
toString()
Returns a string representation of the matrix. abstract void
transposeMatrix(DMatrix output)
Computes the transpose of this matrix. abstract void
zero()
Sets all elements in the matrix to zero. -
-
Method Detail
-
absEq
abstract void absEq()
Replaces each value in the matrix with its absolute value, in-place.
-
addMatrices
abstract 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
-
any
abstract 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
abstract 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
abstract void changeSign()
-
copy
abstract BaseMatrix copy()
Creates and returns a deep copy of this matrix.
- Returns:
a new matrix that is a copy of this instance
-
colIncrease
abstract 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
abstract void concatColumnsInPlace(DMatrix left, DMatrix right, DMatrix output)
Concatenates matrices column-wise.
- Parameters:
left
- the left matrixright
- the right matrixoutput
- the result matrix
-
concatRowsInPlace
abstract void concatRowsInPlace(DMatrix top, DMatrix bottom, DMatrix output)
Concatenates matrices row-wise.
- Parameters:
top
- the top matrixbottom
- the bottom matrixoutput
- the result matrix
-
countEachRow
abstract 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 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 (may be ignored by dense matrices)- Returns:
a new instance of the concrete matrix type
-
determinant
abstract double determinant()
-
divideInPlace
abstract void divideInPlace(double scalar)
-
divideMatrix
abstract void divideMatrix(double scalar, DMatrix output)
-
divideRowsByArray
abstract void divideRowsByArray(Array<double> diag, int offset)
-
divideScalarByMatrix
abstract void divideScalarByMatrix(double scalar, DMatrix output)
-
elementMax
abstract double elementMax()
-
elementMaxAbs
abstract double elementMaxAbs()
-
elementMin
abstract double elementMin()
-
elementMult
abstract DMatrix elementMult(DMatrix A, DMatrix B, DMatrix output)
-
elementSum
abstract double elementSum()
-
equals
abstract boolean equals(Object obj)
Indicates whether some other object is "equal to" this matrix.
- Parameters:
obj
- the reference object with which to compare- Returns:
true
if this object is the same as the obj argument;false
otherwise
-
extractMatrix
abstract void extractMatrix(DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, DMatrix dst, int dstY0, int dstX0)
-
fillMatrix
abstract void fillMatrix(double value)
Fills the matrix with the specified value.
- Parameters:
value
- the value to fill the matrix with
-
findNonNegative
abstract BaseMatrix findNonNegative()
Returns a matrix containing indices of all non-negative elements.
-
get
abstract 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
abstract int getColumnIndex(int col)
-
getColumnIndicesArray
abstract Array<int> getColumnIndicesArray()
-
getData
abstract Object getData()
Returns the underlying data structure. For sparse matrices, returns DMatrix. For dense matrices, may return a wrapped DMatrixRMaj or converted sparse representation.
- Returns:
the underlying data structure
-
setData
abstract void setData(Object newData)
Sets the underlying data structure. Implementation depends on the concrete matrix type.
- Parameters:
newData
- the new data structure
-
getNonZeroLength
abstract 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
abstract void setNonZeroLength(int length)
-
getNonZeroRow
abstract int getNonZeroRow(int index)
-
getNonZeroRows
abstract 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
abstract double getNonZeroValue(int index)
-
getNonZeroValues
abstract 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
abstract 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
abstract int getNumCols()
Returns the number of columns in the matrix.
- Returns:
the number of columns
-
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
abstract int getNumRows()
Returns the number of rows in the matrix.
- Returns:
the number of rows
-
hasDuplicates
abstract boolean hasDuplicates()
Checks if the matrix contains duplicate values.
- Returns:
true if duplicates exist
-
hasFinite
abstract boolean hasFinite()
Checks if the matrix contains any finite (non-infinite, non-NaN) values.
- Returns:
true if at least one finite value exists
-
hasInfinite
abstract boolean hasInfinite()
Checks if the matrix contains any infinite values.
- Returns:
true if at least one infinite value exists
-
hasMultipleFinite
abstract boolean hasMultipleFinite()
Checks if more than one finite value exists.
- Returns:
true if more than one finite value exists
-
hasNaN
abstract boolean hasNaN()
Checks if the matrix contains any NaN values.
- Returns:
true if at least one NaN exists
-
multMatrix
abstract DMatrix multMatrix(DMatrix B, DMatrix output)
Performs matrix multiplication with sparse matrices.
- Parameters:
B
- the right matrixoutput
- the result matrix
-
removeZeros
abstract 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
abstract void removeZerosWithTol(double tolerance)
Removes zero-valued elements from the matrix with specified tolerance.
- Parameters:
tolerance
- the tolerance for considering values as zero
-
reshape
abstract 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
abstract void scaleInPlace(double alpha)
Scales the matrix in-place by the specified factor.
- Parameters:
alpha
- the scaling factor
-
scaleMatrix
abstract void scaleMatrix(double scalar, DMatrix output)
-
set
abstract 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
abstract void setColumnIndex(int col, int value)
-
setNonZeroRow
abstract void setNonZeroRow(int index, int row)
-
setNonZeroValue
abstract void setNonZeroValue(int index, double value)
-
shrinkNumCols
abstract 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
abstract 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
abstract DMatrix sumColsRaw()
-
sumRowsRaw
abstract 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
-
transposeMatrix
abstract void transposeMatrix(DMatrix output)
Computes the transpose of this matrix.
- Parameters:
output
- the transposed matrix
-
zero
abstract void zero()
Sets all elements in the matrix to zero.
-
-
-
-