Class CooFieldVector<T extends Field<T>>
- Type Parameters:
T
- The type of elements stored in this vector, constrained by theField
interface.
- All Implemented Interfaces:
Serializable
,FieldTensorMixin<CooFieldVector<T>,
,FieldVector<T>, T> TensorOverField<CooFieldVector<T>,
,FieldVector<T>, T[], T> RingTensorMixin<CooFieldVector<T>,
,FieldVector<T>, T> TensorOverRing<CooFieldVector<T>,
,FieldVector<T>, T[], T> SemiringTensorMixin<CooFieldVector<T>,
,FieldVector<T>, T> TensorOverSemiring<CooFieldVector<T>,
,FieldVector<T>, T[], T> VectorMixin<CooFieldVector<T>,
CooFieldMatrix<T>, FieldMatrix<T>, T>
Field
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);
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[] indices = {0, 4, 128, 128, 128, 256};
// Create COO vector.
CooFieldVector<Complex128> vector = new CooFieldVector(shape, data, indices);
// Sum vectors.
CooFieldVector<Complex128> sum = vector.add(vector);
// Compute vector inner product.
Complex128 prod = vector.inner(vector);
// Compute vector outer product.
FieldMatrix<Complex128> 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
ConstructorsConstructorDescriptionCooFieldVector
(int size) Creates a zero vector of the specifiedsize
.CooFieldVector
(int size, List<T> entries, List<Integer> indices) Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices.CooFieldVector
(int size, T[] entries, int[] indices) Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices.CooFieldVector
(Shape shape, T[] entries, int[] indices) Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices. -
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.field_arrays.AbstractCooFieldVector
abs, div, H, H, isFinite, isInfinite, isNaN, mag, normalize, sqrt
Methods inherited from class org.flag4j.arrays.backend.ring_arrays.AbstractCooRingVector
inner, sub
Methods inherited from class org.flag4j.arrays.backend.semiring_arrays.AbstractCooSemiringVector
add, coalesce, coalesce, copy, density, dot, dropZeros, elemMult, flatten, flatten, get, get, getZeroElement, join, length, 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.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.semiring_arrays.SemiringTensorMixin
argmax, argmin, max, min
Methods inherited from interface org.flag4j.arrays.backend.ring_arrays.TensorOverRing
H, sub
-
Constructor Details
-
CooFieldVector
Creates sparse COO vector with the specifiedsize
, non-zero data, and non-zero indices.- Parameters:
size
- The size of this vector.entries
- The non-zero data of this vector.indices
- The indices of the non-zero values.
-
CooFieldVector
-
CooFieldVector
-
CooFieldVector
public CooFieldVector(int size) Creates a zero vector of the specifiedsize
.
-
-
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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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 CooFieldMatrix<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, T extends Field<T>[], T extends Field<T>> - Specified by:
makeLikeTensor
in classAbstractTensor<CooFieldVector<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
.
-
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<CooFieldVector<T extends Field<T>>,
CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<T>> - Overrides:
toMatrix
in classAbstractCooSemiringVector<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<T extends Field<T>>, T extends Field<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<CooFieldVector<T extends Field<T>>,
FieldVector<T extends Field<T>>, CooFieldMatrix<T extends Field<T>>, FieldMatrix<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
-
toString
-