Package org.flag4j.arrays.sparse
Class CooTensor
java.lang.Object
org.flag4j.core.TensorBase<CooTensor,Tensor,CooCTensor,CTensor,CooTensor,double[],Double>
org.flag4j.core.sparse_base.SparseTensorBase<CooTensor,Tensor,CooCTensor,CTensor,CooTensor,double[],Double>
org.flag4j.core.sparse_base.RealSparseTensorBase<CooTensor,Tensor,CooCTensor,CTensor>
org.flag4j.arrays.sparse.CooTensor
- All Implemented Interfaces:
Serializable
,RealTensorMixin<CooTensor,
,CooCTensor> SparseTensorMixin
,TensorComparisonsMixin
,TensorExclusiveMixin<CooTensor,
,Tensor, CooTensor, CooCTensor> TensorManipulationsMixin<CooTensor>
,TensorOperationsMixin<CooTensor,
,Tensor, CooCTensor, CTensor, CooTensor, Double> TensorPropertiesMixin
public class CooTensor
extends RealSparseTensorBase<CooTensor,Tensor,CooCTensor,CTensor>
implements TensorExclusiveMixin<CooTensor,Tensor,CooTensor,CooCTensor>
Real sparse tensor. Can be any rank. Stored in coordinate (COO) format.
- See Also:
-
Field Summary
Fields inherited from class org.flag4j.core.sparse_base.SparseTensorBase
indices, nnz
Fields inherited from class org.flag4j.core.TensorBase
DEFAULT_ROUND_TO_ZERO_THRESHOLD, entries, shape
-
Constructor Summary
ConstructorDescriptionConstructs a sparse tensor whose shape and non-zero values/indices are given by another sparse tensor.Creates a sparse tensor with specified shape filled with zeros.Creates a sparse tensor with specified shape and non-zero values/indices.Creates a sparse tensor with specified shape and non-zero values/indices.Creates a sparse tensor with specified shape and non-zero values/indices. -
Method Summary
Modifier and TypeMethodDescriptionadd
(double a) Adds specified value to all entries of this tensor.Computes the element-wise addition between two tensors of the same rank.Computes the element-wise addition between two tensors of the same rank.add
(CooCTensor B) Computes the element-wise addition between two tensors of the same rank.Computes the element-wise addition between two tensors of the same rank.Adds specified value to all entries of this tensor.boolean
Checks if all entries of this tensor are close to the entries of the argumenttensor
.copy()
Creates a copy of this tensor.Computes the element-wise division between two tensors.Computes the element-wise division between two tensors.Computes the element-wise multiplication between two tensors.Computes the element-wise multiplication between two tensors.Computes the element-wise multiplication between two tensors.Computes the element-wise multiplication between two tensors.boolean
Checks if an object is equal to this sparse COO tensor.flatten()
Flattens tensor to single dimension.flatten
(int axis) Flattens a tensor along the specified axis.static CooTensor
get
(int... indices) Gets the element in this tensor at the specified indices.protected CooTensor
getSelf()
Simply returns a reference of this tensor.protected CooCTensor
makeComplexTensor
(Shape shape, CNumber[] entries, int[][] indices) A factory for creating a complex sparse tensor.protected Tensor
makeDenseTensor
(Shape shape, double[] entries) A factory for creating a real dense tensor.protected CooTensor
makeTensor
(Shape shape, double[] entries, int[][] indices) A factory for creating a real sparse tensor.Copies and reshapes tensor if possible.set
(double value, int... indices) Sets an index of this tensor to a specified value.sub
(double a) Adds specified value to all entries of this tensor.Computes the element-wise subtraction between two tensors of the same rank.Computes the element-wise addition between two tensors of the same rank.sub
(CooCTensor B) Computes the element-wise subtraction between two tensors of the same rank.Computes the element-wise subtraction between two tensors of the same rank.Subtracts a specified value from all entries of this tensor.T()
Computes the transpose of a tensor.T
(int... axes) Computes the transpose of this tensor.T
(int axis1, int axis2) Computes the transpose of a tensor.Computes the tensor contraction of this tensor with a specified tensor over the specified set of axes.tensorInv
(int numIndices) Computes the 'inverse' of this tensor.Converts this tensor to an equivalent complex tensor.toDense()
Converts this sparse tensor to an equivalent dense tensor.Converts this tensor to a matrix with the specified shape.Computes the transpose of a tensor.Methods inherited from class org.flag4j.core.sparse_base.RealSparseTensorBase
abs, argMax, argMin, div, div, isNeg, isOnes, isPos, isZeros, max, maxAbs, min, minAbs, mult, mult, nonZeroEntries, recip, reshape, round, round, roundToZero, roundToZero, sortIndices, sqrt, sum
Methods inherited from class org.flag4j.core.sparse_base.SparseTensorBase
density, sparsity
Methods inherited from class org.flag4j.core.TensorBase
allClose, getEntries, getRank, getShape, sameLength, sameShape, tensorEquals, totalEntries
-
Constructor Details
-
CooTensor
Creates a sparse tensor with specified shape filled with zeros.- Parameters:
shape
- Shape of the tensor.
-
CooTensor
Creates a sparse tensor with specified shape and non-zero values/indices.- Parameters:
shape
- Shape of the tensor.nonZeroEntries
- Non-zero entries of the tensor.indices
- Indices of the non-zero entries of the tensor.
-
CooTensor
Creates a sparse tensor with specified shape and non-zero values/indices.- Parameters:
shape
- Shape of the tensor.nonZeroEntries
- Non-zero entries of the tensor.indices
- Indices of the non-zero entries of the tensor.
-
CooTensor
Creates a sparse tensor with specified shape and non-zero values/indices.- Parameters:
shape
- Shape of the tensor.nonZeroEntries
- Non-zero entries of the tensor.indices
- Indices of the non-zero entries of the tensor.
-
CooTensor
Constructs a sparse tensor whose shape and non-zero values/indices are given by another sparse tensor. This effectively copies the tensor.- Parameters:
A
- Tensor to copy.
-
-
Method Details
-
equals
Checks if an object is equal to this sparse COO tensor.- Specified by:
equals
in interfaceTensorComparisonsMixin
- Overrides:
equals
in classObject
- Parameters:
object
- Object to compare this sparse COO tensor to.- Returns:
- True if the object is a
CooTensor
, has the same shape as this tensor, and is element-wise equal to this tensor.
-
toComplex
Converts this tensor to an equivalent complex tensor. That is, the entries of the resultant matrix will be exactly the same value but will have typeCNumber
rather thanDouble
.- Specified by:
toComplex
in interfaceRealTensorMixin<CooTensor,
CooCTensor> - Returns:
- A complex matrix which is equivalent to this matrix.
-
fromDense
-
makeTensor
A factory for creating a real sparse tensor.- Specified by:
makeTensor
in classRealSparseTensorBase<CooTensor,
Tensor, CooCTensor, CTensor> - Parameters:
shape
- Shape of the sparse tensor to make.entries
- Non-zero entries of the sparse tensor to make.indices
- Non-zero indices of the sparse tensor to make.- Returns:
- A tensor created from the specified parameters.
-
makeDenseTensor
A factory for creating a real dense tensor.- Specified by:
makeDenseTensor
in classRealSparseTensorBase<CooTensor,
Tensor, CooCTensor, CTensor> - Parameters:
shape
- Shape of the tensor to make.entries
- Entries of the dense tensor to make.- Returns:
- A tensor created from the specified parameters.
-
makeComplexTensor
A factory for creating a complex sparse tensor.- Specified by:
makeComplexTensor
in classRealSparseTensorBase<CooTensor,
Tensor, CooCTensor, CTensor> - Parameters:
shape
- Shape of the tensor to make.entries
- Non-zero entries of the sparse tensor to make.indices
- Non-zero indices of the sparse tensor to make.- Returns:
- A tensor created from the specified parameters.
-
set
Sets an index of this tensor to a specified value.- Specified by:
set
in interfaceTensorManipulationsMixin<CooTensor>
- Parameters:
value
- Value to set.indices
- The indices of this tensor for which to set the value.- Returns:
- A reference to this tensor.
-
reshape
Copies and reshapes tensor if possible. The total number of entries in this tensor must match the total number of entries in the reshaped tensor.- Specified by:
reshape
in interfaceTensorManipulationsMixin<CooTensor>
- Parameters:
newShape
- Shape of the new tensor.- Returns:
- A tensor which is equivalent to this tensor but with the specified shape.
- Throws:
IllegalArgumentException
- If this tensor cannot be reshaped to the specified dimensions.
-
flatten
Flattens tensor to single dimension. To flatten tensor along a single axis seeflatten(int)
.- Specified by:
flatten
in interfaceTensorManipulationsMixin<CooTensor>
- Returns:
- The flattened tensor.
- See Also:
-
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 interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - 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.
-
T
Computes the transpose of a tensor. Same asTensorExclusiveMixin.transpose(int, int)
. In the context of a tensor, this exchanges the specified axes. Also seetranspose()
andT()
to exchange first and last axes.- Specified by:
T
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
axis1
- First axis to exchange.axis2
- Second axis to exchange.- Returns:
- The transpose of this tensor.
-
T
Computes the transpose of this tensor. That is, interchanges the axes of this tensor so that it matches the specified axes permutation. Same asTensorExclusiveMixin.transpose(int[])
.- Specified by:
T
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - 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:
IllegalArgumentException
- Ifaxes
is not a permutation of{1, 2, 3, ... N-1}
.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Specified by:
add
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
sub
Computes the element-wise addition between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
add
Computes the element-wise addition between two tensors of the same rank.- Specified by:
add
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Second tensor in the addition.- Returns:
- The result of adding the tensor B to this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
add
Adds specified value to all entries of this tensor.- Specified by:
add
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
a
- Value to add to all entries of this tensor.- Returns:
- The result of adding the specified value to each entry of this tensor.
-
add
Adds specified value to all entries of this tensor.- Specified by:
add
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
a
- Value to add to all entries of this tensor.- Returns:
- The result of adding the specified value to each entry of this tensor.
-
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Specified by:
sub
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
B
- Second tensor in element-wise subtraction.- Returns:
- The result of subtracting the tensor B from this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Second tensor in element-wise subtraction.- Returns:
- The result of subtracting the tensor B from this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
sub
Computes the element-wise subtraction between two tensors of the same rank.- Specified by:
sub
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Second tensor in element-wise subtraction.- Returns:
- The result of subtracting the tensor B from this tensor element-wise.
- Throws:
IllegalArgumentException
- If this tensor and B have different shapes.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
sub
Adds specified value to all entries of this tensor.- Specified by:
sub
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
a
- Value to add to all entries of this tensor.- Returns:
- The result of adding the specified value to each entry of this tensor.
-
sub
Subtracts a specified value from all entries of this tensor.- Specified by:
sub
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
a
- Value to subtract from all entries of this tensor.- Returns:
- The result of subtracting the specified value from each entry of this tensor.
-
transpose
Computes the transpose of a tensor. Same asT()
. In the context of a tensor, this exchanges the first and last axis of the tensor. Also seeTensorExclusiveMixin.transpose(int, int)
andT(int, int)
.- Specified by:
transpose
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Returns:
- The transpose of this tensor.
- See Also:
-
T
Computes the transpose of a tensor. Same astranspose()
. In the context of a tensor, this exchanges the first and last axis of the tensor.- Specified by:
T
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Returns:
- The transpose of this tensor.
- See Also:
-
get
Gets the element in this tensor at the specified indices.- Specified by:
get
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
indices
- Indices of element.- Returns:
- The element at the specified indices.
- Throws:
IllegalArgumentException
- If the number of indices does not match the rank of this tensor.
-
copy
Creates a copy of this tensor.- Specified by:
copy
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Returns:
- A copy of this tensor.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Specified by:
elemMult
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If this tensor andB
do not have the same shape.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
elemMult
Computes the element-wise multiplication between two tensors.- Specified by:
elemMult
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Tensor to element-wise multiply to this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
elemDiv
Computes the element-wise division between two tensors.- Specified by:
elemDiv
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
B
- Tensor to element-wise divide with this tensor.- Returns:
- The result of the element-wise tensor division.
- Throws:
IllegalArgumentException
- If the tensors do not have the same shape.
-
elemDiv
Computes the element-wise division between two tensors.- Specified by:
elemDiv
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Specified by:
elemDiv
in interfaceTensorOperationsMixin<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, Double> - Parameters:
B
- Tensor to element-wise divide with this tensor.- Returns:
- The result of the element-wise tensor multiplication.
- Throws:
IllegalArgumentException
- If this tensor andB
do not have the same shape.
-
tensorInv
Computes the 'inverse' of this tensor. That is, computes the tensor
X=this.tensorInv()
such thatthis.tensorDot(X, numIndices)
is the 'identity' tensor for the tensor dot product operation. A tensorI
is the identity for a tensor dot product ifthis.tensorDot(I, numIndices).equals(this)
.WARNING: This method will convert this tensor to a dense tensor.
- Specified by:
tensorInv
in interfaceTensorExclusiveMixin<CooTensor,
Tensor, CooTensor, CooCTensor> - Parameters:
numIndices
- The number of first numIndices which are involved in the inverse sum.- Returns:
- The 'inverse' of this tensor as defined in the above sense.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in interfaceTensorManipulationsMixin<CooTensor>
- Parameters:
axis
- Axis along which to flatten tensor.- Throws:
IllegalArgumentException
- If the axis is not positive or larger thanthis.
.TensorBase.getRank()
-1
-
getSelf
Simply returns a reference of this tensor.- Specified by:
getSelf
in classTensorBase<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, double[], Double> - Returns:
- A reference to this tensor.
-
allClose
Description copied from class:TensorBase
Checks if all entries of this tensor are close to the entries of the argumenttensor
.- Specified by:
allClose
in classTensorBase<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, double[], Double> - Parameters:
tensor
- Tensor to compare this tensor to.relTol
- Relative tolerance.absTol
- Absolute tolerance.- Returns:
- True if the argument
tensor
is the same shape as this tensor and all entries are 'close', i.e. elementsa
andb
at the same positions in the two tensors respectively satisfy|a-b| <= (atol + rtol*|b|)
. Otherwise, returns false. - See Also:
-
toMatrix
-
toDense
Converts this sparse tensor to an equivalent dense tensor.- Specified by:
toDense
in classSparseTensorBase<CooTensor,
Tensor, CooCTensor, CTensor, CooTensor, double[], Double> - Returns:
- A dense tensor which is equivalent to this sparse tensor.
-