Class SemiringTensor<T extends Semiring<T>>

Type Parameters:
T - Type of the semiring element for the tensor.
All Implemented Interfaces:
Serializable, SemiringTensorMixin<SemiringTensor<T>,SemiringTensor<T>,T>, TensorOverSemiring<SemiringTensor<T>,SemiringTensor<T>,T[],T>

public class SemiringTensor<T extends Semiring<T>> extends AbstractDenseSemiringTensor<SemiringTensor<T>,T>

Instances of this class represent a dense tensor backed by a Semiring array. The SemiringTensor class provides functionality for tensor operations whose elements are members of a Semiring, supporting mutable data with a fixed shape.

A SemiringTensor is a generalization of the SemiringMatrix, allowing for higher-dimensional data and operations while maintaining the benefits of Semiring-based arithmetic and dense storage.

Key Features:

  • Support for standard tensor operations like addition, element-wise multiplication, and reshaping.
  • Conversion methods to other representations, including SemiringMatrix, SemiringVector, and COO format.
  • Utility methods for computing properties like rank and shape

Example Usage:

  • Constructing a tensor from a Shape shape and flat data array. This is generally the preferred and most efficient method of constructing a tensor.
    
     // Constructing a complex tensor from a shape and flat data array.
     BoolSemiring[] data = {
         new BoolSemiring(true),  new BoolSemiring(false),
         new BoolSemiring(true),  new BoolSemiring(true),
         new BoolSemiring(false), new BoolSemiring(false),
         new BoolSemiring(true),  new BoolSemiring(false)
     };
    
     SemiringTensor<Complex128> tensor = new SemiringTensor<>(data);
     
  • Constructing a tensor from an nD array. This is provided for convenience but is generally much less efficient than SemiringTensor(Shape, T[]).
    
     // Constructing a complex tensor from a 3D array of complex numbers
     BoolSemiring[][][] data = {
         {{ new BoolSemiring(true),  new BoolSemiring(false) },
         {  new BoolSemiring(true),  new BoolSemiring(true) }},
    
         {{ new BoolSemiring(false), new BoolSemiring(false) },
         {  new BoolSemiring(true),  new BoolSemiring(false) }}
     };
     SemiringTensor<BoolSemiring> tensor = new SemiringTensor<>(data);
     
  • Operations with/on tensors.
    
     // Performing element-wise addition
     SemiringTensor<BoolSemiring> result = tensor.add(tensor);
    
     // Reshape tensor
     SemiringTensor<BoolSemiring> reshape = tensor.reshape(new Shape(4, 1, 2));
    
     // Converting the tensor to a matrix
     SemiringMatrix<BoolSemiring> matrix = tensor.toMatrix(new Shape(4, 2));
    
     // Computing the tensor dot product.
     SemiringTensor<BoolSemiring> dot = tensor.tensorDot(tensor,
          new int[]{0, 1},
          new int[]{2, 0}
     );
     
See Also:
  • Constructor Details

    • SemiringTensor

      public SemiringTensor(Shape shape, T[] 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.
    • SemiringTensor

      public SemiringTensor(Object nDArray)
      Creates a tensor from an nD array. The tensors shape will be inferred from.
      Parameters:
      nDArray - Array to construct tensor from. Must be a rectangular array.
      Throws:
      IllegalArgumentException - If nDArray is not an array or not rectangular.
    • SemiringTensor

      public SemiringTensor(Shape shape, T fillValue)
      Creates a dense semiring tensor with the specified data and filled with filledValue.
      Parameters:
      shape - Shape of this tensor.
      fillValue - Entries of this tensor.
  • Method Details

    • makeLikeCooTensor

      protected CooSemiringTensor<T> makeLikeCooTensor(Shape shape, T[] data, int[][] indices)
      Constructs a sparse COO tensor which is of a similar type as this dense tensor.
      Specified by:
      makeLikeCooTensor in class AbstractDenseSemiringTensor<SemiringTensor<T extends Semiring<T>>,T extends Semiring<T>>
      Parameters:
      shape - Shape of the COO tensor.
      data - Non-zero data of the COO tensor.
      indices -
      Returns:
      A sparse COO tensor which is of a similar type as this dense tensor.
    • makeLikeTensor

      public SemiringTensor<T> makeLikeTensor(Shape shape, T[] 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.
      Specified by:
      makeLikeTensor in interface TensorOverSemiring<SemiringTensor<T extends Semiring<T>>,SemiringTensor<T extends Semiring<T>>,T extends Semiring<T>[],T extends Semiring<T>>
      Specified by:
      makeLikeTensor in class AbstractTensor<SemiringTensor<T extends Semiring<T>>,T extends Semiring<T>[],T extends Semiring<T>>
      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.
    • toVector

      public SemiringVector<T> toVector()
      Converts this tensor to an equivalent vector. If this tensor is not rank 1, then it will be flattened.
      Returns:
      A vector equivalent of this tensor.
    • toMatrix

      public SemiringMatrix<T> toMatrix(Shape matShape)
      Converts this tensor to a matrix with the specified shape.
      Parameters:
      matShape - Shape of the resulting matrix. Must be broadcastable with the shape of this tensor.
      Returns:
      A matrix of shape matShape with the values of this tensor.
      Throws:
      LinearAlgebraException - If matShape is not of rank 2.
    • equals

      public boolean equals(Object object)
      Checks if an object is equal to this tensor object.
      Overrides:
      equals in class Object
      Parameters:
      object - Object to check equality with this tensor.
      Returns:
      true if the two tensors have the same shape, are numerically equivalent, and are of type SemiringTensor. false otherwise.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Formats this tensor as a human-readable string. Specifically, a string containing the shape and flattened data of this tensor.
      Overrides:
      toString in class Object
      Returns:
      A human-readable string representing this tensor.