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 theSemiring
which 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
shape
of the tensor. - The non-zero
AbstractTensor.data
of the tensor. All other data in the tensor are assumed to be zero. Zero value can also explicitly be stored inAbstractTensor.data
. The
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.The
indices
array has shape(nnz, rank)
wherennz
is the number of non-zero data in this sparse tensor andrank
is thetensor rank
of 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 int
The number of non-zero data in this sparse tensor.final double
Stores the sparsity of this matrix.protected V
The 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
ConstructorsModifierConstructorDescriptionprotected
AbstractCooSemiringTensor
(Shape shape, V[] data, int[][] indices) Creates a tensor with the specified data and shape. -
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.double
density()
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 U
makeLikeDenseTensor
(Shape shape, V[] entries) Constructs a dense tensor that is a similar type as this sparse COO tensor.abstract T
makeLikeTensor
(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 T
makeLikeTensor
(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.void
setZeroElement
(V zeroElement) Sets the zero element for the field of this tensor.void
Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.double
sparsity()
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 exchangingaxis1
andaxis2
.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, totalEntries
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin
add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sum
Methods 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)
wherennz
is 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.
-
-
Method Details
-
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)
thennull
will be returned.
-
setZeroElement
Sets the zero element for the field of this tensor.- Parameters:
zeroElement
- The zero element of this tensor.- Throws:
IllegalArgumentException
- IfzeroElement
is 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:
add
in 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 andb
do not have the same shape.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMult
in 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 andb
do 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:
tensorDot
in 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 forsrc2
tensor.- Returns:
- The tensor dot product over the specified axes.
- Throws:
IllegalArgumentException
- If the two tensors shapes do not match along the specified axes pairwise inaAxes
andbAxes
.IllegalArgumentException
- IfaAxes
andbAxes
do 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
axis1
andaxis2
. The shape of the resulting tensor is equal to this tensor with theaxis1
andaxis2
removed.- Specified by:
tensorTr
in 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
axis1
andaxis2
. - Throws:
IndexOutOfBoundsException
- If the two axes are not both larger than zero and less than this tensors rank.IllegalArgumentException
- Ifaxis1 == axis2
orthis.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 exchangingaxis1
andaxis2
.- Specified by:
T
in 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 eitheraxis1
oraxis2
are 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:
T
in 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 lengthN
which is a permutation of{0, 1, 2, ..., N-1}
.- Returns:
- The transpose of this tensor with its axes permuted by the
axes
array. - Throws:
IndexOutOfBoundsException
- If any element ofaxes
is out of bounds for the rank of this tensor.IllegalArgumentException
- Ifaxes
is 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:
min
in 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:
max
in 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:
argmin
in 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:
argmax
in 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:
get
in 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)
thennull
will 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:
set
in 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
- Iftarget
is not within the bounds of this tensor.
-
flatten
-
flatten
Flattens a tensor along the specified axis. Unlikeflatten()
- Specified by:
flatten
in 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:
reshape
in 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
- IfnewShape
is 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.
-