Tensors
The tensors will eventually be replaced by the linear algebra module's array.
At a Glance
- Tensors in Aleph are multidimensional arrays that represent tensor components in a specific basis.
- The choice of basis is user-determined; Aleph provides the array representation, not the abstract tensor object.
- Tensors can hold real or complex values and are the fundamental data structure for multilinear algebra.
- Tensors support operations like contraction, factorization, and element-wise arithmetic.
Overview
In Aleph, a tensor is represented as a multidimensional array of components. There are currently two types of tensors: RealTensor and ComplexTensor. For each of its dimensions, a tensor has one index, and the number of dimensions is called the order of the tensor:
- Order 0: Scalar (no indices) -
- Order 1: Vector (one index) -
- Order 2: Matrix (two indices) -
- Order 3+: Higher-order tensor -
Mathematically, an order- tensor over a field (typically or ) with dimensions can be written as:
where each index ranges from to .
The number of dimensions is sometimes also called "rank" in tensor literature, but we avoid this term to prevent confusion with matrix rank, which quantifies the sparsity properties of matrices and tensors.
In Aleph, tensors are component arrays in a specific basis, not abstract mathematical tensor objects. The choice of basis is user-determined and context-dependent. For example, a quantum state vector might be represented in the position basis or the momentum basis; Aleph simply provides the numerical array of components in whichever basis you choose to work with.
Creating Tensors in Aleph
Basic Tensor Creation
// Create a tensor with specific dimensions
var T1 = zero_tensor([2, 3, 4],as_real) // 2×3×4 tensor of zeros
var T2 = filled_tensor([5, 5],1,as_real) // 5×5 matrix of ones
var T3 = random_tensor([3, 4, 5],as_real) // 3×4×5 tensor with random real values
// Create complex tensors
var C1 = zeros_tensor([2, 3],as_complex) // 2×3 complex matrix
var C2 = random_tensor([4, 4, 4],as_complex) // 4×4×4 complex tensor
Tensor Properties
Shape and Dimensions
var T = random_tensor([2, 3, 4],as_real)
print(T.shape()) // [2, 3, 4]
print(T.order()) // 3
Indexing and Slicing
var T = random_tensor([3, 4, 5],as_real)
// Access individual elements
var element = T[0, 2, 1]
Common Tensor Operations
Element-wise Operations
var A = random_tensor([3, 4],as_real)
var B = random_tensor([3, 4],as_real)
// Element-wise arithmetic
var E = A * B // Element-wise multiplication (Hadamard product)
var F = A / B // Element-wise division
// Scalar operations
var G = 2.0 * A // Scalar multiplication
var H = A + 1.0 // Scalar addition (not yet supported.)
Tensor Contraction
See the Contraction page for detailed information.
// Matrix multiplication as tensor contraction
var A = random_tensor([3, 4],as_real)
var B = random_tensor([4, 5],as_real)
var C = contract(A, B, [1], [0]) // Result: 3×5 matrix
Reshaping and Transposition
// Not yet implemented
var T = random_tensor([2, 3, 4],as_real)
// Reshape (total size must match)
var T_reshaped = T.reshape([6, 4]) // 2*3 = 6
// Transpose (permute dimensions)
var T_transposed = T.transpose([2, 0, 1]) // New shape: [4, 2, 3]
Complex Tensors
Complex tensors are essential in quantum mechanics and signal processing.
// Create complex tensors
var C = random_tensor([3, 3],as_complex)
// Complex conjugation
var C_conj = C.conjugate()
// Hermitian transpose (conjugate transpose)
var C_dagger = C.conjugate().transpose([1, 0])
Applications
Tensors are useful in many domains such as mechanical strain analysis, quantum mechanics, tensor networks and machine learning.
Further Reading
- For detailed information on tensor contraction operations, see the Contraction page
- For tensor decomposition methods, see the Factorization page