Tensor Operations
LinearAlgebra.cross
LinearAlgebra.dot
LinearAlgebra.norm
LinearAlgebra.tr
Tensorial.contract
Tensorial.minorsymmetric
Tensorial.otimes
Tensorial.rotate
Tensorial.rotmat
Tensorial.rotmat
Tensorial.rotmat
Tensorial.rotmat
Tensorial.rotmatx
Tensorial.rotmaty
Tensorial.rotmatz
Tensorial.skew
Tensorial.skew
Tensorial.symmetric
LinearAlgebra.cross
— Methodcross(x::Vec{3}, y::Vec{3}) -> Vec{3}
cross(x::Vec{2}, y::Vec{2}) -> Vec{3}
cross(x::Vec{1}, y::Vec{1}) -> Vec{3}
x × y
Compute the cross product between two vectors. The vectors are expanded to 3D frist for dimensions 1 and 2. The infix operator ×
(written \times
) can also be used. x × y
(where ×
can be typed by \times<tab>
) is a synonym for cross(x, y)
.
Examples
julia> x = rand(Vec{3})
3-element Vec{3, Float64}:
0.32597672886359486
0.5490511363155669
0.21858665481883066
julia> y = rand(Vec{3})
3-element Vec{3, Float64}:
0.8942454282009883
0.35311164439921205
0.39425536741585077
julia> x × y
3-element Vec{3, Float64}:
0.13928086435138393
0.0669520417303531
-0.37588028973385323
LinearAlgebra.dot
— Methoddot(x::AbstractTensor, y::AbstractTensor)
x ⋅ y
Compute dot product such as $a = x_i y_i$. This is equivalent to contract(::AbstractTensor, ::AbstractTensor, Val(1))
. x ⋅ y
(where ⋅
can be typed by \cdot<tab>
) is a synonym for dot(x, y)
.
Examples
julia> x = rand(Vec{3})
3-element Vec{3, Float64}:
0.32597672886359486
0.5490511363155669
0.21858665481883066
julia> y = rand(Vec{3})
3-element Vec{3, Float64}:
0.8942454282009883
0.35311164439921205
0.39425536741585077
julia> a = x ⋅ y
0.5715585109976284
LinearAlgebra.norm
— Methodnorm(::AbstractTensor)
Compute norm of a tensor.
Examples
julia> x = rand(Mat{3, 3})
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
0.325977 0.894245 0.953125
0.549051 0.353112 0.795547
0.218587 0.394255 0.49425
julia> norm(x)
1.8223398556552728
LinearAlgebra.tr
— Methodtr(::AbstractSecondOrderTensor)
tr(::AbstractSymmetricSecondOrderTensor)
Compute the trace of a square tensor.
Examples
julia> x = rand(Mat{3,3})
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
0.325977 0.894245 0.953125
0.549051 0.353112 0.795547
0.218587 0.394255 0.49425
julia> tr(x)
1.1733382401532275
Tensorial.contract
— Methodcontract(::AbstractTensor, ::AbstractTensor, ::Val{N})
Conduct contraction of N
inner indices. For example, N=2
contraction for third-order tensors $A_{ij} = B_{ikl} C_{klj}$ can be computed as follows:
julia> B = rand(Tensor{Tuple{3,3,3}});
julia> C = rand(Tensor{Tuple{3,3,3}});
julia> A = contract(B, C, Val(2))
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
3.70978 2.47156 3.91807
2.90966 2.30881 3.25965
1.78391 1.38714 2.2079
Following symbols are also available for specific contractions:
x ⊗ y
(where⊗
can be typed by\otimes<tab>
):contract(x, y, Val(0))
x ⋅ y
(where⋅
can be typed by\cdot<tab>
):contract(x, y, Val(1))
x ⊡ y
(where⊡
can be typed by\boxdot<tab>
):contract(x, y, Val(2))
Tensorial.minorsymmetric
— Methodminorsymmetric(::AbstractFourthOrderTensor) -> SymmetricFourthOrderTensor
Compute the minor symmetric part of a fourth order tensor.
Examples
julia> x = rand(Tensor{Tuple{3,3,3,3}});
julia> minorsymmetric(x) ≈ @einsum (i,j,k,l) -> (x[i,j,k,l] + x[j,i,k,l] + x[i,j,l,k] + x[j,i,l,k]) / 4
true
Tensorial.otimes
— Methodotimes(x::AbstractTensor, y::AbstractTensor)
x ⊗ y
Compute tensor product such as $A_{ij} = x_i y_j$. x ⊗ y
(where ⊗
can be typed by \otimes<tab>
) is a synonym for otimes(x, y)
.
Examples
julia> x = rand(Vec{3})
3-element Vec{3, Float64}:
0.32597672886359486
0.5490511363155669
0.21858665481883066
julia> y = rand(Vec{3})
3-element Vec{3, Float64}:
0.8942454282009883
0.35311164439921205
0.39425536741585077
julia> A = x ⊗ y
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
0.291503 0.115106 0.128518
0.490986 0.193876 0.216466
0.19547 0.0771855 0.086179
Tensorial.rotate
— Methodrotate(x::Vec, R::SecondOrderTensor)
rotate(x::SecondOrderTensor, R::SecondOrderTensor)
rotate(x::SymmetricSecondOrderTensor, R::SecondOrderTensor)
Rotate x
by rotation matrix R
. This function can hold the symmetry of SymmetricSecondOrderTensor
.
Examples
julia> A = rand(SymmetricSecondOrderTensor{3})
3×3 SymmetricSecondOrderTensor{3, Float64, 6}:
0.325977 0.549051 0.218587
0.549051 0.894245 0.353112
0.218587 0.353112 0.394255
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.0
julia> rotate(A, R)
3×3 SymmetricSecondOrderTensor{3, Float64, 6}:
0.0610599 -0.284134 -0.0951235
-0.284134 1.15916 0.404252
-0.0951235 0.404252 0.394255
julia> R ⋅ A ⋅ R'
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
0.0610599 -0.284134 -0.0951235
-0.284134 1.15916 0.404252
-0.0951235 0.404252 0.394255
Tensorial.rotmat
— Methodrotmat(θ, n::Vec)
Construct rotation matrix from angle θ
and axis n
.
Examples
julia> x = Vec(1.0, 0.0, 0.0)
3-element Vec{3, Float64}:
1.0
0.0
0.0
julia> n = Vec(0.0, 0.0, 1.0)
3-element Vec{3, Float64}:
0.0
0.0
1.0
julia> rotmat(π/2, n) ⋅ x
3-element Vec{3, Float64}:
6.123233995736766e-17
1.0
0.0
Tensorial.rotmat
— Methodrotmat(θ::Number)
Construct 2D rotation matrix.
\[\bm{R} = \begin{bmatrix} \cos{\theta} & -\sin{\theta} \\ \sin{\theta} & \cos{\theta} \end{bmatrix}\]
Examples
julia> rotmat(deg2rad(30))
2×2 Tensor{Tuple{2, 2}, Float64, 2, 4}:
0.866025 -0.5
0.5 0.866025
Tensorial.rotmat
— Methodrotmat(θ::Vec{3}; sequence::Symbol)
Convert Euler angles to rotation matrix. Use 3 characters belonging to the set (X, Y, Z) for intrinsic rotations, or (x, y, z) for extrinsic rotations.
Examples
julia> α, β, γ = map(deg2rad, rand(3));
julia> rotmat(Vec(α,β,γ), sequence = :XYZ) ≈ rotmatx(α) ⋅ rotmaty(β) ⋅ rotmatz(γ)
true
julia> rotmat(Vec(α,β,γ), sequence = :xyz) ≈ rotmatz(γ) ⋅ rotmaty(β) ⋅ rotmatx(α)
true
julia> rotmat(Vec(α,β,γ), sequence = :XYZ) ≈ rotmat(Vec(γ,β,α), sequence = :zyx)
true
Tensorial.rotmat
— Methodrotmat(a => b)
Construct rotation matrix rotating vector a
to b
. The norms of two vectors must be the same.
Examples
julia> a = normalize(rand(Vec{3}))
3-element Vec{3, Float64}:
0.4829957515506539
0.8135223859352438
0.3238771859304809
julia> b = normalize(rand(Vec{3}))
3-element Vec{3, Float64}:
0.8605677447967596
0.3398133016944055
0.3794075336718636
julia> R = rotmat(a => b)
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
-0.00540771 0.853773 0.520617
0.853773 -0.267108 0.446905
0.520617 0.446905 -0.727485
julia> R ⋅ a ≈ b
true
Tensorial.rotmatx
— Methodrotmatx(θ::Number)
Construct rotation matrix around x
axis.
\[\bm{R}_x = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos{\theta} & -\sin{\theta} \\ 0 & \sin{\theta} & \cos{\theta} \end{bmatrix}\]
Tensorial.rotmaty
— Methodrotmaty(θ::Number)
Construct rotation matrix around y
axis.
\[\bm{R}_y = \begin{bmatrix} \cos{\theta} & 0 & \sin{\theta} \\ 0 & 1 & 0 \\ -\sin{\theta} & 0 & \cos{\theta} \end{bmatrix}\]
Tensorial.rotmatz
— Methodrotmatz(θ::Number)
Construct rotation matrix around z
axis.
\[\bm{R}_z = \begin{bmatrix} \cos{\theta} & -\sin{\theta} & 0 \\ \sin{\theta} & \cos{\theta} & 0 \\ 0 & 0 & 1 \end{bmatrix}\]
Tensorial.skew
— Methodskew(::AbstractSecondOrderTensor)
skew(::AbstractSymmetricSecondOrderTensor)
Compute skew-symmetric (anti-symmetric) part of a second order tensor.
Tensorial.skew
— Methodskew(ω::Vec{3})
Construct a skew-symmetric (anti-symmetric) tensor W
from a vector ω
as
\[\bm{\omega} = \begin{Bmatrix} \omega_1 \\ \omega_2 \\ \omega_3 \end{Bmatrix}, \quad \bm{W} = \begin{bmatrix} 0 & -\omega_3 & \omega_2 \\ \omega_3 & 0 & -\omega_1 \\ -\omega_2 & \omega_1 & 0 \end{bmatrix}\]
Examples
julia> skew(Vec(1,2,3))
3×3 Tensor{Tuple{3, 3}, Int64, 2, 9}:
0 -3 2
3 0 -1
-2 1 0
Tensorial.symmetric
— Methodsymmetric(::AbstractSecondOrderTensor)
symmetric(::AbstractSecondOrderTensor, uplo)
Compute the symmetric part of a second order tensor.
Examples
julia> x = rand(Mat{3,3})
3×3 Tensor{Tuple{3, 3}, Float64, 2, 9}:
0.325977 0.894245 0.953125
0.549051 0.353112 0.795547
0.218587 0.394255 0.49425
julia> symmetric(x)
3×3 SymmetricSecondOrderTensor{3, Float64, 6}:
0.325977 0.721648 0.585856
0.721648 0.353112 0.594901
0.585856 0.594901 0.49425
julia> symmetric(x, :U)
3×3 SymmetricSecondOrderTensor{3, Float64, 6}:
0.325977 0.894245 0.953125
0.894245 0.353112 0.795547
0.953125 0.795547 0.49425