← All notes

Quaternions

Quaternions

Definition

q=q0+q1i+q2j+q3k,i2=j2=k2=ijk=−1q = q_0 + q_1 i + q_2 j + q_3 k, \qquad i^2 = j^2 = k^2 = ijk = -1
From that rule: ij=k, jk=i, ki=jij = k,\ jk = i,\ ki = j but ji=−k, kj=−i, ik=−jji = -k,\ kj=-i,\ ik=-j — multiplication is not commutative.

Scalar–vector form (used in this project, scalar first): q=(s, v)=[q0,q1,q2,q3]q = (s,\ \mathbf v) = [q_0, q_1, q_2, q_3].

Operations

Operation Formula Code
Product (s1,v1)⊗(s2,v2)=(s1s2−v1 ⁣⋅ ⁣v2,  s1v2+s2v1+v1×v2)(s_1,\mathbf v_1)\otimes(s_2,\mathbf v_2) = (s_1 s_2 - \mathbf v_1\!\cdot\!\mathbf v_2,\ \ s_1\mathbf v_2 + s_2\mathbf v_1 + \mathbf v_1\times\mathbf v_2) Quaternion.__mul__, qmul
Conjugate q∗=(s,−v)q^* = (s, -\mathbf v) qconj
Norm ∥q∥=s2+∥v∥2\lVert q\rVert = \sqrt{s^2 + \lVert\mathbf v\rVert^2} np.linalg.norm
Inverse q−1=q∗/∥q∥2q^{-1} = q^*/\lVert q\rVert^2 ( =q∗= q^* for unit qq ) Quaternion.inv
Identity (1,0)(1, \mathbf 0) Quaternion()

The cross product term v1×v2\mathbf v_1\times\mathbf v_2 is what makes ⊗\otimes non-commutative — just like rotations.

Unit quaternions are rotations

Rotation by angle θ about unit axis u:

q=(cos⁡θ2, usin⁡θ2)q = \left(\cos\tfrac\theta2,\ \mathbf u\sin\tfrac\theta2\right)
Rotate a vector v by embedding it as a pure quaternion (0,v)(0,\mathbf v):
(0, v′)=q⊗(0, v)⊗q−1(0,\ \mathbf v') = q \otimes (0,\ \mathbf v) \otimes q^{-1}
The inverse rotation (world → body in this project) is q−1⊗(0,v)⊗qq^{-1}\otimes(0,\mathbf v)\otimes q — exactly the accelerometer model in gen_z_set / qrotate_inv.

Why half the angle?

The sandwich q v q−1q\,v\,q^{-1} applies qq twice (once from each side), so each side carries θ/2.

Double cover

qq and −q-q give the same rotation (the signs cancel in the sandwich). The unit quaternions (the 3-sphere S3S^3) cover the rotations SO(3)SO(3) twice. Consequence: when you compare or average quaternions, pick the sign with q0≥0q_0 \ge 0 first, otherwise two identical rotations look 360° apart. → [[Code Review - Bugs and Fixes#4. axis_angle() — no shortest path, no clipping|bug #4]]

Composition and frames

In this project qq rotates body → world.

  • A body-frame increment (e.g. gyro rotation ωΔt\omega\Delta t) multiplies on the right: qk+1=qk⊗exp⁡(ωΔt)q_{k+1} = q_k \otimes \exp(\omega\Delta t).
  • A world-frame increment multiplies on the left: q′=exp⁡(δ)⊗qq' = \exp(\delta) \otimes q.

Mixing the two by accident is exactly bug #1.

Kinematics

For body angular velocity ω\boldsymbol\omega:

q˙=12 q⊗(0, ω)\dot q = \tfrac12\, q \otimes (0,\ \boldsymbol\omega)
With ω constant over Δt the exact solution is q(t+Δt)=q(t)⊗exp⁡(ωΔt)q(t+\Delta t) = q(t)\otimes\exp(\boldsymbol\omega\Delta t), using the exponential map. That's the UKF process model.

Quaternion → rotation matrix

For unit q=(w,x,y,z)q = (w, x, y, z):

R(q)=[1−2(y2+z2)2(xy−wz)2(xz+wy)2(xy+wz)1−2(x2+z2)2(yz−wx)2(xz−wy)2(yz+wx)1−2(x2+y2)]R(q) = \begin{bmatrix} 1-2(y^2+z^2) & 2(xy - wz) & 2(xz + wy)\\ 2(xy + wz) & 1-2(x^2+z^2) & 2(yz - wx)\\ 2(xz - wy) & 2(yz + wx) & 1-2(x^2+y^2) \end{bmatrix}
Composition matches: R(q1⊗q2)=R(q1) R(q2)R(q_1\otimes q_2) = R(q_1)\,R(q_2).

Why quaternions for a filter?

Euler angles Rotation matrix Quaternion
Numbers 3 9 4
Constraints none 6 (orthonormal, det 1) 1 (unit norm)
Singularities gimbal lock none none
Composition messy trig matrix product quaternion product
Renormalising — SVD / Gram-Schmidt divide by norm

The catch: a quaternion has 4 numbers but only 3 DOF, so you can't put it straight into a Gaussian. The UKF keeps a 3-D error covariance and moves between spaces with exp/log → Quaternion UKF (Kraft).

Self-check

  • What quaternion rotates 180° about x? (0, 1, 0, 0)
  • Is q1⊗q2=q2⊗q1q_1\otimes q_2 = q_2\otimes q_1? (only if the vector parts are parallel)
  • Why must you flip qq to q0≥0q_0\ge0 before taking a log? (double cover → shortest rotation)

Next: Rotation Representations · Exponential and Log Maps

Connections