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 the Semiring which this tensor's data belong to.
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 Link icon

    Fields
    Modifier and Type
    Field
    Description
    protected V
    The zero element for the semiring that this tensor's elements belong to.

    Fields inherited from class org.flag4j.arrays.backend.AbstractTensor Link icon

    data, rank, shape
  • Constructor Summary Link icon

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a tensor with the specified data and shape.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    add(T b)
    Computes the element-wise sum between two tensors of the same shape.
    void
    addEq(T b)
    Computes the element-wise sum between two tensors of the same shape and stores the result in this tensor.
    int[]
    Finds the indices of the maximum value in this tensor.
    int[]
    Finds the indices of the minimum value in this tensor.
    Creates a deep copy of this tensor.
    int
    Gets the size of the 1D data array backing this tensor.
    Computes the element-wise multiplication of two tensors of the same shape.
    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.
    reshape(Shape newShape)
    Copies and reshapes this tensor.
    set(V value, int... indices)
    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 exchanging axis1 and axis2.
    tensorDot(T src2, int[] aAxes, int[] bAxes)
    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 sparse COO tensor.
    toCoo(double estimatedSparsity)
    Converts this tensor to an equivalent sparse COO tensor.

    Methods inherited from class org.flag4j.arrays.backend.AbstractTensor Link icon

    getData, getRank, getShape, makeLikeTensor, reshape, sameShape, T, totalEntries

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.SemiringTensorMixin Link icon

    add, addEq, isOnes, isZeros, makeEmptyDataArray, mult, multEq, prod, sum

    Methods inherited from interface org.flag4j.arrays.backend.semiring_arrays.TensorOverSemiring Link icon

    getData, getRank, getShape, makeLikeTensor, tensorDot, tensorDot, tensorDot, tensorTr
  • Field Details Link icon

    • zeroElement Link icon

      protected V extends Semiring<V> zeroElement
      The zero element for the semiring that this tensor's elements belong to.
  • Constructor Details Link icon

    • AbstractDenseSemiringTensor Link icon

      protected AbstractDenseSemiringTensor(Shape shape, V[] 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.
      Throws:
      IllegalArgumentException - If shape.totalEntriesIntValueExact() != data.length
  • Method Details Link icon

    • dataLength Link icon

      public int dataLength()
      Gets the size of the 1D data array backing this tensor.
      Specified by:
      dataLength in class AbstractTensor<T extends AbstractDenseSemiringTensor<T,V>,V extends Semiring<V>[],V extends Semiring<V>>
      Returns:
      The size of the 1D data array backing this tensor.
    • setZeroElement Link icon

      public void setZeroElement(V zeroElement)
      Sets the zero element for the field of this tensor.
      Parameters:
      zeroElement - The zero element of this tensor.
      Throws:
      IllegalArgumentException - If zeroElement is not an additive identity for the field.
      See Also:
    • getZeroElement Link icon

      public V 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) then null will be returned.
      See Also:
    • makeLikeCooTensor Link icon

      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.
      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 Link icon

      public V get(int... indices)
      Gets the element of this tensor at the specified indices.
      Specified by:
      get in class AbstractTensor<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 Link icon

      public T set(V value, int... indices)
      Sets the element of this tensor at the specified indices.
      Specified by:
      set in class AbstractTensor<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 - If indices is not within the bounds of this tensor.
    • flatten Link icon

      public T flatten()
      Flattens tensor to single dimension while preserving order of data.
      Specified by:
      flatten in class AbstractTensor<T extends AbstractDenseSemiringTensor<T,V>,V extends Semiring<V>[],V extends Semiring<V>>
      Returns:
      The flattened tensor.
      See Also:
    • flatten Link icon

      public T flatten(int axis)
      Flattens a tensor along the specified axis.
      Specified by:
      flatten in class AbstractTensor<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 than this.{@link #getRank()}-1.
      See Also:
    • reshape Link icon

      public T reshape(Shape newShape)
      Copies and reshapes this tensor.
      Specified by:
      reshape in class AbstractTensor<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 - If newShape is not broadcastable to this.shape.
    • add Link icon

      public T add(T b)
      Computes the element-wise sum between two tensors of the same shape.
      Specified by:
      add in interface TensorOverSemiring<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 and b do not have the same shape.
    • addEq Link icon

      public void addEq(T b)
      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 Link icon

      public T elemMult(T b)
      Computes the element-wise multiplication of two tensors of the same shape.
      Specified by:
      elemMult in interface TensorOverSemiring<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 and b do not have the same shape.
    • tensorDot Link icon

      public T tensorDot(T src2, int[] aAxes, int[] bAxes)
      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 interface TensorOverSemiring<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 for src2 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 in aAxes and bAxes.
      IllegalArgumentException - If aAxes and bAxes do not match in length, or if any of the axes are out of bounds for the corresponding tensor.
    • tensorTr Link icon

      public T tensorTr(int axis1, int axis2)

      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 and axis2. The shape of the resulting tensor is equal to this tensor with the axis1 and axis2 removed.

      Specified by:
      tensorTr in interface TensorOverSemiring<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 and axis2.
      Throws:
      IndexOutOfBoundsException - If the two axes are not both larger than zero and less than this tensors rank.
      IllegalArgumentException - If axis1 == axis2 or this.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 Link icon

      public V 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 interface SemiringTensorMixin<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 Link icon

      public V 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 interface SemiringTensorMixin<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 Link icon

      public int[] argmin()
      Finds the indices of the minimum value in this tensor.
      Specified by:
      argmin in interface SemiringTensorMixin<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 Link icon

      public int[] argmax()
      Finds the indices of the maximum value in this tensor.
      Specified by:
      argmax in interface SemiringTensorMixin<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 Link icon

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

      public 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.
      Specified by:
      T in class AbstractTensor<T extends AbstractDenseSemiringTensor<T,V>,V extends Semiring<V>[],V extends Semiring<V>>
      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 Link icon

      public T copy()
      Creates a deep copy of this tensor.
      Specified by:
      copy in class AbstractTensor<T extends AbstractDenseSemiringTensor<T,V>,V extends Semiring<V>[],V extends Semiring<V>>
      Returns:
      A deep copy of this tensor.
    • toCoo Link icon

      public AbstractTensor<?,V[],V> 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 Link icon

      public AbstractTensor<?,V[],V> toCoo(double estimatedSparsity)
      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: