Class CooSemiringVector<T extends Semiring<T>>
- Type Parameters:
T
- The type of elements stored in this vector, constrained by theSemiring
interface.
- All Implemented Interfaces:
Serializable
,SemiringTensorMixin<CooSemiringVector<T>,
,SemiringVector<T>, T> TensorOverSemiring<CooSemiringVector<T>,
,SemiringVector<T>, T[], T> VectorMixin<CooSemiringVector<T>,
CooSemiringMatrix<T>, SemiringMatrix<T>, T>
Semiring
type.
The COO format stores sparse vector data as a list of coordinates (indices) coupled with their corresponding non-zero values, rather than allocating memory for every element in the full vector shape. This allows efficient representation and manipulation of large vector containing a substantial number of zeros.
A sparse COO vector is stored as:
- Shape: The full
AbstractTensor.shape
/VectorMixin.size()
of the vector specifying the total number of values (including zeros) in the vector. - 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, useAbstractCooSemiringVector.dropZeros()
. - Indices: Non-zero values are associated with their coordinates in the vector via a single 1D array:
AbstractCooSemiringVector.indices
. This array specifies the positions of each non-zero entry inAbstractTensor.data
. The total number of non-zero elements is given byAbstractCooSemiringVector.nnz
.
The total number of non-zero elements (AbstractCooSemiringVector.nnz
) and the shape/size is fixed for a given instance, but the values
in AbstractTensor.data
and their corresponding AbstractCooSemiringVector.indices
may be updated. Many operations
assume that the indices are sorted lexicographically, 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 AbstractCooSemiringVector.sortIndices()
to ensure they are sorted. COO tensors may also store multiple entries
for the same index (referred to as an uncoalesced tensor). To combine all duplicated entries use AbstractCooSemiringVector.coalesce()
or
AbstractCooSemiringVector.coalesce(BinaryOperator)
.
COO vectors 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 vector.
Shape shape = new Shape(512);
BoolSemiring[] data = {
new BoolSemiring(true), new BoolSemiring(false), new BoolSemiring(false),
new BoolSemiring(false), new BoolSemiring(true), new BoolSemiring(true)
};
int[] indices = {0, 4, 128, 128, 128, 256};
// Create COO vector.
CooSemiringVector<BoolSemiring> vector = new CooSemiringVector(shape, data, indices);
// Sum vectors.
CooSemiringVector<BoolSemiring> sum = vector.add(vector);
// Compute vector inner product.
BoolSemiring prod = vector.inner(vector);
// Compute vector outer product.
SemiringMatrix<BoolSemiring> prod = vector.outer(vector);
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringVector
indices, nnz, size, sparsity
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionCooSemiringVector
(int size) Creates a zero vector of the specifiedsize
.CooSemiringVector
(int size, List<T> entries, List<Integer> indices) Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices.CooSemiringVector
(int size, T[] entries, int[] indices) Creates a tensor with the specified data and shape.Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices.CooSemiringVector
(Shape shape, T[] entries, int[] indices) Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if an object is equal to this vector object.int
hashCode()
makeLikeDenseMatrix
(Shape shape, T... entries) Constructs a dense matrix of a similar type as this vector with the specified shape and data.makeLikeDenseTensor
(Shape shape, T... entries) Constructs a dense vector of a similar type as this vector with the specified shape and data.makeLikeMatrix
(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a COO matrix with the specified shape, non-zero data, and row and column indices.Constructs a COO vector 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[] indices) Constructs a sparse COO vector of the same type as this vector with the specified non-zero data and indices.toMatrix
(boolean columVector) Converts a vector to an equivalent matrix representing either a row or column vector.toString()
Formats this tensor as a human-readable string.toTensor()
Converts this matrix to an equivalent rank 1 tensor.Converts this vector to an equivalent tensor with the specified shape.Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringVector
add, coalesce, coalesce, copy, density, dot, dropZeros, elemMult, flatten, flatten, get, get, getZeroElement, inner, join, length, mag, normalize, outer, repeat, reshape, set, setZeroElement, sortIndices, sparsity, stack, T, T, tensorDot, tensorTr, toDense
Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, reshape, sameShape, T, totalEntries
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin
add, addEq, argmax, argmin, isOnes, isZeros, makeEmptyDataArray, max, min, mult, multEq, prod, sum
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring
getData, getRank, getShape, tensorDot, tensorDot, tensorDot, tensorTr
Methods inherited from interface org.flag4j.arrays.backend.VectorMixin
size, stack, toMatrix
-
Constructor Details
-
CooSemiringVector
Creates a tensor with the specified data and shape.- Parameters:
size
-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.indices
-
-
CooSemiringVector
Creates a tensor with the specified data and shape.- Parameters:
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.indices
-size
-
-
CooSemiringVector
Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices.- Parameters:
entries
- The non-zero data of this vector.indices
- The indices of the non-zero values.Shape
- The shape of this vector. Must be rank-1.
-
CooSemiringVector
public CooSemiringVector(int size) Creates a zero vector of the specifiedsize
. -
CooSemiringVector
-
-
Method Details
-
makeLikeTensor
Constructs a sparse COO vector of the same type as this vector with the specified non-zero data and indices.- Specified by:
makeLikeTensor
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the vector to construct.entries
- Non-zero data of the vector to construct.indices
- Non-zero row indices of the vector to construct.- Returns:
- A sparse COO vector of the same type as this vector with the specified non-zero data and indices.
-
makeLikeDenseTensor
Constructs a dense vector of a similar type as this vector with the specified shape and data.- Specified by:
makeLikeDenseTensor
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the vector to construct.entries
- Entries of the vector to construct.- Returns:
- A dense vector of a similar type as this vector with the specified data.
-
makeLikeDenseMatrix
Constructs a dense matrix of a similar type as this vector with the specified shape and data.- Specified by:
makeLikeDenseMatrix
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the matrix to construct.entries
- Entries of the matrix to construct.- Returns:
- A dense matrix of a similar type as this vector with the specified data.
-
makeLikeTensor
Constructs a COO vector with the specified shape, non-zero data, and non-zero indices.- Specified by:
makeLikeTensor
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the vector.entries
- Non-zero values of the vector.indices
- Indices of the non-zero values in the vector.- Returns:
- A COO vector of the same type as this vector with the specified shape, non-zero data, and non-zero indices.
-
makeLikeMatrix
public CooSemiringMatrix<T> makeLikeMatrix(Shape shape, T[] entries, int[] rowIndices, int[] colIndices) Constructs a COO matrix with the specified shape, non-zero data, and row and column indices.- Specified by:
makeLikeMatrix
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the matrix to construct.entries
- Non-zero data of the matrix.rowIndices
- Row indices of the matrix.colIndices
- Column indices of the matrix.- Returns:
- A COO matrix of similar type as this vector with the specified shape, non-zero data, and non-zero row/col indices.
-
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<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, T extends Semiring<T>[], T extends Semiring<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<CooSemiringVector<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
shape
anddata
.
-
equals
Checks if an object is equal to this vector object.- Overrides:
equals
in classObject
- Parameters:
object
- Object to check equality with this vector.- Returns:
- True if the two vectors have the same shape, are numerically equivalent, and are of type
CooFieldVector
. False otherwise.
-
hashCode
-
toMatrix
Converts a vector to an equivalent matrix representing either a row or column vector.- Specified by:
toMatrix
in interfaceVectorMixin<CooSemiringVector<T extends Semiring<T>>,
CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Overrides:
toMatrix
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
columVector
- Flag indicating whether to convert this vector to a matrix representing a row or column vector:If
true
, the vector will be converted to a matrix representing a column vector.If
false
, The vector will be converted to a matrix representing a row vector.- Returns:
- A matrix equivalent to this vector.
-
toTensor
Converts this matrix to an equivalent rank 1 tensor.- Specified by:
toTensor
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<T>> - Returns:
- A tensor which is equivalent to this matrix.
-
toTensor
Converts this vector to an equivalent tensor with the specified shape.- Specified by:
toTensor
in classAbstractCooSemiringVector<CooSemiringVector<T extends Semiring<T>>,
SemiringVector<T extends Semiring<T>>, CooSemiringMatrix<T extends Semiring<T>>, SemiringMatrix<T extends Semiring<T>>, T extends Semiring<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
-
toString
-