Class NativeTensor<T>

java.lang.Object
org.episteme.nativ.mathematics.linearalgebra.tensors.NativeTensor<T>
Type Parameters:
T - the element type (must be Float or Double for native storage)
All Implemented Interfaces:
Serializable, Tensor<T>

public class NativeTensor<T> extends Object implements Tensor<T>
Tensor implementation backed by native memory (off-heap).

Supports hardware acceleration via FFM (Project Panama). Currently optimized for Float and Double types.

Since:
1.2
Author:
Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
See Also:
  • Constructor Details

    • NativeTensor

      public NativeTensor(Class<T> type, int... shape)
      Creates a new NativeTensor.
      Parameters:
      type - the element class (Float.class or Double.class)
      shape - the tensor shape
  • Method Details

    • shape

      public int[] shape()
      Description copied from interface: Tensor
      Returns the shape of this tensor.
      Specified by:
      shape in interface Tensor<T>
      Returns:
      array of dimension sizes
    • rank

      public int rank()
      Description copied from interface: Tensor
      Returns the rank (number of dimensions) of this tensor.
      Specified by:
      rank in interface Tensor<T>
      Returns:
      rank ≥ 0
    • size

      public int size()
      Description copied from interface: Tensor
      Returns the total number of elements.
      Specified by:
      size in interface Tensor<T>
      Returns:
      size = product of shape
    • get

      public T get(int... indices)
      Description copied from interface: Tensor
      Gets the element at the specified indices.
      Specified by:
      get in interface Tensor<T>
      Parameters:
      indices - the indices (length must equal rank)
      Returns:
      the element
    • set

      public void set(T value, int... indices)
      Description copied from interface: Tensor
      Sets the element at the specified indices.
      Specified by:
      set in interface Tensor<T>
      Parameters:
      value - the value to set
      indices - the indices (length must equal rank)
    • add

      public Tensor<T> add(Tensor<T> other)
      Description copied from interface: Tensor
      Element-wise addition.
      Specified by:
      add in interface Tensor<T>
      Parameters:
      other - tensor with same shape
      Returns:
      this + other
    • subtract

      public Tensor<T> subtract(Tensor<T> other)
      Description copied from interface: Tensor
      Element-wise subtraction.
      Specified by:
      subtract in interface Tensor<T>
      Parameters:
      other - tensor with same shape
      Returns:
      this - other
    • multiply

      public Tensor<T> multiply(Tensor<T> other)
      Description copied from interface: Tensor
      Element-wise multiplication (Hadamard product).
      Specified by:
      multiply in interface Tensor<T>
      Parameters:
      other - tensor with same shape
      Returns:
      this ⊙ other
    • scale

      public Tensor<T> scale(T scalar)
      Description copied from interface: Tensor
      Scalar multiplication.
      Specified by:
      scale in interface Tensor<T>
      Parameters:
      scalar - the scalar value
      Returns:
      scalar * this
    • reshape

      public Tensor<T> reshape(int... newShape)
      Description copied from interface: Tensor
      Reshapes this tensor to a new shape.

      The new shape must have the same total size.

      Specified by:
      reshape in interface Tensor<T>
      Parameters:
      newShape - the new shape
      Returns:
      reshaped tensor
    • broadcast

      public Tensor<T> broadcast(int... newShape)
      Description copied from interface: Tensor
      Broadcasts this tensor to a new shape.

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

      Specified by:
      broadcast in interface Tensor<T>
      Parameters:
      newShape - the target shape
      Returns:
      broadcasted tensor (view)
    • transpose

      public Tensor<T> transpose(int... permutation)
      Description copied from interface: Tensor
      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.

      Specified by:
      transpose in interface Tensor<T>
      Parameters:
      permutation - permutation of dimension indices
      Returns:
      transposed tensor
    • slice

      public Tensor<T> slice(int[] starts, int[] sizes)
      Description copied from interface: Tensor
      Returns a slice (sub-tensor) of this tensor.
      Specified by:
      slice in interface Tensor<T>
      Parameters:
      starts - starting index for each dimension
      sizes - size of the slice for each dimension
      Returns:
      sliced tensor (view)
    • map

      public Tensor<T> map(Function<T,T> function)
      Description copied from interface: Tensor
      Applies a function to each element of the tensor.
      Specified by:
      map in interface Tensor<T>
      Parameters:
      function - the function to apply
      Returns:
      a new tensor with transformed elements
    • sum

      public T sum()
      Description copied from interface: Tensor
      Sums all elements.
      Specified by:
      sum in interface Tensor<T>
      Returns:
      Σ elements
    • sum

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

      public Tensor<T> copy()
      Description copied from interface: Tensor
      Returns a copy of this tensor.
      Specified by:
      copy in interface Tensor<T>
      Returns:
      independent copy
    • toArray

      public Object toArray()
      Description copied from interface: Tensor
      Converts this tensor to a multidimensional array.
      Specified by:
      toArray in interface Tensor<T>
      Returns:
      nested array representation
    • getSegment

      public MemorySegment getSegment()