Constructing tensors

From an AbstractArray

julia> Vec{2}([1,2])2-element Vec{2, Int64}:
 1
 2
julia> Vec{2,Float64}([1,2])2-element Vec{2, Float64}: 1.0 2.0
julia> Mat{2,2}([1 2; 3 4])2×2 Tensor{Tuple{2, 2}, Int64, 2, 4}: 1 2 3 4
julia> Mat{2,2,Float64}([1 2; 3 4])2×2 Tensor{Tuple{2, 2}, Float64, 2, 4}: 1.0 2.0 3.0 4.0
julia> SymmetricSecondOrderTensor{2}([1 2; 3 4]) # InexactErrorERROR: InexactError: convert(SymmetricSecondOrderTensor{2}, [1 2; 3 4])
julia> SymmetricSecondOrderTensor{2}([1 2; 2 4])2×2 SymmetricSecondOrderTensor{2, Int64, 3}: 1 2 2 4

From a function

julia> δ = one(Mat{2,2})2×2 Tensor{Tuple{2, 2}, Float64, 2, 4}:
 1.0  0.0
 0.0  1.0
julia> I = SymmetricFourthOrderTensor{2}((i,j,k,l) -> (δ[i,k]*δ[j,l] + δ[i,l]*δ[j,k])/2)2×2×2×2 SymmetricFourthOrderTensor{2, Float64, 9}: [:, :, 1, 1] = 1.0 0.0 0.0 0.0 [:, :, 2, 1] = 0.0 0.5 0.5 0.0 [:, :, 1, 2] = 0.0 0.5 0.5 0.0 [:, :, 2, 2] = 0.0 0.0 0.0 1.0
julia> I == one(SymmetricFourthOrderTensor{2})true

Identity tensors

Base.oneFunction
one(TensorType)

Return the identity tensor associated with the tensor space represented by TensorType.

The identity tensor is defined as the linear operator that leaves a tensor unchanged under contraction. It exists only for even-order tensors whose first and second halves span the same space. For symmetric spaces, component multiplicities are accounted for accordingly.

Examples

julia> I = one(Tensor{Tuple{@Symmetry{2, 2}, @Symmetry{2, 2}}});

julia> A = rand(Tensor{Tuple{@Symmetry{2, 2}}})
2×2 SymmetricSecondOrderTensor{2, Float64, 3}:
 0.325977  0.549051
 0.549051  0.218587

julia> I ⊡₂ A
2×2 SymmetricSecondOrderTensor{2, Float64, 3}:
 0.325977  0.549051
 0.549051  0.218587
source

Zero tensors

Base.zeroFunction
zero(TensorType)

Construct a zero tensor.

julia> zero(Vec{2})
2-element Vec{2, Float64}:
 0.0
 0.0

julia> zero(Mat{2,3})
2×3 Mat{2, 3, Float64, 6}:
 0.0  0.0  0.0
 0.0  0.0  0.0
source

Macros

Tensorial.@VecMacro
@Vec [a, b, c, d]
@Vec [i for i in 1:2]
@Vec ones(2)

A convenient macro to construct Vec.

source
Tensorial.@MatMacro
@Mat [a b c d]
@Mat [[a, b];[c, d]]
@Mat [i+j for i in 1:2, j in 1:2]
@Mat ones(2, 2)

A convenient macro to construct Mat.

source
Tensorial.@TensorMacro
@Tensor [a b; c d]
@Tensor [[a, b];[c, d]]
@Tensor [i+j for i in 1:2, j in 1:2]
@Tensor ones(2, 2, 2)

A convenient macro to construct Tensor with arbitrary dimension.

source

Other special tensors

Levi-Civita

Tensorial.levicivitaFunction
levicivita(::Val{N} = Val(3))

Return N dimensional Levi-Civita tensor.

Examples

julia> ϵ = levicivita()
3×3×3 Tensor{Tuple{3, 3, 3}, Int64, 3, 27}:
[:, :, 1] =
 0   0  0
 0   0  1
 0  -1  0

[:, :, 2] =
 0  0  -1
 0  0   0
 1  0   0

[:, :, 3] =
  0  1  0
 -1  0  0
  0  0  0
source