Class CooTensor
- All Implemented Interfaces:
Serializable
,TensorOverField<CooTensor,
,CooTensor, double[], Double> TensorOverRing<CooTensor,
,CooTensor, double[], Double> TensorOverSemiring<CooTensor,
CooTensor, double[], Double>
A real sparse tensor stored in coordinate list (COO) format. The AbstractTensor.data
of this COO tensor are
primitive doubles.
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]
.
Some ops on sparse tensors behave differently than on dense tensors. For instance, add(double)
will not
add the scalar to all data of the tensor since this would cause catastrophic loss of sparsity. Instead, such non-zero preserving
element-wise ops only act on the non-zero data of the sparse tensor as to not affect the sparsity.
Note: many ops assume that the data of the COO tensor are sorted lexicographically. However, this is not explicitly verified. Every operation implemented in this class will preserve the lexicographical sorting.
If indices need to be sorted for any reason, call sortIndices()
.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal int[][]
The non-zero indices of this tensor.final int
The number of non-zero data in this tensor.Fields inherited from class org.flag4j.arrays.backend.AbstractTensor
data, rank, shape
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a zero matrix with the specified shape.Creates a tensor with the specified data and shape.Creates a sparse COO matrix with the specified shape, non-zero data, and indices.Creates a tensor with the specified data and shape.Constructs a copy of the specified matrix. -
Method Summary
Modifier and TypeMethodDescriptionadd
(double b) Adds a scalar value to each non-zero value of this tensor.Adds a scalar field value to each non-zero entry of this tensor.add
(CooCTensor b) Computes the element-wise sum between two tensors of the same shape.Computes the element-wise sum between two tensors of the same shape.void
Adds a scalar value to each non-zero entry of this tensor and stores the result in this tensor.int[]
argmax()
Finds the indices of the maximum value in this tensor.int[]
Finds the indices of the maximum absolute value in this tensor.int[]
argmin()
Finds the indices of the minimum value in this tensor.int[]
Finds the indices of the minimum absolute value in this tensor.coalesce()
Coalesces this sparse COO tensor.coalesce
(BinaryOperator<Double> aggregator) Coalesces this sparse COO tensor.copy()
Creates a deep copy of this tensor.Computes the element-wise quotient between two tensors.Drops any explicit zeros in this sparse COO tensor.Computes the element-wise multiplication of two tensors of the same shape.boolean
Checks if an object is equal to this tensor object.flatten()
Flattens tensor to single dimension while preserving order of data.flatten
(int axis) Flattens a tensor along the specified axis.get
(int... indices) Gets the element of this tensor at the specified indices.int
hashCode()
makeDenseTensor
(Shape shape, double[] entries) Makes a dense tensor with the specified shape and data which is a similar type to this sparse tensor.makeLikeTensor
(Shape shape, double[] 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, double[] entries, 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, List<Double> 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.prod()
Computes the product of all non-zero values in this tensor.recip()
Computes the element-wise reciprocals of the non-zero elements of this sparse tensor.Copies and reshapes this tensor.Sets the element of this tensor at the specified indices.void
Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index.double
sparsity()
The sparsity of this sparse tensor.sub
(double b) Subtracts a scalar value from each non-zero value of this tensor.Subtracts a scalar value from each non-zero entry of this tensor.Computes the element-wise difference between two tensors of the same shape.void
Subtracts a scalar value from each non-zero entry of this tensor and stores the result in 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.Converts this tensor to an equivalent complex tensor.toDense()
Converts this sparse tensor to an equivalent dense tensor.toString()
Formats this sparse COO tensor as a human-readable string specifying the full shape, non-zero data, and non-zero indices.Methods inherited from class org.flag4j.arrays.backend.primitive_arrays.AbstractDoubleTensor
abs, addEq, conj, div, div, divEq, divEq, H, H, isFinite, isInfinite, isNaN, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, multEq, multEq, round, round, roundToZero, roundToZero, sqrt, subEq, sum
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.ring_arrays.TensorOverRing
H
-
Field Details
-
indices
public final int[][] indicesThe non-zero indices of this tensor. Must have shape(nnz, rank)
. -
nnz
public final int nnzThe number of non-zero data in this tensor.
-
-
Constructor Details
-
CooTensor
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
-
-
CooTensor
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
-
-
CooTensor
Creates a zero matrix with the specified shape.- Parameters:
shape
- The shape of the zero matrix to construct.
-
CooTensor
Creates a sparse COO matrix with the specified shape, non-zero data, and indices.- Parameters:
shape
- Shape of the matrix to construct.entries
- Non-zero data of the sparse COO matrix.indices
- Indices of the non-zero data in the sparse COO matrix.
-
CooTensor
Constructs a copy of the specified matrix.- Parameters:
b
- Matrix to make copy of.
-
-
Method Details
-
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<CooTensor,
CooTensor, double[], Double> - Specified by:
makeLikeTensor
in classAbstractTensor<CooTensor,
double[], Double> - 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.- 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.
-
makeLikeTensor
Constructs a sparse tensor of the same type as this tensor with the given the shape, non-zero data, and non-zero indices.- 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.
-
makeDenseTensor
Makes a dense tensor with the specified shape and data which is a similar type to this sparse tensor.- Parameters:
shape
- Shape of the dense tensor.entries
- Entries of the dense tensor.- Returns:
- A dense tensor with the specified shape and data which is a similar type to this sparse tensor.
-
sparsity
public double sparsity()The sparsity of this sparse tensor. That is, the percentage of elements in this tensor which are zero as a decimal.- Returns:
- The density of this sparse tensor.
-
toComplex
Converts this tensor to an equivalent complex tensor.- Returns:
- A complex COO tensor whose non-zero data have real component equal to the non-zero data of this tensor and imaginary components zero.
-
toDense
Converts this sparse tensor to an equivalent dense tensor.- Returns:
- A dense tensor equivalent to this sparse tensor.
-
get
Gets the element of this tensor at the specified indices.- Specified by:
get
in classAbstractTensor<CooTensor,
double[], Double> - Parameters:
indices
- Indices of the element to get.- Returns:
- The element of this tensor at the specified indices.
- Throws:
ArrayIndexOutOfBoundsException
- If any indices are not within this matrix.
-
set
Sets the element of this tensor at the specified indices.- Specified by:
set
in classAbstractTensor<CooTensor,
double[], Double> - Parameters:
value
- New value to set the specified index of this tensor to.index
- Indices of the element to set.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.
-
flatten
Flattens tensor to single dimension while preserving order of data.- Specified by:
flatten
in classAbstractTensor<CooTensor,
double[], Double> - Returns:
- The flattened tensor.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<CooTensor,
double[], Double> - 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<CooTensor,
double[], Double> - 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
.
-
sub
Subtracts a scalar value from each non-zero entry of this tensor.- Specified by:
sub
in interfaceTensorOverRing<CooTensor,
CooTensor, double[], Double> - Overrides:
sub
in classAbstractDoubleTensor<CooTensor>
- Parameters:
b
- Scalar value in difference.- Returns:
- The difference of this tensor and the scalar
b
.
-
subEq
Subtracts a scalar value from each non-zero entry of this tensor and stores the result in this tensor.- Specified by:
subEq
in interfaceTensorOverRing<CooTensor,
CooTensor, double[], Double> - Overrides:
subEq
in classAbstractDoubleTensor<CooTensor>
- Parameters:
b
- Scalar value in difference.
-
add
Adds a scalar field value to each non-zero entry of this tensor.- Specified by:
add
in interfaceTensorOverSemiring<CooTensor,
CooTensor, double[], Double> - Overrides:
add
in classAbstractDoubleTensor<CooTensor>
- Parameters:
b
- Scalar field value in sum.- Returns:
- The sum of this tensor with the scalar
b
.
-
addEq
Adds a scalar value to each non-zero entry of this tensor and stores the result in this tensor.- Specified by:
addEq
in interfaceTensorOverSemiring<CooTensor,
CooTensor, double[], Double> - Overrides:
addEq
in classAbstractDoubleTensor<CooTensor>
- Parameters:
b
- Scalar field value in sum.
-
add
Computes the element-wise sum between two tensors of the same shape.- Parameters:
b
- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
add
Computes the element-wise sum between two tensors of the same shape.- Parameters:
b
- Second tensor in the element-wise sum.- Returns:
- The sum of this tensor with
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
sub
Computes the element-wise difference between two tensors of the same shape.- Parameters:
b
- Second tensor in the element-wise difference.- Returns:
- The difference of this tensor with
b
. - Throws:
IllegalArgumentException
- If this tensor andb
do not have the same shape.
-
argmin
public int[] argmin()Finds the indices of the minimum value in this tensor.- Returns:
- The indices of the minimum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmax
public int[] argmax()Finds the indices of the maximum value in this tensor.- Returns:
- The indices of the maximum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argminAbs
public int[] argminAbs()Finds the indices of the minimum absolute value in this tensor.- Returns:
- The indices of the minimum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
argmaxAbs
public int[] argmaxAbs()Finds the indices of the maximum absolute value in this tensor.- Returns:
- The indices of the maximum value in this tensor. If this value occurs multiple times, the indices of the first entry (in row-major ordering) are returned.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- 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.- 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.- 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
. This will be a tensor of rankthis.getRank() - 2
with the same shape as this tensor but withaxis1
andaxis2
removed. - 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.)
-
prod
Computes the product of all non-zero values in this tensor.
NOTE: This is only the product of the non-zero values in this tensor.
- Specified by:
prod
in interfaceTensorOverSemiring<CooTensor,
CooTensor, double[], Double> - Overrides:
prod
in classAbstractDoubleTensor<CooTensor>
- Returns:
- The product of all non-zero values in this tensor.
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- Specified by:
T
in classAbstractTensor<CooTensor,
double[], Double> - 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<CooTensor,
double[], Double> - 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:
-
recip
Computes the element-wise reciprocals of the non-zero elements of this sparse tensor.
Note: This method only computes the reciprocals of the non-zero elements.
- Specified by:
recip
in interfaceTensorOverField<CooTensor,
CooTensor, double[], Double> - Overrides:
recip
in classAbstractDoubleTensor<CooTensor>
- Returns:
- A tensor containing the reciprocal non-zero elements of this tensor.
-
copy
Creates a deep copy of this tensor.- Overrides:
copy
in classAbstractDoubleTensor<CooTensor>
- Returns:
- A deep copy of this tensor.
-
add
Adds a scalar value to each non-zero value of this tensor.- Specified by:
add
in interfaceTensorOverField<CooTensor,
CooTensor, double[], Double> - Overrides:
add
in classAbstractDoubleTensor<CooTensor>
- Parameters:
b
- Value to add to each non-zero value of this tensor.- Returns:
- The result of adding the specified scalar value to each entry of this tensor.
-
sub
Subtracts a scalar value from each non-zero value of this tensor.- Specified by:
sub
in interfaceTensorOverField<CooTensor,
CooTensor, double[], Double> - Overrides:
sub
in classAbstractDoubleTensor<CooTensor>
- Parameters:
b
- Value to subtract from each non-zero value of this tensor.- Returns:
- The result of subtracting the specified scalar value from each entry of this tensor.
-
div
Computes the element-wise quotient between two tensors.
WARNING: This method is not supported for sparse tensors. If called on a sparse tensor, an
UnsupportedOperationException
will be thrown. Element-wise division is undefined for sparse tensors as it would almost certainly result in a division by zero.- Parameters:
b
- Second tensor in the element-wise quotient.- Returns:
- The element-wise quotient of this tensor with
b
.
-
sortIndices
public void sortIndices()Sorts the indices of this tensor in lexicographical order while maintaining the associated value for each index. -
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.
-
equals
Checks if an object is equal to this tensor object. -
hashCode
-
toString
-