Rotations and Rigid Transforms

Turn a drone through a loop. At 90° its roll and yaw readings jump from 0° to 180° in one step. Over the same step, the four quaternion numbers move by about 0.01.

Drag the drone through 90° and watch the Euler readout jump. Then switch to the quaternion and do it again. Then we build, piece by piece, rotation matrices, order, gimbal lock, axis-angle, quaternions, rigid transforms and the safe way to nudge an orientation.

You need vectors and matrix times vector and sine and cosine. For the flat 2D case first, read Rigid-Body Transforms, SE(2) and SE(3). We build the 3D rest from zero.

Foundations for robotics and state estimation. We checked every matrix, quaternion and worked number on this page with NumPy and scipy.spatial.transform.

Fly a loop, read the attitude

60°

Exact maths: the drone turns about the world y axis. Euler angles use the ZYX order (yaw, then pitch, then roll); the quaternion is Hamilton, scalar first. Red marks a jump in the readout, not in the drone.

Chapter 0

Order Is Part of the Answer

Turn a phone twice, swap the order, and land in a different pose

A drone gets two corrections: pitch 90°, then yaw 90°. A refactor applies them yaw first. The two numbers are the same. The drone ends up facing a different way.

Position does not behave like this. Walk 3 m east, then 4 m north, and you stand where 4 m north, then 3 m east, puts you. Position changes add, and addition ignores order. An orientation (which way a body faces) changes by turns, and a turn acts on the result of the turn before it.

Swap the order of two turns

The warm face is the front (body x). The blue face is the top (body z). Press one order, then the other, and compare the faces.

Exact maths. Pitch is a turn about the world y axis, yaw about the world z axis, both +90°.

Work the number. Pitch then yaw sends the front to (0, 0, −1), straight down, and the top to (0, 1, 0). Yaw then pitch sends the front to (0, 1, 0) and the top to (1, 0, 0). Same two turns, two poses.

QuantityChanges combine byOrder matters?
Positionadding vectorsno
Orientationmultiplying matrices or quaternionsyes

The failure, as symptom, measurement and fix

The symptom is a vehicle that tips the wrong way after a harmless-looking refactor. To measure it, apply both orders to the vector (1, 0, 0) in a unit test and compare. The fix is one documented order, tested, in one function.

The road

  1. 3D rotation matrices: the columns are where the axes land.
  2. Order: rightmost first, and world axes against body axes.
  3. Euler angles and gimbal lock: the jump in the hero's Euler readout.
  4. Axis-angle: one axis, one angle, Rodrigues' formula.
  5. Quaternions: the hero's smooth readout, and why it uses half the angle.
  6. Slerp: move between two orientations at a steady rate.
  7. SE(3): rotate, then shift, in one 4 by 4 matrix.
  8. Exp and log: nudge a rotation without breaking it.
  9. Frame lab: a world, base, camera and IMU chain.
  10. Field guide: which representation, conventions, code.
You turn a book 90° about its vertical axis, then 90° about a horizontal axis. Then you reset and do the two turns in the other order. What happens?

Chapter 1

3D Rotation Matrices: Columns Are the New Axes

Turn a frame about one axis and read where its three arrows land

In the plane, a rotation is a 2 by 2 matrix built from where the two axis arrows land (the SE(2) lesson builds it). In 3D the same idea gives a 3 by 3 matrix. A rotation matrix R is a 3 by 3 matrix whose three columns are where the x, y and z arrows land after the turn.

The simplest turns leave one axis alone. Call the angle of a turn about z γ (gamma). The z arrow stays put; x and y turn in their plane, like the 2D case.

Say it aloud

R-z of gamma is the three by three matrix: cos gamma, minus sine gamma, zero; sine gamma, cos gamma, zero; zero, zero, one.

Rz(γ)
R-z of gamma the matrix that turns any vector by the angle gamma about the z axis, counter-clockwise when z points at you.
column 1
cos gamma, sine gamma, zero where the x arrow lands.
column 2
minus sine gamma, cos gamma, zero where the y arrow lands.
column 3
zero, zero, one where the z arrow lands: nowhere new.
In plain words

To turn a vector v by gamma about z, scale each column by the matching part of v. Column 1 takes v's x part, column 2 its y part, column 3 its z part. Add the three.

With numbers

Gamma = 30°: cos is 0.866, sin is 0.5. The vector (1, 0, 0) lands on column 1, (0.866, 0.5, 0). Its length is still 1.

Turn a frame, read its columns

Grey arrows are the world axes. Coloured arrows are the turned axes, and each one is a column of the matrix below. Pick an axis and drag the angle.

30°

Exact maths. Right-handed axes, z up.

Every rotation matrix passes two checks. Together they define the set SO(3), all 3D rotations.

CheckWhat it meansFor Rz(30°)
RTR = Icolumns have length 1 and meet at right angles; so R−1 = RT0.866 × (−0.5) + 0.5 × 0.866 = 0
det R = +1a turn, not a mirror (a mirror has −1)0.866² + 0.5² = 1

This page fixes its conventions once. Every device follows them.

ChoiceThis page
Axesright-handed, z up
Active or passiveactive: we turn the object; the axes stay
Vectorscolumns, so R acts as R v
Positive anglecounter-clockwise when the axis points at you

The failure, as symptom, measurement and fix

The symptom is a "rotation" that slowly shrinks or skews an object over many updates. Rounding has pulled the columns off length 1. To measure it, log the size of RTR − I. The fix is to re-orthonormalise R now and then, or to store a quaternion (Chapter 5).

A 3 by 3 matrix has columns of length 1 that meet at right angles, and its determinant is −1. What is it?

Chapter 2

Compose Rotations: Rightmost First, World or Body Axes

Chain two turns, then see that body axes reverse the order

Two turns in a row are one turn. Call the first turn R1 (R-one) and the second R2 (R-two). The vector v first meets R-one, so R-one sits next to it.

Say it aloud

v-prime equals R-two times R-one times v.

v′
v-prime the vector after both turns.
R2
R-two the second turn, written on the left.
R1
R-one the first turn, written next to v.
v
v the vector before any turn.
In plain words

Apply R-one to v. Then apply R-two to the result. Read the product from right to left.

With numbers

R-one is 90° about x, R-two is 90° about z, v = (1, 0, 0). R-one leaves v at (1, 0, 0); R-two sends it to (0, 1, 0). Swap them: Rz first gives (0, 1, 0), then Rx gives (0, 0, 1).

There is a second trap. A turn can be about a world axis, fixed in the room. Or it can be about a body axis, fixed to the object, so it moved with the first turn. Turns about world axes are extrinsic; turns about body axes are intrinsic.

Turn about world axes or body axes

Pick a recipe and press Play. Watch the warm front arrow. Two of the three recipes end in the same pose.

Exact maths, both turns +90°.

The rule: turning about body axes in one order equals turning about world axes in the reverse order. Body x then body y is RxRy, the same product as world y then world x. Both send the front to (0, 1, 0).

The failure, as symptom, measurement and fix

The symptom is code ported between libraries that gives a mirrored or twisted pose. One library meant world axes, the other body axes. To measure it, push (1, 0, 0) through both with angles of 90°. The fix is to name the convention in the function name, for example euler_zyx_intrinsic.

You compute R = RzRyRx and apply it as R v. Which turn touches v first?

Chapter 3

Euler Angles and Gimbal Lock

Store an orientation as three angles, then watch two of them fuse at 90°

Three numbers are easy to read: "nose up 15°, banked 10°". Call the turn about x the roll φ (phi), about y the pitch θ (theta), about z the yaw ψ (psi). Stacked in the aerospace order, they make one matrix.

Say it aloud

R equals R-z of psi, times R-y of theta, times R-x of phi.

R
R the orientation as one rotation matrix.
Rz(ψ)
R-z of psi yaw: the turn about z by psi. It acts last.
Ry(θ)
R-y of theta pitch: the turn about y by theta, from −90° to 90°.
Rx(φ)
R-x of phi roll: the turn about x by phi. It acts first.
In plain words

Roll by phi about world x, then pitch by theta about world y, then yaw by psi about world z. By Chapter 2 that equals yaw, then pitch, then roll about body axes: the "ZYX" or "3-2-1" order.

With numbers

Set theta = 90°. Roll 40° with yaw 10° gives the matrix [[0, 0.5, 0.866], [0, 0.866, −0.5], [−1, 0, 0]]. Roll 30° with yaw 0° gives the same matrix.

At pitch 90° the matrix depends only on roll minus yaw. Any pair with the same difference gives the same pose, so one of the three numbers does nothing. This is gimbal lock. In a mechanical gimbal (three nested rings), the roll ring's axis swings onto the yaw ring's axis.

Find the lock

Blue is the yaw ring, teal the pitch ring, warm the roll ring. Drag the pitch toward 90° and watch the warm axis meet the blue one.

20°
45°
10°

Exact maths. The read-out turns the body by 0.5° about its own z axis and reads the Euler angles again with the standard ZYX formulas.

The failure, as symptom, measurement and fix

Near the lock, reading angles back from a matrix divides by cos θ, which is near zero. At pitch 45°, a 0.5° body nudge moves yaw by 0.7°. At pitch 89.9°, the same nudge moves yaw by 60°. The symptom is jittery roll and yaw on a smooth flight. The fix is to keep the state as a quaternion and show Euler angles only on screens.

What happens at gimbal lock?

Chapter 4

One Axis, One Angle: Rodrigues' Formula

Describe any turn as a spin about one line, and build its matrix

Euler proved that every 3D rotation is one turn about one fixed line through the origin. So a rotation needs only a unit axis n and an angle θ. Pack them as one vector, the rotation vector θn: its direction is the axis, its length the angle.

To build the matrix we need the cross product as a matrix. The hat operator turns n = (nx, ny, nz) into a skew-symmetric matrix [n]×, so that [n]×v = n × v. Its rows are [0, −nz, ny], [nz, 0, −nx] and [−ny, nx, 0].

Say it aloud

R equals I, plus sine theta times K, plus one minus cos theta, times K squared.

R
R the rotation matrix for the turn.
I
I the identity: keep the vector as it is.
sin θ
sine theta how much of the sideways push to add.
K
K the hat of the unit axis, [n]×: K v = n × v, a push at right angles to both.
1 − cos θ
one minus cos theta how much to pull the vector in toward the axis.
K²
K squared applied to v, it removes the part along the axis and flips the rest.
In plain words

Split v into a part along the axis n and a part across it. The part along n stays. The part across turns by theta in its own plane, which is the 2D rotation again.

With numbers

Axis n = (1, 1, 1)/√3, theta = 120°, v = (0, 0, 1). The three terms give (0, 0, −0.5) + (0.5, −0.5, 0) + (0.5, 0.5, 0.5) = (1, 0, 0). A third of a turn about the cube's diagonal sends z to x.

Sweep a vector around an axis

Purple is the axis n. Pink is v = (0, 0, 1) turned by theta. Pick an axis and drag theta; the faint circle is every place v can reach.

120°

Exact maths: Rodrigues' formula, checked against Rotation.from_rotvec.

The failure, as symptom, measurement and fix

At theta = 0 every axis gives the identity, so no single axis exists. Code that divides by theta to find n returns garbage for a body at rest. To measure it, feed the zero rotation to your conversion. The fix is a small-angle branch: below about 10−8 rad, return the identity.

Turn v = (0, 1, 0) by 180° about the axis n = (1, 0, 0). Here n × v = (0, 0, 1) and n · v = 0. Where does v land?

Chapter 5

Quaternions: Four Numbers, Half the Angle

Store a turn in four numbers that never lock and combine with one product

Axis-angle has no lock, but two axis-angle turns have no simple combination rule. A matrix combines well but carries nine numbers for three degrees of freedom. A unit quaternion sits between: four numbers (w, x, y, z) with w² + x² + y² + z² = 1.

Build it from the axis n and the angle θ of Chapter 4.

Say it aloud

q equals cos of theta over two, then sine of theta over two times n.

q
q the unit quaternion, written (w, x, y, z) with the scalar first.
cos(θ/2)
cos of theta over two the scalar part w, set by half the angle.
sin(θ/2) n
sine of theta over two, times n the vector part (x, y, z): the axis, scaled.
In plain words

Halve the angle theta. Put its cosine in w. Put its sine, times the axis n, in x, y and z.

With numbers

A 60° turn about z: half is 30°. q = (0.866, 0, 0, 0.5). Check: 0.866² + 0.5² = 1.

Why half? Because q and −q give the same turn. A full 360° turn takes q to −q, and only 720° brings q home. This is the double cover. Two quaternions combine with the Hamilton product. Split each into a scalar w and a vector part v.

Say it aloud

q-one times q-two equals: w-one w-two minus v-one dot v-two; then w-one v-two plus w-two v-one plus v-one cross v-two.

q1⊗q2
q-one times q-two the combined turn: q-two acts first, then q-one, like R1R2.
w1w2 − v1·v2
w-one w-two minus v-one dot v-two the new scalar part.
w1v2 + w2v1
w-one v-two plus w-two v-one each vector part, scaled by the other's scalar.
v1×v2
v-one cross v-two the cross product: the part that makes order matter.
In plain words

Multiply the scalars and subtract the dot product of the vector parts. For the new vector part, add each vector scaled by the other's scalar, plus their cross product.

With numbers

q-one = 90° about z = (0.707, 0, 0, 0.707); q-two = 90° about x = (0.707, 0.707, 0, 0). Scalar: 0.5 − 0 = 0.5. Vector: (0.5, 0, 0) + (0, 0, 0.5) + (0, 0.5, 0) = (0.5, 0.5, 0.5). That is 120° about (1, 1, 1), and it sends x to y.

Turn twice around

Drag the angle from 0° to 720°. The frame comes home at 360°. Watch the purple w bar: it reaches −1 there and returns to +1 only at 720°.

60°

Exact maths, Hamilton quaternion, scalar first.

To turn a vector v, write it as the quaternion (0, v) and compute q ⊗ (0, v) ⊗ q*. Here the conjugate q* is (w, −x, −y, −z). The result is Rodrigues' formula in other clothes.

The failure, as symptom, measurement and fix

Libraries disagree on the layout. SciPy stores (x, y, z, w), scalar last. The JPL convention in some estimation papers also defines the product with the opposite handedness, so compositions run in reverse. The symptom is a pose that turns the wrong way after a library swap. To measure it, convert 90° about z both ways and compare. The fix is one conversion function at each boundary.

Why does a unit quaternion use the half angle θ/2?

Chapter 6

Interpolate Orientations: Slerp Against Nlerp

Blend two quaternions and compare a steady turn with one that rushes in the middle

A camera must ease from one orientation to another over two seconds. Call the start quaternion q0 (q-zero), the end q1 (q-one), and the fraction of time passed t, from 0 to 1. Unit quaternions live on a 4D sphere. The quick blend, nlerp, mixes them in a straight line and rescales to length 1. The straight line cuts across the sphere, so the turn rushes in the middle.

Slerp (spherical linear interpolation) walks the great arc at a steady rate. Call the angle between the two quaternions on the sphere Ω (omega), found from cos Ω = q0·q1.

Say it aloud

Slerp of q-zero, q-one and t equals: sine of one minus t, times omega, times q-zero, plus sine of t omega times q-one, all over sine omega.

slerp(q0, q1, t)
slerp of q-zero, q-one, t the orientation a fraction t of the way along the arc.
sin((1 − t)Ω)
sine of one minus t, omega the weight on the start; it falls as t grows.
sin(tΩ)
sine of t omega the weight on the end; it grows with t.
sin Ω
sine omega divides so the result keeps length 1. Omega is half the turn between the two orientations.
In plain words

Weight q-zero and q-one by sines of how far along the arc you are. Divide by sine omega. Equal steps in t give equal steps of turn.

With numbers

q-zero is no turn; q-one is 180° about z, so omega = 90°. At t = 0.25, slerp gives a 45° turn, a quarter of 180°. Nlerp gives 36.9°.

Race slerp against nlerp

Left: the quaternion circle, with the purple arc (slerp) and the grey chord (nlerp). Right: turn angle against t. Drag t, then widen the end turn.

0.25
180°

Exact maths. Both ends are turns about z, so every quaternion here lies in the (w, z) plane and the 4D sphere becomes this circle.

The failure, as symptom, measurement and fix

The symptom is a camera that speeds up mid-turn, or that takes the long way round. For a 180° end turn, nlerp turns at twice its end speed in the middle. To measure it, plot turn angle against t. The fix is slerp for far-apart keyframes, and a sign flip when q0·q1 < 0 so the arc is the short one. For close keyframes, nlerp is cheaper and the error is tiny.

You nlerp from no turn to 180° about z. At t = 0.25 the camera has turned 36.9°, not 45°. Why?

Chapter 7

Rigid Transforms: Rotate, Then Shift, in One 4 by 4 Matrix

Chain a world, a robot base and a camera, and carry one point through

A camera sits on a robot base, and the base sits in a room. A feature seen by the camera needs room coordinates. Each link is a rigid transform: a rotation R plus a shift t. The trick is to pad each point with a 1, (x, y, z, 1). These are homogeneous coordinates.

Say it aloud

T is the four by four block matrix: R and t on top, a row of zeros and a one below. T times p-and-one equals R p plus t, and one.

T
T the rigid transform, a 4 by 4 matrix. The set of all of them is SE(3).
R
R the 3 by 3 rotation block.
t
t the shift, a column of 3 numbers, in metres.
0T, 1
zero row, one the bottom row (0, 0, 0, 1). It keeps the padded 1 in place.
(p, 1)
p and one the point with a 1 added.
In plain words

Rotate the point p by R. Then add the shift t. The padded 1 lets one matrix product do both, so chains become products.

With numbers

R = Rz(90°), t = (1, 0, 0), p = (0.2, 0, 0.5). R p = (0, 0.2, 0.5). Add t: (1, 0.2, 0.5).

Name each transform "to from": Twb (T-w-b) is "world from base". It takes base coordinates to world coordinates. Chains cancel the inner letters: Twc = TwbTbc. The inverse flips the name: Tbw = Twb−1, with rotation RT and shift −RTt.

Carry a point through a chain

Grey is the world, the middle frame is the base, the small frame is the camera. The pink point is fixed 0.2 m along the camera's x. Turn the base and tilt the camera.

90°
0°

Illustrative rig: the base stands 1 m along world x; the camera is 0.5 m above the base origin.

Work the default by hand. Twb has Rz(90°) and t = (1, 0, 0). Tbc has no turn and t = (0, 0, 0.5). The camera sits at Rz(90°)(0, 0, 0.5) + (1, 0, 0) = (1, 0, 0.5). The point (0.2, 0, 0) in the camera lands at (1, 0.2, 0.5) in the world. The inverse brings (1, 0.2, 0.5) back to (0.2, 0, 0).

The failure, as symptom, measurement and fix

The symptom is a pose that looks mirrored or offset while every number looks plausible. Someone used Twb where Tbw belongs. To measure it, send the base origin (0, 0, 0) through Twb: you must get the base's position in the world. The fix is the "to from" naming, so that inner letters always match.

Why pad 3D points with a 1 instead of keeping R and t apart?

Chapter 8

Exp and Log: Nudge a Rotation Without Breaking It

Add a small correction the wrong way and the right way, and watch the columns

A filter or an optimiser corrects its orientation estimate R hundreds of times a second. The tempting update adds a small matrix to R. The sum is no longer a rotation. Instead, write the correction as a small rotation vector ω (omega): axis times angle, in radians. Turn it into a rotation and multiply.

Say it aloud

R box-plus omega equals R times exp of the hat of omega.

R ⊕ ω
R box-plus omega the updated orientation.
R
R the current estimate.
exp([ω]×)
exp of the hat of omega the exponential map: it turns the rotation vector omega into a rotation matrix. It is Rodrigues' formula with theta = the length of omega.
In plain words

Turn the small correction omega into a real rotation with exp. Multiply the estimate R by it. A product of two rotations is a rotation, so the result never leaves SO(3).

With numbers

R = Rz(60°), omega = (0, 0, 0.3) rad, which is 17.19°. The update gives a turn of 77.19°, columns of length 1.000, det 1.000.

Add a correction two ways

Left: the first column of R after ten updates, in the xy plane. Red adds R[ω]× each time; green multiplies by exp([ω]×). Right: that column's length. Drag the size of the correction.

0.30 rad

Exact maths. R starts at Rz(60°); the correction is about z.

The log map runs the other way: from R back to omega. The angle comes from the trace of R (the sum of its diagonal).

Say it aloud

Theta equals arc cos of, trace R minus one, over two.

θ
theta the turn angle, from 0 to 180°.
arccos
arc cos the angle whose cosine is the number inside.
tr R
trace R the sum of the three diagonal entries; for any rotation it is 1 + 2 cos theta.
In plain words

Add the diagonal of R, subtract 1, halve it, take the arc cos. That is theta. The axis is the vector inside (R − RT)/(2 sin theta), read through the hat pattern.

With numbers

R = Rz(30°): trace = 0.866 + 0.866 + 1 = 2.732. (2.732 − 1)/2 = 0.866, arc cos gives 30°. R − RT over 2 sin 30° gives the axis (0, 0, 1).

SE(3) has an exp too. A twist (ω, ρ) holds a turn ω and a drive ρ. Its rotation is exp([ω]×); its shift is t = Vρ, where V = I + ((1 − cosθ)/θ²)[ω]× + ((θ − sinθ)/θ³)[ω]ײ. Take ω = (0, 0, π/2) and ρ = (1, 0, 0). A robot drives 1 m on a quarter circle and ends at t = (0.637, 0.637, 0), not (1, 0, 0).

The failure, as symptom, measurement and fix

With a 17° correction, R + R[ω]× gives columns of length 1.044 and det 1.09. After ten updates the columns reach 1.54. The symptom is a pose that grows or shears over time. To measure it, log |det R − 1|. The fix is the ⊕ update.

Why does R exp([ω]×) always give a valid rotation, while R + δ in general does not?

Chapter 9

The Frame-Chain Lab

Pose a world, base, camera and IMU chain, and read one rotation three ways

Here is a robot's sensor chain: World, Base, Camera, IMU. Each joint has one angle. The IMU's orientation in the world is Rwi = Rz(yaw) Ry(pitch) Rx(roll). That is the ZYX Euler matrix of Chapter 3. So the Euler readout should give back the three sliders.

Pose the chain, read the IMU

Drag the camera pitch to 90° with the Euler readout on. Then switch to the quaternion and drag through 90° again.

30°
20°
−15°

Point fixed in

IMU orientation readout

FrameThe pink point in that frame (m)

Illustrative rig: base 1.4 m along world x; camera 0.7 m above the base; IMU 0.3 m ahead and 0.15 m above the camera. The point sits at (1.9, 0.3, 0.5), (0.5, 0, 0.2), (0.3, 0, 0) or (0.1, 0, 0) in the frame it is fixed in.

At the defaults the readouts agree on one rotation. Euler gives roll −15°, pitch 20°, yaw 30°. Axis-angle gives 40.8° about (−0.48, 0.38, 0.79). The quaternion is (0.937, −0.169, 0.133, 0.275). The point fixed 0.1 m ahead of the IMU sits at (1.770, 0.214, 0.704) in the world.

Push the pitch to 90°. The Euler readout can no longer split roll from yaw, so it reports yaw 0° and folds both into roll. The quaternion and the axis-angle move on smoothly. One pose, three readings; only one of them panics.

Predict before you look. With the point fixed in the IMU, nudge the base yaw. Only the World row changes: base, camera and IMU still hold the same rigid shape. Fix the point in the World instead, and the other three rows change.

Chapter 10

Field Guide

Pick a representation, fix the conventions, and keep the code

Every representation answers one question: which way does this body face? Each trades size for trouble spots.

RepresentationNumbersTrouble spotCombines byUse it for
Euler angles (ZYX)3lock at pitch ±90°convert to a matrix firstlogs, dashboards, sliders
Rotation vector (axis-angle)3no single axis at 0; n and −n at 180°convert firstsmall corrections ω in filters and optimisers
Unit quaternion4, length 1none; q and −q are the same turnone Hamilton productattitude state, gyro integration, slerp
Rotation matrix9, orthogonaldrifts off SO(3) with roundingmatrix productturning many points, proofs
SE(3) transformrotation + 3those of its rotation4 by 4 productframe chains, extrinsics, kinematics

Terms and numbers

TermOne lineNumber to remember
SO(3)all 3D rotations: RTR = I, det R = +13 degrees of freedom
Gimbal lockat pitch ±90° roll and yaw act on one axis0.5° nudge at 89.9° moves yaw 60°
RodriguesR = I + sinθ K + (1 − cosθ)K²120° about (1, 1, 1) sends z to x
Double coverq and −q are one turn720° brings q home
Slerpsteady turn along the great arcnlerp at t = 0.25 of 180°: 36.9°
Log mapθ = arccos((tr R − 1)/2)trace 2.732 means 30°

Conventions to check in any library

QuestionThis pageWatch for
Quaternion layout(w, x, y, z), scalar firstSciPy and ROS messages store (x, y, z, w)
Quaternion productHamiltonJPL papers reverse the product order
Euler orderZYX intrinsic = XYZ extrinsicSciPy: capital letters mean intrinsic
Update sideR exp([ω]×), body frameexp([ω]×) R, world frame

Checklist

The core, as code

pythonimport numpy as np
from scipy.spatial.transform import Rotation as Rot

def hat(w):                                  # 3-vector -> 3x3 skew matrix
    return np.array([[0, -w[2], w[1]], [w[2], 0, -w[0]], [-w[1], w[0], 0]])

def exp_so3(w):                              # rotation vector (3,) -> R (3,3)
    th = np.linalg.norm(w)
    if th < 1e-8: return np.eye(3) + hat(w)          # small-angle branch
    K = hat(w / th)
    return np.eye(3) + np.sin(th) * K + (1 - np.cos(th)) * K @ K

def log_so3(R):                              # R (3,3) -> rotation vector (3,), angle below 180 deg
    th = np.arccos(np.clip((np.trace(R) - 1) / 2, -1, 1))
    if th < 1e-8: return np.zeros(3)
    v = np.array([R[2,1] - R[1,2], R[0,2] - R[2,0], R[1,0] - R[0,1]])
    return th * v / (2 * np.sin(th))

def qmul(a, b):                              # Hamilton, (w, x, y, z): a after b
    w1, v1, w2, v2 = a[0], a[1:], b[0], b[1:]
    return np.r_[w1*w2 - v1 @ v2, w1*v2 + w2*v1 + np.cross(v1, v2)]

R = Rot.from_euler('ZYX', [30, 20, -15], degrees=True).as_matrix()   # yaw, pitch, roll
R = R @ exp_so3(np.radians([0, 0, 5]))           # safe update: R (+) omega
x, y, z, w = Rot.from_matrix(R).as_quat()       # SciPy order is scalar LAST

Keep going

The rule that settles most choices: keep the state as a quaternion or a matrix, update it with ⊕, chain frames with SE(3), and show Euler angles only to people.

Now press Present or Teach and explain from memory why the Euler readout jumps at 90° and the quaternion does not. Then go back to the hero, fly the loop again, and say which chapter explains each line on the plot.

Rotations and Rigid Transforms
The 2D case: SE(2) · SciPy Rotation docs · Solà, Quaternion kinematics for the error-state Kalman filter · Back to Gleams