Class AbstractTensor<T extends AbstractTensor<T,U,V>,U,V>
java.lang.Object
org.flag4j.arrays.backend.AbstractTensor<T,U,V>
- Type Parameters:
T
- The specific type of the tensor (used for method return types in a fluent API).U
- The type of the data storage container for this tensor. This should be an array, list, or similar list-like structure.V
- The type (or wrapper) of the individual data elements in this tensor. If the tensors elements are primitive types, this should be their corresponding wrapper class. If the elements are anObject
, then this should be the same type as the object.
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
AbstractCooSemiringMatrix
,AbstractCooSemiringTensor
,AbstractCooSemiringVector
,AbstractCsrSemiringMatrix
,AbstractDenseSemiringTensor
,AbstractDoubleTensor
public abstract class AbstractTensor<T extends AbstractTensor<T,U,V>,U,V>
extends Object
implements Serializable
The base abstract class for all tensors, including matrices and vectors.
A tensor is a multidimensional array that consists of:
- Shape: The
shape
of the tensor specifies the dimensions of the tensor along each axis. The number of axes in the tensor is referred to as the "rank
" and corresponds to the number of indices required to uniquely identify an element within the tensor. - Data: A one-dimensional container for the
data
of the tensor. If the tensor is dense, this contains all the data of the tensor. If the tensor is sparse, this contains only the non-zero elements of the tensor.
This abstract class provides common functionality and properties for all tensor types. Subclasses should implement the abstract methods to provide specific behaviors for different tensor types and data storage mechanisms (e.g., dense or sparse).
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractTensor
(Shape shape, U data) Creates a tensor with the specified data and shape. -
Method Summary
Modifier and TypeMethodDescriptionabstract T
copy()
Creates a deep copy of this tensor.abstract T
flatten()
Flattens tensor to single dimension while preserving order of data.abstract T
flatten
(int axis) Flattens a tensor along the specified axis.abstract V
get
(int... indices) Gets the element of this tensor at the specified indices.getData()
Gets the entry data of this tensor as a 1D array.int
getRank()
Gets the rank of this tensor.getShape()
Gets the shape of this tensor.abstract T
makeLikeTensor
(Shape shape, U entries) Constructs a tensor of the same type as this tensor with the given theshape
anddata
.reshape
(int... dims) Copies and reshapes this tensor.abstract T
Copies and reshapes this tensor.boolean
sameShape
(AbstractTensor<?, ?, ?> B) Checks if a tensor has the same shape as this tensor.abstract T
Sets the element of this tensor at the specified indices.T()
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.abstract T
T
(int... axes) Computes the transpose of this tensor.abstract T
T
(int axis1, int axis2) Computes the transpose of a tensor by exchangingaxis1
andaxis2
.Gets the total number of data in this tensor.
-
Field Details
-
data
Entry data of this tensor. If this tensor is dense, then this specifies all data within this tensor. If this tensor is sparse, this specifies only the non-zero data of this tensor. -
shape
The shape of this tensor. -
rank
public final int rankThe rank of this tensor. That is, the number of indices required to uniquely specify an element in the tensor (the number of axes within this tensor).
-
-
Constructor Details
-
AbstractTensor
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.
-
-
Method Details
-
getShape
-
get
Gets the element of this tensor at the specified indices.- 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 tensor.
-
set
Sets the element of this tensor at the specified indices.- 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.
-
getRank
public int getRank()Gets the rank of this tensor. That is, number of indices needed to uniquely select an element of the tensor. This is also te number of dimensions (i.e. order/degree) of the tensor.
Note, this method is distinct from the
matrix rank
.- Returns:
- The rank of this tensor.
-
getData
Gets the entry data of this tensor as a 1D array.- Returns:
- The data of this tensor.
-
totalEntries
Gets the total number of data in this tensor.- Returns:
- The total number of data in this tensor.
-
sameShape
Checks if a tensor has the same shape as this tensor.- Parameters:
B
- Second tensor.- Returns:
- True if this tensor and B have the same shape. False otherwise.
-
flatten
Flattens tensor to single dimension while preserving order of data.- Returns:
- The flattened tensor.
- See Also:
-
flatten
Flattens a tensor along the specified axis.- 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.- 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
.
-
reshape
Copies and reshapes this tensor.- Parameters:
dims
- The dimensions of the new shape.- Returns:
- A copy of this tensor with the new shape.
- Throws:
TensorShapeException
- Ifdims
does not represent a shape broadcastable tothis.shape
.
-
makeLikeTensor
Constructs a tensor of the same type as this tensor with the given theshape
anddata
. The resulting tensor will also have the same non-zero indices as this tensor.- Parameters:
shape
- Shape of the tensor to construct.entries
- Entries of the tensor to construct.- Returns:
- A tensor of the same type and with the same non-zero indices as this tensor with the given the
shape
anddata
.
-
T
Computes the transpose of a tensor by exchanging the first and last axes of this tensor.- Returns:
- The transpose of this tensor.
- See Also:
-
T
Computes the transpose of a tensor by exchangingaxis1
andaxis2
.- 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
.- 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
-