Class SemiringMatrix<T extends Semiring<T>>
- Type Parameters:
T- Type of thesemiringelement for the matrix.
- All Implemented Interfaces:
Serializable,MatrixMixin<SemiringMatrix<T>,,SemiringMatrix<T>, SemiringVector<T>, T> SemiringTensorMixin<SemiringMatrix<T>,,SemiringMatrix<T>, T> TensorOverSemiring<SemiringMatrix<T>,SemiringMatrix<T>, T[], T>
Instances of this class represents a dense matrix backed by a Semiring array. The SemiringMatrix class
provides functionality for matrix operations whose elements are members of a semiring, supporting mutable data with a fixed shape.
A SemiringMatrix is essentially equivalent to a rank-2 tensor but includes extended functionality
and may offer improved performance for certain operations compared to general rank-n tensors.
Key Features:
- Support for standard matrix operations like addition, subtraction, multiplication, and exponentiation.
- Conversion methods to other matrix representations, such as COO (Coordinate) and CSR (Compressed Sparse Row) formats.
- Utility methods for checking properties like being unitary, real, or complex.
Example Usage:
Using a boolean semiring:
// Constructing an integer matrix from a 2D array of booleans
BoolSemiring[][] data = {
{ new BoolSemiring(true), new BoolSemiring(false) },
{ new BoolSemiring(true), new BoolSemiring(true) }
};
SemiringMatrix<BoolSemiring> matrix = new FieldMatrix(data);
// Add matrices (equivalent to element-wise OR).
SemiringMatrix<BoolSemiring> sum = matrix.add(matrix);
// Element-wise product of matrices (equivalent to element-wise AND).
SemiringMatrix<BoolSemiring> prod = matrix.elemMult(matrix);
// Performing matrix multiplication.
SemiringMatrix<BoolSemiring> result = matrix.mult(matrix);
// Performing matrix transpose.
SemiringMatrix<BoolSemiring> transpose = matrix.T();
Using 128-bit complex numbers:
// Constructing a complex matrix from a 2D array of complex numbers
Complex128[][] complexData = {
{ new Complex128(1, 2), new Complex128(3, 4) },
{ new Complex128(5, 6), new Complex128(7, 8) }
};
SemiringMatrix<Complex128> matrix = new FieldMatrix(complexData);
// Performing matrix multiplication.
SemiringMatrix<Complex128> result = matrix.mult(matrix);
// Performing matrix transpose.
SemiringMatrix<Complex128> transpose = matrix.T();
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringMatrix
numCols, numRowsFields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
zeroElementFields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape -
Constructor Summary
ConstructorsConstructorDescriptionSemiringMatrix(int rows, int cols, T fillValue) Creates a dense semiring matrix with the specified data and filled withfilledValue.SemiringMatrix(int rows, int cos, T[] entries) Creates a dense semiring matrix with the specified data and shape.SemiringMatrix(int rows, int cols, T[][] entries) Creates a dense semiring matrix with the specified data and shape.SemiringMatrix(Shape shape, T fillValue) Creates a dense semiring matrix with the specified data and filled withfilledValue.SemiringMatrix(Shape shape, T[] entries) Creates a tensor with the specified data and shape.SemiringMatrix(T[][] entries) Creates a dense semiring matrix with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(MatrixVisitor<R> visitor) Accepts a visitor that implements theMatrixVisitorinterface.div(SemiringMatrix<T> b) Computes the element-wise quotient of two matrices.booleanChecks if an object is equal to this matrix object.inthashCode()static <T extends Semiring<T>>
SemiringMatrix<T> I(int numRows, int numCols, T fieldValue) Constructs an identity-like matrix of the specified shape.static <T extends Semiring<T>>
SemiringMatrix<T> I(int size, T fieldValue) Constructs an identity matrix of the specified size.static <T extends Semiring<T>>
SemiringMatrix<T> Constructs an identity-like matrix of the specified shape.protected CooSemiringMatrix<T> makeLikeCooMatrix(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO matrix which is of a similar type as this dense matrix.protected CooSemiringTensor<T> makeLikeCooTensor(Shape shape, T[] data, int[][] indices) Constructs a sparse COO tensor which is of a similar type as this dense tensor.makeLikeCsrMatrix(Shape shape, T[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix which is of a similar type as this dense matrix.makeLikeTensor(Shape shape, T[] entries) Constructs a tensor of the same type as this tensor with the given theshapeanddata.protected SemiringVector<T> makeLikeVector(Shape shape, T[] entries) Constructs a vector of a similar type as this matrix.protected SemiringVector<T> makeLikeVector(T[] entries) Constructs a vector of a similar type as this matrix.pow(int n) Computes the matrix multiplication of this matrix with itselfntimes.sub(SemiringMatrix<T> b) Computes the element-wise difference of two matrices.toCoo()Converts this dense tensor to an equivalent sparse COO tensor.toCoo(double estimatedSparsity) Converts this matrix to an equivalent sparse COO matrix.toCsr()Converts this matrix to an equivalent sparse CSR matrix.toCsr(double estimatedSparsity) Converts this matrix to an equivalent sparse CSR matrix.toString()Generates a human-readable string representing this matrix.toTensor()Converts this matrix to an equivalent tensor.Converts this matrix to an equivalent tensor with the specifiednewShape.Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringMatrix
augment, augment, dataLength, flatten, flatten, get, getCol, getDiag, getRow, getSlice, getTriL, getTriU, H, isHermitian, isI, isOrthogonal, isSymmetric, isTriL, isTriU, mult, mult, multTranspose, numCols, numRows, removeCol, removeCols, removeRow, removeRows, set, setCol, setCol, setRow, setRow, setSlice, setSliceCopy, setValues, stack, swapCols, swapRows, T, toVector, trMethods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor
add, addEq, argmax, argmin, copy, elemMult, get, getZeroElement, max, min, reshape, set, setZeroElement, T, T, tensorDot, tensorTrMethods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, totalEntriesMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.flag4j.arrays.backend.MatrixMixin
add, copy, elemMult, fib, getCol, getDiag, getRow, getShape, getTriL, getTriU, isDiag, isSquare, isTri, isVector, stack, trace, vectorTypeMethods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin
add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sum
-
Constructor Details
-
SemiringMatrix
Creates a tensor with the specified data and shape.- Parameters:
shape- Shape of this tensor.entries- Entries of this tensor. If this tensor is dense, this specifies all data within the tensor. If this tensor is sparse, this specifies only the non-zero data of the tensor.
-
SemiringMatrix
Creates a dense semiring matrix with the specified data and shape.- Parameters:
rows- Number of rows in the matrix.entries- Entries of this matrix.cols- Number of columns in the matrix.
-
SemiringMatrix
Creates a dense semiring matrix with the specified data and shape.- Parameters:
entries- Entries of this matrix.shape- Shape of this matrix.
-
SemiringMatrix
-
SemiringMatrix
Creates a dense semiring matrix with the specified data and shape.- Parameters:
rows- Number of rows in the matrix.cols- Number of columns in the matrix.entries- Entries of this matrix.shape- Shape of this matrix.
-
SemiringMatrix
Creates a dense semiring matrix with the specified data and filled withfilledValue.- Parameters:
rows- Number of rows in the matrix.cols- Number of columns in the matrix.fillValue- Entries of this matrix.
-
-
Method Details
-
makeLikeVector
Constructs a vector of a similar type as this matrix.- Specified by:
makeLikeVectorin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape- Shape of the vector to construct. Must be rank 1.entries- Entries of the vector.- Returns:
- A vector of a similar type as this matrix.
-
makeLikeVector
Constructs a vector of a similar type as this matrix.- Specified by:
makeLikeVectorin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
entries- Entries of the vector.- Returns:
- A vector of a similar type as this matrix.
-
makeLikeCooMatrix
protected CooSemiringMatrix<T> makeLikeCooMatrix(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO matrix which is of a similar type as this dense matrix.- Specified by:
makeLikeCooMatrixin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape- Shape of the COO matrix.entries- Non-zero data of the COO matrix.rowIndices- Non-zero row indices of the COO matrix.colIndices- Non-zero column indices of the COO matrix.- Returns:
- A sparse COO matrix which is of a similar type as this dense matrix.
-
makeLikeCsrMatrix
public CsrSemiringMatrix<T> makeLikeCsrMatrix(Shape shape, T[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix which is of a similar type as this dense matrix.- Specified by:
makeLikeCsrMatrixin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape- Shape of the CSR matrix.entries- Non-zero data of the CSR matrix.rowPointers- Non-zero row pointers of the CSR matrix.colIndices- Non-zero column indices of the CSR matrix.- Returns:
- A sparse CSR matrix which is of a similar type as this dense matrix.
-
makeLikeCooTensor
Constructs a sparse COO tensor which is of a similar type as this dense tensor.- Specified by:
makeLikeCooTensorin classAbstractDenseSemiringTensor<SemiringMatrix<T extends Semiring<T>>,T extends Semiring<T>> - Parameters:
shape- Shape of the COO tensor.data- Non-zero data of the COO tensor.indices- Non-zero indices of the COO tensor.- Returns:
- A sparse COO tensor which is of a similar type as this dense tensor.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given theshapeanddata. The resulting tensor will also have the same non-zero indices as this tensor.- Specified by:
makeLikeTensorin interfaceTensorOverSemiring<SemiringMatrix<T extends Semiring<T>>,SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>[], T extends Semiring<T>> - Specified by:
makeLikeTensorin classAbstractTensor<SemiringMatrix<T extends Semiring<T>>,T extends Semiring<T>[], T extends Semiring<T>> - Parameters:
shape- Shape of the tensor to construct.entries- Entries of the tensor to construct.- Returns:
- A tensor of the same type and with the same non-zero indices as this tensor with the given the
shapeanddata.
-
toTensor
Converts this matrix to an equivalent tensor.- Specified by:
toTensorin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Returns:
- A tensor with the same shape and data as this matrix.
-
toTensor
Converts this matrix to an equivalent tensor with the specifiednewShape.- Specified by:
toTensorin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
newShape- Shape of the tensor. Can be any rank but must be broadcastable to the shape of this matrix.- Returns:
- A tensor with the specified
newShapeand the same data as this matrix.
-
I
Constructs an identity matrix of the specified size.- Parameters:
size- Size of the identity matrix.fieldValue- Value of field to create identity matrix for.- Returns:
- An identity matrix of specified size.
- Throws:
IllegalArgumentException- If the specified size is less than 1.- See Also:
-
I
Constructs an identity-like matrix of the specified shape. That is, a matrix of zeros with ones along the principle diagonal.- Parameters:
numRows- Number of rows in the identity-like matrix.numCols- Number of columns in the identity-like matrix.fieldValue- Value of field to create identity matrix for.- Returns:
- An identity matrix of specified shape.
- Throws:
IllegalArgumentException- If the specified number of rows or columns is less than 1.- See Also:
-
I
Constructs an identity-like matrix of the specified shape. That is, a matrix of zeros with ones along the principle diagonal.- Parameters:
shape- The shape of the identity-like matrix to construct.fieldValue- Value of field to create identity matrix for.- Returns:
- An identity matrix of specified shape.
- Throws:
IllegalArgumentException- If the specified number of rows or columns is less than 1.- See Also:
-
toCoo
Converts this matrix to an equivalent sparse COO matrix.- Overrides:
toCooin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
estimatedSparsity- Estimated sparsity of the matrix. Must be between 0 and 1 inclusive. If this is an accurate estimation it may provide a slight speedup and can reduce unneeded memory consumption. If memory is a concern, it is better to over-estimate the sparsity. If speed is the concern it is better to under-estimate the sparsity.- Returns:
- A sparse COO matrix that is equivalent to this dense matrix.
- See Also:
-
toCoo
Converts this dense tensor to an equivalent sparse COO tensor.- Overrides:
toCooin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Returns:
- A sparse COO tensor equivalent to this dense tensor.
- See Also:
-
toCsr
Converts this matrix to an equivalent sparse CSR matrix.- Overrides:
toCsrin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Returns:
- A sparse CSR matrix that is equivalent to this dense matrix.
- See Also:
-
toCsr
Converts this matrix to an equivalent sparse CSR matrix.- Overrides:
toCsrin classAbstractDenseSemiringMatrix<SemiringMatrix<T extends Semiring<T>>,SemiringVector<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
estimatedSparsity- Estimated sparsity of the matrix. Must be between 0 and 1 inclusive. If this is an accurate estimation it may provide a slight speedup and can reduce unneeded memory consumption. If memory is a concern, it is better to over-estimate the sparsity. If speed is the concern it is better to under-estimate the sparsity.- Returns:
- A sparse CSR matrix that is equivalent to this dense matrix.
- See Also:
-
pow
Computes the matrix multiplication of this matrix with itself
ntimes. This matrix must be square.For large
nvalues, this method may significantly more efficient than calling#mult(Matrix) this.mult(this)ntimes.- Parameters:
n- Number of times to multiply this matrix with itself. Must be non-negative.- Returns:
- If
n=0, then the identity
-
accept
Accepts a visitor that implements theMatrixVisitorinterface. This method is part of the "Visitor Pattern" and allows operations to be performed on the matrix without modifying the matrix's class directly.- Type Parameters:
R- The return type of the visitor's operation.- Parameters:
visitor- The visitor implementing the operation to be performed.- Returns:
- The result of the visitor's operation, typically another matrix or a scalar value.
- Throws:
NullPointerException- if the visitor isnull.
-
sub
Computes the element-wise difference of two matrices.
This method will throw an
UnsupportedOperationExceptionas subtraction is not defined for a general semiring.- Parameters:
b- Second matrix in the element-wise difference.- Returns:
- The element-wise difference of this matrix and
b.
-
div
Computes the element-wise quotient of two matrices.
This method will throw an
UnsupportedOperationExceptionas division is not defined for a general semiring.- Parameters:
b- Second matrix in the element-wise quotient.- Returns:
- The element-wise quotient of this matrix and
b.
-
equals
Checks if an object is equal to this matrix object.- Overrides:
equalsin classObject- Parameters:
object- Object to check equality with this matrix.- Returns:
trueif the two matrices have the same shape, are numerically equivalent, and are of typeSemiringMatrix;falseotherwise.
-
hashCode
-
toString
-