Part I · 第 1 章 线性代数 · 1.13Part I · Ch. 1 Linear Algebra · 1.13

最小二乘与伪逆Least Squares & the Pseudo-inverse

真实的数据总是又多又脏:方程比未知数多,谁也没法全部满足。那就换一个问题:让总的平方误差最小。几何上,这就是把数据向量投影到列空间上,找最近的那一点。反过来,如果方程不够、解有无穷多,就挑最短的那个。伪逆用同一个公式 \(A^{+} = V\Sigma^{+}U^\top\) 把这两种情形一起解决,再加一点“阻尼”,机械臂就能平稳地走过奇异位形。 Real data is plentiful and noisy: more equations than unknowns, and no solution satisfies them all. So ask a different question: minimise the total squared error. Geometrically, that means projecting the data vector onto the column space and taking the nearest point. In the opposite case, with too few equations and infinitely many solutions, pick the shortest one. The pseudo-inverse handles both with one formula, \(A^{+} = V\Sigma^{+}U^\top\), and with a little “damping” a robot arm can glide through a singularity.

约 35 分钟~35 min 前置:1.6 逆、秩与零空间 · 1.9 投影 · 1.12 SVDPrereqs: 1.6 Inverse, rank & null space · 1.9 Projection · 1.12 SVD 5 个交互5 interactives

    1为什么机器人需要它Why robots need this

    机器人拿到的数据几乎从来不是“刚刚好”的。激光雷达扫过一面墙,返回两百个带噪声的点,而一条直线只有两个参数(斜率和截距)。轮子转了几千圈的里程日志,只为估计一个数:轮半径。标定陀螺仪的零偏和比例、相机内参、手眼关系,都要拿几十组测量去定几个未知数。

    这时方程比未知数多得多,而且每个方程都带着噪声,没有任何一个解能同时满足所有方程。我们能做的,是找一个“最不坏”的折中。这就是最小二乘 (least squares)。

    反方向的情况同样常见:一台 7 自由度机械臂只需要满足 6 个末端约束,多出来的那个自由度让关节速度有无穷多种选择。我们得有一个规则来挑,比如“关节动得最少”。再加上第三种麻烦:靠近奇异位形时,精确的逆会把微小的误差放大成巨大的关节速度(1.12 节),需要给逆加上“阻尼”。

    The data a robot gets is almost never “just right”. A lidar sweeps a wall and returns two hundred noisy points, while a line has only two parameters (slope and intercept). An odometry log with thousands of wheel turns exists to estimate a single number, the wheel radius. Calibrating a gyro's bias and scale, camera intrinsics or a hand–eye transform means fixing a few unknowns from dozens of measurements.

    Now there are far more equations than unknowns, each one corrupted by noise, and no solution satisfies all of them. The best we can do is find the “least bad” compromise. That is least squares.

    The opposite case is just as common: a 7-DoF arm only has to meet 6 end-effector constraints, so the extra degree of freedom leaves infinitely many joint velocities to choose from. We need a rule to pick one, for example “move the joints as little as possible”. And a third nuisance: near a singularity the exact inverse turns tiny errors into huge joint speeds (Lesson 1.12), so we must add “damping” to it.

    情形Situation 方程数 m 与未知数 nEquations m vs unknowns n 怎么办What we do 机器人里的例子Robot example
    超定 (overdetermined)Overdetermined \(m > n\) 没有精确解:最小化总平方误差no exact solution: minimise the total squared error 拟合墙面直线,标定传感器,估计轮半径fit a wall line, calibrate a sensor, estimate a wheel radius
    欠定 (underdetermined)Underdetermined \(m < n\) 无穷多解:取最短(最小范数)的一个infinitely many solutions: take the shortest (minimum-norm) one 冗余机械臂的速度级逆运动学velocity-level inverse kinematics of a redundant arm
    病态 (ill-conditioned)Ill-conditioned \(m = n\),但 \(\sigma_{\min}\approx 0\), but \(\sigma_{\min}\approx 0\) 精确逆会放大噪声:加阻尼the exact inverse amplifies noise: add damping 手臂接近伸直时的逆运动学inverse kinematics near a fully stretched arm

    三种情形用的是同一件工具:伪逆 (pseudo-inverse) \(A^{+}\)。它把本章的几位主角串了起来:列空间 (1.6 节)、投影 (1.9 节)、奇异值分解 (1.12 节)。

    One tool serves all three: the pseudo-inverse \(A^{+}\). It ties together the main characters of this chapter: the column space (Lesson 1.6), projection (Lesson 1.9) and the SVD (Lesson 1.12).

    2直觉:无法全部满足,就让总的“错过”最小Intuition: can't satisfy everything, so minimise the total miss

    先看最小的例子。平面上有三个点 \((-1,\,1)\)、\((0,\,0)\)、\((1,\,2)\),想找一条直线 \(y = mt + c\) 穿过它们。每个点给一个方程:

    Start with the smallest example. Three points \((-1,\,1)\), \((0,\,0)\), \((1,\,2)\) lie in the plane, and we want a line \(y = mt + c\) through all of them. Each point gives one equation:

    $$ \underbrace{\begin{bmatrix} -1 & 1 \\ 0 & 1 \\ 1 & 1 \end{bmatrix}}_{A}\underbrace{\begin{bmatrix} m \\ c \end{bmatrix}}_{\mathbf x} = \underbrace{\begin{bmatrix} 1 \\ 0 \\ 2 \end{bmatrix}}_{\mathbf b} $$

    前两行说 \(c = 0\)、\(m = -1\),第三行却要求 \(m + c = 2\)。矛盾:这三个点不共线,任何直线都会“错过”其中某些点。在 1.6 节的语言里,\(\mathbf b\) 不在 \(A\) 的列空间里。

    既然不可能全部满足,就换一个问题:让每个点的偏差,也就是残差 (residual) \(r_i = y_i - (mt_i + c)\),合起来尽量小。怎么把几个残差合成一个数?取平方和。为什么是平方?

    • 大偏差被重罚。偏差翻倍,代价翻四倍,所以一个大错比很多小错更要紧。
    • 平方是光滑的。最小值可以用求导或者几何直接解出,不需要迭代。
    • 它就是长度的平方。残差向量的平方和是 \(\|\mathbf r\|^2\),最小化它就是找最近的点,而最近点由垂线给出 (1.9 节)。
    • 它对应高斯噪声下的最大似然(4.6 节会看到)。

    The first two rows say \(c = 0\) and \(m = -1\), yet the third demands \(m + c = 2\). Contradiction: the three points are not collinear, so every line misses some of them. In the language of Lesson 1.6, \(\mathbf b\) is not in the column space of \(A\).

    Since we cannot satisfy everything, ask a different question: make the deviations, the residuals \(r_i = y_i - (mt_i + c)\), small all together. How do we turn several residuals into one number? Take the sum of squares. Why squares?

    • Big misses are punished hard. Double the miss and the cost quadruples, so one large error matters more than many small ones.
    • Squares are smooth. The minimum can be found by calculus or geometry directly, with no iteration.
    • It is a squared length. The sum of squared residuals is \(\|\mathbf r\|^2\), so minimising it means finding the nearest point, and the nearest point comes from a perpendicular (Lesson 1.9).
    • It is maximum likelihood under Gaussian noise (we will see this in Lesson 4.6).
    核心洞见Key insight

    当 \(A\mathbf x = \mathbf b\) 无解,说明 \(\mathbf b\) 够不到 \(A\) 的列空间。最小二乘就是在列空间里找离 \(\mathbf b\) 最近的点 \(A\hat{\mathbf x}\),也就是 \(\mathbf b\) 在列空间上的投影。残差 \(\mathbf b - A\hat{\mathbf x}\) 垂直于整个列空间。

    When \(A\mathbf x = \mathbf b\) has no solution, \(\mathbf b\) lies outside the column space of \(A\). Least squares finds the point \(A\hat{\mathbf x}\) of the column space nearest to \(\mathbf b\): the projection of \(\mathbf b\) onto it. The residual \(\mathbf b - A\hat{\mathbf x}\) is perpendicular to the whole column space.

    3动手玩:用一条线拟合带噪声的点Play: fit a line to noisy points

    下图里 8 个点是带噪声的测量(比如激光雷达打在一面斜墙上)。两个琥珀色手柄决定你的直线 \(y = mt + c\)。每个点到直线的竖直线段是它的残差,以残差为边长画出的红色方块,面积就是残差的平方。所有方块面积之和就是 SSE (sum of squared errors)。你的任务只有一个:让红色的总面积尽量小。点也可以拖动。

    The 8 points below are noisy measurements (say, a lidar hitting a slanted wall). The two amber handles set your line \(y = mt + c\). The vertical segment from each point to the line is its residual, and the red square built on it has area equal to the squared residual. The total red area is the SSE (sum of squared errors). Your job: make the red area as small as you can. The points can be dragged too.

    FIG 1最小化红色方块的总面积Minimise the total red area
    横轴 t,纵轴 yhorizontal t, vertical y
    琥珀色:你的直线。紫色虚线:最小二乘 (LS) 直线,用右下方的按钮显示。十字是数据的重心 \((\bar t,\bar y)\)。下方的公式是由这些点直接算出的“正规方程”,本节后面会推导它。青色虚线是 TLS 线(量垂直距离而不是竖直距离),见误区 3。 Amber: your line. Dashed purple: the least-squares (LS) line, shown with the toggle below. The cross is the data's centroid \((\bar t,\bar y)\). The formula underneath is the “normal equation” computed straight from these points; we derive it later in this lesson. The dashed cyan line is the TLS line (it measures perpendicular rather than vertical distance), see Pitfall 3.
    试一试Try this
    • 只用手柄,把你的 SSE 压到最优 SSE 的 105% 以内。Using only the handles, get your SSE within 105% of the optimal SSE.
    • 点“加离群点”,看最小二乘线被拉偏多少(读数会显示)。一个坏点就能把整条线拖走,因为平方让大误差占了主导。鲁棒损失见 6.9 节,RANSAC 见第三部分 11.5 节。Press “add outlier” and see how far the LS line gets dragged (the readout tells you). One bad point can pull the whole line, because squaring lets large errors dominate. Robust losses come in Lesson 6.9, and RANSAC in Lesson 11.5 (Part III).
    • 造一条很差的线,让残差之和 \(\sum r_i\) 接近 0(\(|\sum r_i| < 1\)),同时 SSE 至少是最优的 2 倍。提示:直线必须经过哪个点?零和说明不了好坏,起作用的是平方。Build a bad line whose residuals still sum to nearly zero (\(|\sum r_i| < 1\)) while the SSE is at least twice the optimum. Hint: which point must the line pass through? A zero sum says nothing about quality; the squares do the work.

    玩一会儿你会发现:最优线并不是让每个方块都变小,它是让总面积最小,有的点会为大局让步。最优线一定穿过重心 \((\bar t,\bar y)\);而穿过重心的线残差之和都是零,所以零和只是必要条件。把 8 个点写成 8 行方程,就是 \(A\mathbf x\approx\mathbf b\),其中 \(A\) 的每一行是 \([\,t_i\;\;1\,]\),\(\mathbf x = (m, c)^\top\),\(\mathbf b = (y_1,\dots,y_8)^\top\)。

    Play for a while and you will notice that the optimal line does not shrink every square; it minimises the total area, and some points give way for the common good. The optimal line always passes through the centroid \((\bar t,\bar y)\), but so does every line whose residuals sum to zero, so a zero sum is only a necessary condition. Writing the 8 points as 8 equations gives \(A\mathbf x\approx\mathbf b\), where each row of \(A\) is \([\,t_i\;\;1\,]\), \(\mathbf x = (m, c)^\top\) and \(\mathbf b = (y_1,\dots,y_8)^\top\).

    4第二种视角:数据空间里的垂线A second view: the perpendicular in data space

    FIG 1 的坐标是 \((t, y)\):每个数据点是平面上的一个点(“模型空间”)。现在换一个空间看同一个问题:把所有测量值排成一个向量 \(\mathbf b\),每个测量占一根坐标轴。这样整组数据就是一个点。为了能画出来,只取三个点,固定在 \(t = -1, 0, 1\) 上,于是 \(\mathbf b = (y_1, y_2, y_3)\) 是三维空间里的一个点。

    \(A\) 的两列是 \(\mathbf a_1 = (-1, 0, 1)\)(\(t\) 的取值)和 \(\mathbf a_2 = (1, 1, 1)\)(常数 1)。\(A\mathbf x = m\,\mathbf a_1 + c\,\mathbf a_2\) 是它们的线性组合,所以“能被某条直线精确解释的数据”正好是这两列张成的一张平面(列空间)。真实数据 \(\mathbf b\) 一般不在平面上,最近的点 \(A\hat{\mathbf x}\) 是它的垂足。

    拖动右图里的三个点(或者说,在左图里移动 \(\mathbf b\)),再用滑块试别的 \((m, c)\)。

    The axes of FIG 1 are \((t, y)\): each data point is a point in the plane (“model space”). Now look at the same problem in another space: stack all the measurements into one vector \(\mathbf b\), one coordinate axis per measurement. The whole data set becomes a single point. To be able to draw it, take just three points fixed at \(t = -1, 0, 1\); then \(\mathbf b = (y_1, y_2, y_3)\) is a point in 3D.

    The two columns of \(A\) are \(\mathbf a_1 = (-1, 0, 1)\) (the values of \(t\)) and \(\mathbf a_2 = (1, 1, 1)\) (the constant 1). \(A\mathbf x = m\,\mathbf a_1 + c\,\mathbf a_2\) is a linear combination of them, so the data “that some line explains exactly” form precisely the plane spanned by the two columns (the column space). Real data \(\mathbf b\) is usually off the plane, and the nearest point \(A\hat{\mathbf x}\) is its foot of the perpendicular.

    Drag the three points in the right panel (equivalently, move \(\mathbf b\) on the left), and use the sliders to try other \((m, c)\).

    FIG 2数据空间:b、列空间与垂足Data space: b, the column space and the foot
    数据空间 ℝ³(拖动旋转)data space ℝ³ (drag to orbit)
    同一组数据,模型空间(拖动三个点)same data, model space (drag the points)
    琥珀色箭头 \(\mathbf b\);青色平面是列空间;紫色 \(A\hat{\mathbf x}\) 是垂足,它由红色 \(\hat m\,\mathbf a_1\) 和绿色 \(\hat c\,\mathbf a_2\) 两步组合而成;红色虚线是残差 \(\mathbf r\),和平面之间有直角标记。蓝点是你用滑块选的 \(A\mathbf x\),蓝色虚线的长度平方就是那条直线的 SSE。勾股定理:\(\|\mathbf b - A\mathbf x\|^2 = \|\mathbf r\|^2 + \|A\hat{\mathbf x} - A\mathbf x\|^2\),所以垂足一定最近。 The amber arrow is \(\mathbf b\); the cyan plane is the column space; the purple \(A\hat{\mathbf x}\) is the foot, built in two steps from the red \(\hat m\,\mathbf a_1\) and the green \(\hat c\,\mathbf a_2\); the dashed red segment is the residual \(\mathbf r\), with a right-angle mark at the plane. The blue dot is the \(A\mathbf x\) you chose with the sliders, and the squared length of the dashed blue segment is that line's SSE. Pythagoras: \(\|\mathbf b - A\mathbf x\|^2 = \|\mathbf r\|^2 + \|A\hat{\mathbf x} - A\mathbf x\|^2\), so the foot must be the nearest point.
    试一试Try this
    • 拖动三个点,让它们共线(\(\|\mathbf r\| < 0.08\))。这时 \(\mathbf b\) 在 3D 图里躺在哪里?残差去哪了?Drag the three points until they are collinear (\(\|\mathbf r\| < 0.08\)). Where does \(\mathbf b\) sit in the 3D view now? What happened to the residual?
    • 用滑块把蓝点移到离 \(\mathbf b\) 最近的位置,也就是紫色的垂足(\(\|A\hat{\mathbf x}-A\mathbf x\| < 0.12\))。需要有一定的残差(\(\|\mathbf r\|^2 > 0.3\))才算数。Use the sliders to move the blue point to the spot nearest \(\mathbf b\), the purple foot (\(\|A\hat{\mathbf x}-A\mathbf x\| < 0.12\)). The data needs a real residual (\(\|\mathbf r\|^2 > 0.3\)) for this to count.

    垂直这件事可以写成方程。残差 \(\mathbf r = \mathbf b - A\hat{\mathbf x}\) 垂直于平面,就是同时垂直于两列:\(\mathbf a_1^\top\mathbf r = 0\),\(\mathbf a_2^\top\mathbf r = 0\)。把两个条件叠起来就是 \(A^\top(\mathbf b - A\hat{\mathbf x}) = \mathbf 0\),即正规方程 (normal equations)

    Perpendicularity can be written as an equation. The residual \(\mathbf r = \mathbf b - A\hat{\mathbf x}\) is perpendicular to the plane, which means perpendicular to both columns: \(\mathbf a_1^\top\mathbf r = 0\) and \(\mathbf a_2^\top\mathbf r = 0\). Stack the two conditions and you get \(A^\top(\mathbf b - A\hat{\mathbf x}) = \mathbf 0\), the normal equations

    $$ A^\top A\,\hat{\mathbf x} = A^\top\mathbf b \quad\Longrightarrow\quad \underbrace{\begin{bmatrix} 2 & 0 \\ 0 & 3 \end{bmatrix}}_{A^\top A}\begin{bmatrix}\hat m\\ \hat c\end{bmatrix} = \underbrace{\begin{bmatrix} y_3 - y_1 \\ y_1 + y_2 + y_3 \end{bmatrix}}_{A^\top\mathbf b} $$ $$ \Longrightarrow\quad \hat m = \frac{y_3 - y_1}{2},\qquad \hat c = \frac{y_1 + y_2 + y_3}{3}. $$

    这里两列恰好正交(\(\mathbf a_1\cdot\mathbf a_2 = 0\)),所以 \(A^\top A\) 是对角阵:最优斜率只看两端的点,最优截距就是三个值的平均。一般情形下 \(A^\top A\) 不是对角的,但结论一样:\(\hat{\mathbf x} = (A^\top A)^{-1}A^\top\mathbf b\)。把它乘回 \(A\),就得到投影矩阵:

    The two columns happen to be orthogonal here (\(\mathbf a_1\cdot\mathbf a_2 = 0\)), so \(A^\top A\) is diagonal: the best slope looks only at the two end points, and the best intercept is just the average of the three values. In general \(A^\top A\) is not diagonal, but the conclusion is the same: \(\hat{\mathbf x} = (A^\top A)^{-1}A^\top\mathbf b\). Multiply by \(A\) and you get the projection matrix:

    $$ A\hat{\mathbf x} = P\mathbf b,\qquad P = A(A^\top A)^{-1}A^\top = \frac16\begin{bmatrix} 5 & 2 & -1 \\ 2 & 2 & 2 \\ -1 & 2 & 5 \end{bmatrix}. $$

    它正是 1.9 节的那个 \(P\):\(P^\top = P\),\(P^2 = P\),把任何数据向量压到列空间里。下面把 FIG 2 的数代进去检验:当 \(\mathbf b = (1, 0, 2)\) 时,\(P\mathbf b = (0.5, 1, 1.5)\),也就是直线 \(y = 0.5t + 1\) 在三个位置上的取值;残差 \((0.5, -1, 0.5)\) 的平方和 1.5 就是这组数据最小的 SSE。

    It is exactly the \(P\) of Lesson 1.9: \(P^\top = P\), \(P^2 = P\), and it squashes any data vector into the column space. Plug in the numbers of FIG 2 to check: for \(\mathbf b = (1, 0, 2)\) we get \(P\mathbf b = (0.5, 1, 1.5)\), the values of the line \(y = 0.5t + 1\) at the three positions; the residual \((0.5, -1, 0.5)\) has squared length 1.5, the smallest possible SSE for this data.

    5第三种视角:方程不够,挑最短的解A third view: too few equations, pick the shortest solution

    现在反过来:只有一个方程,却有两个未知数,\(a_1x_1 + a_2x_2 = \beta\),也就是 \(A = [\,a_1\;\;a_2\,]\),\(b = \beta\)。在 \(x_1x_2\) 平面上,所有解排成一条直线。按 1.6 节,这条线是一个特解加上零空间:\(\mathbf x = \mathbf x_p + \mathbf n\),\(\mathbf n\in\operatorname{null}(A)\)。

    该选哪一个?一个自然的规则是选离原点最近的,也就是范数最小的。如果 \(x_1, x_2\) 是两个关节的速度,这就是“让关节总共动得最少”。几何上,离原点最近的点是原点向解线作垂线的垂足,所以它垂直于零空间方向。零空间正是与每一行都垂直的向量(\(A\mathbf n = \mathbf 0\) 就是说每一行与 \(\mathbf n\) 的点积为 0),所以垂直于零空间的方向就是各行张成的方向,叫作 \(A\) 的行空间(这里就是行向量 \((a_1, a_2)\) 的方向)。最短解一定落在行空间里。这个点就是 \(A^{+}\mathbf b\)。

    拖动青色的行向量 \((a_1, a_2)\) 的箭头尖端,用滑块改 \(\beta\),再把琥珀色的点沿解线滑动,看看有没有比紫色点更短的解。

    Now the reverse: just one equation for two unknowns, \(a_1x_1 + a_2x_2 = \beta\), i.e. \(A = [\,a_1\;\;a_2\,]\), \(b = \beta\). In the \(x_1x_2\) plane the solutions form a line. By Lesson 1.6 that line is a particular solution plus the null space: \(\mathbf x = \mathbf x_p + \mathbf n\) with \(\mathbf n\in\operatorname{null}(A)\).

    Which one should we pick? A natural rule is the one closest to the origin, the one of minimum norm. If \(x_1, x_2\) are two joint velocities, this means “move the joints as little as possible in total”. Geometrically, the closest point is the foot of the perpendicular dropped from the origin onto the solution line, so it is perpendicular to the null-space direction. The null space is exactly the set of vectors perpendicular to every row (\(A\mathbf n = \mathbf 0\) says each row has zero dot product with \(\mathbf n\)), so the directions perpendicular to it are those spanned by the rows: the row space of \(A\) (here, the direction of the row vector \((a_1, a_2)\)). The shortest solution must lie in the row space. That point is \(A^{+}\mathbf b\).

    Drag the tip of the cyan row-vector arrow \((a_1, a_2)\), change \(\beta\) with the slider, and slide the amber point along the solution line to see whether any solution beats the purple one.

    FIG 3一个方程,两个未知数:最小范数解One equation, two unknowns: the minimum-norm solution
    解空间 (x₁, x₂)solution space (x₁, x₂)
    琥珀色直线是所有解 \(a_1x_1 + a_2x_2 = \beta\);青色箭头是行向量 \((a_1, a_2)\),青色虚线是零空间(和行向量垂直)。紫色点是 \(\mathbf x^{+} = A^{+}\mathbf b\),紫色虚线圆以它的长度为半径,刚好与解线相切;琥珀色点 \(\mathbf q\) 是解线上任意一点,琥珀色圆更大。勾股定理:\(\|\mathbf q\|^2 = \|\mathbf x^{+}\|^2 + s^2\),其中 \(s\) 是沿零空间走的距离。 The amber line holds all solutions of \(a_1x_1 + a_2x_2 = \beta\); the cyan arrow is the row vector \((a_1, a_2)\) and the dashed cyan line is the null space (perpendicular to the row). The purple point is \(\mathbf x^{+} = A^{+}\mathbf b\); the dashed purple circle has its length as radius and just touches the solution line. The amber point \(\mathbf q\) is any point on the line, with a larger amber circle. Pythagoras: \(\|\mathbf q\|^2 = \|\mathbf x^{+}\|^2 + s^2\), where \(s\) is the distance walked along the null space.
    试一试Try this
    • 把琥珀色点沿解线拖到最短的位置(\(|s| < 0.08\))。它和紫色点重合时,圆会变成什么样?Drag the amber point along the solution line to the shortest position (\(|s| < 0.08\)). What do the two circles do when it meets the purple point?
    • 拖动行向量,让最小范数解落在 \(x_1\) 轴上(\(|x_2^{+}| < 0.06\))。这时 \(a_2\) 是多少?为什么?Drag the row vector so that the minimum-norm solution lands on the \(x_1\)-axis (\(|x_2^{+}| < 0.06\)). What is \(a_2\) then, and why?

    统一:奇异值分解一次说清楚Unifying: the SVD says it all at once

    最小二乘(方程太多)和最小范数(方程太少)看上去是两个问题,SVD 能一起解决。把 \(A = U\Sigma V^\top\) 代进 \(A\mathbf x = \mathbf b\),并换坐标:\(\mathbf y = V^\top\mathbf x\),\(\mathbf c = U^\top\mathbf b\)。因为 \(U\)、\(V\) 是正交矩阵(不改变长度),问题就拆成了互不相干的标量方程:

    \(\sigma_i\,y_i = c_i,\qquad i = 1,\dots,r\),\(\qquad 0\cdot y_i = c_i,\qquad i > r\)。

    • \(\sigma_i > 0\) 的方向:唯一合理的选择是 \(y_i = c_i/\sigma_i\),这就是“把每个方向除以 \(\sigma_i\)”。
    • \(\sigma_i = 0\)(\(i > r\))的方向,方程 \(0\cdot y_i = c_i\) 有两件事:\(c_i\) 无法被满足,这是不可避免的残差,也是 \(\mathbf b\) 垂直于列空间的那一部分(最小二乘:不管它)。同时 \(y_i\) 取什么都不影响残差,这是零空间方向,取 0 让范数最短(最小范数)。

    合起来就是 \(\mathbf x^{+} = V\Sigma^{+}U^\top\mathbf b\):非零奇异值取倒数,零奇异值忽略。同一个公式,既处理方程太多,也处理方程太少,还包括两者混合的秩亏情形。

    Least squares (too many equations) and minimum norm (too few) look like two problems, and the SVD solves both at once. Substitute \(A = U\Sigma V^\top\) into \(A\mathbf x = \mathbf b\) and change coordinates: \(\mathbf y = V^\top\mathbf x\), \(\mathbf c = U^\top\mathbf b\). Because \(U\) and \(V\) are orthogonal (they keep lengths), the problem falls apart into independent scalar equations:

    \(\sigma_i\,y_i = c_i,\qquad i = 1,\dots,r\), \(\qquad 0\cdot y_i = c_i,\qquad i > r\).

    • Directions with \(\sigma_i > 0\): the only sensible choice is \(y_i = c_i/\sigma_i\), which is “divide each direction by \(\sigma_i\)”.
    • Directions with \(\sigma_i = 0\) (\(i > r\)): the equation \(0\cdot y_i = c_i\) says two things. \(c_i\) cannot be satisfied, an unavoidable residual, the part of \(\mathbf b\) perpendicular to the column space (least squares: leave it alone). And \(y_i\) does not affect the residual at all, a null-space direction, so set it to 0 to keep the norm smallest (minimum norm).

    Together that is \(\mathbf x^{+} = V\Sigma^{+}U^\top\mathbf b\): invert the nonzero singular values and ignore the zero ones. One formula covers too many equations, too few, and the rank-deficient mixture of both.

    \(A\) 的形状与秩Shape and rank of \(A\) 问题Problem \(A^{+}\)
    瘦高,列满秩(\(m > n\),\(r = n\))tall, full column rank (\(m > n\), \(r = n\)) 超定:唯一的最小二乘解overdetermined: unique least-squares solution \((A^\top A)^{-1}A^\top\)
    矮胖,行满秩(\(m < n\),\(r = m\))wide, full row rank (\(m < n\), \(r = m\)) 欠定:最小范数解underdetermined: minimum-norm solution \(A^\top(AA^\top)^{-1}\)
    方阵,可逆(\(r = m = n\))square, invertible (\(r = m = n\)) 恰定:普通的逆exactly determined: the ordinary inverse \(A^{-1}\)
    秩亏(\(r < \min(m, n)\))rank-deficient (\(r < \min(m, n)\)) 两者混合:在所有最小二乘解里取范数最小的a mixture: the minimum-norm one among all least-squares solutions \(V\Sigma^{+}U^\top\)
    冗余机械臂:伪逆之外还能加一项Redundant arms: one more term beyond the pseudo-inverse

    对于 \(J\dot{\mathbf q} = \dot{\mathbf x}\) 里的冗余机械臂(\(m < n\)),所有解是 \(\dot{\mathbf q} = J^{+}\dot{\mathbf x} + (I - J^{+}J)\,\dot{\mathbf q}_0\)。第一项是最小范数的特解,第二项把任意速度 \(\dot{\mathbf q}_0\) 投影到 \(J\) 的零空间上,也就是不影响末端的“自运动”(1.6 节)。可以用它同时去避障、避开关节极限。\(I - J^{+}J\) 是到零空间的投影矩阵 (1.9 节)。

    For a redundant arm in \(J\dot{\mathbf q} = \dot{\mathbf x}\) (\(m < n\)), all solutions are \(\dot{\mathbf q} = J^{+}\dot{\mathbf x} + (I - J^{+}J)\,\dot{\mathbf q}_0\). The first term is the minimum-norm particular solution; the second projects an arbitrary velocity \(\dot{\mathbf q}_0\) onto the null space of \(J\), the “self-motion” that leaves the end effector alone (Lesson 1.6). You can spend it on avoiding obstacles or joint limits at the same time. \(I - J^{+}J\) is the projection onto the null space (Lesson 1.9).

    6再看数学The math

    问题The problem

    给定 \(A\in\R^{m\times n}\) 和 \(\mathbf b\in\R^m\),求

    Given \(A\in\R^{m\times n}\) and \(\mathbf b\in\R^m\), find

    $$ \hat{\mathbf x} = \arg\min_{\mathbf x}\ \|A\mathbf x - \mathbf b\|^2 = \arg\min_{\mathbf x}\ \sum_{i=1}^{m}\big(\mathbf a_i^\top\mathbf x - b_i\big)^2, $$

    其中 \(\mathbf a_i^\top\) 是 \(A\) 的第 \(i\) 行:每一行是一次测量(一个方程)。

    where \(\mathbf a_i^\top\) is row \(i\) of \(A\): each row is one measurement (one equation).

    推导一:垂直与正规方程Derivation 1: perpendicularity and the normal equations

    $$ \hl{r}{\mathbf r} = \hl{b}{\cv{\mathbf b}} - \hl{Axh}{\cw{A\hat{\mathbf x}}} \;\perp\; \hl{col}{\operatorname{col}(A)} \iff A^\top\big(\mathbf b - A\hat{\mathbf x}\big) = \mathbf 0 \iff \boxed{\,A^\top A\,\hat{\mathbf x} = A^\top\mathbf b\,} $$

    “垂直于列空间”等价于“垂直于每一列”,也就是 \(\mathbf a_j^\top\mathbf r = 0\)(\(j = 1,\dots,n\));把 \(n\) 个条件叠成一个矩阵就是 \(A^\top\mathbf r = \mathbf 0\)。

    “Perpendicular to the column space” means “perpendicular to every column”, i.e. \(\mathbf a_j^\top\mathbf r = 0\) for \(j = 1,\dots,n\); stacking the \(n\) conditions into a matrix gives \(A^\top\mathbf r = \mathbf 0\).

    提示:把鼠标悬停在公式中的 \(\mathbf b\)、\(A\hat{\mathbf x}\)、\(\mathbf r\)、列空间上,FIG 2 会加粗对应的对象。Tip: hover \(\mathbf b\), \(A\hat{\mathbf x}\), \(\mathbf r\) or the column space in the formula; FIG 2 thickens the matching object.

    推导二:令梯度为零Derivation 2: set the gradient to zero

    展开目标函数:\(f(\mathbf x) = \|A\mathbf x - \mathbf b\|^2 = \mathbf x^\top A^\top A\,\mathbf x - 2\,\mathbf b^\top A\mathbf x + \mathbf b^\top\mathbf b\)。它是 \(\mathbf x\) 的二次函数,梯度为 \(\nabla f = 2A^\top A\,\mathbf x - 2A^\top\mathbf b\)。令梯度为零,得到同样的正规方程。这是第 3 章(梯度)和第 6 章(优化)的预告:最小二乘是最简单的优化问题,因为目标是凸二次函数,驻点就是最小值点;\(A\) 列满秩时驻点唯一,否则最小值点构成一整片(见下文)。

    Expand the objective: \(f(\mathbf x) = \|A\mathbf x - \mathbf b\|^2 = \mathbf x^\top A^\top A\,\mathbf x - 2\,\mathbf b^\top A\mathbf x + \mathbf b^\top\mathbf b\). It is quadratic in \(\mathbf x\), with gradient \(\nabla f = 2A^\top A\,\mathbf x - 2A^\top\mathbf b\). Setting the gradient to zero gives the same normal equations. This previews Chapter 3 (gradients) and Chapter 6 (optimisation): least squares is the simplest optimisation problem, because the objective is a convex quadratic, so every stationary point is a minimum; with full column rank there is exactly one, otherwise the minimisers form a whole affine set (see below).

    唯一性与投影矩阵Uniqueness and the projection matrix

    如果 \(A\) 的列线性无关(列满秩,零空间只有 \(\mathbf 0\)),\(A^\top A\) 可逆,解唯一:

    If the columns of \(A\) are linearly independent (full column rank, null space \(\{\mathbf 0\}\)), \(A^\top A\) is invertible and the solution is unique:

    $$ \hat{\mathbf x} = (A^\top A)^{-1}A^\top\mathbf b,\qquad A\hat{\mathbf x} = P\mathbf b,\quad P = A(A^\top A)^{-1}A^\top,\quad P^\top = P,\; P^2 = P. $$

    如果列线性相关,\(A^\top A\) 不可逆,最小二乘解不唯一:\(\hat{\mathbf x} + \mathbf n\)(\(\mathbf n\in\operatorname{null}(A)\))都一样好。这时要再加一条规则,比如下面的最小范数。

    If the columns are dependent, \(A^\top A\) is singular and the least-squares solution is not unique: \(\hat{\mathbf x} + \mathbf n\) (\(\mathbf n\in\operatorname{null}(A)\)) is just as good. Then we need one more rule, such as minimum norm below.

    最小范数解(方程不够)The minimum-norm solution (too few equations)

    如果 \(A\) 的行线性无关(行满秩,\(m < n\)),\(A\mathbf x = \mathbf b\) 总有解,且有无穷多个。其中范数最小的是

    If the rows of \(A\) are linearly independent (full row rank, \(m < n\)), \(A\mathbf x = \mathbf b\) is always solvable, with infinitely many solutions. The one of smallest norm is

    $$ \hl{xp}{\cw{\mathbf x^{+}}} = A^\top(AA^\top)^{-1}\mathbf b $$ $$ \text{every solution is } \mathbf x = \mathbf x^{+} + \mathbf n,\ \ \mathbf n\in\hl{ns}{\operatorname{null}(A)},\qquad \|\mathbf x\|^2 = \|\mathbf x^{+}\|^2 + \|\mathbf n\|^2. $$

    理由:\(\mathbf x^{+} = A^\top\mathbf w\) 是行的线性组合,在行空间里;\(A\mathbf x^{+} = AA^\top(AA^\top)^{-1}\mathbf b = \mathbf b\),所以它是解。零空间里的 \(\mathbf n\) 满足 \(A\mathbf n = \mathbf 0\),即与每一行都垂直,所以行空间和零空间互相垂直(第 5 节开头已说明),勾股定理成立,任何别的解都更长。这就是 FIG 3 里的直角。

    Why: \(\mathbf x^{+} = A^\top\mathbf w\) is a combination of rows, so it lies in the row space; and \(A\mathbf x^{+} = AA^\top(AA^\top)^{-1}\mathbf b = \mathbf b\), so it is a solution. A null-space vector \(\mathbf n\) has \(A\mathbf n = \mathbf 0\), i.e. it is perpendicular to every row, so the row space and null space are perpendicular (as noted at the start of Section 5) and Pythagoras applies and every other solution is longer. That is the right angle in FIG 3.

    提示:悬停 \(\mathbf x^{+}\) 或零空间,FIG 3 会加粗对应的点或直线。Tip: hover \(\mathbf x^{+}\) or the null space; FIG 3 thickens the matching point or line.

    Moore–Penrose 伪逆The Moore–Penrose pseudo-inverse

    $$ A = U\Sigma V^\top \;\Longrightarrow\; A^{+} = V\,\Sigma^{+}\,U^\top,\qquad \Sigma^{+} = \operatorname{diag}\!\Big(\tfrac{1}{\sigma_1},\dots,\tfrac{1}{\sigma_r},0,\dots,0\Big)^{\!\top} $$ $$ \mathbf x^{+} = A^{+}\mathbf b = \sum_{i=1}^{r}\frac{\mathbf u_i^\top\mathbf b}{\sigma_i}\,\mathbf v_i. $$

    \(\mathbf x^{+} = A^{+}\mathbf b\) 是所有使 \(\|A\mathbf x - \mathbf b\|\) 最小的 \(\mathbf x\) 里范数最小的那个(推导见第 5 节的拆分)。几个常用事实:

    • \(AA^{+}\) 是到列空间的投影,\(A^{+}A\) 是到行空间的投影。列满秩时 \(AA^{+} = P\)。
    • 四个 Penrose 条件:\(AA^{+}A = A\),\(A^{+}AA^{+} = A^{+}\),\((AA^{+})^\top = AA^{+}\),\((A^{+}A)^\top = A^{+}A\)。满足它们的 \(A^{+}\) 是唯一的。
    • 列满秩时,\((A^\top A)^{-1}A^\top = V\Sigma^{-2}V^\top\cdot V\Sigma U^\top = V\Sigma^{-1}U^\top\),与 SVD 公式一致。行满秩时同理。

    \(\mathbf x^{+} = A^{+}\mathbf b\) is the smallest-norm vector among all \(\mathbf x\) that minimise \(\|A\mathbf x - \mathbf b\|\) (the derivation is the decoupling in Section 5). A few handy facts:

    • \(AA^{+}\) projects onto the column space and \(A^{+}A\) onto the row space. With full column rank, \(AA^{+} = P\).
    • The four Penrose conditions: \(AA^{+}A = A\), \(A^{+}AA^{+} = A^{+}\), \((AA^{+})^\top = AA^{+}\), \((A^{+}A)^\top = A^{+}A\). Exactly one matrix \(A^{+}\) satisfies them.
    • With full column rank, \((A^\top A)^{-1}A^\top = V\Sigma^{-2}V^\top\cdot V\Sigma U^\top = V\Sigma^{-1}U^\top\), matching the SVD formula. Likewise for full row rank.

    阻尼最小二乘 (DLS)Damped least squares (DLS)

    小奇异值是麻烦的来源:\(1/\sigma_i\) 把噪声放大。阻尼的想法是同时惩罚解的大小,让“准确”和“别太大”折中:

    Small singular values are the source of trouble: \(1/\sigma_i\) amplifies noise. Damping penalises the size of the solution too, trading accuracy against size:

    $$ \min_{\mathbf x}\ \|A\mathbf x - \mathbf b\|^2 + \lambda^2\|\mathbf x\|^2 \;\Longrightarrow\; \mathbf x_\lambda = (A^\top A + \lambda^2 I)^{-1}A^\top\mathbf b = \sum_{i}\hl{dls}{\frac{\sigma_i}{\sigma_i^2 + \lambda^2}}\,(\mathbf u_i^\top\mathbf b)\,\mathbf v_i. $$

    第一个等号的推导很简洁:这等价于一个普通的最小二乘问题,只是在方程后面多叠了 \(n\) 个方程 \(\lambda\mathbf x\approx\mathbf 0\):\(\begin{bmatrix}A\\ \lambda I\end{bmatrix}\mathbf x\approx\begin{bmatrix}\mathbf b\\ \mathbf 0\end{bmatrix}\)。对它写正规方程,立刻得到 \((A^\top A + \lambda^2 I)\mathbf x = A^\top\mathbf b\)。\(A^\top A + \lambda^2 I\) 的特征值都不小于 \(\lambda^2\),所以永远可逆。统计里这叫岭回归 (ridge),反问题里叫 Tikhonov 正则化。

    在 SVD 坐标里,每个方向的增益从 \(1/\sigma\) 换成了 \(\sigma/(\sigma^2+\lambda^2)\)(悬停公式里的这一项):

    • \(\sigma\gg\lambda\):增益 \(\approx 1/\sigma\),几乎不受影响。
    • \(\sigma\ll\lambda\):增益 \(\approx\sigma/\lambda^2\to 0\),微弱的方向被安静地放弃,不再放大噪声。
    • 增益最大值是 \(1/(2\lambda)\)(在 \(\sigma = \lambda\) 处),所以 \(\lambda\) 直接决定了最坏情况下的放大倍数。

    代价是不再精确:沿弱方向的目标只完成了一部分,残差变大。\(\lambda\) 越大,解越小、残差越大;\(\lambda\to 0\) 回到伪逆。把 \(A = J\)、\(\mathbf x = \dot{\mathbf q}\)、\(\mathbf b = \dot{\mathbf x}\) 代入,就是逆运动学里的 DLS。还有一个等价的写法 \(\dot{\mathbf q} = J^\top(JJ^\top + \lambda^2 I)^{-1}\dot{\mathbf x}\),当任务维数 \(m\) 比关节数 \(n\) 小时,要求逆的矩阵更小。

    The first equality has a neat derivation: it is an ordinary least-squares problem with \(n\) extra equations \(\lambda\mathbf x\approx\mathbf 0\) stacked underneath: \(\begin{bmatrix}A\\ \lambda I\end{bmatrix}\mathbf x\approx\begin{bmatrix}\mathbf b\\ \mathbf 0\end{bmatrix}\). Write its normal equations and you immediately get \((A^\top A + \lambda^2 I)\mathbf x = A^\top\mathbf b\). The eigenvalues of \(A^\top A + \lambda^2 I\) are all at least \(\lambda^2\), so it is always invertible. Statisticians call this ridge regression; in inverse problems it is Tikhonov regularisation.

    In SVD coordinates the gain of each direction changes from \(1/\sigma\) to \(\sigma/(\sigma^2+\lambda^2)\) (hover that term in the formula):

    • \(\sigma\gg\lambda\): gain \(\approx 1/\sigma\), almost untouched.
    • \(\sigma\ll\lambda\): gain \(\approx\sigma/\lambda^2\to 0\); weak directions are quietly given up instead of amplifying noise.
    • The largest gain is \(1/(2\lambda)\) (at \(\sigma = \lambda\)), so \(\lambda\) directly caps the worst-case amplification.

    The price is exactness: the goal along weak directions is only partly achieved and the residual grows. The larger \(\lambda\), the smaller the solution and the larger the residual; \(\lambda\to 0\) gives back the pseudo-inverse. Put \(A = J\), \(\mathbf x = \dot{\mathbf q}\), \(\mathbf b = \dot{\mathbf x}\) and you have DLS for inverse kinematics. An equivalent form is \(\dot{\mathbf q} = J^\top(JJ^\top + \lambda^2 I)^{-1}\dot{\mathbf x}\), where the matrix to invert is smaller when the task dimension \(m\) is below the number of joints \(n\).

    FIG 4阻尼:增益曲线与准确度–大小折中Damping: gain curves and the accuracy–size trade-off
    设 \(J = U\,\mathrm{diag}(\sigma_1,\sigma_2)\,V^\top\),\(\sigma_1 = 1\),\(\sigma_2\) 是可调的“弱方向”,目标末端速度 \(\dot{\mathbf x}\) 在 \(\mathbf u_1,\mathbf u_2\) 上的分量是 \((0.8,\,0.6)\)。左图:每个方向的增益。红色虚线是 \(1/\sigma\),紫色是 \(\sigma/(\sigma^2+\lambda^2)\);竖线标出两个奇异值(琥珀色 \(\sigma_1\),青色 \(\sigma_2\)),圆点是它们此刻的增益。右图:随着 \(\lambda\) 从 0 增大,关节速度 \(\|\dot{\mathbf q}\|\)(纵轴)和跟踪误差 \(\|J\dot{\mathbf q}-\dot{\mathbf x}\|\)(横轴)怎么变化;绿色方框是“速度 ≤ 4 且误差 ≤ 0.5”的可接受区。 Let \(J = U\,\mathrm{diag}(\sigma_1,\sigma_2)\,V^\top\) with \(\sigma_1 = 1\) and an adjustable “weak direction” \(\sigma_2\); the desired end-effector velocity \(\dot{\mathbf x}\) has components \((0.8,\,0.6)\) along \(\mathbf u_1,\mathbf u_2\). Left: the gain of each direction. The dashed red curve is \(1/\sigma\), the purple one is \(\sigma/(\sigma^2+\lambda^2)\); vertical lines mark the two singular values (amber \(\sigma_1\), cyan \(\sigma_2\)) and the dots show their gains right now. Right: as \(\lambda\) grows from 0, how the joint speed \(\|\dot{\mathbf q}\|\) (vertical) and the tracking error \(\|J\dot{\mathbf q}-\dot{\mathbf x}\|\) (horizontal) change; the green box is the acceptable zone “speed ≤ 4 and error ≤ 0.5”.
    试一试Try this
    • 保持 \(\sigma_2\) 在 0.04 到 0.08 之间,调 \(\lambda\),让关节速度 \(\|\dot{\mathbf q}\|\le 4\) 同时跟踪误差 \(\le 0.5\)。\(\lambda = 0\) 时关节速度是多少?Keep \(\sigma_2\) between 0.04 and 0.08 and tune \(\lambda\) so that the joint speed \(\|\dot{\mathbf q}\|\le 4\) while the tracking error is \(\le 0.5\). What is the joint speed at \(\lambda = 0\)?

    加权最小二乘Weighted least squares

    前面默认每个测量一样可靠。如果第 \(i\) 个测量的噪声标准差是 \(s_i\),就该让可靠的测量说话更响:最小化 \((A\mathbf x - \mathbf b)^\top W(A\mathbf x - \mathbf b)\),\(W = \operatorname{diag}(1/s_i^2)\),解为

    So far every measurement was trusted equally. If measurement \(i\) has noise standard deviation \(s_i\), reliable measurements should speak louder: minimise \((A\mathbf x - \mathbf b)^\top W(A\mathbf x - \mathbf b)\) with \(W = \operatorname{diag}(1/s_i^2)\), which gives

    $$ \hat{\mathbf x} = (A^\top W A)^{-1}A^\top W\,\mathbf b. $$

    做法等价于先把每一行和每个 \(b_i\) 除以 \(s_i\)(“白化”),再解普通的最小二乘。更一般地,\(W\) 取噪声协方差的逆,这正是 4.6 节的最大似然和 4.8 节卡尔曼滤波里反复出现的形式。

    This is the same as dividing each row and each \(b_i\) by \(s_i\) (“whitening”) and then solving ordinary least squares. More generally \(W\) is the inverse noise covariance, the form that keeps returning in maximum likelihood (Lesson 4.6) and the Kalman filter (Lesson 4.8).

    7机器人中的它:穿过奇异位形的速度级逆运动学In robotics: velocity-level IK through a singularity

    让平面两连杆机械臂(和 1.12 节的同一台,\(\ell_1 = 1.2\),\(\ell_2 \approx 0.85\))的末端去追一个目标点。每一帧:

    1. 想要的末端速度 \(\dot{\mathbf x} = K(\mathbf x_{\text{target}} - \mathbf x)\)(朝目标走,限制最大速度)。
    2. 解 \(J(\mathbf q)\,\dot{\mathbf q} = \dot{\mathbf x}\),得到关节速度 \(\dot{\mathbf q}\)。解法有三种:逆 \(J^{-1}\);伪逆,把 \(\sigma_i<0.08\) 的方向直接丢弃;阻尼 DLS,增益 \(\sigma/(\sigma^2+\lambda^2)\)。
    3. 真实电机有速度上限,所以每个关节速度被截到 \(\pm 6\) rad/s,然后更新 \(\mathbf q \leftarrow \mathbf q + \dot{\mathbf q}\,\Delta t\)。

    拖动紫色目标点。用快捷按钮把目标放到工作空间的边缘之外,或者放进靠近底座的“洞”里(外圈和内圈都是奇异位形,1.12 节),换着三种解法看。右侧的条形图是解法要求的关节速度(截断之前),虚线是电机上限;下方曲线是过去 4 秒里的最大需求。

    Let the end effector of a planar two-link arm (the same one as in Lesson 1.12, \(\ell_1 = 1.2\), \(\ell_2 \approx 0.85\)) chase a target point. Each frame:

    1. The desired end-effector velocity is \(\dot{\mathbf x} = K(\mathbf x_{\text{target}} - \mathbf x)\) (head for the target, with a speed cap).
    2. Solve \(J(\mathbf q)\,\dot{\mathbf q} = \dot{\mathbf x}\) for the joint velocity \(\dot{\mathbf q}\), in one of three ways: the inverse \(J^{-1}\); the pseudo-inverse, which simply drops directions with \(\sigma_i<0.08\); or damped DLS with gain \(\sigma/(\sigma^2+\lambda^2)\).
    3. Real motors have a speed limit, so each joint speed is clipped to \(\pm 6\) rad/s before the update \(\mathbf q \leftarrow \mathbf q + \dot{\mathbf q}\,\Delta t\).

    Drag the purple target. Use the shortcut buttons to put it just outside the workspace edge, or into the “hole” near the base (both the outer and inner rings are singular, Lesson 1.12), and switch between the three solvers. The bars at the right show the joint speeds the solver demands (before clipping), the dashed line is the motor limit, and the strip below plots the largest demand over the last 4 seconds.

    FIG 5追目标:逆、伪逆还是阻尼?Chasing a target: inverse, pseudo-inverse or damped?
    要求的最大关节速度(rad/s),过去 4 秒largest demanded joint speed (rad/s), last 4 s
    琥珀色椭圆是当前姿态的可操作度椭圆(1.12 节)。末端的紫色短箭头是想要的速度 \(\dot{\mathbf x}\),青色短箭头是截断之后实际得到的速度 \(J\dot{\mathbf q}\):两者方向不同,说明关节速度被截断后“跑偏”了。靠近奇异位形时 \(\sigma_2\to 0\),\(J^{-1}\) 要求的速度趋于无穷,截断后手臂左右抖动;伪逆的增益在阈值附近从 \(1/0.08 = 12.5\) 直接跳到 0:跳之前要求的速度仍会冲过电机上限,跳之后沿弱方向的运动戛然而止,而不是逐渐减速;阻尼解法的增益连续变化,手臂平滑地伸向极限。 The amber ellipse is the manipulability ellipse of the current pose (Lesson 1.12). The short purple arrow at the hand is the desired velocity \(\dot{\mathbf x}\); the short cyan one is the velocity \(J\dot{\mathbf q}\) actually obtained after clipping: when they point in different directions, clipping has thrown the motion off course. Near a singularity \(\sigma_2\to 0\) and \(J^{-1}\) demands unbounded speeds, so after clipping the arm jitters; the pseudo-inverse's gain jumps from \(1/0.08 = 12.5\) straight to 0 at the threshold: just before the jump the demanded speed still overshoots the motor limit, and after it the motion along the weak direction stops abruptly instead of tapering off; the damped gain changes continuously and the arm glides toward the limit.
    试一试Try this
    • 选阻尼解法,把目标拖到外圈附近(离底座不超过约 2.35,“边缘外 (2.1)”按钮就可以),让手臂平滑地伸到几乎伸直(末端到底座的距离 \(\ge 1.97\))并停稳,过去 4 秒内要求的关节速度没有超过 6 rad/s(看下方曲线)。Pick the damped solver and drag the target to the outer ring (no farther than about 2.35 from the base; the “just beyond (2.1)” button works) so that the arm reaches almost full stretch smoothly (hand-to-base distance \(\ge 1.97\)) and settles, with the demanded joint speed never exceeding 6 rad/s during the last 4 seconds.
    • 选逆解法,把目标放到工作空间之外,看要求的关节速度飙到 \(\ge 30\) rad/s(超过电机上限 5 倍)。再换成阻尼,对比有多平静。Pick the plain inverse and put the target outside the workspace; watch the demanded joint speed spike to \(\ge 30\) rad/s (5 times the motor limit). Then switch to damped and compare how calm it is.

    注意一个细节:对可达的目标,逆在边缘附近也能工作,因为越靠近目标,想要的速度越小,\(\dot{\mathbf x}/\sigma_2\) 才没有爆炸。真正出问题的是目标在可达范围之外,或者目标本身在高速掠过奇异方向:此时需要的是“沿弱方向走一大步”,而弱方向的增益正是 \(1/\sigma_2\)。阻尼解法对两种情形都有效,代价是在奇异方向上反应变慢(滞后)。

    One subtlety: for a reachable target the plain inverse can still work near the edge, because the desired speed shrinks as the hand nears the target, so \(\dot{\mathbf x}/\sigma_2\) does not explode. Trouble starts when the target is out of reach, or when it sweeps quickly along the singular direction: then the demand is “a big step along the weak direction”, and the gain of that direction is exactly \(1/\sigma_2\). The damped solver copes with both, at the price of reacting more slowly along the singular direction (lag).

    真实系统里:标定就是一个大最小二乘In real systems: calibration is one big least-squares problem

    传感器的比例和零偏:测量 \(z_k = s\,u_k + o\),每次测量一行 \([\,u_k\;\;1\,]\,(s, o)^\top = z_k\)。这就是 FIG 1 的直线拟合。轮半径:每段日志有 \(d_k = r\,\varphi_k\)(位移 = 半径 × 转角),一个未知数,\(\hat r = \sum\varphi_k d_k / \sum\varphi_k^2\)。手眼标定 \(AX = XB\):旋转部分 \(R_{A_i}R_X = R_XR_{B_i}\) 可以化成两组旋转轴之间的对齐问题,用 SVD 求解;平移部分 \((R_{A_i} - I)\,\mathbf t_X = R_X\mathbf t_{B_i} - \mathbf t_{A_i}\) 对 \(\mathbf t_X\) 是线性的,把每次运动的 3 行叠起来,得到一个 \(3N\times 3\) 的最小二乘问题。必须有至少两次旋转轴不平行的运动,否则叠出来的矩阵秩亏(用 1.12 节的奇异值检查)。

    Sensor scale and offset: a measurement \(z_k = s\,u_k + o\) gives one row \([\,u_k\;\;1\,]\,(s, o)^\top = z_k\) per sample. That is exactly the line fit of FIG 1. Wheel radius: each log segment gives \(d_k = r\,\varphi_k\) (distance = radius × angle), a single unknown with \(\hat r = \sum\varphi_k d_k / \sum\varphi_k^2\). Hand–eye calibration \(AX = XB\): the rotation part \(R_{A_i}R_X = R_XR_{B_i}\) reduces to aligning two sets of rotation axes, solved with an SVD; the translation part \((R_{A_i} - I)\,\mathbf t_X = R_X\mathbf t_{B_i} - \mathbf t_{A_i}\) is linear in \(\mathbf t_X\), so stacking 3 rows per motion gives a \(3N\times 3\) least-squares problem. You need at least two motions with non-parallel rotation axes, otherwise the stacked matrix is rank-deficient (check the singular values from Lesson 1.12).

    真实系统里:怎么选 λIn real systems: choosing λ

    固定的 \(\lambda\) 在远离奇异位形时也在白白降低精度。常见做法是让 \(\lambda\) 随 \(\sigma_{\min}\) 变化:\(\sigma_{\min}\) 大于阈值 \(\epsilon\) 时 \(\lambda = 0\),小于 \(\epsilon\) 时取 \(\lambda^2 = \lambda_{\max}^2\,(1 - \sigma_{\min}^2/\epsilon^2)\),这是一种常见的自适应阻尼方案。数值逆运动学库里常带有类似的阻尼项,例如 KDL 的加权阻尼最小二乘速度求解器。

    A fixed \(\lambda\) needlessly costs accuracy even far from a singularity. A common remedy lets \(\lambda\) depend on \(\sigma_{\min}\): \(\lambda = 0\) when \(\sigma_{\min}\) exceeds a threshold \(\epsilon\), and \(\lambda^2 = \lambda_{\max}^2\,(1 - \sigma_{\min}^2/\epsilon^2)\) below it, a common adaptive-damping scheme. Numerical IK libraries often carry a similar damping term, for example KDL's weighted damped least-squares velocity solver.

    8常见误区Common pitfalls

    误区 1:直接解正规方程就行Pitfall 1: just solve the normal equations

    教材里的 \((A^\top A)^{-1}A^\top\mathbf b\) 适合推导,不适合写代码。构造 \(A^\top A\) 会把条件数平方:\(\kappa(A^\top A) = \kappa(A)^2\)。比如用时间戳 \(t = 1000, 1002,\dots,1010\) 拟合 \(y = mt + c\),\(\kappa(A)\approx 3\times10^{5}\),\(\kappa(A^\top A)\approx 9\times10^{10}\),双精度的 16 位有效数字丢掉约 11 位。实际用 QR 分解或 SVD(np.linalg.lstsq、Eigen 的 BDCSVD、colPivHouseholderQr),它们直接对 \(A\) 操作,1.12 节的“实际怎么算”说的是同一个道理。

    The textbook \((A^\top A)^{-1}A^\top\mathbf b\) is for derivations, not for code. Forming \(A^\top A\) squares the condition number: \(\kappa(A^\top A) = \kappa(A)^2\). For example, fitting \(y = mt + c\) with timestamps \(t = 1000, 1002,\dots,1010\) gives \(\kappa(A)\approx 3\times10^{5}\) and \(\kappa(A^\top A)\approx 9\times10^{10}\), eating about 11 of double precision's 16 digits. Use QR or the SVD instead (np.linalg.lstsq, Eigen's BDCSVD or colPivHouseholderQr); they work on \(A\) directly. The callout in Lesson 1.12 makes the same point.

    误区 2:最小二乘对坏数据很宽容Pitfall 2: least squares is forgiving about bad data

    恰恰相反。残差被平方,一个离群点的影响和它偏差的平方成正比,FIG 1 里加一个离群点就能把整条线拖偏。激光里的反射错误、特征匹配的误匹配都是离群点。对策是鲁棒损失(Huber、Cauchy)或者先用 RANSAC 挑出内点再拟合,分别见 6.9 节和 11.5 节。

    Quite the opposite. Residuals are squared, so an outlier's influence grows with the square of its deviation; in FIG 1 a single outlier drags the whole line. Reflection errors in lidar and wrong feature matches are outliers. The remedies are robust losses (Huber, Cauchy) or RANSAC to pick the inliers first and fit afterwards; see Lessons 6.9 and 11.5 respectively.

    误区 3:它最小化的是点到线的距离Pitfall 3: it minimises point-to-line distance

    普通最小二乘最小化的是竖直距离 \(y_i - (mt_i + c)\),默认 \(t\) 是准确的,只有 \(y\) 带噪声。如果 \(t\) 和 \(y\) 都有噪声,比如两个坐标都来自激光点,就该最小化垂直距离,这叫总体最小二乘 (total least squares, TLS),解是点云的主方向,用 1.12 节的 PCA 即可。FIG 1 里打开 TLS 线,把点拉成近乎竖直的一列,两条线差得很远;对竖直的墙,普通最小二乘还会得到斜率无穷大。

    Ordinary least squares minimises the vertical distance \(y_i - (mt_i + c)\), assuming \(t\) is exact and only \(y\) is noisy. If both \(t\) and \(y\) are noisy, say both coordinates come from lidar points, you should minimise the perpendicular distance instead. That is total least squares (TLS), whose solution is the cloud's principal direction, obtained by PCA as in Lesson 1.12. Switch on the TLS line in FIG 1 and stretch the points into a nearly vertical column: the two lines differ a lot, and for a vertical wall ordinary least squares even demands an infinite slope.

    误区 4:列的尺度无所谓Pitfall 4: column scales do not matter

    如果 \(A\) 的一列以米为单位、另一列以毫米为单位(差 1000 倍),\(\kappa(A)\) 会变大几百倍:上面的 \([\,t\;\;1\,]\),\(t\in[0,1]\) 时 \(\kappa\approx 3.7\),换成毫米后 \(\kappa\approx 1100\)。手眼标定里旋转(无量纲)和平移(米或毫米)经常混在一个系统里,也是同样的问题。对策:把每一列除以它的范数(列归一化)、把数据减去均值再拟合,求完解再换回原来的单位。

    If one column of \(A\) is in metres and another in millimetres (a factor of 1000 apart), \(\kappa(A)\) grows by a factor of several hundred: for the \([\,t\;\;1\,]\) above, \(\kappa\approx 3.7\) when \(t\in[0,1]\) but \(\kappa\approx 1100\) once \(t\) is in the thousands. Hand–eye calibration mixes rotations (unitless) and translations (metres or millimetres) in one system, with the same issue. The remedy: divide each column by its norm (column normalisation), subtract the mean before fitting, and convert the solution back to the original units afterwards.

    误区 5:阻尼系数越大越安全Pitfall 5: a larger damping factor is always safer

    \(\lambda\) 太小,奇异位形附近的增益峰值 \(1/(2\lambda)\) 还是很高,关节速度抖动。\(\lambda\) 太大,所有方向的增益都被压低,末端跟不上目标,手臂变得迟钝,甚至在远离奇异的地方也停滞。FIG 4 右图就是这个折中。折中点取决于电机速度上限和你能容忍的跟踪误差,常需要实验调参,或者用上面“随 \(\sigma_{\min}\) 变化”的自适应方案。

    With \(\lambda\) too small, the gain peak \(1/(2\lambda)\) near a singularity is still large and the joints jitter. With \(\lambda\) too large, the gain of every direction is pushed down, the hand cannot keep up with the target, and the arm turns sluggish even far from singularities. The right panel of FIG 4 shows this trade-off. The sweet spot depends on the motor speed limit and the tracking error you can live with; it often takes experiments, or the adaptive scheme above that varies \(\lambda\) with \(\sigma_{\min}\).

    9小练习Check yourself

    1. 三个点 \((-1,\,1)\)、\((0,\,1)\)、\((1,\,4)\) 的最小二乘直线 \(y = mt + c\) 是?1. What is the least-squares line \(y = mt + c\) for the points \((-1,\,1)\), \((0,\,1)\), \((1,\,4)\)?
    这三个点的 \(t\) 值是 \(-1, 0, 1\),和 FIG 2 一样两列正交:\(\hat m = (y_3 - y_1)/2 = 1.5\),\(\hat c = (y_1 + y_2 + y_3)/3 = 2\)。残差是 \((0.5, -1, 0.5)\),SSE = 1.5。过两端的点那条线 SSE = 2.25,\(y = 2\) 是 6,\(y = 1.5t + 1\) 是 4.5,都更大。The \(t\) values are \(-1, 0, 1\), so as in FIG 2 the columns are orthogonal: \(\hat m = (y_3 - y_1)/2 = 1.5\), \(\hat c = (y_1 + y_2 + y_3)/3 = 2\). The residuals are \((0.5, -1, 0.5)\) and SSE = 1.5. The line through the end points has SSE = 2.25, \(y = 2\) has 6, and \(y = 1.5t + 1\) has 4.5, all larger.
    2. 一个方程 \(x_1 + 2x_2 = 5\),两个关节速度未知。最小范数解是?2. One equation \(x_1 + 2x_2 = 5\) and two unknown joint velocities. What is the minimum-norm solution?
    四个选项都满足方程,区别在长度:\(\mathbf x^{+} = A^\top(AA^\top)^{-1}b = (1,2)\cdot 5/5 = (1,2)\),\(\|\mathbf x\|^2 = 5\)。其余三个的 \(\|\mathbf x\|^2\) 分别是 25、10、6.25。最小范数解沿着行向量 \((1,2)\) 的方向,垂直于零空间 \((2,-1)\)。All four satisfy the equation; they differ in length: \(\mathbf x^{+} = A^\top(AA^\top)^{-1}b = (1,2)\cdot 5/5 = (1,2)\) with \(\|\mathbf x\|^2 = 5\). The other three have \(\|\mathbf x\|^2\) equal to 25, 10 and 6.25. The minimum-norm solution points along the row vector \((1,2)\), perpendicular to the null space \((2,-1)\).
    3. 在最小二乘解 \(\hat{\mathbf x}\) 处,残差 \(\mathbf r = \mathbf b - A\hat{\mathbf x}\) 一定满足哪一条?3. At the least-squares solution \(\hat{\mathbf x}\), which statement about the residual \(\mathbf r = \mathbf b - A\hat{\mathbf x}\) is always true?
    这就是正规方程 \(A^\top\mathbf r = \mathbf 0\)。\(\mathbf r = \mathbf 0\) 只在 \(\mathbf b\) 恰好在列空间里时成立。“和为 0”只是特例:当 \(A\) 有一列全是 1(直线拟合里的截距)时,\(\mathbf 1^\top\mathbf r = 0\);没有这一列,和不必为 0。FIG 1 里零和的坏线说明这个条件远不够。That is the normal equations \(A^\top\mathbf r = \mathbf 0\). \(\mathbf r = \mathbf 0\) holds only when \(\mathbf b\) happens to lie in the column space. “Sums to zero” is a special case: when \(A\) has a column of ones (the intercept in a line fit), \(\mathbf 1^\top\mathbf r = 0\); without that column the sum need not vanish. The bad zero-sum lines of FIG 1 show it is far from sufficient.
    4. 某个姿态下 \(J\) 的奇异值是 \(\sigma_1 = 1.5\),\(\sigma_2 = 0.01\),阻尼 \(\lambda = 0.1\)。DLS 在弱方向上的增益 \(\sigma_2/(\sigma_2^2 + \lambda^2)\) 约是多少(不阻尼时是 \(1/\sigma_2 = 100\))?强方向的增益呢?4. At some pose \(J\) has singular values \(\sigma_1 = 1.5\), \(\sigma_2 = 0.01\), and the damping is \(\lambda = 0.1\). What is the DLS gain \(\sigma_2/(\sigma_2^2 + \lambda^2)\) along the weak direction (undamped it would be \(1/\sigma_2 = 100\)), and what happens along the strong one?
    弱方向:\(0.01/(0.0001 + 0.01) = 0.99\),从 100 降到约 1。强方向:\(1.5/(2.25 + 0.01) = 0.664\),而不阻尼时是 \(1/1.5 = 0.667\)。阻尼几乎只改变弱方向,这正是想要的效果。代价:沿弱方向的目标只完成了约 \(\sigma_2\cdot 0.99 = 1\%\)。Weak direction: \(0.01/(0.0001 + 0.01) = 0.99\), down from 100 to about 1. Strong direction: \(1.5/(2.25 + 0.01) = 0.664\), versus \(1/1.5 = 0.667\) undamped. Damping changes almost only the weak direction, which is the point. The price: only about \(\sigma_2\cdot 0.99 = 1\%\) of the goal along the weak direction gets achieved.
    5. \(\kappa(A) = 10^{5}\),用双精度(约 16 位有效数字)。先算 \(A^\top A\) 再解正规方程,大约会丢掉多少位有效数字?5. \(\kappa(A) = 10^{5}\) in double precision (about 16 significant digits). Forming \(A^\top A\) and solving the normal equations loses roughly how many digits?
    \(\kappa(A^\top A) = \kappa(A)^2 = 10^{10}\),相对误差约 \(10^{10}\times10^{-16} = 10^{-6}\),只剩约 6 位,丢掉约 10 位。直接对 \(A\) 做 QR 或 SVD,误差只有约 \(\kappa\epsilon = 10^{-11}\),只丢约 5 位。\(\kappa(A^\top A) = \kappa(A)^2 = 10^{10}\), so the relative error is about \(10^{10}\times10^{-16} = 10^{-6}\): only about 6 digits survive, 10 are lost. QR or the SVD applied to \(A\) itself has error about \(\kappa\epsilon = 10^{-11}\), losing only about 5 digits.

    10延伸阅读Further reading

    • G. Strang, Introduction to Linear Algebra, §4.3 — 最小二乘近似(正规方程与投影)Least squares approximations (normal equations and projections)
    • G. Strang, Introduction to Linear Algebra, Ch. 7 — SVD 与伪逆The SVD and the pseudoinverse
    • G. Strang, MIT 18.06 Linear Algebra (OCW) — 第 16 讲:投影矩阵与最小二乘Lecture 16: Projection matrices and least squares
    • K. Lynch & F. Park, Modern Robotics, §6.2 — 数值逆运动学(伪逆迭代)Numerical inverse kinematics (pseudo-inverse iteration)
    • S. R. Buss, “Introduction to Inverse Kinematics with Jacobian Transpose, Pseudoinverse and Damped Least Squares Methods”, 2004 — 三种雅可比方法的清晰对比,含奇异位形处的实验A clear comparison of the three Jacobian methods, with experiments at singularities
    • Y. Nakamura & H. Hanafusa, J. Dynamic Systems, Measurement, and Control 108(3), 1986; C. W. Wampler, IEEE Trans. Systems, Man, and Cybernetics 16(1), 1986 — 阻尼最小二乘用于逆运动学的原始论文The original papers on damped least squares for inverse kinematics