Class CooFieldMatrix<T extends Field<T>>
- Type Parameters:
T
- The type of elements stored in this matrix, constrained by theField
interface.
- All Implemented Interfaces:
Serializable
,FieldTensorMixin<CooFieldMatrix<T>,
,FieldMatrix<T>, T> TensorOverField<CooFieldMatrix<T>,
,FieldMatrix<T>, T[], T> MatrixMixin<CooFieldMatrix<T>,
,FieldMatrix<T>, CooFieldVector<T>, T> RingTensorMixin<CooFieldMatrix<T>,
,FieldMatrix<T>, T> TensorOverRing<CooFieldMatrix<T>,
,FieldMatrix<T>, T[], T> SemiringTensorMixin<CooFieldMatrix<T>,
,FieldMatrix<T>, T> TensorOverSemiring<CooFieldMatrix<T>,
FieldMatrix<T>, T[], T>
Instances of this class represent a sparse matrix whose non-zero elements are stored in Coordinate List (COO) format, with all
data elements belonging to a specified Field
type.
The COO format stores sparse matrix data as a list of coordinates (row and column indices) coupled with their corresponding non-zero values, rather than allocating memory for every element in the full matrix shape. This allows efficient representation and manipulation of large matrices containing a substantial number of zeros.
COO Representation:
A sparse COO matrix is stored as:- Shape: The full
AbstractTensor.shape
of the matrix specifying its number of rows and columns. - Data: Non-zero values are stored in a one-dimensional array,
AbstractTensor.data
. Any element not specified indata
is implicitly zero. It is also possible to explicitly store zero values in this array, although this is generally not desirable. To remove explicitly defined zeros, useAbstractCooSemiringMatrix.dropZeros()
. - Indices: Non-zero values are associated with their coordinates in the matrix via two parallel 1D arrays:
AbstractCooSemiringMatrix.rowIndices
andAbstractCooSemiringMatrix.colIndices
. These arrays specify the row and column positions of each non-zero entry inAbstractTensor.data
. The total number of non-zero elements is given byAbstractCooSemiringMatrix.nnz
. Each pair of indices corresponds directly to the position of a single non-zero value inAbstractTensor.data
.
The total number of non-zero elements (AbstractCooSemiringMatrix.nnz
) and the shape are fixed for a given instance, but the values
in AbstractTensor.data
and their corresponding AbstractCooSemiringMatrix.rowIndices
and AbstractCooSemiringMatrix.colIndices
may be updated. Many operations
assume that the indices are sorted lexicographically by row, and then by column, but this is not strictly enforced.
All provided operations preserve the lexicographical sorting of indices. If there is any doubt about the ordering of
indices, use AbstractCooSemiringMatrix.sortIndices()
to ensure they are explicitly sorted. COO tensors may also store multiple entries
for the same index (referred to as an uncoalesced tensor). To combine all duplicated entries use AbstractCooSemiringMatrix.coalesce()
or
AbstractCooSemiringMatrix.coalesce(BinaryOperator)
.
COO matrices are optimized for "hyper-sparse" scenarios where the proportion of non-zero elements is extremely low, offering significant memory savings and potentially more efficient computational operations than equivalent dense representations.
Example Usage:
// shape, data, and indices for COO matrix.
Shape shape = new Shape(512, 1024);
Complex128[] data = {
new Complex128(1, 2), new Complex128(3, 4), new Complex128(5, 6)
new Complex128(7, 8), new Complex128(9, 10), new Complex128(11, 12)
};
int[] rowIndices = {0, 4, 128, 128, 128, 256};
int[] colIndices = {16, 2, 5, 512, 1000, 28};
// Create COO matrix.
CooFieldMatrix<Complex128> matrix = new CooFieldMatrix<>(
shape, data, rowIndices, colIndices
);
// Sum matrices.
CooFieldMatrix<Complex128> sum = matrix.add(matrix);
// Multiply matrix to it's Hermitian transpose.
FieldMatrix<Complex128> prod = matrix.mult(matrix.H());
prod = matrix.multTranspose(matrix);
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringMatrix
colIndices, nnz, numCols, numRows, rowIndices, sparsity, zeroElement
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionCooFieldMatrix
(int rows, int cols, List<T> entries, List<Integer> rowIndices, List<Integer> colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.CooFieldMatrix
(int rows, int cols, T[] entries, int[] rowIndices, int[] colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.CooFieldMatrix
(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape. -
Method Summary
Modifier and TypeMethodDescription<R> R
accept
(MatrixVisitor<R> visitor) Accepts a visitor that implements theMatrixVisitor
interface.boolean
Checks if an object is equal to this matrix object.int
hashCode()
makeLikeCsrMatrix
(Shape shape, T[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix of a similar type to this sparse COO matrix.makeLikeDenseTensor
(Shape shape, T[] entries) Constructs a dense tensor with the specifiedshape
anddata
which is a similar type to this sparse tensor.Constructs a COO matrix with the specified shape, non-zero data, and non-zero indices.makeLikeTensor
(Shape shape, T[] entries) Constructs a tensor of the same type as this tensor with the given theshape
anddata
.makeLikeTensor
(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO tensor of the same type as this tensor with the specified non-zero data and indices.makeLikeVector
(Shape shape, T[] entries, int[] indices) Constructs a sparse COO vector of a similar type to this COO matrix.mult
(CooFieldVector<T> b) Computes the matrix-vector multiplication of a vector with this matrix.tensorDot
(CooFieldMatrix<T> src2, int[] aAxes, int[] bAxes) Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes.toCsr()
Converts this sparse COO matrix to an equivalent compressed sparse row (CSR) matrix.toString()
Formats this sparse matrix as a human-readable string.toTensor()
Converts this matrix to an equivalent tensor.Converts this matrix to an equivalent tensor with the specified shape.Methods inherited from class org.flag4j.arrays.backend.field_arrays.AbstractCooFieldMatrix
abs, div, H, H, H, isFinite, isInfinite, isNaN, multTranspose, sqrt
Methods inherited from class org.flag4j.arrays.backend.ring_arrays.AbstractCooRingMatrix
isCloseToI, sub
Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringMatrix
add, argmax, argmin, augment, augment, coalesce, coalesce, copy, dataLength, density, dropZeros, elemMult, flatten, flatten, get, get, getCol, getDiag, getRow, getSlice, getTriL, getTriU, getZeroElement, isHermitian, isI, isOrthogonal, isSymmetric, isTriL, isTriU, max, min, mult, numCols, numRows, removeCol, removeCols, removeRow, removeRows, reshape, set, set, setCol, setRow, setSliceCopy, setZeroElement, sortIndices, sparsity, stack, swapCols, swapRows, T, T, T, tensorTr, toDense, toVector, tr
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.field_arrays.FieldTensorMixin
add, add, addEq, addEq, argmax, argmaxAbs, argmin, argminAbs, conj, div, div, divEq, divEq, isOnes, isZeros, makeEmptyDataArray, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, norm, norm, prod, recip, sub, sub, subEq, subEq, sum
Methods inherited from interface org.flag4j.arrays.backend.MatrixMixin
add, augment, augment, copy, dataLength, elemMult, fib, get, getCol, getCol, getDiag, getDiag, getRow, getRow, getShape, getSlice, getTriL, getTriL, getTriU, getTriU, isDiag, isHermitian, isI, isOrthogonal, isSquare, isSymmetric, isTri, isTriL, isTriU, isVector, mult, numCols, numRows, removeCol, removeCols, removeRow, removeRows, set, setCol, setRow, setSliceCopy, stack, stack, sub, swapCols, swapRows, T, toVector, tr, trace, vectorType
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.TensorOverRing
sub
-
Constructor Details
-
CooFieldMatrix
Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.- Parameters:
shape
- Shape of this tensor.entries
- Non-zero data of this sparse matrix.rowIndices
- Non-zero row indices of this sparse matrix.colIndices
- Non-zero column indies of this sparse matrix.
-
CooFieldMatrix
public CooFieldMatrix(Shape shape, List<T> entries, List<Integer> rowIndices, List<Integer> colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.- Parameters:
shape
- Shape of this tensor.entries
- Non-zero data of this sparse matrix.rowIndices
- Non-zero row indices of this sparse matrix.colIndices
- Non-zero column indies of this sparse matrix.
-
CooFieldMatrix
Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.- Parameters:
rows
- Rows in the coo matrix.cols
- Columns in the coo matrix.entries
- Non-zero data of this sparse matrix.rowIndices
- Non-zero row indices of this sparse matrix.colIndices
- Non-zero column indies of this sparse matrix.
-
CooFieldMatrix
public CooFieldMatrix(int rows, int cols, List<T> entries, List<Integer> rowIndices, List<Integer> colIndices) Creates a sparse coo matrix with the specified non-zero data, non-zero indices, and shape.- Parameters:
rows
- Rows in the coo matrix.cols
- Columns in the coo matrix.entries
- Non-zero data of this sparse matrix.rowIndices
- Non-zero row indices of this sparse matrix.colIndices
- Non-zero column indies of this sparse matrix.
-
-
Method Details
-
makeLikeTensor
public CooFieldMatrix<T> makeLikeTensor(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a sparse COO tensor of the same type as this tensor with the specified non-zero data and indices.- Specified by:
makeLikeTensor
in classAbstractCooSemiringMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the matrix.entries
- Non-zero data of the matrix.rowIndices
- Non-zero row indices of the matrix.colIndices
- Non-zero column indices of the matrix.- Returns:
- A sparse COO tensor of the same type as this tensor with the specified non-zero data and indices.
-
makeLikeTensor
public CooFieldMatrix<T> makeLikeTensor(Shape shape, List<T> entries, List<Integer> rowIndices, List<Integer> colIndices) Constructs a COO matrix with the specified shape, non-zero data, and non-zero indices.- Specified by:
makeLikeTensor
in classAbstractCooSemiringMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the matrix.entries
- Non-zero values of the matrix.rowIndices
- Non-zero row indices of the matrix.colIndices
- Non-zero column indices of the matrix.- Returns:
- A COO matrix with the specified shape, non-zero data, and non-zero indices.
-
makeLikeVector
Constructs a sparse COO vector of a similar type to this COO matrix.- Specified by:
makeLikeVector
in classAbstractCooSemiringMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the vector. Must be rank 1.entries
- Non-zero data of the COO vector.indices
- Non-zero indices of the COO vector.- Returns:
- A sparse COO vector of a similar type to this COO matrix.
-
makeLikeDenseTensor
Constructs a dense tensor with the specifiedshape
anddata
which is a similar type to this sparse tensor.- Specified by:
makeLikeDenseTensor
in classAbstractCooSemiringMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the dense tensor.entries
- Entries of the dense tensor.- Returns:
- A dense tensor with the specified
shape
anddata
which is a similar type to this sparse tensor.
-
makeLikeCsrMatrix
public CsrFieldMatrix<T> makeLikeCsrMatrix(Shape shape, T[] entries, int[] rowPointers, int[] colIndices) Constructs a sparse CSR matrix of a similar type to this sparse COO matrix.- Specified by:
makeLikeCsrMatrix
in classAbstractCooFieldMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the CSR matrix to construct.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 CSR matrix of a similar type to this sparse COO matrix.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given theshape
anddata
. The resulting tensor will also have the same non-zero indices as this tensor.- Specified by:
makeLikeTensor
in interfaceTensorOverSemiring<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, T extends Field<T>[], T extends Field<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<CooFieldMatrix<T extends Field<T>>,
T extends Field<T>[], T extends Field<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
shape
anddata
.
-
tensorDot
Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes. That is, computes the sum of products between the two tensors along the specified set of axes.
For matrices, calling
this.tensorDot(src2, new int[]{1}, new int[]{0})
is equivalent to matrix multiplication. However, it is highly recommended to usemult(CooFieldVector)
instead.- Parameters:
src2
- Tensor to contract with this tensor.aAxes
- Axes along which to compute products for this tensor.bAxes
- Axes along which to compute products forsrc2
tensor.- Returns:
- The tensor dot product over the specified axes.
- Throws:
IllegalArgumentException
- If the two tensors shapes do not match along the specified axes pairwise inaAxes
andbAxes
.IllegalArgumentException
- IfaAxes
andbAxes
do not match in length, or if any of the axes are out of bounds for the corresponding tensor.
-
toCsr
Converts this sparse COO matrix to an equivalent compressed sparse row (CSR) matrix.
It is often easier and more efficient to construct a matrix in COO format first then convert to a CSR matrix for efficient computations.
- Overrides:
toCsr
in classAbstractCooFieldMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Returns:
- A CSR matrix equivalent to this COO matrix.
-
toTensor
Converts this matrix to an equivalent tensor.- Specified by:
toTensor
in classAbstractCooRingMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Returns:
- A tensor which is equivalent to this matrix.
-
toTensor
Converts this matrix to an equivalent tensor with the specified shape.- Specified by:
toTensor
in classAbstractCooRingMatrix<CooFieldMatrix<T extends Field<T>>,
FieldMatrix<T extends Field<T>>, CooFieldVector<T extends Field<T>>, T extends Field<T>> - Parameters:
newShape
- New shape for the tensor. Can be any rank but must be broadcastable tothis.shape
.- Returns:
- A tensor equivalent to this matrix which has been reshaped to
newShape
-
mult
Computes the matrix-vector multiplication of a vector with this matrix.- Parameters:
b
- Vector in the matrix-vector multiplication.- Returns:
- The result of multiplying this matrix with
b
. - Throws:
LinearAlgebraException
- If the number of columns in this matrix do not equal the size ofb
.
-
accept
Accepts a visitor that implements theMatrixVisitor
interface. 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
.
-
equals
Checks if an object is equal to this matrix object.- Overrides:
equals
in classObject
- Parameters:
object
- Object to check equality with this matrix.- Returns:
- True if the two matrices have the same shape, are numerically equivalent, and are of type
CooFieldMatrix
. False otherwise.
-
hashCode
-
toString
-