Rodrigues Rotation Formula
Convert axis-angle representation to a rotation matrix using Rodrigues' formula.
Axis-angle is a compact representation of 3D rotation: a unit axis k and angle θ. Rodrigues' formula converts this to a 3×3 rotation matrix:
R=I+sinθ⋅[k]×+(1−cosθ)⋅[k]×2
where [k]× is the skew-symmetric matrix of axis k=(kx,ky,kz):
[k]×=0kz−ky−kz0kxky−kx0
This formula is used extensively in optimization because axis-angle has only 3 parameters (vs. 9 for a rotation matrix) and avoids singularities.
Example:
rodrigues([0, 0, 1], 1.5708)
[[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]]
Rotating 90° (π/2) around Z-axis:
- Build skew-symmetric K for axis [0,0,1]: K = [[0, -1, 0], [1, 0, 0], [0, 0, 0]]
- Compute K²: K² = [[-1, 0, 0], [0, -1, 0], [0, 0, 0]]
- With θ = π/2: sin(θ) ≈ 1, cos(θ) ≈ 0 R = I + 1·K + 1·K² R = [[1,0,0],[0,1,0],[0,0,1]] + K + K² R ≈ [[0,-1,0],[1,0,0],[0,0,1]] This rotates the X-axis to Y-axis (counterclockwise 90°).
Constraints:
- axis: unit vector [kx, ky, kz] with ||k|| = 1
- angle: rotation angle in radians
- Return 3x3 rotation matrix as nested list
More from CV: Structure from Motion and SLAM
Axis–angle is a minimal 3-parameter representation of 3D rotations: a unit vector (rotation axis) and a scalar angle, while a rotation matrix is a 3×3 orthonormal matrix in SO(3). Rodrigues’ formula gives a direct, smooth mapping from axis–angle to a rotation matrix using sines and cosines of the rotation angle and a skew-symmetric matrix built from the axis. This is heavily used in SfM/SLAM because it is both numerically stable and easy to differentiate for optimization.
In practice, you often store a rotation as a 3D vector \mathbf{r}∈R3, where its direction is the rotation axis and its magnitude is the rotation angle: \theta=∥\mathbf{r}∥, \mathbf{k}=\mathbf{r}/\theta. Rodrigues’ formula then produces a 3×3 matrix R that you can use to rotate 3D points, compose poses, and compute Jacobians with respect to the three parameters.
1. Background Knowledge (concepts/theory)
-
SO(3) and rotation matrices
-
3D rotations form a Lie group called SO(3): the set of 3×3 real matrices R such that R⊤R=I and det(R)=1.
-
Rotation matrices are redundant (9 entries with 6 constraints) and can be awkward in optimization.
-
Axis–angle and the Lie algebra so(3)
-
Any 3D rotation can be represented by a unit axis k and an angle θ.
-
The Lie algebra so(3) is the space of skew-symmetric 3×3 matrices; each such matrix corresponds to a 3D vector via the hat operator:
-
The exponential map exp:so(3)→\text{SO}(3) is what turns an axis–angle (a vector in R3) into a rotation matrix.
-
Rodrigues as the matrix exponential
-
Rodrigues’ formula is exactly the closed-form expression of exp(\theta[\mathbf{k}]×):
- This is preferred in SfM/SLAM because:
- It uses 3 parameters.
- It is smooth and avoids gimbal lock (unlike Euler angles).
- It has nice derivatives for gradient-based optimization.
2. Algorithm / Approach
The pattern for this type of problem:
- Interpret the input:
- Decide whether the input is:
- A unit axis k and angle θ, or
- A 3D vector r where r=\thetak.
- Normalize and extract axis/angle:
- Compute θ=∥\mathbf{r}∥ and k=\mathbf{r}/θ if needed.
- Handle the small-angle case separately.
- Build the skew-symmetric matrix:
- Construct [\mathbf{k}]× from kx,ky,kz.
- Apply Rodrigues’ formula:
- Use the given formula with sinθ, cosθ, and [\mathbf{k}]×2.
- Return the 3×3 rotation matrix.
This is a direct formula implementation problem with attention to numerical stability.
3. Step-by-Step Strategy
Assume your function receives a 3D vector r (axis–angle):
- Compute angle and early exit for no rotation
theta = np.linalg.norm(r)
if theta < eps:
return np.eye(3) # approximately zero rotation
- Compute unit axis
k = r / theta # shape (3,)
kx, ky, kz = k
- Construct skew-symmetric matrix [k]_x
K = np.array([[ 0, -kz, ky],
[ kz, 0, -kx],
[ -ky, kx, 0]])
- Precompute trig functions
c = np.cos(theta)
s = np.sin(theta)
- Compute K^2
K2 = K @ K
- Assemble rotation matrix using Rodrigues
I = np.eye(3)
R = I + s * K + (1 - c) * K2
return R
- Optional small-angle refinement
- For very small θ, using the above may be fine, but you can improve stability by using the series:
- sin\theta≈θ
- 1−cos\theta \approx \tfrac{\theta^2}{2}
- Or directly approximate:
when ∥\mathbf{r}∥ is tiny.
4. Common Pitfalls
-
Not handling the zero / tiny angle case
-
When θ≈0, dividing by θ to get k is unstable.
-
Always add a small ε check to:
-
Return I, or
-
Use the series expansions.
-
Axis not normalized
-
If the problem gives you k and θ separately, ensure k is unit length before using Rodrigues; otherwise the result is not a valid rotation.
-
Skew-symmetric matrix sign mistakes
-
Carefully follow the definition:
-
A swapped sign will flip the rotation direction.
-
Floating-point drift
-
Due to numerical error, R⊤R might not be exactly I.
-
Usually acceptable, but if needed, you can re-orthogonalize R in downstream code.
-
Confusing degrees and radians
-
Most math libraries use radians; convert from degrees if needed.
5. Time & Space Complexity
-
Time complexity:
-
All operations are on fixed-size 3×3 matrices and 3D vectors: constant number of multiplications, additions, and trig calls.
-
So the time complexity is O(1).
-
Space complexity:
-
You store a few 3D vectors and 3×3 matrices, independent of input size.
-
So the space complexity is also O(1).
This problem is mainly about correctly implementing a small, constant-time linear algebra formula with good numerical hygiene.