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

矩阵即变换Matrices as Transformations

矩阵不是一张数表,而是一个动作:它把整个空间拉伸、旋转、剪切。只要知道 î 和 ĵ 去了哪里,就知道每一个点去了哪里。同一个矩阵还能讲第二个故事:点不动,换一把尺子去量它。 A matrix is not a table of numbers but an action: it stretches, rotates and shears all of space. Know where î and ĵ land and you know where every point lands. The same matrix also tells a second story: nothing moves, you just measure with a different ruler.

约 20 分钟~20 min 前置:1.1 向量 · 1.2 基Prereqs: 1.1 Vectors · 1.2 Basis 5 个交互5 interactives

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

    机器人的相机看到桌上一个杯子,报告说:"杯子在我前方 0.8 米、偏左 0.2 米。"可机械臂要的是杯子在世界坐标系里的位置。机器人转了个身,同一个杯子在相机里的读数全变了,杯子本身却一动没动。

    "把一堆点按同一个规则统一挪动"和"同一个点换一个坐标系重新描述",是机器人软件里最常做的两件事。它们背后是同一个数学对象:矩阵。机器人代码里很多 bug 都出在一个问题上:这个矩阵到底是在搬动点,还是在换坐标系?

    A robot's camera spots a cup and reports: "the cup is 0.8 m ahead of me and 0.2 m to my left." The arm, however, needs the cup's position in the world frame. When the robot turns around, every number the camera reports changes, yet the cup has not moved at all.

    "Move a set of points by one consistent rule" and "describe the same point in a different frame" are the two most common operations in robot software. Both are the same mathematical object: a matrix. A large share of bugs in robot code come down to one question: is this matrix moving points, or changing frames?

    2直觉:什么样的变换叫"线性"Intuition: what makes a transformation "linear"

    把平面想象成一张画着方格的橡皮膜。变换就是把膜上每个点挪到新位置。在所有挪法里,有一类特别"守规矩",叫线性变换:

    • 直线变换后仍然是直线;
    • 平行、等距的网格线变换后仍然平行、等距;
    • 原点不动。

    下面切换三种变换,看看哪一种满足全部三条。

    Think of the plane as a rubber sheet printed with a square grid. A transformation moves every point of the sheet somewhere new. Among all possible ways to do this, one family is especially well-behaved. These are the linear transformations:

    • straight lines stay straight;
    • parallel, evenly spaced grid lines stay parallel and evenly spaced;
    • the origin stays put.

    Switch between the three transformations below and see which one passes all three tests.

    FIG 1哪个是线性变换?Which one is linear?
    淡色网格是变换前,亮色网格是变换后。注意"非线性"那一档:原点虽然没动,网格却弯了,所以原点不动并不够。 The faint grid is before, the bright grid after. Look at "nonlinear": the origin stays put but the grid bends, so a fixed origin alone is not enough.

    线性变换有一个惊人的推论。任何向量都能写成基向量的组合:\( \mathbf v = x\,\ihat + y\,\jhat \)。网格变换后仍然"平行且等距",这个组合关系就原封不动地保留下来:

    Linearity has a striking consequence. Every vector is a combination of the basis vectors, \( \mathbf v = x\,\ihat + y\,\jhat \). Because the grid stays "parallel and evenly spaced", that recipe survives the transformation unchanged:

    $$ T(\mathbf v) \;=\; \hl{x}{x}\, \hl{a1}{\ci{T(\hat\imath)}} \;+\; \hl{y}{y}\, \hl{a2}{\cj{T(\hat\jmath)}} $$
    核心洞见Key insight

    一个线性变换完全由两个向量决定:î 落在哪里、ĵ 落在哪里。把这两个落点并排写成两列,就是这个变换的矩阵。矩阵的第 1 列是 î 的落点,第 2 列是 ĵ 的落点。

    A linear transformation is completely determined by two vectors: where î lands and where ĵ lands. Write those two landing spots side by side as columns and you have the transformation's matrix. Column 1 is where î lands; column 2 is where ĵ lands.

    3动手玩:矩阵实验室Play: the matrix lab

    拖动红色 Aî 和绿色 Aĵ 的箭头尖,矩阵会跟着变。也可以直接改矩阵里的数(左右拖动数字可以连续调节),或者点一个预设。黄色向量 Av 也可以拖动。

    Drag the tips of the red Aî and green Aĵ arrows and the matrix follows. You can also edit the entries directly (drag a number sideways to scrub it) or pick a preset. The amber vector Av is draggable too.

    FIG 2矩阵实验室Matrix lab
    矩阵 A(拖动或输入)Matrix A (drag or type)
    预设Presets
    淡色网格是原始坐标网格;亮色网格是变换后的网格;浅色平行四边形是单位正方形变换后的样子(它的面积就是 1.5 节的主角:行列式)。虚线显示 Av 是怎样由 x 份 Aî 加 y 份 Aĵ 拼成的。 The faint grid is the original grid; the bright grid is the transformed one; the shaded parallelogram is the image of the unit square (its area stars in Lesson 1.5 as the determinant). The dashed path shows how Av is assembled from x copies of Aî plus y copies of Aĵ.
    试一试Try this
    • 只拖箭头,做出一个逆时针 90° 旋转。提示:î 应该去哪?ĵ 呢?Using only the arrows, build a 90° counter-clockwise rotation. Hint: where should î go? And ĵ?
    • 让整个平面塌成一条直线。这时黄色向量为什么拖不动了?Squash the whole plane onto a single line. Why can't you drag the amber vector any more?
    • 让网格翻面:Aî 转到 Aĵ 的"顺时针一侧"。平行四边形的颜色会变。Make the grid flip over: put Aî on the clockwise side of Aĵ. Watch the parallelogram change color.
    • 矩阵不是单位矩阵时,拖动黄色向量,让它正好落在变换后网格的格点 (2, −1) 上。With a non-identity matrix, drag the amber vector onto the grid point (2, −1) of the transformed grid.

    小游戏:猜矩阵Mini-game: guess the matrix

    紫色虚线是一个"目标变换"作用后的网格。拖动 Aî、Aĵ,让你的网格和它重合。读出目标矩阵的诀窍:找到原来的格点 (1, 0) 和 (0, 1) 跑到了哪里。

    The dashed purple grid is the result of some "target" transformation. Drag Aî and Aĵ until your grid lines up with it. The trick for reading off the target matrix: find where the old grid points (1, 0) and (0, 1) went.

    FIG 3猜矩阵Guess the matrix
    • 累计猜中 3 题。Get 3 right.

    4两种视角:搬动点,还是换尺子Two views: move the points, or change the ruler

    同一个等式 \( \mathbf p = A\mathbf c \) 可以讲两个完全不同的故事。

    主动视角(变换):坐标系不动,点被搬走。\( \mathbf c \) 是旧位置,\( \mathbf p \) 是新位置,两者用同一把尺子量。

    被动视角(换基):点不动,换一把尺子。\( A \) 的两列 \( \mathbf b_1, \mathbf b_2 \) 是一套新的基向量。同一支箭头用新基去量,读数是 \( \mathbf c \);用标准基去量,读数是 \( \mathbf p \)。这时 \( A \) 的作用是把新坐标翻译成标准坐标。

    在下面两张图里拖动任意一个黄色箭头,两张图会同步变化。

    The single equation \( \mathbf p = A\mathbf c \) can tell two entirely different stories.

    Active view (transformation): the frame stays, the point moves. \( \mathbf c \) is the old position and \( \mathbf p \) the new one, both measured with the same ruler.

    Passive view (change of basis): the point stays, the ruler changes. The columns \( \mathbf b_1, \mathbf b_2 \) of \( A \) form a new basis. Measured with the new basis, one arrow reads \( \mathbf c \); measured with the standard basis, the same arrow reads \( \mathbf p \). Here \( A \) translates new-basis coordinates into standard coordinates.

    Drag either amber arrow below; the two panels stay in sync.

    FIG 4同一个矩阵,两个故事One matrix, two stories
    主动:点被搬走ACTIVE · the point moves
    被动:换一把尺子PASSIVE · the ruler changes
    仔细比较:左图里"被搬动后的网格",和右图里"新的尺子",是同一张网格。左图中被搬走的点在新网格里的读数,恰好是它搬家前在旧网格里的读数 \(\mathbf c\)。这就是两种视角相通的地方。 Compare closely: the "moved grid" on the left and the "new ruler" on the right are the same grid. On the left, the moved point reads, in the new grid, exactly what it read in the old grid before moving: \(\mathbf c\). That is where the two views meet.
    主动视角(变换)Active (transformation)被动视角(换基)Passive (change of basis)
    什么在动What moves点(整个空间)The points (all of space)坐标系 / 尺子The frame / ruler
    \(A\) 的列Columns of \(A\)\(\ihat, \jhat\) 被送到哪里Where \(\ihat, \jhat\) are sent新基向量在旧坐标系中的样子The new basis vectors, written in old coordinates
    \(\mathbf c \to \mathbf p\)旧位置 → 新位置old position → new position新坐标系读数 → 标准坐标系读数new-frame reading → standard-frame reading
    机器人例子Robot example把点云整体旋转 30°Rotate a point cloud by 30°把相机坐标系里的点换算到世界坐标系Express a camera-frame point in the world frame

    5再看数学The math

    提示:把鼠标悬停在公式中带底色的符号上,FIG 2 中对应的元素会被高亮。Tip: hover the highlighted symbols in the formulas; the matching element in FIG 2 lights up.

    定义Definition

    变换 \(T:\R^2\to\R^2\) 是线性的,当且仅当它保持加法和数乘:

    A map \(T:\R^2\to\R^2\) is linear exactly when it preserves addition and scaling:

    $$ T(\mathbf u + \mathbf w) = T(\mathbf u) + T(\mathbf w), \qquad T(s\,\mathbf u) = s\,T(\mathbf u). $$

    取 \(s = 0\) 就得到 \(T(\mathbf 0)=\mathbf 0\),也就是原点不动。所以平移不是线性变换。

    Taking \(s = 0\) gives \(T(\mathbf 0)=\mathbf 0\): the origin stays put. That is why translation is not linear.

    从基向量推出矩阵From basis vectors to the matrix

    设 î 被送到 \(\ci{(a, c)}\),ĵ 被送到 \(\cj{(b, d)}\)。由线性:

    Say î is sent to \(\ci{(a, c)}\) and ĵ to \(\cj{(b, d)}\). By linearity:

    $$ \hl{v}{\cv{A\mathbf v}} = \hl{x}{x}\hl{a1}{\ci{\begin{bmatrix} a \\ c \end{bmatrix}}} + \hl{y}{y}\hl{a2}{\cj{\begin{bmatrix} b \\ d \end{bmatrix}}} = \begin{bmatrix} \ci{a} & \cj{b} \\ \ci{c} & \cj{d} \end{bmatrix}\begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} \ci{a}x + \cj{b}y \\ \ci{c}x + \cj{d}y \end{bmatrix}. $$
    矩阵乘向量的两种读法Two ways to read a matrix–vector product

    列视角(本节的视角):\(A\mathbf v\) 是 \(A\) 各列的线性组合,组合系数就是 \(\mathbf v\) 的各个分量。这是几何直觉的来源。
    行视角(计算时常用):\(A\mathbf v\) 的第 \(i\) 个分量,是 \(A\) 的第 \(i\) 行与 \(\mathbf v\) 的点积(1.7 节)。两种读法算出的结果完全一样。

    Column view (this lesson): \(A\mathbf v\) is a combination of the columns of \(A\), weighted by the entries of \(\mathbf v\). This is where the geometric intuition comes from.
    Row view (handy for computing): the \(i\)-th entry of \(A\mathbf v\) is the dot product of row \(i\) with \(\mathbf v\) (Lesson 1.7). Both readings give exactly the same result.

    换基公式The change-of-basis formula

    设新基为 \(B = [\,\mathbf b_1\;\mathbf b_2\,]\)。同一支箭头在新基下坐标为 \(\mathbf c\),在标准基下坐标为 \(\mathbf p\),则:

    Let the new basis be \(B = [\,\mathbf b_1\;\mathbf b_2\,]\). If one arrow has coordinates \(\mathbf c\) in the new basis and \(\mathbf p\) in the standard basis, then:

    $$ \mathbf p = c_1\mathbf b_1 + c_2 \mathbf b_2 = B\,\mathbf c, \qquad \mathbf c = B^{-1}\mathbf p. $$

    注意方向:\(B\) 把"新坐标"翻译成"标准坐标";反方向要用 \(B^{-1}\)。1.10 节会讲到,对旋转矩阵 \(R\) 来说 \(R^{-1} = R^\top\),求逆只需要转置,这是机器人里最常用的技巧之一。

    Mind the direction: \(B\) translates "new coordinates" into "standard coordinates"; going the other way needs \(B^{-1}\). For a rotation matrix \(R\) we will see in Lesson 1.10 that \(R^{-1} = R^\top\), so inverting is just transposing. This is one of the most-used tricks in robotics.

    6机器人中的它:相机坐标与世界坐标In robotics: camera frame vs. world frame

    一个移动机器人站在世界原点(平移先放一边,5.3 节再加回来),朝向角为 \(\theta\)。它的"前方"和"左方"两根坐标轴,在世界坐标系中写出来,就是旋转矩阵 \(R(\theta)\) 的两列:

    A mobile robot stands at the world origin (we leave translation aside until Lesson 5.3) with heading \(\theta\). Its "forward" and "left" axes, written in world coordinates, are the two columns of the rotation matrix \(R(\theta)\):

    $$ R(\theta) = \begin{bmatrix} \ci{\cos\theta} & \cj{-\sin\theta} \\ \ci{\sin\theta} & \cj{\cos\theta} \end{bmatrix}, \qquad \mathbf p_{\text{world}} = R(\theta)\,\mathbf p_{\text{robot}}. $$

    切换下面两种模式,转动机器人。"物体固定在世界中"对应被动视角:杯子不动,机器人测到的读数在变。"物体随机器人转动"对应主动视角:托盘上的杯子被机器人带着走。

    Switch between the two modes and turn the robot. "Object fixed in the world" is the passive view: the cup is still, but the robot's reading of it changes. "Object carried by the robot" is the active view: a cup on the robot's tray is carried around.

    FIG 5机器人看到的杯子The cup as the robot sees it
    可以拖动杯子(黄点),也可以拖动机器人红色"前方"轴的末端来转动它。虚线是杯子在两个坐标系里的读数:灰色对应世界坐标轴,彩色对应机器人坐标轴。 Drag the cup (amber dot), or drag the tip of the robot's red "forward" axis to turn it. The dashed lines are the cup's readings in the two frames: grey along the world axes, colored along the robot's axes.
    • 在"物体固定在世界中"模式下,转动机器人,让杯子正好位于它正前方(机器人坐标的 y 分量为 0,x 分量为正)。In "object fixed in the world" mode, turn the robot until the cup is dead ahead (robot-frame y = 0, x > 0).
    真实系统里In real systems

    ROS 的 tf2 每秒要做成千上万次这样的换算:激光雷达系 → 底盘系 → 地图系。机械臂的末端执行器、相机光心、每个关节,都各自带着一个坐标系。这一节讲的"列是新基向量在旧坐标系中的样子",就是理解这些坐标变换的钥匙。

    ROS's tf2 performs thousands of these conversions per second: lidar frame → base frame → map frame. The end effector, the camera's optical center and every joint each carry a frame of their own. "The columns are the new basis vectors written in the old frame" is the key to reading all of those transforms.

    7常见误区Common pitfalls

    误区 1:平移也能用 2×2 矩阵表示Pitfall 1: translation is a 2×2 matrix

    不能。平移会移动原点,而线性变换必须保持原点不动。解决办法是给坐标添一个 1,升一维,这就是 5.3 节的齐次坐标。

    It isn't. Translation moves the origin, and a linear map must keep the origin fixed. The fix is to append a 1 and lift into one more dimension: the homogeneous coordinates of Lesson 5.3.

    误区 2:搞反主动与被动,于是 R 和 Rᵀ 用反Pitfall 2: mixing up active and passive, so R and Rᵀ get swapped

    "把点逆时针转 30°"和"把坐标系逆时针转 30°"得到的读数,正好互为逆变换。写代码前,先问自己:我是在搬点,还是在换坐标系?矩阵是从哪个坐标系到哪个坐标系?推荐在变量名里写清楚,例如 R_world_cam。

    "Rotate the points 30° counter-clockwise" and "rotate the frame 30° counter-clockwise" give readings that are exact inverses of each other. Before writing code, ask: am I moving points or changing frames? From which frame to which? Spell it out in variable names, e.g. R_world_cam.

    误区 3:把行当成基向量Pitfall 3: reading the rows as the basis vectors

    基向量的像放在列里。有些库按行优先存储矩阵;有些资料用行向量写成 \(\mathbf v^\top A\),这时一切都要转置。读别人代码时先确认约定。

    The images of the basis vectors live in the columns. Some libraries store matrices row-major, and some texts use row vectors, \(\mathbf v^\top A\), which transposes everything. Check the convention before reading someone else's code.

    8小练习Check yourself

    1. 矩阵 \(\begin{bmatrix}0&-1\\1&0\end{bmatrix}\) 做了什么?1. What does \(\begin{bmatrix}0&-1\\1&0\end{bmatrix}\) do?
    看列:î → (0, 1),ĵ → (−1, 0)。x 轴转到了 y 轴,这是逆时针 90°。Read the columns: î → (0, 1) and ĵ → (−1, 0). The x-axis turns onto the y-axis: 90° counter-clockwise.
    2. 一个线性变换把 î 送到 (2, 1),把 ĵ 送到 (−1, 1)。向量 (3, 2) 被送到哪里?2. A linear map sends î to (2, 1) and ĵ to (−1, 1). Where does (3, 2) go?
    \(3\,\ci{(2,1)} + 2\,\cj{(-1,1)} = (6-2,\; 3+2) = (4, 5)\)。\(3\,\ci{(2,1)} + 2\,\cj{(-1,1)} = (6-2,\; 3+2) = (4, 5)\).
    3. "把每个点向右平移 1"是线性变换吗?3. Is "shift every point right by 1" a linear transformation?
    平移满足前两条,但 \(T(\mathbf 0) = (1, 0) \ne \mathbf 0\)。这类"线性变换 + 平移"叫仿射变换,用齐次坐标可以把它写成一个矩阵(5.3 节)。It passes the first two tests, but \(T(\mathbf 0) = (1, 0) \ne \mathbf 0\). "Linear map plus a shift" is called an affine map, and homogeneous coordinates turn it into a single matrix (Lesson 5.3).
    4. 相机坐标轴在世界坐标系中的方向,组成旋转矩阵 \(R\) 的各列。已知物体在相机坐标系中的位置 \(\mathbf p_c\),它在世界坐标系中的位置是?4. The camera's axes, written in world coordinates, form the columns of \(R\). Given an object at \(\mathbf p_c\) in the camera frame, where is it in the world frame?
    被动视角:\(\mathbf p_w = p_{c,1}\,\mathbf b_1 + p_{c,2}\,\mathbf b_2 + \dots = R\,\mathbf p_c\)。反方向,从世界坐标换算回相机坐标,才用 \(R^\top\)。Passive view: \(\mathbf p_w = p_{c,1}\,\mathbf b_1 + p_{c,2}\,\mathbf b_2 + \dots = R\,\mathbf p_c\). Going the other way, from world back to camera, uses \(R^\top\).

    9延伸阅读Further reading

    • 3Blue1Brown, Essence of Linear Algebra — 第 3 章(线性变换)与第 13 章(基变换)Ch. 3 (linear transformations) and Ch. 13 (change of basis)
    • G. Strang, Introduction to Linear Algebra, Ch. 8 — 线性变换Linear transformations
    • K. Lynch & F. Park, Modern Robotics, Ch. 3 — 刚体运动与坐标系Rigid-body motions and frames
    • J. Ström et al., Immersive Linear Algebra — 带交互插图的线代教材An interactive linear algebra textbook