Class AbstractDenseSemiringTensor<T extends AbstractDenseSemiringTensor<T,V>,V extends Semiring<V>>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<T,V[],V>
org.flag4j.arrays.backend.semiring_arrays.AbstractDenseSemiringTensor<T,V>
- Type Parameters:
T
- The type of this dense semiring tensor.V
- The type of theSemiring
which this tensor's data belong to.U
- Type of sparse tensor equivalent toT
. This type parameter is required because some ops (e.g.toCoo()
) may result in a sparse tensor.
- All Implemented Interfaces:
Serializable
,SemiringTensorMixin<T,
,T, V> TensorOverSemiring<T,
T, V[], V>
- Direct Known Subclasses:
AbstractDenseRingTensor
,AbstractDenseSemiringMatrix
,AbstractDenseSemiringVector
,SemiringTensor
public abstract class AbstractDenseSemiringTensor<T extends AbstractDenseSemiringTensor<T,V>,V extends Semiring<V>>
extends AbstractTensor<T,V[],V>
implements SemiringTensorMixin<T,T,V>
The base class for all dense Semiring
tensors.
The AbstractTensor.data
of an AbstractDenseSemiringTensor are mutable but the AbstractTensor.shape
is fixed.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected 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
ModifierConstructorDescriptionprotected
AbstractDenseSemiringTensor
(Shape shape, V[] data) 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.void
Computes the element-wise sum between two tensors of the same shape and stores the result in this tensor.int[]
argmax()
Finds the indices of the maximum value in this tensor.int[]
argmin()
Finds the indices of the minimum value in this tensor.copy()
Creates a deep copy of this 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... indices) Gets the element of this tensor at the specified indices.Gets the zero element for the field of this tensor.protected abstract AbstractTensor
<?, V[], V> makeLikeCooTensor
(Shape shape, V[] data, int[][] indices) Constructs a sparse COO tensor which is of a similar type as this dense tensor.max()
Finds the maximum value in this tensor.min()
Finds the minimum value in this tensor.Copies and reshapes this tensor.Sets the element of this tensor at the specified indices.void
setZeroElement
(V zeroElement) Sets the zero element for the field 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.AbstractTensor
<?, V[], V> toCoo()
Converts this tensor to an equivalent sparse COO tensor.AbstractTensor
<?, V[], V> toCoo
(double estimatedSparsity) Converts this tensor to an equivalent sparse COO tensor.Methods inherited from class org.flag4j.arrays.backend.AbstractTensor
getData, getRank, getShape, makeLikeTensor, reshape, sameShape, T, 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
-
-
Constructor Details
-
AbstractDenseSemiringTensor
Creates a tensor with the specified data and shape.- Parameters:
shape
- Shape of this tensor.data
- Entries 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.- Throws:
IllegalArgumentException
- Ifshape.totalEntriesIntValueExact() != data.length
-
-
Method Details
-
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 field.- See Also:
-
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. - See Also:
-
makeLikeCooTensor
protected abstract AbstractTensor<?,V[], makeLikeCooTensorV> (Shape shape, V[] data, int[][] indices) Constructs a sparse COO tensor which is of a similar type as this dense tensor.- Parameters:
shape
- Shape of the COO tensor.data
- Non-zero data of the COO tensor.rowIndices
- Non-zero row indices of the COO tensor.colIndices
- Non-zero column indices of the COO tensor.- Returns:
- A sparse COO tensor which is of a similar type as this dense tensor.
-
get
Gets the element of this tensor at the specified indices.- Specified by:
get
in classAbstractTensor<T extends AbstractDenseSemiringTensor<T,
V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
indices
- Indices of the element to get.- Returns:
- The element of this tensor at the specified indices.
- Throws:
IndexOutOfBoundsException
- If any indices are not within this tensor.
-
set
Sets the element of this tensor at the specified indices.- Specified by:
set
in classAbstractTensor<T extends AbstractDenseSemiringTensor<T,
V>, V extends Semiring<V>[], V extends Semiring<V>> - Parameters:
value
- New value to set the specified index of this tensor to.indices
- Indices 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
- Ifindices
is not within the bounds of this tensor.
-
flatten
-
flatten
Flattens a tensor along the specified axis.- Specified by:
flatten
in classAbstractTensor<T extends AbstractDenseSemiringTensor<T,
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 AbstractDenseSemiringTensor<T,
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
.
-
add
Computes the element-wise sum between two tensors of the same shape.- Specified by:
add
in interfaceTensorOverSemiring<T extends AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, 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.
-
addEq
Computes the element-wise sum between two tensors of the same shape and stores the result in this tensor.- Parameters:
b
- Second tensor in the element-wise sum.
-
elemMult
Computes the element-wise multiplication of two tensors of the same shape.- Specified by:
elemMult
in interfaceTensorOverSemiring<T extends AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, 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 AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, 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 AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, 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.)
-
min
Finds the minimum 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 AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, V>, V extends Semiring<V>> - Returns:
- The minimum value (smallest in magnitude for a complex valued tensor) in this tensor.
-
max
Finds the maximum value in this tensor. If this tensor is complex, then this method finds the largest value in magnitude.- Specified by:
max
in interfaceSemiringTensorMixin<T extends AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, V>, V extends Semiring<V>> - Returns:
- The maximum value (largest in magnitude for a complex valued tensor) in this tensor.
-
argmin
public int[] argmin()Finds the indices of the minimum value in this tensor.- Specified by:
argmin
in interfaceSemiringTensorMixin<T extends AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, V>, V extends Semiring<V>> - 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.- Specified by:
argmax
in interfaceSemiringTensorMixin<T extends AbstractDenseSemiringTensor<T,
V>, T extends AbstractDenseSemiringTensor<T, V>, V extends Semiring<V>> - 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.
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- Specified by:
T
in classAbstractTensor<T extends AbstractDenseSemiringTensor<T,
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 AbstractDenseSemiringTensor<T,
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
-
toCoo
Converts this tensor to an equivalent sparse COO tensor.- Returns:
- A sparse COO tensor that is equivalent to this dense tensor.
- See Also:
-
toCoo
Converts this tensor to an equivalent sparse COO tensor.- Parameters:
estimatedSparsity
- Estimated sparsity of the tensor. Must be between 0 and 1 inclusive. If this is an accurate estimation it may provide a slight speedup and can reduce unneeded memory consumption. If memory is a concern, it is better to over-estimate the sparsity. If speed is the concern it is better to under-estimate the sparsity.- Returns:
- A sparse COO tensor that is equivalent to this dense tensor.
- See Also:
-