Operations
Tensorial operations act on tensor values directly. When an operation returns a tensor, the result keeps the corresponding tensor space.
This page shows the common operations used in formulas. Detailed signatures are collected in the API Reference.
Basic array-like operations
Tensorial values are AbstractArrays, so ordinary Julia and LinearAlgebra functions work as expected:
julia> x = @Vec [1.0, 2.0, 3.0]3-element Vec{3, Float64}: 1.0 2.0 3.0julia> A = @Mat [1.0 2.0 0.0; 0.0 3.0 4.0; 5.0 0.0 6.0]3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}: 1.0 2.0 0.0 0.0 3.0 4.0 5.0 0.0 6.0julia> norm(x)3.7416573867739413julia> tr(A)10.0
Julia's uniform-scaling identity I can be used in tensor formulas. For a symmetric tensor, adding I keeps the result symmetric:
julia> S = symmetric(@Mat [1.0 2.0; 2.0 4.0])2×2 SymmetricSecondOrderTensor{2, Float64, 3}: 1.0 2.0 2.0 4.0julia> S + I2×2 SymmetricSecondOrderTensor{2, Float64, 3}: 2.0 2.0 2.0 5.0julia> S ⊡₂ I ≈ tr(S)true
Inverse of tensor operators
Even-order tensors can be inverted when they represent square linear operators:
julia> C = rand(SymmetricFourthOrderTensor{3});julia> inv(C) ⊡₂ C ≈ one(SymmetricFourthOrderTensor{3})true
Contractions and tensor products
Use ⊡, ⊡₂, and ⊗ to write contractions and tensor products in tensor notation:
julia> A ⊡ x3-element Vec{3, Float64}: 5.0 18.0 23.0julia> A ⊡ x ≈ A * xtruejulia> A ⊡₂ A91.0julia> x ⊗ x3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}: 1.0 2.0 3.0 2.0 4.0 6.0 3.0 6.0 9.0julia> contract(A, x, Val(1)) ≈ A ⊡ xtrue
⊡contracts one neighboring index.⊡₂contracts two neighboring indices.⊗forms a tensor product.
The ASCII aliases are contract1, contract2, and tensor.
Indexed formulas
Use @einsum for formulas that are easier to read with indices. Repeated indices are contracted:
julia> (@einsum A[i,k] * A[k,j]) ≈ A * Atruejulia> (@einsum A[i,j] * A[i,j]) ≈ A ⋅ Atrue
Free indices can also be written explicitly when the output order matters:
julia> (@einsum (j,i) -> A[i,k] * A[k,j]) ≈ transpose(A * A)true
For a named result, put the free indices on the left-hand side:
julia> @einsum B[i,j] := A[i,k] * A[k,j];julia> B ≈ A * Atrue
Symmetric and skew parts
symmetric computes the symmetric part of a second-order tensor. skew computes the skew part:
julia> B = @Mat [1.0 3.0; 2.0 4.0]2×2 Tensor{Tuple{2, 2}, Float64, 2, 4}: 1.0 3.0 2.0 4.0julia> symmetric(B)2×2 SymmetricSecondOrderTensor{2, Float64, 3}: 1.0 2.5 2.5 4.0julia> skew(B)2×2 Tensor{Tuple{2, 2}, Float64, 2, 4}: 0.0 0.5 -0.5 0.0julia> symmetric(B) + skew(B) ≈ Btrue
For the type-level symmetry notation, see Tensor Types and Spaces.
Continuum mechanics helpers
The vol and dev helpers split a 3D tensor into volumetric and deviatoric parts. Fourth-order projector tensors are also available:
julia> σ = SymmetricSecondOrderTensor{3}((2.0, 0.4, 0.2, 1.2, 0.1, 0.9))3×3 SymmetricSecondOrderTensor{3, Float64, 6}: 2.0 0.4 0.2 0.4 1.2 0.1 0.2 0.1 0.9julia> vol(σ) + dev(σ) ≈ σtruejulia> tr(dev(σ))-7.771561172376096e-16julia> vonmises(σ)1.2649110640673518julia> Ivol = vol(SymmetricFourthOrderTensor{3});julia> Idev = dev(SymmetricFourthOrderTensor{3});julia> (Ivol + Idev) ⊡₂ σ ≈ σtrue
Rotations and spectral functions
Rotation helpers work with Tensorial vectors and tensors:
julia> R = rotmatz(π/4)3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}: 0.707107 -0.707107 0.0 0.707107 0.707107 0.0 0.0 0.0 1.0julia> R * (@Vec [1.0, 0.0, 0.0])3-element Vec{3, Float64}: 0.7071067811865476 0.7071067811865475 0.0julia> rotate(σ, R) isa SymmetricSecondOrderTensor{3}true
For symmetric second-order tensors, spectral functions such as sqrt, exp, and log return symmetric tensor types:
julia> sqrt(one(SymmetricSecondOrderTensor{3})) ≈ one(SymmetricSecondOrderTensor{3})truejulia> exp(zero(SymmetricSecondOrderTensor{3})) ≈ one(SymmetricSecondOrderTensor{3})true
For operation docstrings, see Core operations, Continuum mechanics API, Rotations and quaternions, and Spectral functions.