Class CooFieldTensor<T extends Field<T>>
- Type Parameters:
T
- The type of elements stored in this tensor, constrained by theField
interface.
- All Implemented Interfaces:
Serializable
,FieldTensorMixin<CooFieldTensor<T>,
,CooFieldTensor<T>, T> TensorOverField<CooFieldTensor<T>,
,CooFieldTensor<T>, T[], T> RingTensorMixin<CooFieldTensor<T>,
,CooFieldTensor<T>, T> TensorOverRing<CooFieldTensor<T>,
,CooFieldTensor<T>, T[], T> SemiringTensorMixin<CooFieldTensor<T>,
,CooFieldTensor<T>, T> TensorOverSemiring<CooFieldTensor<T>,
CooFieldTensor<T>, T[], T>
Field
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)
Complex128[] data = {
new Complex128(1, 2), new Complex128(3, 4), new Complex128(5, 6), new Complex128(7, 8)
};
int[][] indices = {
{0, 1, 2, 3},
{1, 2, 3, 4},
{12, 22, 40, 3},
{12, 22, 41, 0}
};
// Create COO tensor.
CooFieldTensor<Complex128> tensor = new CooFieldTensor(shape, data, indices);
// Compute element-wise sum.
CooFieldTensor<Complex128> sum = tensor.add(tensor);
// Sum of all non-zero entries.
Complex128 = tensor.sum();
// Reshape tensor.
CooFieldTensor<Complex128> reshaped = tensor.reshape(15, 150, 45)
// Compute tensor dot product (result is 5-by-5 dense tensor).
FieldTensor<Complex128> dot = tensor.dot(tensor,
new int[]{0, 1, 2},
new int[]{0, 1, 2}
);
// Compute tensor transposes.
CooFieldTensor<Complex128> 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
ConstructorsConstructorDescriptionCooFieldTensor
(Shape shape, List<T> entries, List<int[]> indices) Creates a tensor with the specified data and shape.CooFieldTensor
(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 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> entries, List<int[]> indices) Constructs a sparse tensor of the same type as this tensor with the given the shape, non-zero data, and non-zero indices.makeLikeTensor
(Shape shape, T[] entries) Constructs a sparse tensor of the same type as this tensor with the same indices as this sparse tensor and with the provided the shape and data.makeLikeTensor
(Shape shape, T[] entries, int[][] indices) Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.Sets the element of this tensor at the specified indices.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.field_arrays.AbstractCooFieldTensor
abs, div, H, H, isFinite, isInfinite, isNaN, sqrt
Methods inherited from class org.flag4j.arrays.backend.ring_arrays.AbstractCooRingTensor
sub
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, 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.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.ring_arrays.TensorOverRing
H, sub
-
Constructor Details
-
CooFieldTensor
creates a tensor with the specified data and shape.- Parameters:
shape
- shape of this tensor.entries
- 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
-
-
CooFieldTensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.entries
- 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<CooFieldTensor<T extends Field<T>>,
FieldTensor<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the tensor to construct.entries
- 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.
-
set
Sets the element of this tensor at the specified indices.- Overrides:
set
in classAbstractCooSemiringTensor<CooFieldTensor<T extends Field<T>>,
FieldTensor<T extends Field<T>>, T extends Field<T>> - Parameters:
value
- New value to set the specified index of this tensor to.index
- Indices of the element to set.- Returns:
- A copy of this tensor with the updated value is returned.
- Throws:
IndexOutOfBoundsException
- Ifindices
is not within the bounds of this tensor.
-
makeLikeTensor
Constructs a sparse tensor of the same type as this tensor with the same indices as this sparse tensor and with the provided the shape and data.- Specified by:
makeLikeTensor
in interfaceTensorOverSemiring<CooFieldTensor<T extends Field<T>>,
CooFieldTensor<T extends Field<T>>, T extends Field<T>[], T extends Field<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<CooFieldTensor<T extends Field<T>>,
T extends Field<T>[], T extends Field<T>> - Parameters:
shape
- Shape of the sparse tensor to construct.entries
- Entries of the spares tensor to construct.- Returns:
- A sparse tensor of the same type as this tensor with the same indices as this sparse tensor and with the provided the shape and data.
-
makeLikeTensor
Constructs a sparse tensor of the same type as this tensor with the given the shape, non-zero data, and non-zero indices.- Specified by:
makeLikeTensor
in classAbstractCooSemiringTensor<CooFieldTensor<T extends Field<T>>,
FieldTensor<T extends Field<T>>, T extends Field<T>> - Parameters:
shape
- Shape of the sparse tensor to construct.entries
- Non-zero data of the sparse tensor to construct.indices
- Non-zero indices of the sparse tensor to construct.- Returns:
- A sparse tensor of the same type as this tensor with the given the shape and data.
-
makeLikeDenseTensor
Constructs a dense tensor that is a similar type as this sparse COO tensor.- Specified by:
makeLikeDenseTensor
in classAbstractCooSemiringTensor<CooFieldTensor<T extends Field<T>>,
FieldTensor<T extends Field<T>>, T extends Field<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.
-
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
CooFieldTensor
. False otherwise.
-
hashCode
-
toString
-