Class AbstractCooSemiringTensor<T extends AbstractCooSemiringTensor<T,U,V>,U extends AbstractDenseSemiringTensor<U,V>,V extends Semiring<V>>
- Type Parameters:
T- Type of this sparse COO tensor.U- Type of dense tensor equivalent toT. This type parameter is required because some ops (e.g.tensorDot(AbstractCooSemiringTensor, int[], int[])) between two sparse tensors results in a dense tensor.V- Type of theSemiringwhich the data of this tensor belong to.
- All Implemented Interfaces:
Serializable,SemiringTensorMixin<T,,T, V> TensorOverSemiring<T,T, V[], V>
- Direct Known Subclasses:
AbstractCooRingTensor,CooSemiringTensor
Base class for all sparse tensors stored in coordinate list (COO) format. The data of this COO tensor are elements of a
Semiring
The non-zero data and non-zero indices of a COO tensor are mutable but the AbstractTensor.shape
and total number of non-zero data is fixed.
Sparse tensors allow for the efficient storage of and ops on tensors that contain many zero values.
COO tensors are optimized for hyper-sparse tensors (i.e. tensors which contain almost all zeros relative to the size of the tensor).
A sparse COO tensor is stored as:
- The full
shapeof the tensor. - The non-zero
AbstractTensor.dataof the tensor. All other data in the tensor are assumed to be zero. Zero value can also explicitly be stored inAbstractTensor.data. The
indicesof 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.The
indicesarray has shape(nnz, rank)wherennzis the number of non-zero data in this sparse tensor andrankis thetensor rankof the tensor. This meansindices[i]is the nD index ofdata[i].
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int[][]The non-zero indices of this sparse tensor.final intThe number of non-zero data in this sparse tensor.final doubleStores the sparsity of this matrix.protected VThe zero element for the semiring that this tensor's elements belong to.Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractCooSemiringTensor(Shape shape, V[] data, int[][] indices) Creates a tensor with the specified data and shape.protectedAbstractCooSemiringTensor(Shape shape, V[] data, int[][] indices, Object dummy) Constructor useful for avoiding parameter validation while constructing COO tensors. -
Method Summary
Modifier and TypeMethodDescriptionComputes the element-wise sum between two tensors of the same shape.int[]argmax()Finds the indices of the maximum (non-zero) value in this tensor.int[]argmin()Finds the indices of the minimum (non-zero) value in this tensor.coalesce()Coalesces this sparse COO tensor.coalesce(BinaryOperator<V> aggregator) Coalesces this sparse COO tensor.copy()Creates a deep copy of this tensor.intGets the size of the 1D data array backing this tensor.doubledensity()Gets the density of this tensor as a decimal percentage.Drops any explicit zeros in this sparse COO tensor.Computes the element-wise multiplication of two tensors of the same shape.flatten()Flattens tensor to single dimension while preserving order of data.flatten(int axis) Flattens a tensor along the specified axis.get(int... target) Gets the element of this tensor at the specified target.Gets the zero element for the field of this tensor.abstract UmakeLikeDenseTensor(Shape shape, V[] entries) Constructs a dense tensor that is a similar type as this sparse COO tensor.abstract TmakeLikeTensor(Shape shape, List<V> data, List<int[]> indices) Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.abstract TmakeLikeTensor(Shape shape, V[] data, int[][] indices) Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.max()Finds the maximum (non-zero) value in this tensor.min()Finds the minimum (non-zero) value in this tensor.Copies and reshapes this tensor.Sets the element of this tensor at the specified target.voidsetZeroElement(V zeroElement) Sets the zero element for the field of this tensor.voidSorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.doublesparsity()Gets the sparsity of this tensor as a decimal percentage.T()Computes the transpose of a tensor by exchanging the first and last axes of this tensor.T(int... axes) Computes the transpose of this tensor.T(int axis1, int axis2) Computes the transpose of a tensor by exchangingaxis1andaxis2.Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes.tensorTr(int axis1, int axis2) Computes the generalized trace of this tensor along the specified axes.toDense()Converts this COO tensor to an equivalent dense tensor.Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, makeLikeTensor, reshape, sameShape, totalEntriesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin
add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sumMethods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring
getData, getRank, getShape, makeLikeTensor, tensorDot, tensorDot, tensorDot, tensorTr
-
Field Details
-
zeroElement
-
indices
public final int[][] indicesThe non-zero indices of this sparse tensor.
Has shape
(nnz, rank)wherennzis the number of non-zero data in this sparse tensor. -
nnz
public final int nnzThe number of non-zero data in this sparse tensor. -
sparsity
public final double sparsityStores the sparsity of this matrix.
-
-
Constructor Details
-
AbstractCooSemiringTensor
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.
-
AbstractCooSemiringTensor
Constructor useful for avoiding parameter validation while constructing COO tensors.- Parameters:
shape- The shape of the tensor to construct.data- The non-zero data of this tensor.indices- The indices of the non-zero data.dummy- Dummy object to distinguish this constructor from the safe variant. It is completely ignored in this constructor.
-
-
Method Details
-
dataLength
public int dataLength()Gets the size of the 1D data array backing this tensor.- Specified by:
dataLengthin classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Returns:
- The size of the 1D data array backing this tensor.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the specified shape and non-zero data.- 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.- 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.- 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.
-
getZeroElement
Gets the zero element for the field of this tensor.- Returns:
- The zero element for the field of this tensor. If it could not be determined during construction of this object
and has not been set explicitly by
setZeroElement(Semiring)thennullwill be returned.
-
setZeroElement
Sets the zero element for the field of this tensor.- Parameters:
zeroElement- The zero element of this tensor.- Throws:
IllegalArgumentException- IfzeroElementis not an additive identity for the semiring.
-
sparsity
public double sparsity()Gets the sparsity of this tensor as a decimal percentage. That is, the percentage of data in this tensor that are zero.- Returns:
- The sparsity of this tensor as a decimal percentage.
- See Also:
-
density
public double density()Gets the density of this tensor as a decimal percentage. That is, the percentage of data in this tensor that are non-zero.- Returns:
- The density of this tensor as a decimal percentage.
- See Also:
-
add
Computes the element-wise sum between two tensors of the same shape.- Specified by:
addin interfaceTensorOverSemiring<T extends AbstractCooSemiringTensor<T,U, V>, T extends AbstractCooSemiringTensor<T, U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
b- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b. - Throws:
TensorShapeException- If this tensor andbdo not have the same shape.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMultin interfaceTensorOverSemiring<T extends AbstractCooSemiringTensor<T,U, V>, T extends AbstractCooSemiringTensor<T, U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
b- Second tensor in the element-wise product.- Returns:
- The element-wise product between this tensor and
b. - Throws:
IllegalArgumentException- If this tensor andbdo not have the same shape.
-
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.- Specified by:
tensorDotin interfaceTensorOverSemiring<T extends AbstractCooSemiringTensor<T,U, V>, T extends AbstractCooSemiringTensor<T, U, V>, V extends Semiring<V>[], V extends Semiring<V>> - 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 forsrc2tensor.- Returns:
- The tensor dot product over the specified axes.
- Throws:
IllegalArgumentException- If the two tensors shapes do not match along the specified axes pairwise inaAxesandbAxes.IllegalArgumentException- IfaAxesandbAxesdo not match in length, or if any of the axes are out of bounds for the corresponding tensor.
-
tensorTr
Computes the generalized trace of this tensor along the specified axes.
The generalized tensor trace is the sum along the diagonal values of the 2D sub-arrays of this tensor specified by
axis1andaxis2. The shape of the resulting tensor is equal to this tensor with theaxis1andaxis2removed.- Specified by:
tensorTrin interfaceTensorOverSemiring<T extends AbstractCooSemiringTensor<T,U, V>, T extends AbstractCooSemiringTensor<T, U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
axis1- First axis for 2D sub-array.axis2- Second axis for 2D sub-array.- Returns:
- The generalized trace of this tensor along
axis1andaxis2. - Throws:
IndexOutOfBoundsException- If the two axes are not both larger than zero and less than this tensors rank.IllegalArgumentException- Ifaxis1 == axis2orthis.shape.get(axis1) != this.shape.get(axis1)(i.e. the axes are equal or the tensor does not have the same length along the two axes.)
-
T
-
T
Computes the transpose of a tensor by exchangingaxis1andaxis2.- Specified by:
Tin classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
axis1- First axis to exchange.axis2- Second axis to exchange.- Returns:
- The transpose of this tensor according to the specified axes.
- Throws:
IndexOutOfBoundsException- If eitheraxis1oraxis2are out of bounds for the rank of this tensor.- See Also:
-
T
Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified byaxes.- Specified by:
Tin classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
axes- Permutation of tensor axis. If the tensor has rankN, then this must be an array of lengthNwhich is a permutation of{0, 1, 2, ..., N-1}.- Returns:
- The transpose of this tensor with its axes permuted by the
axesarray. - Throws:
IndexOutOfBoundsException- If any element ofaxesis out of bounds for the rank of this tensor.IllegalArgumentException- Ifaxesis not a permutation of{1, 2, 3, ... N-1}.- See Also:
-
copy
-
min
Finds the minimum (non-zero) value in this tensor. If this tensor is complex, then this method finds the smallest value in magnitude.- Specified by:
minin interfaceSemiringTensorMixin<T extends AbstractCooSemiringTensor<T,U, V>, U extends AbstractDenseSemiringTensor<U, V>, V extends Semiring<V>> - Returns:
- The minimum (non-zero) value in this tensor.
-
max
Finds the maximum (non-zero) value in this tensor.- Specified by:
maxin interfaceSemiringTensorMixin<T extends AbstractCooSemiringTensor<T,U, V>, U extends AbstractDenseSemiringTensor<U, V>, V extends Semiring<V>> - Returns:
- The maximum (non-zero) value in this tensor.
-
argmin
public int[] argmin()Finds the indices of the minimum (non-zero) value in this tensor.- Specified by:
argminin interfaceSemiringTensorMixin<T extends AbstractCooSemiringTensor<T,U, V>, U extends AbstractDenseSemiringTensor<U, V>, V extends Semiring<V>> - Returns:
- The indices of the minimum (non-zero) value in this tensor.
-
argmax
public int[] argmax()Finds the indices of the maximum (non-zero) value in this tensor.- Specified by:
argmaxin interfaceSemiringTensorMixin<T extends AbstractCooSemiringTensor<T,U, V>, U extends AbstractDenseSemiringTensor<U, V>, V extends Semiring<V>> - Returns:
- The indices of the maximum (non-zero) value in this tensor.
-
get
Gets the element of this tensor at the specified target.- Specified by:
getin classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
target- Index of the element to get.- Returns:
- The element of this tensor at the specified index. If there is a non-zero value with the specified index, that value
will be returned. If there is no non-zero value at the specified index than the zero element will attempt to be
returned (i.e. the additive identity of the semiring). However, if the zero element could not be determined during
construction or if it was not set with
setZeroElement(Semiring)thennullwill be returned. - Throws:
ArrayIndexOutOfBoundsException- If any target are not within this tensor.
-
set
Sets the element of this tensor at the specified target.- Specified by:
setin classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
value- New value to set the specified index of this tensor to.target- Index of the element to set.- Returns:
- If this tensor is dense, a reference to this tensor is returned. If this tensor is sparse, a copy of this tensor with the updated value is returned.
- Throws:
IndexOutOfBoundsException- Iftargetis not within the bounds of this tensor.
-
flatten
-
flatten
Flattens a tensor along the specified axis. Unlikeflatten()- Specified by:
flattenin classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
axis- Axis along which to flatten tensor.- Throws:
ArrayIndexOutOfBoundsException- If the axis is not positive or larger thanthis.{@link #getRank()}-1.- See Also:
-
reshape
Copies and reshapes this tensor.- Specified by:
reshapein classAbstractTensor<T extends AbstractCooSemiringTensor<T,U, V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
newShape- New shape for the tensor.- Returns:
- A copy of this tensor with the new shape.
- Throws:
TensorShapeException- IfnewShapeis not broadcastable tothis.shape.
-
sortIndices
public void sortIndices()Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index. -
toDense
Converts this COO tensor to an equivalent dense tensor.- Returns:
- A dense tensor which is equivalent to this COO tensor.
- Throws:
ArithmeticException- If the number of data in the dense tensor exceeds 2,147,483,647.
-
coalesce
Coalesces this sparse COO tensor. An uncoalesced tensor is a sparse tensor with multiple data for a single index. This method will ensure that each index only has one non-zero value by summing duplicated data. If another form of aggregation other than summing is desired, usecoalesce(BinaryOperator).- Returns:
- A new coalesced sparse COO tensor which is equivalent to this COO tensor.
- See Also:
-
coalesce
Coalesces this sparse COO tensor. An uncoalesced tensor is a sparse tensor with multiple data for a single index. This method will ensure that each index only has one non-zero value by aggregating duplicated data usingaggregator.- Parameters:
aggregator- Custom aggregation function to combine multiple.- Returns:
- A new coalesced sparse COO tensor which is equivalent to this COO tensor.
- See Also:
-
dropZeros
Drops any explicit zeros in this sparse COO tensor.- Returns:
- A copy of this COO tensor with any explicitly stored zeros removed.
-