Quaternion

Quaternions are mainly useful for 3D rotations. Tensorial provides a small Quaternion type, angle-axis constructors, conversion to rotation matrices, and vector rotation.

Note

Quaternion support is experimental and may change in a future version.

Construction and access

A quaternion stores a scalar part and a 3D vector part:

julia> q = Quaternion(1.0, 2.0, 3.0, 4.0)1.0 + 2.0𝙞 + 3.0𝙟 + 4.0𝙠
julia> q.scalar1.0
julia> q.vector3-element Vec{3, Float64}: 2.0 3.0 4.0
julia> Tuple(q)(1.0, 2.0, 3.0, 4.0)

You can also construct a pure-vector quaternion from a Vec:

julia> Quaternion(Vec(1.0, 2.0, 3.0))0.0 + 1.0𝙞 + 2.0𝙟 + 3.0𝙠

Angle-axis rotations

Use quaternion to construct a unit quaternion from an angle and a unit axis:

julia> axis = Vec(0.0, 0.0, 1.0)3-element Vec{3, Float64}:
 0.0
 0.0
 1.0
julia> q = quaternion(π/4, axis)0.9238795325112867 + 0.0𝙞 + 0.0𝙟 + 0.3826834323650898𝙠
julia> θ, n = angleaxis(q)(0.7853981633974484, [0.0, 0.0, 1.0])
julia> θ ≈ π/4true
julia> n ≈ axistrue

The quaternion and the corresponding rotation matrix represent the same rotation:

julia> v = @Vec [1.0, 0.0, 0.0]3-element Vec{3, Float64}:
 1.0
 0.0
 0.0
julia> rotate(v, q) ≈ rotmat(q) * vtrue

For quaternion docstrings, see Rotations and quaternions.