Quaternion
Tensorial.Quaternion
— TypeQuaternion
represents $q_w + q_x \bm{i} + q_y \bm{j} + q_z \bm{k}$. The salar part and vector part can be accessed by q.scalar
and q.vector
, respectively.
Examples
julia> Quaternion(1,2,3,4)
1 + 2𝙞 + 3𝙟 + 4𝙠
julia> Quaternion(1)
1 + 0𝙞 + 0𝙟 + 0𝙠
julia> Quaternion(Vec(1,2,3))
0 + 1𝙞 + 2𝙟 + 3𝙠
See also quaternion
.
Quaternion
is experimental and could change or disappear in future versions of Tensorial.
Base.exp
— Methodexp(::Quaternion)
Compute the exponential of quaternion as
\[\exp(q) = e^{q_w} \left( \cos\| \bm{v} \| + \frac{\bm{v}}{\| \bm{v} \|} \sin\| \bm{v} \| \right)\]
Base.log
— Methodlog(::Quaternion)
Compute the logarithm of quaternion as
\[\ln(q) = \ln\| q \| + \frac{\bm{v}}{\| \bm{v} \|} \arccos\frac{q_w}{\| q \|}\]
Tensorial.quaternion
— Methodquaternion(θ, n::Vec; normalize = true)
Construct Quaternion
from angle θ
and axis n
as
\[q = \cos\frac{\theta}{2} + \bm{n} \sin\frac{\theta}{2}\]
The constructed quaternion is normalized such as norm(q) ≈ 1
by default.
Examples
julia> q = quaternion(π/4, Vec(0,0,1))
0.9238795325112867 + 0.0𝙞 + 0.0𝙟 + 0.3826834323650898𝙠
julia> x = rand(Vec{3})
3-element Vec{3, Float64}:
0.32597672886359486
0.5490511363155669
0.21858665481883066
julia> (q * x / q).vector ≈ rotmatz(π/4) * x
true
Tensorial.rotate
— Methodrotate(x::Vec, q::Quaternion)
Rotate x
by quaternion q
.
Examples
julia> v = Vec(1.0, 0.0, 0.0)
3-element Vec{3, Float64}:
1.0
0.0
0.0
julia> rotate(v, quaternion(π/4, Vec(0,0,1)))
3-element Vec{3, Float64}:
0.7071067811865475
0.7071067811865476
0.0
Tensorial.rotmat
— Methodrotmat(::Quaternion)
Construct rotation matrix from quaternion.
Examples
julia> q = quaternion(π/4, Vec(0,0,1))
0.9238795325112867 + 0.0𝙞 + 0.0𝙟 + 0.3826834323650898𝙠
julia> rotmat(q)
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