The cross product term v1×v2 is what makes ⊗ non-commutative — just like rotations.
Unit quaternions are rotations
Rotation by angle θ about unit axis u:
q=(cos2θ,usin2θ)
Rotate a vector v by embedding it as a pure quaternion (0,v):
(0,v′)=q⊗(0,v)⊗q−1
The inverse rotation (world → body in this project) is q−1⊗(0,v)⊗q — exactly the accelerometer model in gen_z_set / qrotate_inv.
Why half the angle?
The sandwich qvq−1 applies q twice (once from each side), so each side carries θ/2.
Double cover
q and −q give the same rotation (the signs cancel in the sandwich). The unit quaternions (the 3-sphere S3) cover the rotations SO(3) twice. Consequence: when you compare or average quaternions, pick the sign with q0≥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 q rotates body → world.
A body-frame increment (e.g. gyro rotation ωΔt) multiplies on the right: qk+1=qk⊗exp(ωΔt).
A world-frame increment multiplies on the left: q′=exp(δ)⊗q.
Mixing the two by accident is exactly bug #1.
Kinematics
For body angular velocity ω:
q˙=21q⊗(0,ω)
With ω constant over Δt the exact solution is q(t+Δt)=q(t)⊗exp(ωΔt), using the exponential map. That's the UKF process model.
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⊗q1? (only if the vector parts are parallel)
Why must you flip q to q0≥0 before taking a log? (double cover → shortest rotation)