项目作者: KeitaNakamura

项目描述 :
Statically sized tensors and related operations for Julia
高级语言: Julia
项目地址: git://github.com/KeitaNakamura/Tensorial.jl.git
创建时间: 2021-01-16T15:38:18Z
项目社区:https://github.com/KeitaNakamura/Tensorial.jl

开源协议:MIT License

下载


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:

  • Contraction, tensor product (), and a flexible @einsum macro for Einstein summation convention
  • A @Symmetry macro to define the tensor symmetries, eliminating unnecessary calculations
  • Automatic differentiation via gradient and hessian functions, leveraging ForwardDiff.jl
  • Performance comparable to SArray (see benchmarks)

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:

  • Single contraction: has been replaced by ( now behaves as in LinearAlgebra).
  • Double contraction: has been replaced by ⊡₂ (which can be typed by \boxdot<tab>\_2<tab>).
  • Broadcasting: Scalar-like behavior has been removed. Broadcasting now behaves the same as with other AbstractArrays.
  • mean: The specialized mean definition in Statistics has been removed.

Quick start

  1. julia> using Tensorial
  2. julia> x = Vec{3}(rand(3)); # constructor similar to SArray.jl
  3. julia> A = @Mat rand(3,3); # @Vec, @Mat and @Tensor, analogous to @SVector, @SMatrix and @SArray
  4. julia> A x A * x # single contraction (⊡)
  5. true
  6. julia> A ⊡₂ A A A # double contraction (⊡₂)
  7. true
  8. julia> x x x * x' # tensor product (⊗)
  9. true
  10. julia> (@einsum x[i] * A[j,i] * x[j]) ≈ x ⋅ (A' * x) # Einstein summation (@einsum)
  11. true
  12. julia> S = rand(Tensor{Tuple{@Symmetry{3,3}}}); # specify symmetry S₍ᵢⱼ₎
  13. julia> SS = rand(Tensor{Tuple{@Symmetry{3,3}, @Symmetry{3,3}}}); # SS₍ᵢⱼ₎₍ₖₗ₎
  14. julia> inv(SS) ⊡₂ S @einsum inv(SS)[i,j,k,l] * S[k,l] # it just works
  15. true
  16. julia> δ = one(Mat{3,3}) # identity tensor
  17. 3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
  18. 1.0 0.0 0.0
  19. 0.0 1.0 0.0
  20. 0.0 0.0 1.0
  21. julia> gradient(identity, S) one(SS) # ∂Sᵢⱼ/∂Sₖₗ = (δᵢₖδⱼₗ + δᵢₗδⱼₖ) / 2
  22. 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:

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