Package jline.util.matrix
Class BaseMatrix
java.lang.Object
jline.util.matrix.BaseMatrix
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
DenseMatrix,SparseMatrix
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
- Author:
- QORE Lab, Imperial College London
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidabsEq()Replaces each value in the matrix with its absolute value, in-place.protected abstract org.ejml.data.DMatrixaddMatrices(double alpha, org.ejml.data.DMatrix A, double beta, org.ejml.data.DMatrix B, org.ejml.data.DMatrix output) Performs the operation: output = alpha*A + beta*B.abstract booleanany()Checks if the current matrix contains any non-zero elements.protected abstract voidapplyConditionalTransform(double source, double target, double tol, String operation) Applies a transformation to matrix elements based on conditions.protected abstract voidabstract voidcolIncrease(int col, double a) Adds a scalar value to each element in the specified column.protected abstract voidconcatColumnsInPlace(org.ejml.data.DMatrix left, org.ejml.data.DMatrix right, org.ejml.data.DMatrix output) Concatenates matrices column-wise.protected abstract voidconcatRowsInPlace(org.ejml.data.DMatrix top, org.ejml.data.DMatrix bottom, org.ejml.data.DMatrix output) Concatenates matrices row-wise.abstract BaseMatrixcopy()Creates and returns a deep copy of this matrix.abstract BaseMatrixcountEachRow(double val) Counts occurrences of each unique value in each row.protected abstract BaseMatrixcreateNewInstance(int rows, int cols, int nzLength) Creates a new instance of the concrete matrix implementation.protected abstract doubleprotected abstract voiddivideInPlace(double scalar) protected abstract voiddivideMatrix(double scalar, org.ejml.data.DMatrix output) protected abstract voiddivideRowsByArray(double[] diag, int offset) protected abstract voiddivideScalarByMatrix(double scalar, org.ejml.data.DMatrix output) protected abstract doubleprotected abstract doubleprotected abstract doubleprotected abstract org.ejml.data.DMatrixelementMult(org.ejml.data.DMatrix A, org.ejml.data.DMatrix B, org.ejml.data.DMatrix output) protected abstract doubleabstract booleanIndicates whether some other object is "equal to" this matrix.protected abstract voidextractMatrix(org.ejml.data.DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, org.ejml.data.DMatrix dst, int dstY0, int dstX0) protected abstract voidfillMatrix(double value) Fills the matrix with the specified value.abstract BaseMatrixReturns a matrix containing indices of all non-negative elements.abstract doubleget(int row, int col) Returns the value at the specified matrix position.protected abstract intgetColumnIndex(int col) protected abstract int[]protected abstract ObjectgetData()Returns the underlying data structure.abstract intReturns the number of stored non-zero elements.protected abstract intgetNonZeroRow(int index) abstract int[]Returns array of row indices of non-zero entries.abstract intReturns the number of non-zero elements in the matrix.protected abstract doublegetNonZeroValue(int index) abstract double[]Returns array of non-zero values.abstract intReturns the number of columns in the matrix.intReturns the number of non-zero elements in the matrix.abstract intReturns the number of rows in the matrix.abstract booleanChecks if the matrix contains duplicate values.abstract booleanChecks if the matrix contains any finite (non-infinite, non-NaN) values.abstract booleanChecks if the matrix contains any infinite values.abstract booleanChecks if more than one finite value exists.abstract booleanhasNaN()Checks if the matrix contains any NaN values.protected abstract org.ejml.data.DMatrixmultMatrix(org.ejml.data.DMatrix B, org.ejml.data.DMatrix output) Performs matrix multiplication with sparse matrices.protected abstract voidRemoves zero-valued elements from the matrix with default tolerance.protected abstract voidremoveZerosWithTol(double tolerance) Removes zero-valued elements from the matrix with specified tolerance.abstract voidreshape(int numRows, int numCols) Reshapes the matrix to the specified dimensions.protected abstract voidscaleInPlace(double alpha) Scales the matrix in-place by the specified factor.protected abstract voidscaleMatrix(double scalar, org.ejml.data.DMatrix output) abstract voidset(int row, int col, double value) Sets the value at the specified matrix position.protected abstract voidsetColumnIndex(int col, int value) protected abstract voidSets the underlying data structure.protected abstract voidsetNonZeroLength(int length) protected abstract voidsetNonZeroRow(int index, int row) protected abstract voidsetNonZeroValue(int index, double value) abstract voidshrinkNumCols(int newmax) Reduce the maximum number of columns by setting the internal column count.abstract voidshrinkNumRows(int newmax) Reduce the maximum number of rows by setting the internal row count.protected abstract org.ejml.data.DMatrixprotected abstract org.ejml.data.DMatrixtoString()Returns a string representation of the matrix.protected abstract voidtransposeMatrix(org.ejml.data.DMatrix output) Computes the transpose of this matrix.abstract voidzero()Sets all elements in the matrix to zero.
-
Constructor Details
-
BaseMatrix
public BaseMatrix()
-
-
Method Details
-
absEq
public abstract void absEq()Replaces each value in the matrix with its absolute value, in-place. -
addMatrices
protected abstract org.ejml.data.DMatrix addMatrices(double alpha, org.ejml.data.DMatrix A, double beta, org.ejml.data.DMatrix B, org.ejml.data.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
public abstract boolean any()Checks if the current matrix contains any non-zero elements.- Returns:
trueif at least one non-zero element exists;falseotherwise.
-
applyConditionalTransform
protected 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
protected abstract void changeSign() -
copy
Creates and returns a deep copy of this matrix.- Returns:
- a new matrix that is a copy of this instance
-
colIncrease
public 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
protected abstract void concatColumnsInPlace(org.ejml.data.DMatrix left, org.ejml.data.DMatrix right, org.ejml.data.DMatrix output) Concatenates matrices column-wise.- Parameters:
left- the left matrixright- the right matrixoutput- the result matrix
-
concatRowsInPlace
protected abstract void concatRowsInPlace(org.ejml.data.DMatrix top, org.ejml.data.DMatrix bottom, org.ejml.data.DMatrix output) Concatenates matrices row-wise.- Parameters:
top- the top matrixbottom- the bottom matrixoutput- the result matrix
-
countEachRow
Counts occurrences of each unique value in each row. -
createNewInstance
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
protected abstract double determinant() -
divideInPlace
protected abstract void divideInPlace(double scalar) -
divideMatrix
protected abstract void divideMatrix(double scalar, org.ejml.data.DMatrix output) -
divideRowsByArray
protected abstract void divideRowsByArray(double[] diag, int offset) -
divideScalarByMatrix
protected abstract void divideScalarByMatrix(double scalar, org.ejml.data.DMatrix output) -
elementMax
protected abstract double elementMax() -
elementMaxAbs
protected abstract double elementMaxAbs() -
elementMin
protected abstract double elementMin() -
elementMult
protected abstract org.ejml.data.DMatrix elementMult(org.ejml.data.DMatrix A, org.ejml.data.DMatrix B, org.ejml.data.DMatrix output) -
elementSum
protected abstract double elementSum() -
equals
Indicates whether some other object is "equal to" this matrix. -
extractMatrix
protected abstract void extractMatrix(org.ejml.data.DMatrix src, int srcX0, int srcX1, int srcY0, int srcY1, org.ejml.data.DMatrix dst, int dstY0, int dstX0) -
fillMatrix
protected abstract void fillMatrix(double value) Fills the matrix with the specified value.- Parameters:
value- the value to fill the matrix with
-
findNonNegative
Returns a matrix containing indices of all non-negative elements. -
get
public 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
protected abstract int getColumnIndex(int col) -
getColumnIndicesArray
protected abstract int[] getColumnIndicesArray() -
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
Sets the underlying data structure. Implementation depends on the concrete matrix type.- Parameters:
newData- the new data structure
-
getNonZeroLength
public 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
protected abstract void setNonZeroLength(int length) -
getNonZeroRow
protected abstract int getNonZeroRow(int index) -
getNonZeroRows
public abstract 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
protected abstract double getNonZeroValue(int index) -
getNonZeroValues
public abstract 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
public 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
public abstract int getNumCols()Returns the number of columns in the matrix.- Returns:
- the number of columns
-
getNumNonZeros
public int getNumNonZeros()Returns the number of non-zero elements in the matrix. This is an alias forgetNonZeros().- Returns:
- the number of non-zero elements
-
getNumRows
public abstract int getNumRows()Returns the number of rows in the matrix.- Returns:
- the number of rows
-
hasDuplicates
public abstract boolean hasDuplicates()Checks if the matrix contains duplicate values.- Returns:
- true if duplicates exist
-
hasFinite
public 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
public abstract boolean hasInfinite()Checks if the matrix contains any infinite values.- Returns:
- true if at least one infinite value exists
-
hasMultipleFinite
public abstract boolean hasMultipleFinite()Checks if more than one finite value exists.- Returns:
- true if more than one finite value exists
-
hasNaN
public abstract boolean hasNaN()Checks if the matrix contains any NaN values.- Returns:
- true if at least one NaN exists
-
multMatrix
protected abstract org.ejml.data.DMatrix multMatrix(org.ejml.data.DMatrix B, org.ejml.data.DMatrix output) Performs matrix multiplication with sparse matrices.- Parameters:
B- the right matrixoutput- the result matrix
-
removeZeros
protected 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
protected 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
public 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
protected abstract void scaleInPlace(double alpha) Scales the matrix in-place by the specified factor.- Parameters:
alpha- the scaling factor
-
scaleMatrix
protected abstract void scaleMatrix(double scalar, org.ejml.data.DMatrix output) -
set
public 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
protected abstract void setColumnIndex(int col, int value) -
setNonZeroRow
protected abstract void setNonZeroRow(int index, int row) -
setNonZeroValue
protected abstract void setNonZeroValue(int index, double value) -
shrinkNumCols
public 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
public 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
protected abstract org.ejml.data.DMatrix sumColsRaw() -
sumRowsRaw
protected abstract org.ejml.data.DMatrix sumRowsRaw() -
toString
Returns a string representation of the matrix. Elements are formatted to 4 decimal places in a grid layout. -
transposeMatrix
protected abstract void transposeMatrix(org.ejml.data.DMatrix output) Computes the transpose of this matrix.- Parameters:
output- the transposed matrix
-
zero
public abstract void zero()Sets all elements in the matrix to zero.
-