Interface Tensor<T>
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
CUDATensor, DenseTensor, NativeTensor, SparseTensor
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 TypeMethodDescriptionElement-wise addition.broadcast(int... newShape) Broadcasts this tensor to a new shape.copy()Returns a copy of this tensor.Performs Einstein summation.get(int... indices) Gets the element at the specified indices.Applies a function to each element of the tensor.Element-wise multiplication (Hadamard product).static <T> Tensor<T> of(T[] data, int... shape) Creates a tensor from data.intrank()Returns the rank (number of dimensions) of this tensor.reshape(int... newShape) Reshapes this tensor to a new shape.Scalar multiplication.voidSets the element at the specified indices.int[]shape()Returns the shape of this tensor.intsize()Returns the total number of elements.slice(int[] starts, int[] sizes) Returns a slice (sub-tensor) of this tensor.Element-wise subtraction.sum()Sums all elements.sum(int axis) Sums along the specified axis.toArray()Converts this tensor to a multidimensional array.Transposes a rank-2 tensor (matrix).transpose(int... permutation) Transposes dimensions according to the given permutation.static <T> Tensor<T> Creates a tensor of zeros with the specified shape.
-
Method Details
-
zeros
-
of
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
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
Sets the element at the specified indices.- Parameters:
value- the value to setindices- the indices (length must equal rank)- Throws:
IndexOutOfBoundsException- if indices are invalid
-
add
-
subtract
-
multiply
-
scale
-
reshape
-
broadcast
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
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
Transposes a rank-2 tensor (matrix).- Returns:
- transposed matrix
- Throws:
UnsupportedOperationException- if rank != 2
-
slice
Returns a slice (sub-tensor) of this tensor.- Parameters:
starts- starting index for each dimensionsizes- size of the slice for each dimension- Returns:
- sliced tensor (view)
- Throws:
IndexOutOfBoundsException- if slice is out of bounds
-
map
-
sum
-
sum
-
copy
-
toArray
Object toArray()Converts this tensor to a multidimensional array.- Returns:
- nested array representation
-
einsum
-