Skip to content

Commit 6c6f2ad

Browse files
committed
feat(solvers): add damped pseudo-inverse and null-space projector
Deploy roadmap item 62. One SVD (tall or wide) gives the damped least-squares solve Σ σ/(σ² + λ²)(uᵢᵀb)vᵢ, which falls back to a thresholded undamped inverse for λ = 0, plus the exact orthogonal null-space projector I − Σ_{i<r} vᵢvᵢᵀ. The projector is left undamped so that A·P stays zero whatever λ is. Checked against closed-form (AᵀA + λ²I)⁻¹Aᵀb / Aᵀ(AAᵀ + λ²I)⁻¹b in double on random 2×3, 6×7, 7×6 and 4×4 matrices (≈1e-5 agreement, P² − P ≈ 1e-6). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q53m7MZDmHwocmKbhhd2o5
1 parent a246fc2 commit 6c6f2ad

13 files changed

Lines changed: 384 additions & 182 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Refer to the documentation to quickly integrate and utilize the library's signal
2525
| [Regularization](doc/regularization/README.md) | L1 (Lasso), L2 (Ridge) |
2626
| [Math](doc/math/README.md) | CORDIC, Quaternion, MatrixNorms, Step Response Metrics, MatrixExponential, Angle Utilities, Tridiagonal (Thomas) Solver, Cubic Spline Interpolation |
2727
| [Motion Profiles](doc/motion_profiles/README.md) | Polynomial (Cubic/Quintic) Point-to-Point Profile, Trapezoidal (LSPB) Profile, S-Curve (Jerk-Limited) Profile |
28-
| [Solvers](doc/solvers/README.md) | Gaussian Elimination, Levinson-Durbin, Durand-Kerner, Cholesky, DARE, Runge-Kutta ODE Integrators (RK4 + Dormand-Prince), Spectral Radius & Discrete Stability Margin, QR Decomposition (Householder / Givens), LU Decomposition with Partial Pivoting, Singular Value Decomposition (Golub-Kahan), Symplectic Integrators (Semi-Implicit Euler, Velocity Verlet) |
28+
| [Solvers](doc/solvers/README.md) | Gaussian Elimination, Levinson-Durbin, Durand-Kerner, Cholesky, DARE, Runge-Kutta ODE Integrators (RK4 + Dormand-Prince), Spectral Radius & Discrete Stability Margin, QR Decomposition (Householder / Givens), LU Decomposition with Partial Pivoting, Singular Value Decomposition (Golub-Kahan), Symplectic Integrators (Semi-Implicit Euler, Velocity Verlet), Damped Pseudo-Inverse + Null-Space Projector |
2929
| [Nonlinear Control](doc/nonlinear_control/README.md) | Feedback Linearization, Backstepping Control, Model Reference Adaptive Control (MRAC) |
3030
| [Robust Control](doc/robust_control/README.md) | Active Disturbance Rejection Control (ADRC + ESO), Sliding Mode Control (SMC), Disturbance Observer (DOB), H∞ State-Feedback Control |
3131
| [Performance Optimization](doc/performance-optimization/README.md) | Compiler optimizations |

‎ROADMAP.md‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Difficulty legend:
2727
| # | Component | Target module | Difficulty |
2828
|----|------------------------------------------------------|-------------------------|------------|
2929
| 53 | SE(3) transform, twists, wrenches, adjoint, exp/log | `math` | ★★☆☆☆ |
30-
| 62 | Damped pseudo-inverse + null-space projector | `solvers` | ★★☆☆☆ |
3130
| 63 | Incremental QR least squares (rank-revealing) | `estimators/offline` | ★★★☆☆ |
3231
| 64 | Levenberg–Marquardt (retraction, pluggable damping) | `optimization` | ★★★☆☆ |
3332
| 65 | Dense convex QP (Goldfarb–Idnani dual active set) | `solvers` | ★★★★☆ |
@@ -55,17 +54,6 @@ the library does not yet expose. Detailed below under
5554
- **Reuses:** `Geometry3D` (`RotationAboutAxis`, `SkewSymmetric`), `Quaternion`.
5655
- **Spec:** [roadmap/math/SE3Transform](roadmap/math/SE3Transform/implementation.md).
5756

58-
### 62. Damped pseudo-inverse and null-space projector
59-
- **What:** `A⁺_λ·b` and the exact projector `I − A⁺A` for any `Rows × Cols` matrix from one SVD.
60-
- **Embedded value:** Singularity-robust inversion for redundant robots, over-actuated control allocation
61-
and regularized calibration; the projector exposes spare actuation to a secondary objective.
62-
- **Algorithm / paper:** C. Wampler, *IEEE Trans. SMC*, 16(1), 1986; A. Liégeois, *IEEE Trans. SMC*,
63-
7(12), 1977.
64-
- **Reuses:** `SingularValueDecomposition` (tall and wide).
65-
- **Spec:** [roadmap/solvers/DampedPseudoInverse](roadmap/solvers/DampedPseudoInverse/implementation.md).
66-
67-
---
68-
6957
## Tier 3 — Moderate ★★★☆☆
7058

7159
### 63. Incremental QR least squares

‎doc/solvers/DampedPseudoInverse.md‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Damped Pseudo-Inverse and Null-Space Projector
2+
3+
## Overview & Motivation
4+
5+
Mapping desired outputs to inputs through a non-square or ill-conditioned matrix $A$ is everywhere: joint rates from a tool velocity, motor commands from a desired thrust and torque, calibration corrections from residuals. The plain pseudo-inverse divides by every singular value, so near rank loss it explodes and commands huge, saturating inputs. **Damping** replaces the division by a gain that equals $1/\sigma$ for strong directions and fades to zero for weak ones, trading a small, bounded accuracy loss for a bounded answer.
6+
7+
When there are more inputs than outputs, $A$ has a null space: input motions it cannot feel. A **null-space projector** removes from any vector every component the matrix sees, so a secondary goal (stay away from limits, share load, save energy) can use the spare freedom without disturbing the primary one. Both tools come from one thin SVD, factored once per matrix.
8+
9+
## Mathematical Theory
10+
11+
### Factorization and Rank
12+
13+
For $A \in \mathbb{R}^{m \times n}$, tall or wide, and $k = \min(m, n)$, the thin SVD is $A = U\Sigma V^\top$ with $\sigma_0 \geq \cdots \geq \sigma_{k-1} \geq 0$, left vectors $u_i \in \mathbb{R}^m$ and right vectors $v_i \in \mathbb{R}^n$. With a relative tolerance $\tau$ (default $10^{-4}$) the rank threshold and rank are
14+
15+
$$\theta = \tau\,\sigma_0, \qquad r = \#\{\,i : \sigma_i > \theta\,\}.$$
16+
17+
Since the values are sorted, the counted directions are the leading $r$. The zero matrix has $\sigma_0 = 0$, hence $r = 0$.
18+
19+
### Damped Gain
20+
21+
Damped least squares (Tikhonov regularization) minimizes $\|Ax - b\|^2 + \lambda^2\|x\|^2$. Its normal equations give
22+
23+
$$x = (A^\top A + \lambda^2 I)^{-1}A^\top b = A^\top (AA^\top + \lambda^2 I)^{-1} b,$$
24+
25+
and in the singular basis each direction is scaled by the gain
26+
27+
$$g(\sigma) = \begin{cases} \dfrac{\sigma}{\sigma^2 + \lambda^2} & \lambda > 0, \\[2mm] 1/\sigma & \lambda = 0,\ \sigma > \theta, \\[1mm] 0 & \lambda = 0,\ \sigma \leq \theta. \end{cases}$$
28+
29+
For $\sigma \gg \lambda$ the gain is $\approx 1/\sigma$; for $\sigma \ll \lambda$ it is $\approx \sigma/\lambda^2 \to 0$. Its maximum is at $\sigma = \lambda$, so $g \leq 1/(2\lambda)$ always. Without damping the threshold zeroes weak directions, so no division by zero occurs. The solution and the damped inverse are
30+
31+
$$x = \sum_{i=0}^{k-1} g(\sigma_i)\,(u_i^\top b)\,v_i, \qquad A^\dagger_\lambda = \sum_{i=0}^{k-1} g(\sigma_i)\,v_i u_i^\top.$$
32+
33+
Every term lies in the span of the $v_i$, the row space of $A$, so $x$ has no null-space component: for $\lambda = 0$ it is the minimum-norm solution.
34+
35+
### Null-Space Projector
36+
37+
$$P = I - \sum_{i<r} v_i v_i^\top, \qquad Px = x - \sum_{i<r}(v_i^\top x)\,v_i.$$
38+
39+
The second form applies the projection without forming $P$: $r$ inner products and $r$ vector updates. The projector is deliberately undamped. Built from the damped inverse, $I - A^\dagger_\lambda A$ leaves a residual in the primary task:
40+
41+
$$A\,(I - A^\dagger_\lambda A) = \sum_i \sigma_i \frac{\lambda^2}{\sigma_i^2 + \lambda^2}\,u_i v_i^\top = \lambda^2 (AA^\top + \lambda^2 I)^{-1} A \neq 0.$$
42+
43+
Along each singular direction its eigenvalue is $\lambda^2/(\sigma_i^2 + \lambda^2)$, strictly between $0$ and $1$ for $\sigma_i > 0$, so it is not idempotent either. The thresholded projector is exact: $P^2 = P$, $P^\top = P$, and
44+
45+
$$AP = \sum_{i \geq r} \sigma_i\,u_i v_i^\top, \qquad \sigma_i \leq \theta,$$
46+
47+
which is zero for exact rank loss and at rounding level for full rank.
48+
49+
### Damping Bias
50+
51+
For $b \in \operatorname{range}(A)$ the residual is $b - Ax = \sum_i \frac{\lambda^2}{\sigma_i^2 + \lambda^2}(u_i^\top b)\,u_i$, hence
52+
53+
$$\|Ax - b\| \leq \frac{\lambda^2}{\sigma_{\min}^2 + \lambda^2}\,\|b\| \leq \frac{\lambda^2}{\sigma_{\min}^2}\,\|b\|,$$
54+
55+
with $\sigma_{\min}$ the smallest nonzero singular value. Robustness costs accuracy of order $\lambda^2/\sigma_{\min}^2$. Near rank loss the threshold grows the null space: the lost direction is released to the projector by design, while the damped solution stays bounded.
56+
57+
## Complexity Analysis
58+
59+
| Operation | Time | Space | Notes |
60+
|-----------|------|-------|-------|
61+
| Factor | $O(Mk^2)$ | $O(mk + nk)$ | One Golub–Kahan SVD; wide matrices through their transpose |
62+
| Solve | $O((m + n)\,k)$ | $O(n)$ | $k$ inner products of length $m$, $k$ updates of length $n$; no division |
63+
| Project | $O(n\,r)$ | $O(n)$ | $r$ inner products and updates; $P$ never formed |
64+
| Explicit inverse, projector | $O(mnk)$, $O(n^2 r)$ | $O(mn)$, $O(n^2)$ | Diagnostics; sums of rank-one terms |
65+
66+
Here $M = \max(m, n)$. The SVD factors are the only persistent storage: $U$ is $m \times k$, $V$ is $n \times k$, plus $k$ singular values. No heap is used, and the factorization is paid once per matrix; later solves and projections are inner products only.
67+
68+
## Step-by-Step Walkthrough
69+
70+
**Input:** $A = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \end{pmatrix}$, $b = (1, 2)$, $\tau = 10^{-4}$.
71+
72+
**Factor.** $AA^\top = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}$ has eigenvalues $3$ and $1$, so $\sigma = (\sqrt{3}, 1)$. Then $u_0 = (1, 1)/\sqrt{2}$, $u_1 = (1, -1)/\sqrt{2}$, $v_0 = (1, 1, 2)/\sqrt{6}$, $v_1 = (1, -1, 0)/\sqrt{2}$ (signs are arbitrary). The threshold is $\theta \approx 1.7 \times 10^{-4}$, so $r = 2$; the null space is spanned by $(1, 1, -1)$.
73+
74+
**Gains and projections** at $\lambda = 0.1$:
75+
76+
| $i$ | $\sigma_i$ | $u_i^\top b$ | $1/\sigma_i$ | $g(\sigma_i)$ |
77+
|-----|-----------|--------------|--------------|---------------|
78+
| 0 | $1.732051$ | $2.121320$ | $0.577350$ | $0.575432$ |
79+
| 1 | $1$ | $-0.707107$ | $1$ | $0.990099$ |
80+
81+
**Solution.** Without damping, $x = A^\dagger b = (0, 1, 1)$: it satisfies $Ax = b$ and is orthogonal to the null space ($0 + 1 - 1 = 0$). With $\lambda = 0.1$, $x = (0.003289, 0.993388, 0.996678)$. The residual is $b - Ax = \lambda^2(AA^\top + \lambda^2 I)^{-1}b$, of norm $0.009934$, within the bound $0.022139$ (the looser $\lambda^2\|b\|/\sigma_{\min}^2$ gives $0.022361$).
82+
83+
**Projector.** With both singular directions counted,
84+
85+
$$P = I - v_0 v_0^\top - v_1 v_1^\top = \tfrac{1}{3}\begin{pmatrix} 1 & 1 & -1 \\ 1 & 1 & -1 \\ -1 & -1 & 1 \end{pmatrix}, \qquad \operatorname{tr} P = 1 = n - r.$$
86+
87+
Projecting $(1, 0, 0)$ gives $(1/3, 1/3, -1/3)$, which $A$ maps to $0$. Projecting the damped solution gives $\approx 0$, since it lies in the row space.
88+
89+
**Rank loss and check.** For $A = \begin{pmatrix} 1 & 1 & 0 \\ 2 & 2 & 0 \end{pmatrix}$ the singular values are $(\sqrt{10}, 0)$: $r = 1$ and $\operatorname{tr} P = 2$. Undamped, the zero direction gets zero gain, and $A^\dagger(1, 2) = (0.5, 0.5, 0)$. The results were compared with the closed forms $(A^\top A + \lambda^2 I)^{-1}A^\top b$ (tall) and $A^\top (AA^\top + \lambda^2 I)^{-1} b$ (wide), evaluated in double precision, on random $2 \times 3$, $6 \times 7$, $7 \times 6$ and $4 \times 4$ matrices: solutions agree to about $10^{-5}$ and $\|P^2 - P\|$ is about $10^{-6}$.
90+
91+
## Pitfalls & Edge Cases
92+
93+
- **Choice of $\lambda$:** too small leaves large gains (up to $1/(2\lambda)$) near singularities; too large biases every direction by about $\lambda^2/\sigma^2$. Choose it from the smallest singular value that must still be tracked.
94+
- **Hard rank decision:** the rank and the projector jump when a singular value crosses $\theta$, so secondary motion can switch abruptly near rank loss; smoothing or hysteresis belongs to the caller. The damped solution also keeps a small component $\sigma/(\sigma^2 + \lambda^2)\,(u^\top b)$ along directions already released to the projector, which the projection does not remove.
95+
- **Degenerate shapes:** a zero matrix gives rank $0$, a zero solution and a projection equal to the identity; undamped, a zero singular value has zero gain, never a division. A tall full-rank matrix has a zero projector and an undamped least-squares solution.
96+
- **Precision and failure:** $\tau$ must exceed the rounding level of $\sigma_0$ (about $10^{-7}$ in single precision), or noise is counted as rank. If the SVD does not converge the call reports failure and the results must not be used; $\lambda$ and $\tau$ must be non-negative. Singular-vector signs are arbitrary, and the solution and projector are invariant to them.
97+
98+
## Variants & Generalizations
99+
100+
- **Truncated SVD ($\lambda = 0$):** hard cutoff at $\theta$; exact on retained directions, discontinuous in $A$.
101+
- **Adaptive damping:** $\lambda$ zero away from singularity, rising as $\sigma_{\min}$ falls below a margin (Nakamura and Hanafusa), so accuracy is lost only when needed.
102+
- **Weighted damped least squares:** $\min \|W(Ax - b)\|^2 + \lambda^2\|Mx\|^2$ with task and joint weights, reduced to the plain form by scaling.
103+
104+
## Applications
105+
106+
- **Redundant manipulators and damped IK:** joint rates $\dot q = J^\dagger_\lambda \dot x + P z$ track the task velocity and use the null space for a secondary objective (joint-limit avoidance, posture) through gradient projection.
107+
- **Control allocation:** distribute a commanded thrust and torque over more actuators than axes, with the projector holding the spare freedom for load sharing or energy minimization.
108+
- **Regularized calibration:** correction estimates from residuals when the sensitivity matrix is ill-conditioned.
109+
110+
## Connections to Other Algorithms
111+
112+
- **Singular Value Decomposition** is the factorization; its plain thresholded pseudo-inverse is the undamped special case, and wide matrices are handled there through the transpose.
113+
- **Condition Number** ($\sigma_0/\sigma_{\min}$) indicates when damping is needed and how to size $\lambda$.
114+
- **QR Decomposition, LU Decomposition, Gaussian Elimination** solve full-rank least-squares or square systems more cheaply but reveal no rank and cannot damp gracefully.
115+
- **Regularization and Levenberg–Marquardt:** the $\lambda^2\|x\|^2$ penalty is ridge regression; Levenberg–Marquardt applies the same damping per iteration to keep a Gauss–Newton step bounded.
116+
117+
## References & Further Reading
118+
119+
- C. W. Wampler, "Manipulator inverse kinematic solutions based on vector formulations and damped least-squares methods," *IEEE Trans. Systems, Man, and Cybernetics*, 16(1), 1986.
120+
- Y. Nakamura and H. Hanafusa, "Inverse kinematic solutions with singularity robustness for robot manipulator control," *J. Dynamic Systems, Measurement, and Control*, 108, 1986.
121+
- G. H. Golub and C. F. Van Loan, *Matrix Computations*, 4th ed., Johns Hopkins UP, 2013, §5.5.

‎doc/solvers/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Numerical solvers for linear systems, polynomial roots, and matrix equations.
1919
| [Singular Value Decomposition](SingularValueDecomposition.md) | Golub-Kahan bidiagonalization and implicit-shift QR for SVD, pseudo-inverse, rank, and condition number |
2020
| [Jacobi Eigenvalue Solver](JacobiEigenSolver.md) | Cyclic Jacobi rotations for the full symmetric eigenvalue/eigenvector problem |
2121
| [Symplectic Integrators](SymplecticIntegrators.md) | Semi-implicit Euler and velocity Verlet for second-order systems — bounded energy error on conservative plants |
22+
| [Damped Pseudo-Inverse](DampedPseudoInverse.md) | Damped (Tikhonov) least-squares inverse and exact null-space projector from one SVD — tall or wide matrices |

‎numerical/solvers/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ target_link_libraries(numerical.solver ${NUMERICAL_VISIBILITY}
1212

1313
target_sources(numerical.solver PRIVATE
1414
ConditionNumber.hpp
15+
DampedPseudoInverse.hpp
1516
DiscreteAlgebraicRiccatiEquation.hpp
1617
DormandPrince45.hpp
1718
DurandKerner.hpp
@@ -31,6 +32,7 @@ target_sources(numerical.solver PRIVATE
3132
)
3233

3334
numerical_add_coverage_sources(numerical.solver
35+
DampedPseudoInverse.cpp
3436
DiscreteAlgebraicRiccatiEquation.cpp
3537
DurandKerner.cpp
3638
GaussianElimination.cpp
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "numerical/solvers/DampedPseudoInverse.hpp"
2+
3+
namespace solvers
4+
{
5+
template class DampedPseudoInverse<float, 2, 3>;
6+
template class DampedPseudoInverse<float, 6, 7>;
7+
}

0 commit comments

Comments
 (0)