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 an Object, 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
    Modifier and Type
    Field
    Description
    final U
    Entry data of this tensor.
    final int
    The rank of this tensor.
    final Shape
    The shape of this tensor.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractTensor(Shape shape, U data)
    Creates a tensor with the specified data and shape.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract T
    Creates a deep copy of this tensor.
    abstract T
    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.
    Gets the entry data of this tensor as a 1D array.
    int
    Gets the rank of this tensor.
    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 the shape and data.
    reshape(int... dims)
    Copies and reshapes this tensor.
    abstract T
    reshape(Shape newShape)
    Copies and reshapes this tensor.
    boolean
    Checks if a tensor has the same shape as this tensor.
    abstract T
    set(V value, int... indices)
    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 exchanging axis1 and axis2.
    Gets the total number of data in this tensor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • data

      public final U 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

      public final Shape shape
      The shape of this tensor.
    • rank

      public final int rank
      The 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

      protected AbstractTensor(Shape shape, U data)
      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

      public Shape getShape()
      Gets the shape of this tensor.
      Returns:
      The shape of this tensor.
    • get

      public abstract V get(int... indices)
      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

      public abstract T set(V value, int... indices)
      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 - If indices 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

      public U getData()
      Gets the entry data of this tensor as a 1D array.
      Returns:
      The data of this tensor.
    • totalEntries

      public BigInteger totalEntries()
      Gets the total number of data in this tensor.
      Returns:
      The total number of data in this tensor.
    • sameShape

      public boolean sameShape(AbstractTensor<?,?,?> B)
      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

      public abstract T flatten()
      Flattens tensor to single dimension while preserving order of data.
      Returns:
      The flattened tensor.
      See Also:
    • flatten

      public abstract T flatten(int axis)
      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 than this.{@link #getRank()}-1.
      See Also:
    • reshape

      public abstract T reshape(Shape newShape)
      Copies and reshapes this tensor.
      Parameters:
      newShape - New shape for the tensor.
      Returns:
      A copy of this tensor with the new shape.
      Throws:
      TensorShapeException - If newShape is not broadcastable to this.shape.
    • reshape

      public T reshape(int... dims)
      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 - If dims does not represent a shape broadcastable to this.shape.
    • makeLikeTensor

      public abstract T makeLikeTensor(Shape shape, U entries)
      Constructs a tensor of the same type as this tensor with the given the shape and data. 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 and data.
    • T

      public T 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

      public abstract T T(int axis1, int axis2)
      Computes the transpose of a tensor by exchanging axis1 and axis2.
      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 either axis1 or axis2 are out of bounds for the rank of this tensor.
      See Also:
    • T

      public abstract T T(int... axes)
      Computes the transpose of this tensor. That is, permutes the axes of this tensor so that it matches the permutation specified by axes.
      Parameters:
      axes - Permutation of tensor axis. If the tensor has rank N, then this must be an array of length N 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 of axes is out of bounds for the rank of this tensor.
      IllegalArgumentException - If axes is not a permutation of {1, 2, 3, ... N-1}.
      See Also:
    • copy

      public abstract T copy()
      Creates a deep copy of this tensor.
      Returns:
      A deep copy of this tensor.