Interface Tensor<T>

All Superinterfaces:
Serializable
All Known Implementing Classes:
CUDATensor, DenseTensor, NativeTensor, SparseTensor

public interface Tensor<T> extends Serializable
Represents a multidimensional array (tensor).

Tensors generalize scalars (rank 0), vectors (rank 1), and matrices (rank 2) to arbitrary dimensions. They are fundamental in physics, machine learning, and numerical computing.

Rank and Shape

  • Rank: Number of dimensions (e.g., rank 3 = 3D tensor)
  • Shape: Size of each dimension (e.g., [2, 3, 4] = 2×3×4)
  • Size: Total number of elements (product of shape)
Since:
1.0
Author:
Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Tensor<T> other)
    Element-wise addition.
    broadcast(int... newShape)
    Broadcasts this tensor to a new shape.
    Returns a copy of this tensor.
    default Tensor<T>
    einsum(String equation, Tensor<T>... operands)
    Performs Einstein summation.
    get(int... indices)
    Gets the element at the specified indices.
    map(Function<T,T> function)
    Applies a function to each element of the tensor.
    multiply(Tensor<T> other)
    Element-wise multiplication (Hadamard product).
    static <T> Tensor<T>
    of(T[] data, int... shape)
    Creates a tensor from data.
    int
    Returns the rank (number of dimensions) of this tensor.
    reshape(int... newShape)
    Reshapes this tensor to a new shape.
    scale(T scalar)
    Scalar multiplication.
    void
    set(T value, int... indices)
    Sets the element at the specified indices.
    int[]
    Returns the shape of this tensor.
    int
    Returns the total number of elements.
    slice(int[] starts, int[] sizes)
    Returns a slice (sub-tensor) of this tensor.
    subtract(Tensor<T> other)
    Element-wise subtraction.
    sum()
    Sums all elements.
    sum(int axis)
    Sums along the specified axis.
    Converts this tensor to a multidimensional array.
    default Tensor<T>
    Transposes a rank-2 tensor (matrix).
    transpose(int... permutation)
    Transposes dimensions according to the given permutation.
    static <T> Tensor<T>
    zeros(Class<T> elementType, int... shape)
    Creates a tensor of zeros with the specified shape.
  • Method Details

    • zeros

      static <T> Tensor<T> zeros(Class<T> elementType, int... shape)
      Creates a tensor of zeros with the specified shape.
    • of

      static <T> Tensor<T> of(T[] data, int... shape)
      Creates a tensor from data.
    • shape

      int[] shape()
      Returns the shape of this tensor.
      Returns:
      array of dimension sizes
    • rank

      int rank()
      Returns the rank (number of dimensions) of this tensor.
      Returns:
      rank ≥ 0
    • size

      int size()
      Returns the total number of elements.
      Returns:
      size = product of shape
    • get

      T get(int... indices)
      Gets the element at the specified indices.
      Parameters:
      indices - the indices (length must equal rank)
      Returns:
      the element
      Throws:
      IndexOutOfBoundsException - if indices are invalid
    • set

      void set(T value, int... indices)
      Sets the element at the specified indices.
      Parameters:
      value - the value to set
      indices - the indices (length must equal rank)
      Throws:
      IndexOutOfBoundsException - if indices are invalid
    • add

      Tensor<T> add(Tensor<T> other)
      Element-wise addition.
      Parameters:
      other - tensor with same shape
      Returns:
      this + other
    • subtract

      Tensor<T> subtract(Tensor<T> other)
      Element-wise subtraction.
      Parameters:
      other - tensor with same shape
      Returns:
      this - other
    • multiply

      Tensor<T> multiply(Tensor<T> other)
      Element-wise multiplication (Hadamard product).
      Parameters:
      other - tensor with same shape
      Returns:
      this ⊙ other
    • scale

      Tensor<T> scale(T scalar)
      Scalar multiplication.
      Parameters:
      scalar - the scalar value
      Returns:
      scalar * this
    • reshape

      Tensor<T> reshape(int... newShape)
      Reshapes this tensor to a new shape.

      The new shape must have the same total size.

      Parameters:
      newShape - the new shape
      Returns:
      reshaped tensor
    • broadcast

      Tensor<T> broadcast(int... newShape)
      Broadcasts this tensor to a new shape.

      Follows standard broadcasting rules (dimensions of size 1 can be expanded).

      Parameters:
      newShape - the target shape
      Returns:
      broadcasted tensor (view)
      Throws:
      IllegalArgumentException - if broadcasting is not possible
    • transpose

      Tensor<T> transpose(int... permutation)
      Transposes dimensions according to the given permutation.

      For a matrix (rank 2), transpose() swaps rows and columns. For higher-rank tensors, specify the permutation explicitly.

      Parameters:
      permutation - permutation of dimension indices
      Returns:
      transposed tensor
    • transpose

      default Tensor<T> transpose()
      Transposes a rank-2 tensor (matrix).
      Returns:
      transposed matrix
      Throws:
      UnsupportedOperationException - if rank != 2
    • slice

      Tensor<T> slice(int[] starts, int[] sizes)
      Returns a slice (sub-tensor) of this tensor.
      Parameters:
      starts - starting index for each dimension
      sizes - size of the slice for each dimension
      Returns:
      sliced tensor (view)
      Throws:
      IndexOutOfBoundsException - if slice is out of bounds
    • map

      Tensor<T> map(Function<T,T> function)
      Applies a function to each element of the tensor.
      Parameters:
      function - the function to apply
      Returns:
      a new tensor with transformed elements
    • sum

      T sum()
      Sums all elements.
      Returns:
      Σ elements
    • sum

      Tensor<T> sum(int axis)
      Sums along the specified axis.
      Parameters:
      axis - the axis to sum over (0 to rank-1)
      Returns:
      tensor with rank reduced by 1
    • copy

      Tensor<T> copy()
      Returns a copy of this tensor.
      Returns:
      independent copy
    • toArray

      Object toArray()
      Converts this tensor to a multidimensional array.
      Returns:
      nested array representation
    • einsum

      default Tensor<T> einsum(String equation, Tensor<T>... operands)
      Performs Einstein summation.
      Parameters:
      equation - the einsum equation (e.g. "ij,jk->ik")
      operands - the other operands
      Returns:
      the result