Class CooSemiringTensor<T extends Semiring<T>>
- Type Parameters:
T
- The type of elements stored in this tensor, constrained by theSemiring
interface.
- All Implemented Interfaces:
Serializable
,SemiringTensorMixin<CooSemiringTensor<T>,
,CooSemiringTensor<T>, T> TensorOverSemiring<CooSemiringTensor<T>,
CooSemiringTensor<T>, T[], T>
Semiring
type.
The COO format stores sparse data as a list of coordinates (indices) coupled with their corresponding non-zero values, rather than allocating memory for every element in the full tensor shape. This allows efficient representation and manipulation of high-dimensional tensors that contain a substantial number of zeros.
A sparse COO tensor is stored as:
- Shape: The full
AbstractTensor.shape
of the tensor specifying its total dimensionality and size along each dimension. Although the shape is fixed, it can represent tensors of any rank. - 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. To remove any explicitly defined zeros in the tensor useAbstractCooSemiringTensor.dropZeros()
. The
AbstractCooSemiringTensor.indices
of the non-zero value in the sparse tensor. Many ops assume indices to be sorted in a row-major format (i.e. last index increased fastest) but often this is not explicitly verified.- Indices: The
AbstractCooSemiringTensor.indices
array, which has dimensions(nnz, rank)
, associates each non-zero value with its coordinates in the tensor. Here,AbstractCooSemiringTensor.nnz
is the count of non-zero elements andAbstractTensor.rank
is the tensor’s number of dimensions. Each row inAbstractCooSemiringTensor.indices
corresponds to the multidimensional index of the corresponding entry inAbstractTensor.data
.
The total number of non-zero elements (AbstractCooSemiringTensor.nnz
) and the shape are fixed for a given instance, but the specific
values in AbstractTensor.data
and their corresponding AbstractCooSemiringTensor.indices
may be updated. Many operations assume the indices
are sorted lexicographically in row-major order (i.e., the last dimension’s index varies fastest), although this is not explicitly
enforced. All provided operations will preserve lexicographically row-major sorting of the indices.
If there is any doubt that the indices of this tensor may not be sorted, use AbstractCooSemiringTensor.sortIndices()
to insure the indices 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 AbstractCooSemiringTensor.coalesce()
or AbstractCooSemiringTensor.coalesce(BinaryOperator)
.
COO tensors are optimized for "hyper-sparse" tensors 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:
// Define shape, data, and indices.
Shape shape = new Shape(15, 30, 45, 5)
BoolSemiring[] data = {
new BoolSemiring(true), new BoolSemiring(false), new BoolSemiring(false), new BoolSemiring(true)
};
int[][] indices = {
{0, 1, 2, 3},
{1, 2, 3, 4},
{12, 22, 40, 3},
{12, 22, 41, 0}
};
// Create COO tensor.
CooSemiringTensor<BoolSemiring> tensor = new CooSemiringTensor(shape, data, indices);
// Compute element-wise sum.
CooSemiringTensor<BoolSemiring> sum = tensor.add(tensor);
// Sum of all non-zero entries.
RealInt32 = tensor.sum();
// Reshape tensor.
CooSemiringTensor<BoolSemiring> reshaped = tensor.reshape(15, 150, 45)
// Compute tensor dot product (result is 5-by-5 dense tensor).
SemiringTensor<BoolSemiring> dot = tensor.dot(tensor,
new int[]{0, 1, 2},
new int[]{0, 1, 2}
);
// Compute tensor transposes.
CooSemiringTensor<RBoolSemiring> transpose = tensor.T();
transpose = tensor.T(0, 1);
transpose = tensor.T(1, 3, 0, 2);
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringTensor
indices, nnz, sparsity, zeroElement
Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionCooSemiringTensor
(Shape shape, List<T> data, List<int[]> indices) Creates a tensor with the specified data and shape.CooSemiringTensor
(Shape shape, T[] data, int[][] indices) Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if an object is equal to this tensor object.int
hashCode()
makeLikeDenseTensor
(Shape shape, T[] entries) Constructs a dense tensor that is a similar type as this sparse COO tensor.makeLikeTensor
(Shape shape, List<T> data, List<int[]> indices) Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.makeLikeTensor
(Shape shape, T[] entries) Constructs a tensor of the same type as this tensor with the given theshape
anddata
.makeLikeTensor
(Shape shape, T[] data, int[][] indices) Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.toMatrix()
Converts this tensor to an equivalent matrix.Converts this tensor to a matrix with the specified shape.toString()
Formats this sparse COO tensor as a human-readable string specifying the full shape, non-zero data, and non-zero indices.toVector()
Converts this tensor to an equivalent vector.Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringTensor
add, argmax, argmin, coalesce, coalesce, copy, density, dropZeros, elemMult, flatten, flatten, get, getZeroElement, max, min, reshape, set, setZeroElement, sortIndices, sparsity, T, T, T, tensorDot, tensorTr, toDense
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.semiring_arrays.SemiringTensorMixin
add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sum
-
Constructor Details
-
CooSemiringTensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.data
- Non-zero data of this tensor 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
-
-
CooSemiringTensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.data
- Non-zero data of this tensor 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
-
-
-
Method Details
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.- Specified by:
makeLikeTensor
in classAbstractCooSemiringTensor<CooSemiringTensor<T extends Semiring<T>>,
SemiringTensor<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the tensor to construct.data
- Non-zero data of the tensor to construct.indices
- Indices of the non-zero data of the tensor.- Returns:
- A tensor of the same type as this tensor with the specified shape and non-zero data.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.- Specified by:
makeLikeTensor
in classAbstractCooSemiringTensor<CooSemiringTensor<T extends Semiring<T>>,
SemiringTensor<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the tensor to construct.data
- Non-zero data of the tensor to construct.indices
- Indices of the non-zero data of the tensor.- Returns:
- A tensor of the same type as this tensor with the specified shape and non-zero data.
-
makeLikeDenseTensor
Constructs a dense tensor that is a similar type as this sparse COO tensor.- Specified by:
makeLikeDenseTensor
in classAbstractCooSemiringTensor<CooSemiringTensor<T extends Semiring<T>>,
SemiringTensor<T extends Semiring<T>>, T extends Semiring<T>> - Parameters:
shape
- Shape of the tensor to construct.entries
- The data of the dense tensor to construct.- Returns:
- A dense tensor that is a similar type as this sparse COO tensor.
-
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<CooSemiringTensor<T extends Semiring<T>>,
CooSemiringTensor<T extends Semiring<T>>, T extends Semiring<T>[], T extends Semiring<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<CooSemiringTensor<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
.
-
toVector
Converts this tensor to an equivalent vector. If this tensor is not rank 1, then it will be flattened.- Returns:
- A vector equivalent of this tensor.
-
toMatrix
Converts this tensor to a matrix with the specified shape.- Parameters:
matShape
- Shape of the resulting matrix. Must bebroadcastable
with the shape of this tensor.- Returns:
- A matrix of shape
matShape
with the values of this tensor. - Throws:
LinearAlgebraException
- IfmatShape
is not of rank 2.
-
toMatrix
Converts this tensor to an equivalent matrix.- Returns:
- If this tensor is rank 2, then the equivalent matrix will be returned. If the tensor is rank 1, then a matrix with a single row will be returned. If the rank of this tensor is larger than 2, it will be flattened to a single row.
-
equals
Checks if an object is equal to this tensor object.- Overrides:
equals
in classObject
- Parameters:
object
- Object to check equality with this tensor.- Returns:
- True if the two tensors have the same shape, are numerically equivalent, and are of type
CooSemiringTensor
. False otherwise.
-
hashCode
-
toString
-