Modeling

Use analytical or automatic derivatives

Avoid numeric differentiation whenever possible. Numeric differentiation is slow, sensitive to step-size choices and floating-point roundoff, and prone to poor convergence.

Ceres can automatically differentiate templated cost functors, which is the recommended approach for most problems. When the derivatives have a simple closed form or the performance overhead of automatic differentiation is too high, analytic derivatives are worth implementing directly.

Reserve numerical derivatives for cases where a templated implementation of the cost function is impossible, such as when calling an external library. Even then, you can often isolate the non-templated evaluation in a helper and combine numeric differentiation of that part with automatic or analytic differentiation of the rest using CostFunctionToFunctor.

Do not return the norm or squared norm of a residual vector from a CostFunction

A CostFunction should output the signed components of the residual vector \(r(x) \in \mathbb{R}^k\) directly, never its Euclidean norm \(\|r(x)\|\) or squared norm \(\|r(x)\|^2\). Ceres already forms the sum of squared components \(\frac{1}{2} \|r(x)\|^2 = \frac{1}{2} \sum_{i=1}^k r_i(x)^2\) internally.

Collapsing a \(k\)-dimensional residual vector into a scalar \(\tilde{r}(x) = \|r(x)\|\) leaves the objective value unchanged, but replaces the \(k \times n\) Jacobian \(J(x)\) with the single row \(\frac{r(x)^\top}{\|r(x)\|} J(x)\). The Gauss-Newton Hessian approximation then drops from \(J(x)^\top J(x)\) to the rank-1 matrix \(\frac{1}{\|r(x)\|^2} J(x)^\top r(x) r(x)^\top J(x)\), discarding curvature information in all directions orthogonal to \(r(x)\) and stalling convergence. Worse, \(\|r(x)\|\) is not differentiable at \(r(x) = 0\), producing NaN Jacobians at the exact point the solver is trying to reach. Returning \(\|r(x)\|^2\) avoids the NaN but changes the objective to \(\frac{1}{2}\|r(x)\|^4\), whose Jacobian \(2 r(x)^\top J(x)\) vanishes as \(r(x) \to 0\), making the Gauss-Newton system singular near the solution.

Do not pack multiple independent measurements into a single residual block when using a LossFunction

A LossFunction operates on the squared norm \(s = \|f_i(x)\|^2 = \sum_{j=1}^{k_i} f_{ij}(x)^2\) of an entire residual block, not on its individual scalar components \(f_{ij}(x)\) separately. For a multi-dimensional measurement such as a 2D image reprojection error, this is desirable: \(\rho(\|f_i(x)\|^2)\) is isotropic (rotationally invariant in measurement space) and down-weights both coordinates together whenever the feature match is an outlier.

Conversely, you should not pack multiple independent measurements into a single CostFunction if you intend to use a non-trivial LossFunction. A single outlier component will inflate \(\|f_i(x)\|^2\) and cause the LossFunction to down-weight every other inlier measurement in the same residual block. Independent measurements that can fail independently should be added as separate residual blocks.

When using Quaternions, consider using QuaternionManifold

Quaternions are a four-dimensional parameterization of the three-dimensional rotation group \(SO(3)\). Because \(SO(3)\) is three-dimensional, the tangent space of a unit quaternion is also three-dimensional, so it is often beneficial to attach a Manifold to quaternion parameter blocks. For parameter blocks stored in \(w, x, y, z\) order, use QuaternionManifold.

Note

If you are using Eigen’s Quaternion, whose memory layout is \(x, y, z, w\), use EigenQuaternionManifold instead.

How do I solve problems with general linear & non-linear inequality constraints with Ceres Solver?

Ceres Solver directly supports only lower and upper bound constraints on parameter blocks.

A crude workaround for general inequality constraints is to have one or more cost functions check whether the inequalities are satisfied and return false when they are violated. Returning false rejects the candidate step and prevents the solver from entering the infeasible region, provided the initial parameter values are feasible. Because the solver cannot see the constraint boundary until a step crosses it, however, this approach can cause premature convergence.

How do I solve problems with general linear & non-linear equality constraints with Ceres Solver?

Ceres Solver does not support general equality constraints directly; it supports only lower and upper bound constraints on parameter blocks. The step-rejection workaround described above for inequality constraints does not work for equality constraints.

How do I set one or more components of a parameter block constant?

Attach a SubsetManifold to the parameter block to hold the desired coordinates constant during optimization.