KeitaNakamura / Tensorial.jl

Statically sized tensors and related operations for Julia
https://keitanakamura.github.io/Tensorial.jl/
MIT License
41 stars 1 forks source link
automatic-differentiation continuum-mechanics einstein-summation julia quaternion symmetric-tensors tensor

Tensorial.jl

Statically sized tensors and related operations for Julia

CI codecov

Tensorial.jl provides statically sized Tensor type that is compatible with AbstractArray, similar to SArray from StaticArrays.jl. In addition to supporting basic AbstractArray operations, the package offers a tensorial interface and several advanced features:

Documentation

Stable

Breaking changes (v0.18)

Starting from version 0.18, Tensorial.jl is now built on TensorCore.jl. The breaking changes are as follows:

Quick start

julia> using Tensorial

julia> x = Vec{3}(rand(3)); # constructor similar to SArray.jl

julia> A = @Mat rand(3,3); # @Vec, @Mat and @Tensor, analogous to @SVector, @SMatrix and @SArray

julia> A ⊡ x ≈ A * x # single contraction (⊡)
true

julia> A ⊡₂ A ≈ A ⋅ A # double contraction (⊡₂)
true

julia> x ⊗ x ≈ x * x' # tensor product (⊗)
true

julia> (@einsum y := x[i] * A[j,i] * x[j]) ≈ x ⊡ A' ⊡ x # Einstein summation (@einsum)
true

julia> As = rand(Tensor{Tuple{@Symmetry{3,3}}}); # specify symmetry S₍ᵢⱼ₎

julia> AAs = rand(Tensor{Tuple{@Symmetry{3,3}, @Symmetry{3,3}}}); # SS₍ᵢⱼ₎₍ₖₗ₎

julia> inv(AAs) ⊡₂ As ≈ @einsum Bs[i,j] := inv(AAs)[i,j,k,l] * As[k,l] # it just works
true

julia> δ = one(Mat{3,3}) # identity tensor
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

julia> gradient(identity, As) ≈ one(AAs) # ∂Asᵢⱼ/∂Asₖₗ = (δᵢₖδⱼₗ + δᵢₗδⱼₖ) / 2
true

Other tensor packages

Inspiration

Some functionalities are inspired from the following packages:

Citation

If you find Tensorial.jl useful in your work, I kindly request that you cite it as below:

@software{NakamuraTensorial2024,
    title = {Tensorial.jl: a {J}ulia package for tensor operations},
   author = {Nakamura, Keita},
      doi = {10.5281/zenodo.13955151},
     year = {2024},
      url = {https://github.com/KeitaNakamura/Tensorial.jl}
  licence = {MIT},
}