Optimization on Manifolds

A camera fits its attitude to six direction pairs. Fit all nine matrix entries and the answer has determinant 0.91: not a rotation. Fit Euler angles from 89.9° pitch and the first step swings yaw and roll by about 3,000°. Six steps later the fit is still 42° off. Step on the manifold and the same Gauss-Newton is within 0.001° after five steps.

Pick a solver and a start, then press Run. Try the start at 89.9° with each solver. Then we build it piece by piece: the rules, the lock, the step, its Jacobian, the loop, and the covariance.

You need Lecture 2 (hat, exp and log of a rotation), Lecture 9 (Gauss-Newton and Levenberg-Marquardt), a little matrix algebra and a little Python. We build the rest from zero.

Lecture 10 of 14 in our follow-along of MIT 16.485, Visual Navigation for Autonomous Vehicles (VNAV, taught by Luca Carlone). Every idea is re-taught here in our own words and devices. Previous: Lecture 9, estimation and nonlinear least squares. Next: Lecture 11, visual and visual-inertial odometry.

Fit an attitude three ways

Illustrative toy: six fixed body directions and a true attitude of yaw 50°, pitch 45°, roll −40°. The noise, 0.05 per component (about 3°), is one seeded draw. Error is the angle to the best rotation. Exact maths in your browser; NumPy and SciPy give the same digits.

Chapter 0

A Rotation Is Nine Numbers With Six Rules

Fit all nine entries and watch the frame stretch as the noise grows

A drone's camera must know where it points. Its attitude is a rotation matrix R = RWB: it turns body numbers into world numbers. The camera has six direction pairs. Each pair is one direction written in body numbers, bi, and in world numbers, wi. The pairs carry noise of 0.05 per component, about 3°.

Lecture 9 gives a recipe. Write the residual ri = R bi − wi, add the squares, and minimise. The residual is linear in the nine entries of R, so Gauss-Newton on nine numbers lands on the minimum in one step. The answer has the lowest cost of any 3 × 3 matrix. Is it a rotation?

Fit nine numbers

The purple arrows are the three columns of the fitted matrix; the pink frame is the true attitude. A rotation's columns end on the grey sphere and meet at right angles. Raise the noise.

0.05

Illustrative toy: the hero's six pairs. The slider scales one fixed standard-normal draw.

A rotation keeps lengths and angles. That gives rules on its nine entries:

Say it aloud

R-transpose R equals I, and the determinant of R equals plus one.

R⊤R
R-transpose R every column of R dotted with every column: a 3 × 3 table of lengths and angles.
I
I the identity: ones on the diagonal (each column has length 1), zeros elsewhere (each pair meets at 90°).
det R
the determinant of R how R scales volume. Plus one rules out mirrors, which also pass the first test.
In plain words

The table R-transpose R has six different entries, because it is symmetric. Each must match I, so the nine entries obey six rules. Nine numbers minus six rules leave three free numbers: a rotation has 3 degrees of freedom.

With numbers

The nine-entry fit at noise 0.05 has columns 0.87, 0.99 and 1.06 long, meeting at 91.6°, 88.8° and 90.8°. Its determinant is 0.91, and the size of R⊤R − I is 0.28. Its cost is 0.0178, below the best rotation's 0.0325: it wins by breaking the rules.

A tiny step breaks the rules too. Add the skew matrix of (0.03, 0.05, 0.10) to I and the determinant is 1.0134. A rotation's three free numbers sit inside nine, on a curved set called a manifold: a shape that looks flat when you zoom in. This lecture optimises on it.

Symptom: a fitted attitude that stretches or shears the map it builds.

Measure: the size of R⊤R − I and det R after every update. A rotation gives about 10−15 and 1.

Fix: keep R on the manifold at every step (Chapter 2). Do not repair it afterwards.

The road

  1. Euler angles: three numbers with no rules, which lock near 90° pitch (the hero's second button).
  2. ⊞ and ⊟: step in the flat tangent space, land on the rotations.
  3. Jacobian: how a residual answers a small turn, and which side the turn goes on.
  4. Gauss-Newton: Lecture 9's loop with one line changed (the hero's third button).
  5. Averaging: the mean of several rotations.
  6. Poses: the same loop for a turn plus a shift.
  7. Covariance: how sure the attitude is, in three numbers.
  8. Showcase: every solver, your goal, your noise.
  9. Field guide: the formulas, the code and the credit.
Gauss-Newton on the nine entries of R finds a cost below every rotation. Why is that a problem?

Chapter 1

Three Angles That Lock

Pitch the camera toward 90° and watch the yaw axis and the roll axis line up

Why not use three numbers and no rules? Euler angles do that: yaw ψ ("psi"), pitch θ ("theta") and roll φ ("phi"), with R = Rz(ψ) Ry(θ) Rx(φ), as in Lecture 1. Any three values give a rotation. Gauss-Newton can step them like Lecture 9's x and y.

The trouble is the map from angle changes to turns. Call a small change of the three angles Δe ("delta e"). It turns the camera by a small body turn δ, a 3-vector in radians.

Say it aloud

Delta equals E of theta and phi, times delta e. The determinant of E equals minus cos theta.

δ
delta the small turn of the camera, about its own axes, in radians.
E(θ, φ)
E of theta and phi a 3 × 3 matrix. Its columns are the body axes that yaw, pitch and roll turn about: (−sin θ, cos θ sin φ, cos θ cos φ), (0, cos φ, −sin φ) and (1, 0, 0).
Δe
delta e the small change of the three angles (Δψ, Δθ, Δφ).
det E
the determinant of E how much body turn one unit of angle change buys. At 0, no angle change reaches some turn.
cos θ
cos theta the cosine of the pitch. It is 0 at θ = 90°.
In plain words

Each angle turns the camera about one axis: the yaw column, the pitch column, the roll column. As the pitch theta nears 90°, the yaw column becomes minus the roll column. Two of the three angles then turn about the same axis, and det E falls to 0.

With numbers

At θ = 89.9°, det E = −0.0017 and the yaw and roll axes are 0.1° apart. To turn the camera 1° about its own z axis, yaw and roll must each move 573° in opposite senses.

Pitch toward the lock

The teal frame is the camera. The two warm arrows are the axes yaw and roll turn about. The bars show how strongly the three angles reach each direction of turn. Slide the pitch to 90°.

80.0°

Exact maths, yaw 0, roll 0. The bars are the singular values of E.

Gauss-Newton on the angles uses the Jacobian J E, so it solves with (J E)⊤(J E). At 89.9° that matrix is near singular, and the hero's first Euler step changes yaw and roll by about 3,000°. At exactly 90° it is singular, and the solver cannot take a step. This is gimbal lock. It belongs to the coordinates, not to the rotations: the camera at 90° can turn any way it likes.

Quaternions have four numbers and one rule: unit length. They never lock, but plain Gauss-Newton on four numbers steps off the unit sphere, like the nine-entry fit, unless you renormalise. Also q and −q are the same rotation.

Symptom: an attitude solver that is fast in level flight and wild or stuck when the camera points straight down.

Measure: cos θ at each iterate, and the condition number of the Gauss-Newton matrix.

Fix: step a small body turn on the manifold (Chapter 2). Keep Euler angles for display only.

At pitch 90°, the Euler Gauss-Newton matrix is singular. What is lost?

Chapter 2

Step in the Tangent Space, Land on the Rotations

Take a step along the flat line and watch exp bend it back onto the circle

Keep R as nine numbers, but move it with three. At any attitude R, the tangent space is the flat space of small body turns δ, a 3-vector. It has no rules, so Gauss-Newton can solve for δ freely. Then one map brings the step back onto the rotations.

Lecture 2 built that map. The hat of δ, written δ∧, is its skew matrix. The exponential, exp, turns it into a rotation by Rodrigues' formula. The log goes back. Two operators package them:

Say it aloud

R box-plus delta equals R times exp of delta-hat.

R
R the current attitude, a rotation.
⊞
box-plus "move a rotation by a tangent step". It replaces + for a state that is not a vector.
δ
delta the step: a small body turn, 3 numbers in radians.
exp(δ∧)
exp of delta-hat the rotation by angle |δ| about the axis δ, from Lecture 2. It sits on the right of R, so δ is in body numbers.
In plain words

Turn the step delta into a rotation with exp. Multiply R by it on the right. A product of two rotations is a rotation, so R box-plus delta obeys all six rules for any delta.

With numbers

R = Rz(30°) and δ = (0.10, 0.05, −0.03). The sum R + δ∧ has determinant 0.982 and R⊤R − I of size 0.085. R ⊞ δ gives 1.000 and 2 × 10−16.

The reverse operator measures the step between two rotations:

Say it aloud

R-two box-minus R-one equals the vee of the log of R-one-transpose R-two.

R2
R-two where we want to go.
⊟
box-minus "the tangent step from one rotation to another". It replaces −.
R1
R-one where we stand.
log(·)∨
the vee of the log Lecture 2's log, read out as 3 numbers: the axis times the angle, the angle at most π.
In plain words

Undo R-one with its transpose, then apply R-two. The log of that leftover turn is the step delta with R-one box-plus delta equal to R-two.

With numbers

R1 = Rz(30°), R2 = Rz(40°): R2 ⊟ R1 = (0, 0, 0.1745), a 10° turn about z. In 3-D, (R1 ⊞ δ) ⊟ R1 returns δ = (0.10, 0.05, −0.03) to 3 × 10−17.

Box-plus on the circle

Planar rotations live on the grey circle. The warm line is the tangent space at the purple R. The red dot is R + Rδ∧; the purple ring is R ⊞ δ. Push the step past π.

40°
1.00 rad

Exact maths in SO(2), the planar case: a rotation is one angle, and δ is one number.

Absil, Mahony and Sepulchre call any such map a retraction: it returns R for a zero step and moves in the direction δ to first order. R exp(δ∧) is one. Projecting R(I + δ∧) to the nearest rotation is another; it agrees to first order.

Symptom: a heading residual that jumps from +179° to −179°, and a solver that kicks the camera round.

Measure: |R2 ⊟ R1| near π.

Fix: the chart covers one turn. Start close enough that steps stay well inside π.

R2 ⊟ R1 returns a 3-vector. What does it mean?

Chapter 3

Differentiate With Respect to the Step

Shrink the step and watch the right Jacobian's error fall twice as fast as the wrong one's

Gauss-Newton needs a Jacobian: how each residual changes per unit of the unknown. The unknown is now the step δ, so we differentiate ri(R ⊞ δ) at δ = 0. That gives a 3 × 3 block per pair, not 3 × 9.

For a small step, exp(δ∧) ≈ I + δ∧. The hat turns a product into a cross product: δ∧ b = δ × b = −b × δ = −b∧ δ. Put that into the residual:

Say it aloud

r-i at R box-plus delta is about r-i at R, minus R b-i-hat, times delta.

ri(R ⊞ δ)
r-i at R box-plus delta the residual R bi − wi after the step.
ri(R)
r-i at R the residual now, a 3-vector.
−R bi∧
minus R b-i-hat the Jacobian Ji, 3 × 3: the skew matrix of bi, then R to bring it into world numbers.
δ
delta the small body turn, in radians.
In plain words

Turning the body by delta swings the direction b-i by delta cross b-i, in body numbers. R carries that swing into the world, where the residual lives. J-i is that whole recipe as a matrix.

With numbers

At R = I with b = (1, 0, 0), J = −b∧ has rows (0, 0, 0), (0, 0, 1), (0, −1, 0). A turn δ = (0, 0, 0.1) about z moves the residual by (0, 0.1, 0): the tip of b swings toward +y.

Check an analytic Jacobian against the truth. Take a step of size s, compare the true residual with the linear prediction, and shrink s. A correct Jacobian misses by an amount that falls like s2. A wrong one misses like s.

Check the Jacobian

Both lines predict r(R ⊞ δ). Purple uses −R b∧, made for a step on the right. Red uses the left-side Jacobian with the same right-side step. Slide the step size.

0.010 rad

Exact maths: R = exp((0.3, −0.2, 0.6)∧), b = the hero's first direction, step axis (0.6, 0, 0.8).

A step can also go on the left, exp(δ∧) R, turning about world axes. Then Ji = −(R bi)∧, which equals the right-side Jacobian times R⊤: Lecture 2's adjoint. At s = 0.01 the matched pair misses by 2.7 × 10−5; the mismatched pair by 2.2 × 10−3, 80 times more. Libraries differ. GTSAM composes its step on the right; Ceres's QuaternionManifold multiplies on the left; Barfoot's book perturbs on the left. This series uses the right.

Symptom: Gauss-Newton converges slowly or wanders, though each piece looks right.

Measure: finite differences with the same ⊞ your solver uses. Shrink the step 10 times: the miss must fall 100 times.

Fix: make the Jacobian and the update use the same side.

You shrink the check step 10 times and the miss falls only 10 times. What does that say?

Chapter 4

Gauss-Newton on the Manifold

Step the solver and watch the six fitted directions slide onto the measured ones

Now Lecture 9's loop runs with one line changed. Stack the six blocks Ji into J, 18 × 3, and the six residuals into r, 18 numbers. Solve for the step, then move with ⊞, not +.

Say it aloud

J transpose J, times delta, equals minus J transpose r. Then R becomes R box-plus delta.

J⊤J
J transpose J a 3 × 3 matrix. Here it is Σ(I − bibi⊤), the same at every R, because R⊤R = I cancels.
δ
delta the Gauss-Newton step: a body turn, in radians.
J⊤r
J transpose r half the slope of the cost for a small turn; minus it points downhill.
R ← R ⊞ δ
R becomes R box-plus delta the update, R exp(δ∧). It is the only line that differs from Lecture 9.
In plain words
  1. At R, compute the six residuals r-i and blocks J-i = minus R b-i-hat.
  2. Build J transpose J and J transpose r, and solve the 3 × 3 system for delta.
  3. Move: R becomes R times exp of delta-hat.
  4. Stop when delta is tiny. Else go to step 1.
With numbers

From the level start, J⊤r = (3.92, −0.50, −3.73). The step is δ = (−0.584, 0.157, 0.801), a 57.5° turn. The cost falls from 10.23 to 1.58, and the error to the best rotation from 87.6° to 32.2°.

Step Gauss-Newton on SO(3)

Blue dots are the measured directions wi; purple dots are R bi for the current R. Yellow lines are the residuals. Choose a start and press Step.

The hero's pairs. Drag sideways on the sphere to turn the view.

From level, the error runs 87.6°, 32.2°, 2.92°, 0.059°, 0.0015°. R⊤R − I stays below 10−15 the whole way. From 500 random starts every run came within 0.001° of the best rotation, after a median of 6 steps. The best rotation matches SciPy's closed-form answer, Rotation.align_vectors, to 10−13°.

Levenberg-Marquardt carries over the same way: add λI to J⊤J, try R ⊞ δ, and keep it only if the cost falls. From level it needs 6 steps, every one downhill.

Symptom: the cost falls but the step δ never shrinks to zero.

Measure: |δ| per step, and the finite-difference check of Chapter 3.

Fix: the update and the Jacobian disagree on the side, or the update adds δ∧ to R.

The solver returns δ = (0.01, −0.02, 0.005). What is the next attitude?

Chapter 5

Rotation Averaging

Move a cluster of headings across 180° and watch the plain average jump to the wrong side

Five sensors each measure the same attitude: R̃1 to R̃5 ("R-tilde"). What is their mean? Averaging nine entries breaks the rules, and averaging Euler angles depends on the chart. On the manifold, the mean is the R that makes the sum of squared tangent distances smallest. That R is the geodesic mean, also called the Karcher mean.

The residual is now a log: ri = R̃i ⊟ R. Its Jacobian is −Jl−1(ri), with Jl Lecture 2's left Jacobian. Because Jl(r)⊤ = Jr(r) and Jr(r) r = r, the slope of the cost is exactly −Σ ri. Replacing the Jacobian by −I gives a famous update:

Say it aloud

Delta equals one over N times the sum over i of R-tilde-i box-minus R-bar.

δ
delta the step; the new mean is R̄ ⊞ δ.
1/N
one over N divide by the number of measurements, five here.
Σi
the sum over i one term per measurement.
R̃i ⊟ R̄
R-tilde-i box-minus R-bar the residual ri: the body turn from the current mean R̄ ("R-bar") to measurement i.
In plain words

Stand at the current mean R-bar. Measure the turn to each measurement with box-minus. Average the turns, and box-plus the average onto R-bar. Repeat until the average turn is zero.

With numbers

In the plane: measurements at 20° and 40°, R̄ = 0°. The turns are 0.349 and 0.698 rad, so δ = 0.524 rad = 30°. One step lands on the mean, 30°.

Average five headings

Blue dots are five measured headings. The ring is the plain average of their angles; the purple dot is the geodesic mean, with its steps in warm. Slide the cluster across 180°.

170°
15°

Illustrative: one fixed standard-normal draw times the spread. The mean starts at the first measurement.

In 3-D, take five measurements with 0.3 rad of noise and start at the identity, 34° away. After two steps the exact Jacobian is 0.0033° off and the mean update 0.0008°. Both stop at the same R, because they share the slope. The average of the Euler angles lands 0.50° off it. The rotation nearest to the plain matrix average lands 0.024° off: close, because the spread is small.

Symptom: an averaged heading on the opposite side of the circle from every measurement.

Measure: the residuals at the answer. They should sum to zero and be small.

Fix: average turns from a nearby rotation with ⊟, and start at one of the measurements.

Two headings: 170° and −170°. What is their geodesic mean?

Chapter 6

Poses: The Same Loop on SE(3)

Align a scan to a map and watch a mismatched update throw the pose away

A pose T = (R, t) adds a shift t to the turn. A lidar scan gives points pi in body numbers, and a map gives the same points qi in world numbers. The residual is ri = R pi + t − qi. The step is now 6 numbers, a twist ξ = (φ, ρ) ("xi equals phi, rho"), rotation first as in Lecture 2.

On the right, T ⊞ ξ = T exp(ξ∧) turns by exp(φ∧) and shifts by R Jl(φ) ρ. To first order the shift is Rρ: ρ is in body numbers, like φ.

Say it aloud

r-i at T box-plus xi is about r-i at T, plus the block row minus R p-i-hat, R, times xi.

ri(T ⊞ ξ)
r-i at T box-plus xi the point residual after the step, in metres.
ri(T)
r-i at T R pi + t − qi now.
[−R pi∧   R]
minus R p-i-hat, R the 3 × 6 Jacobian: Chapter 3's block for the turn, and R for the shift.
ξ
xi the twist (φ, ρ): 3 radians of turn, then 3 metres of shift, in body numbers.
In plain words

A body turn phi swings point p-i by phi cross p-i, as before. A body shift rho moves every point by R rho. Stack the rows, solve the 6 × 6 system, and move with T exp of xi-hat.

With numbers

A planar scan of 8 points with true pose 35°, (1.2, 0.6) m and 0.05 m of noise. From 0°, (0, 0) the cost runs 22.64, 1.87, 0.033, 0.0317. The estimate, 37.14°, (1.213, 0.533) m, matches the closed-form answer.

Align a scan

Blue dots are the map points qi; purple dots are the scan under the current pose. Choose the update and a start, then press Run.

Illustrative toy: SE(2), the planar pose, with a 3-number twist. SE(3) is the same with 6.

The Jacobian assumes the shift is Rρ. Add ρ in world numbers instead and the step points the wrong way whenever R is far from I. From 170° the cost goes 93.3, 245, 554, 730 before it turns round. With T ⊞ ξ it goes 93.3, 18.6, 3.84, 0.22, 0.032. Forster and colleagues' visual-inertial odometry uses the same right-side update; Lecture 11 builds on it.

Symptom: pose estimates that converge from small rotations and blow up from large ones.

Measure: the cost after each step from a start 90° or more away.

Fix: apply the shift as Rρ (or use T exp(ξ∧)) to match the Jacobian's R block.

With the update T exp(ξ∧), why does the Jacobian's shift block equal R rather than I?

Chapter 7

How Sure: Covariance in the Tangent Space

Repeat the fit thousands of times and watch the cloud fill the predicted ellipse

How sure is the attitude? A rotation has no mean and spread in nine entries. But the error can live in the tangent space: the true R is R̂ ⊞ δ ("R-hat box-plus delta"), with δ a small random body turn. Its covariance is a 3 × 3 matrix, like Lecture 9's.

Say it aloud

Sigma-delta is about sigma squared, times J transpose J inverse.

Σδ
Sigma-delta the 3 × 3 covariance of the body-turn error δ, in radians squared.
σ2
sigma squared the noise variance of each residual component: 0.052 here.
(J⊤J)−1
J transpose J inverse the inverse of Chapter 4's matrix, Σ(I − bibi⊤), at the answer.
In plain words

The curvature of the cost for small body turns is J transpose J over sigma squared. Invert it: directions where the cost curves gently are the ones you know least.

With numbers

J⊤J = [[4.06, −0.51, −1.84], [−0.51, 4.50, −0.63], [−1.84, −0.63, 3.44]]. With σ = 0.05 the predicted spread about the body x, y and z axes is 1.68°, 1.41° and 1.84°. In 4,000 seeded repeats in NumPy it was 1.70°, 1.39° and 1.82°, and 95.6% of the errors fell inside the 95% ellipsoid.

Repeat the fit

Each purple dot is one repeat: new noise, a new fit, and its error δ about body x (across) and z (up), in degrees. The purple ellipse is the 95% prediction. Press Run.

Illustrative: the hero's six directions, noise 0.05, fresh seeded draws. Each fit is Gauss-Newton on the manifold, run in your browser.

The ellipse leans because the six directions are not spread evenly. A world-frame covariance is R̂ Σδ R̂⊤, by the adjoint of Chapter 3.

Symptom: a filter that is overconfident in heading after the camera turns.

Measure: the frame of the 3 × 3 covariance: body or world.

Fix: keep one side for steps, Jacobians and covariances; convert with R Σ R⊤.

The attitude covariance is a 3 × 3 matrix. What are its three axes?

Chapter 8

Showcase: Race Every Solver

Choose the true attitude, the start and the noise, and watch four solvers race step by step

Everything at once. Four solvers fit the same six pairs: nine entries, Euler angles, Gauss-Newton on the manifold, and Levenberg-Marquardt on the manifold. The plot shows each one's error to the best rotation per step, and the read-out shows whether each answer is a rotation.

Race four solvers

Set the true pitch, the start and the noise, then press Play to step all four together.

45°
0.05

Illustrative toy: the hero's pairs, true yaw 50° and roll −40°. Levenberg-Marquardt starts with λ = the largest diagonal entry of J⊤J and divides or multiplies it by 10.

Things to try. Put the true pitch at 90° and start level: Euler angles still converge, because the lock bites only where an iterate sits near 90°. Start at 89.9° and they wander. At true pitch 45°, raise the noise to 0.15: the nine-entry fit's determinant falls to 0.74, while both manifold solvers stay rotations to 15 digits.

Chapter 9

Field Guide

Keep the operators, the numbers and the code side by side

A rotation is nine numbers with six rules, so free entries break it and three angles lock. Solve for a small body turn δ in the tangent space, with Jacobians taken with respect to δ, and move with R ⊞ δ = R exp(δ∧). The rest is Lecture 9: Gauss-Newton, Levenberg-Marquardt, and a covariance (J⊤J)−1, now over δ.

PieceFormulaNumber on this pageWatch out
Rules of a rotationR⊤R = I, det R = +1nine-entry fit: det 0.91projecting lands 4.4° off
Euler mapδ = E Δe, det E = −cos θ89.9°: first step about 3,000°every order locks somewhere
Box-plus, box-minusR exp(δ∧), log(R1⊤R2)∨round trip to 3 × 10−17the chart covers one turn
JacobianJi = −R bi∧ (right)miss 2.7 × 10−5 at 0.01 radleft: −(R bi)∧
Gauss-Newton(J⊤J)δ = −J⊤r, R ← R ⊞ δ500 of 500 starts; median 6 stepssame side as J
Rotation averagingδ = (1/N) Σ R̃i ⊟ R̄20°, 40° → 30° in one stepstart near the cluster
Pose Jacobian[−R pi∧   R], ξ = (φ, ρ)37.14°, (1.213, 0.533) mshift is Rρ, not ρ
CovarianceΣδ ≈ σ2(J⊤J)−11.68°, 1.41°, 1.84°; 95.6% insidesay which frame

The core, as code

python
import numpy as np
from scipy.spatial.transform import Rotation

def hat(p): return np.array([[0, -p[2], p[1]], [p[2], 0, -p[0]], [-p[1], p[0], 0]])
def boxplus(R, d):  return R @ Rotation.from_rotvec(d).as_matrix()        # R exp(d^), d in body numbers
def boxminus(R2, R1): return Rotation.from_matrix(R1.T @ R2).as_rotvec()  # log(R1^T R2)^v

def fit_attitude(B, W, R, iters=20, lam=0.0):
    """B, W: (N, 3) body and world directions. Gauss-Newton (lam = 0) or damped, on SO(3)."""
    for _ in range(iters):
        r = (R @ B.T).T - W                                  # residuals, (N, 3)
        J = np.stack([-R @ hat(b) for b in B])                   # (N, 3, 3): J_i = -R b_i^
        H = np.einsum('nki,nkj->ij', J, J)                       # J^T J = sum(I - b b^T)
        g = np.einsum('nki,nk->i', J, r)                         # J^T r
        d = np.linalg.solve(H + lam * np.eye(3), -g)
        R = boxplus(R, d)                                      # the one changed line
        if np.linalg.norm(d) < 1e-12: break
    return R, np.linalg.inv(H)                                 # estimate; covariance / sigma^2

def best_closed_form(B, W):                                   # the SVD answer, as a check
    U, _, Vt = np.linalg.svd(W.T @ B); D = np.diag([1, 1, np.linalg.det(U @ Vt)])
    return U @ D @ Vt

Our loop and the closed-form SVD answer (also SciPy's Rotation.align_vectors) agree to 10−13° on the hero's pairs. Our exp and log match scipy.spatial.transform.Rotation to 5 × 10−14 on 2,000 random turns. The SVD answer exists only for this problem; the loop works for any residual.

Where it sits

Lecture 2 built exp, log, the adjoint and the Jacobians of exp. Lecture 9 built the solvers for vector unknowns. This lecture joins them, so rotations and poses can be unknowns. Lecture 11 runs it on IMU and camera states, and Lecture 13 on thousands of poses at once.

Keep going

The takeaway. Never step the nine entries or three angles. Linearize with respect to a small body turn, solve the flat 3 × 3 (or 6 × 6) system, and move with R ⊞ δ = R exp(δ∧), using the same side for the Jacobian, the step and the covariance.

Now press Present or Teach. Count the rules of a rotation, show where Euler angles lock, define ⊞ and ⊟, derive Ji = −R bi∧, and write the loop. Then go back to the hero and explain why the Euler solver wanders from 89.9° while the manifold solver comes home in five steps.

Credit

VNAV Lecture 10: Optimization on Manifolds
MIT 16.485 VNAV · Previous: estimation and nonlinear least squares · Next: visual-inertial odometry · Back to Gleams