Quaternion
Quaternions are mainly useful for 3D rotations. Tensorial provides a small Quaternion type, angle-axis constructors, conversion to rotation matrices, and vector rotation.
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.0julia> q.vector3-element Vec{3, Float64}: 2.0 3.0 4.0julia> 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.0julia> 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> θ ≈ π/4truejulia> 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.0julia> rotate(v, q) ≈ rotmat(q) * vtrue
For quaternion docstrings, see Rotations and quaternions.