Class FieldTensor<T extends Field<T>>

Type Parameters:
T - Type of the field element for the tensor.
All Implemented Interfaces:
Serializable, FieldTensorMixin<FieldTensor<T>,FieldTensor<T>,T>, TensorOverField<FieldTensor<T>,FieldTensor<T>,T[],T>, RingTensorMixin<FieldTensor<T>,FieldTensor<T>,T>, TensorOverRing<FieldTensor<T>,FieldTensor<T>,T[],T>, SemiringTensorMixin<FieldTensor<T>,FieldTensor<T>,T>, TensorOverSemiring<FieldTensor<T>,FieldTensor<T>,T[],T>

public class FieldTensor<T extends Field<T>> extends AbstractDenseFieldTensor<FieldTensor<T>,T>

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

A FieldTensor is a generalization of the FieldMatrix, allowing for higher-dimensional data and operations while maintaining the benefits of field-based arithmetic and dense storage.

Key Features:

  • Support for standard tensor operations like addition, subtraction, element-wise multiplication, and reshaping.
  • Conversion methods to other representations, including FieldMatrix, FieldVector, 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.
     Complex128[] complexData = {
         new Complex128(1, 2), new Complex128(3, 4),
         new Complex128(5, 6), new Complex128(7, 8),
         new Complex128(9, 10), new Complex128(11, 12),
         new Complex128(13, 14), new Complex128(15, 16)
     };
    
     FieldTensor<Complex128> tensor = new FieldTensor<>(complexData);
     
  • Constructing a tensor from an nD array. This is provided for convenience but is generally much less efficient than FieldTensor(Shape, T[]).
    
     // Constructing a complex tensor from a 3D array of complex numbers
     Complex128[][][] complexData = {
         {{ new Complex128(1, 2), new Complex128(3, 4) },
         {  new Complex128(5, 6), new Complex128(7, 8) }},
    
         {{ new Complex128(9, 10),  new Complex128(11, 12) },
         {  new Complex128(13, 14), new Complex128(15, 16) }}
     };
     FieldTensor<Complex128> tensor = new FieldTensor<>(complexData);
     
  • Operations with/on tensors.
    
     // Performing element-wise addition
     FieldTensor<Complex128> result = tensor.add(tensor);
    
     // Reshape tensor
     FieldTensor<Complex128> reshape = tensor.reshape(new Shape(4, 1, 2));
    
     // Converting the tensor to a matrix
     FieldMatrix<Complex128> matrix = tensor.toMatrix(new Shape(4, 2));
    
     // Computing the tensor dot product.
     FieldTensor<Complex128> dot = tensor.tensorDot(tensor, new int[]{0, 1}, new int[]{2, 0});
     
See Also:
  • Constructor Details

    • FieldTensor

      public FieldTensor(Shape shape, T[] entries)
      Creates a tensor with the specified data and shape.
      Parameters:
      shape - Shape of this tensor.
      entries - 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.
    • FieldTensor

      public FieldTensor(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.
    • FieldTensor

      public FieldTensor(Shape shape, T fillValue)
      Creates a tensor with the specified shape filled with fillValue.
      Parameters:
      shape - Shape of this tensor.
      fillValue - Value to fill tensor with.
  • Method Details

    • makeLikeCooTensor

      protected CooFieldTensor<T> makeLikeCooTensor(Shape shape, T[] entries, int[][] indices)
      Constructs a sparse COO tensor which is of a similar type as this dense tensor.
      Specified by:
      makeLikeCooTensor in class AbstractDenseSemiringTensor<FieldTensor<T extends Field<T>>,T extends Field<T>>
      Parameters:
      shape - Shape of the COO tensor.
      entries - 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 FieldTensor<T> makeLikeTensor(Shape shape, T[] entries)
      Constructs a tensor of the same type as this tensor with the given the shape and data.
      Specified by:
      makeLikeTensor in interface TensorOverSemiring<FieldTensor<T extends Field<T>>,FieldTensor<T extends Field<T>>,T extends Field<T>[],T extends Field<T>>
      Specified by:
      makeLikeTensor in class AbstractTensor<FieldTensor<T extends Field<T>>,T extends Field<T>[],T extends Field<T>>
      Parameters:
      shape - Shape of the tensor to construct.
      entries - Entries of the tensor to construct.
      Returns:
      A tensor of the same type as this tensor with the given the shape and data.
    • toVector

      public FieldVector<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 FieldMatrix<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.
    • toMatrix

      public FieldMatrix<T> toMatrix()
      Converts this tensor to an equivalent matrix.
      Returns:
      If this tensor is rank 2, then the equivalent matrix will be returned. If the tensor is rank 1, then a matrix with a single row will be returned. If the rank of this tensor is larger than 2, it will be flattened to a single row.
    • abs

      public Tensor abs()

      Computes the element-wise absolute value of this tensor.

      Note: the absolute value may not be defined for all fields.

      Returns:
      The element-wise absolute value of this tensor.
    • 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 FieldTensor. 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.