Quaternion

Tensorial.QuaternionType

Quaternion 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.

Note

Quaternion is experimental and could change or disappear in future versions of Tensorial.

source
Base.expMethod
exp(::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)\]

source
Base.logMethod
log(::Quaternion)

Compute the logarithm of quaternion as

\[\ln(q) = \ln\| q \| + \frac{\bm{v}}{\| \bm{v} \|} \arccos\frac{q_w}{\| q \|}\]

source
Tensorial.quaternionMethod
quaternion(θ, 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
source
Tensorial.rotateMethod
rotate(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
source
Tensorial.rotmatMethod
rotmat(::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
source