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

向量Vectors

机器人感知到的、发出去的几乎一切,都是一串带几何含义的数字:杯子在哪里,车跑多快,手爪受多大的力,机械臂六个关节各转了多少。这串数字就是向量。它同时是一支箭头、一张数字清单和空间里的一个点。能在这三种说法之间自由切换,是整门课的第一项本领。 Almost everything a robot senses or commands is a short list of numbers with a geometric meaning: where the cup is, how fast the car moves, how hard the gripper pushes, how far each of an arm's six joints has turned. That list is a vector. It is an arrow, a list of numbers and a point in space all at once. Switching freely between those three pictures is the first skill of this course.

约 20 分钟~20 min 前置:高中数学Prereqs: high-school math 7 个交互7 interactives

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

    相机看见桌上的杯子,报告:"杯子在 (0.8, −0.2, 0.1) 米。"里程计说,这一秒车走了 (0.3, 0.05) 米。力传感器说,手爪受力 (0, 0, −4.9) 牛。机械臂控制器每个周期读到 6 个关节角。激光雷达每转一圈给出几千个点,每个点是一对 (x, y)。

    这些东西看上去毫不相干,却有一个共同点:每一项都是一列数字,而且每个数字都有几何含义。数学上把它们统称为向量 (vector)。一旦承认这一点,就能用同一套操作去处理它们:两段位移相加得到总位移,把速度放大一倍,量两点之间的距离,给一个方向配上长度。后面整门课的数学,都是从这几个朴素的操作里长出来的。

    这一节只做一件事:让"向量"从一个名词变成手感。

    A camera sees a cup on the table and reports: "the cup is at (0.8, −0.2, 0.1) metres." The odometry says the car moved (0.3, 0.05) metres in the last second. A force sensor says the gripper feels (0, 0, −4.9) newtons. An arm controller reads 6 joint angles every cycle. A lidar returns thousands of points per sweep, each one a pair (x, y).

    These look unrelated, yet they share one trait: each is a list of numbers, and every number has a geometric meaning. Mathematics calls all of them vectors. Once you accept that, the same few operations work on all of them: add two displacements to get the total displacement, double a velocity, measure the distance between two points, attach a length to a direction. The mathematics of the rest of the course grows out of these plain operations.

    This lesson does one thing: it turns "vector" from a noun into a feel.

    怎么玩这些页面How these pages work

    图中带彩色圆环的点是手柄,直接拖动就行。"试一试"里的任务做到了会自己打勾。右上角可以切换 中文 / EN 和明暗主题。

    The dots with a colored ring in a figure are handles: just drag them. The "Try this" tasks tick themselves when you get there. The top-right buttons switch 中文 / EN and the light or dark theme.

    2直觉:同一件事的三张脸Intuition: three faces of one idea

    向量有三张脸,说的是同一件事:

    • 箭头:有方向和长度,不在乎尾巴钉在哪里。把它平移(不转、不拉长)到任何地方,它仍然是同一个向量。
    • 一串数字(分量 components):沿 x 走多少,沿 y 走多少。竖着写成 \(\begin{bmatrix}3\\2\end{bmatrix}\)。在机器人代码里,它就是一个数组。
    • 一个点:把尾巴钉在原点,箭头尖落在平面上的某一点,这个点的坐标就是向量的分量。

    拖动下图:琥珀色箭头的尖端用来改变 \(\cv{\mathbf v}\);青色小环抓着 \(\cv{\mathbf v}\) 的一个副本的尾巴,可以把它滑到别处。看看数字变不变。

    A vector has three faces, and they say the same thing:

    • An arrow: it has a direction and a length, and does not care where its tail is pinned. Slide it anywhere (without turning or stretching) and it is still the same vector.
    • A list of numbers (components): how far to go along x, how far along y. Written as a column, \(\begin{bmatrix}3\\2\end{bmatrix}\). In robot code it is just an array.
    • A point: pin the tail at the origin and the arrow tip lands on one point of the plane. That point's coordinates are the vector's components.

    Drag the figure below: the amber arrow tip changes \(\cv{\mathbf v}\); the cyan ring grabs the tail of a copy of \(\cv{\mathbf v}\) and slides it elsewhere. Watch whether the numbers change.

    FIG 1三张脸Three faces
    红、绿虚线是 \(\mathbf v\) 沿 x、y 方向的两个分量。副本换了位置,"尖 − 尾"得到的仍是同一个 \(\mathbf v\):箭头的身份是方向和长度,不是位置。 The red and green dashed lines are the two components of \(\mathbf v\) along x and y. The copy sits elsewhere, yet "tip − tail" still gives the same \(\mathbf v\): an arrow's identity is its direction and length, not its location.
    • 用青色小环把副本滑到别处(至少挪开 1.5),确认右边"尖 − 尾"的结果没有变。Slide the copy away with the cyan ring (at least 1.5 units) and confirm that "tip − tail" on the right does not change.
    • 把 \(\mathbf v\) 拖成 \((-2,\,3)\)。Drag \(\mathbf v\) to \((-2,\,3)\).
    核心洞见:位移与位置Key insight: displacement vs. position

    位移 (displacement) 回答"从这里走到那里要走多少",不需要原点,可以随意平移。位置 (position) 回答"在哪里",必须先约定原点和坐标轴;它其实是"从原点出发的位移"。本节把向量主要当作位移来用。第 4 节会专门玩这个区别,它是后面所有坐标系内容(5.1 节)的伏笔。

    Displacement answers "how far do I go from here to there": it needs no origin and can be slid freely. Position answers "where is it": you must first agree on an origin and axes, and a position is really "the displacement from the origin". This lesson mostly uses vectors as displacements. Section 4 plays with exactly this difference, which sets up everything about frames later (Lesson 5.1).

    3动手玩:向量实验室Play: the vector lab

    现在把两个向量放在一起。拖动琥珀色的 \(\cv{\mathbf a}\) 和紫色的 \(\cw{\mathbf b}\) 的箭头尖(会吸附到 0.5 的格点上),再用右上角的开关选一种运算:

    • 加法 \(\mathbf a+\mathbf b\):先走完 \(\mathbf a\),再从它的尖出发走 \(\mathbf b\)(首尾相接)。先走 \(\mathbf b\) 再走 \(\mathbf a\) 也到同一点,两条路围成一个平行四边形。
    • 减法 \(\mathbf a-\mathbf b\):从 \(\mathbf b\) 的尖指向 \(\mathbf a\) 的尖的那支箭头。
    • 数乘 \(s\,\mathbf a\):沿 \(\mathbf a\) 所在的直线拉长或缩短;\(s\) 为负时掉头。

    Now put two vectors together. Drag the tips of the amber \(\cv{\mathbf a}\) and the purple \(\cw{\mathbf b}\) (they snap to a 0.5 grid), then pick an operation with the switch at the top right:

    • Addition \(\mathbf a+\mathbf b\): walk \(\mathbf a\), then from its tip walk \(\mathbf b\) (tip to tail). Walking \(\mathbf b\) first and \(\mathbf a\) second ends at the same point; the two routes bound a parallelogram.
    • Subtraction \(\mathbf a-\mathbf b\): the arrow from the tip of \(\mathbf b\) to the tip of \(\mathbf a\).
    • Scaling \(s\,\mathbf a\): stretch or shrink along the line of \(\mathbf a\); a negative \(s\) turns it around.
    FIG 2向量实验室Vector lab
    预设Presets
    加法档:虚线是把 \(\mathbf b\) 接在 \(\mathbf a\) 的尖上、再把 \(\mathbf a\) 接在 \(\mathbf b\) 的尖上,两条路到同一点。减法档:\(\mathbf a-\mathbf b\) 的虚线副本被平移到原点,数字相同。数乘档(用下面的滑块调 \(s\)):\(s\,\mathbf a\) 永远在 \(\mathbf a\) 所在的那条虚线上。 Addition: the dashed arrows attach \(\mathbf b\) at the tip of \(\mathbf a\) and \(\mathbf a\) at the tip of \(\mathbf b\); both routes reach the same point. Subtraction: a dashed copy of \(\mathbf a-\mathbf b\) is slid to the origin; same numbers. Scaling (set \(s\) with the slider below): \(s\,\mathbf a\) always stays on the dotted line through \(\mathbf a\).
    试一试Try this
    • 让 \(\mathbf a+\mathbf b=\mathbf 0\)(两条路走回原点)。\(\mathbf b\) 该放在哪里?Make \(\mathbf a+\mathbf b=\mathbf 0\) (the two legs return to the origin). Where must \(\mathbf b\) go?
    • 让 \(|\mathbf a+\mathbf b| = |\mathbf a|+|\mathbf b|\)(看右边的读数)。\(\mathbf a\) 和 \(\mathbf b\) 必须满足什么关系?Make \(|\mathbf a+\mathbf b| = |\mathbf a|+|\mathbf b|\) (watch the readout). What must be true of \(\mathbf a\) and \(\mathbf b\)?
    • 切到 \(\mathbf a-\mathbf b\) 档,让它恰好等于 \((1,\,2)\)。注意 \(\mathbf a\) 和 \(\mathbf b\) 都可以动,答案不止一个。Switch to \(\mathbf a-\mathbf b\) and make it exactly \((1,\,2)\). Both \(\mathbf a\) and \(\mathbf b\) can move, so there are many answers.
    • 切到 \(s\,\mathbf a\) 档,让它与 \(\mathbf a\) 方向相反、长度恰为 \(\mathbf a\) 的一半(\(s=-0.5\))。Switch to \(s\,\mathbf a\) and make it point the opposite way at exactly half the length of \(\mathbf a\) (\(s=-0.5\)).

    小游戏:够到目标Mini-game: reach the target

    机器人每一步只能走两种固定的位移:琥珀色的 \(\cv{\mathbf d_1}\) 或青色的 \(\cx{\mathbf d_2}\),可以前进,也可以后退。点按钮,把一段段位移首尾相接,让代表机器人的大圆点停在紫色的目标 \(\cw{T}\) 上。淡青色的小点是所有"整数份 \(\mathbf d_1\) 加整数份 \(\mathbf d_2\)"能到的位置,目标一定在其中。试试用最少的步数。

    The robot can only take two fixed displacements per move: the amber \(\cv{\mathbf d_1}\) or the cyan \(\cx{\mathbf d_2}\), forward or backward. Press the buttons to chain the moves tip to tail until the large dot (the robot) sits on the purple target \(\cw{T}\). The faint cyan dots mark every position reachable as "a whole number of \(\mathbf d_1\) plus a whole number of \(\mathbf d_2\)"; the target is always one of them. Try to use as few moves as possible.

    FIG 3够到目标Reach the target
    每个目标都有唯一的最短配方:走几次 \(\mathbf d_1\)、几次 \(\mathbf d_2\) 是确定的(两个步向量方向不同,所以组合方式只有一种),先走哪一步则随你。用最少的步数命中,会得到"最短"的标记。 Every target has exactly one shortest recipe: how many \(\mathbf d_1\) steps and how many \(\mathbf d_2\) steps is fixed (the two step vectors point in different directions, so each point has only one recipe), while the order of the steps is up to you. Hit it in the fewest moves and you earn the "shortest" tag.
    • 命中 3 个目标。Hit 3 targets.

    4第二个视角:点与向量不是一回事Second view: points are not vectors

    前面有件事一直悄悄成立:原点在画面中央。可现实里原点是人选的:世界坐标系的原点可能在房间的一角,也可能在机器人底座上。同一个杯子,换一个原点,坐标就变了。杯子没动,动的是尺子。

    于是有一条要紧的区分。点的坐标依赖原点;从点 \(P\) 到点 \(Q\) 的位移 \(\mathbf d = Q-P\) 却不依赖,因为原点一挪,\(P\) 和 \(Q\) 的坐标同时减去同一个数,差不变。这给出四条规则:

    • 点 − 点 = 向量(位移);
    • 点 + 向量 = 点(走了一段位移之后到哪里);
    • 向量 + 向量 = 向量(位移接位移);
    • 点 + 点 = 没有意义:结果会随原点而变。

    下面的图里,杯子 \(P\) 和盒子 \(Q\) 放在桌上,拖动原点 \(O\)(青色)时它们原地不动;你也可以单独拖动 \(P\)、\(Q\)。网格是以 \(O\) 为原点画的坐标纸,右侧显示 \(P\)、\(Q\) 在这套坐标下的读数。

    Something has quietly held so far: the origin sat in the middle of the picture. In real systems the origin is a choice: the world frame may start in a corner of the room or at the robot's base. Change the origin and the same cup gets different coordinates. The cup did not move; the ruler did.

    That gives an important distinction. The coordinates of a point depend on the origin. The displacement \(\mathbf d = Q-P\) from point \(P\) to point \(Q\) does not: when the origin shifts, the coordinates of \(P\) and \(Q\) both lose the same amount, so the difference stays. Four rules follow:

    • point − point = vector (a displacement);
    • point + vector = point (where you land after a displacement);
    • vector + vector = vector (a displacement followed by a displacement);
    • point + point = meaningless: the answer changes with the origin.

    In the figure, the cup \(P\) and the box \(Q\) sit on the table and stay put while you drag the origin \(O\) (cyan); you can also drag \(P\) and \(Q\) themselves. The grid is graph paper drawn from \(O\), and the side panel shows the readings of \(P\) and \(Q\) in that frame.

    FIG 4挪动原点Move the origin
    在以 O 为原点的坐标系里In the frame with origin O
    位移(与原点无关)Displacement (independent of the origin)
    "点 + 点"(依赖原点)"Point + point" (depends on the origin)
    虚线是 \(P\)、\(Q\) 的位置向量(从 \(O\) 出发的箭头):挪动 \(O\),它们跟着变。琥珀色的 \(\mathbf d=Q-P\) 纹丝不动。红色圆环是把两个读数相加得到的"\(P+Q\)",它会随 \(O\) 乱跑。 The dashed arrows are the position vectors of \(P\) and \(Q\) (arrows from \(O\)): they change when \(O\) moves. The amber \(\mathbf d=Q-P\) never budges. The red ring is the "\(P+Q\)" obtained by adding the two readings, and it runs around with \(O\).
    • 把原点 \(O\) 拖到杯子 \(P\) 上,使 \(P\) 读作 \((0,0)\)。这时 \(Q\) 的读数等于什么?Drag the origin \(O\) onto the cup \(P\) so that \(P\) reads \((0,0)\). What does \(Q\) read now?
    • 找一个原点,使"\(P+Q\)"读作 \((0,0)\)。它在 \(P\) 和 \(Q\) 的什么位置?Find an origin for which "\(P+Q\)" reads \((0,0)\). Where does it sit relative to \(P\) and \(Q\)?
    核心洞见Key insight

    机器人里的"点"(杯子位置、关节位置)和"向量"(位移、速度、力)是两类不同的东西,但它们都用数字来写,很容易混。凡是涉及位置,就要问一句:"相对哪个原点、哪个坐标系?"这个问题在 5.1 节会变成整套机器人位姿理论的出发点。

    In robotics, "points" (the cup's position, a joint's position) and "vectors" (displacements, velocities, forces) are two different kinds of thing, yet both are written with numbers, so they are easy to confuse. Whenever a position is involved, ask: "relative to which origin, in which frame?" In Lesson 5.1 this question becomes the starting point of the whole theory of robot poses.

    5第三个视角:很多个分量Third view: many components

    到现在为止,向量只有两三个分量,因为我们能把它画出来。可机器人常常要处理分量很多的向量。一个六轴机械臂的构型 (configuration),也就是每个关节转了多少,是 6 个角度 \(\mathbf q=(q_1,\dots,q_6)\):一个 \(\R^6\) 中的向量。

    六维的箭头画不出来,但向量的本质从来不是"箭头",而是"能相加、能数乘的一串数"。加法、数乘、长度,全都逐分量地照做,和二维时一模一样。

    下图用一个平面三连杆臂代替六轴臂,它的构型是 \(\mathbf q\in\R^3\)。左边是臂,右边是 \(\mathbf q\) 的三个分量,画成三根柱子。拖动臂的关节或柱子的顶端,另一边会跟着变。再试试预设按钮,看看对 \(\mathbf q\) 做加法和数乘,臂会怎么动。

    So far our vectors had two or three components because we could draw them. Robots often handle vectors with many components. The configuration of a six-axis arm, meaning how far each joint has turned, is 6 angles \(\mathbf q=(q_1,\dots,q_6)\): a vector in \(\R^6\).

    We cannot draw a six-dimensional arrow, but the essence of a vector was never "an arrow". It is "a list of numbers that can be added and scaled". Addition, scaling and length all work component by component, exactly as in two dimensions.

    The figure below swaps the six-axis arm for a planar three-link arm, whose configuration is \(\mathbf q\in\R^3\). On the left is the arm; on the right are the three components of \(\mathbf q\), drawn as bars. Drag a joint of the arm or the top of a bar and the other side follows. Then try the preset buttons to see what adding and scaling \(\mathbf q\) does to the arm.

    FIG 5关节角向量 q ∈ ℝ³Joint-angle vector q ∈ ℝ³
    机械臂the arm
    q 的三个分量the 3 components of q
    颜色对应:\(\ci{q_1}\) 是红色的第一节,\(\cj{q_2}\) 是绿色的第二节,\(\ck{q_3}\) 是蓝色的第三节。每个关节角都是相对前一节量的。紫色虚线圆环是目标。预设里的 A 和 B 是两个固定构型。 Color key: \(\ci{q_1}\) is the red first link, \(\cj{q_2}\) the green second, \(\ck{q_3}\) the blue third. Each joint angle is measured relative to the previous link. The dashed purple ring is the target. In the presets, A and B are two fixed configurations.
    • 让手(末端)碰到紫色目标环。Bring the hand (the tip) onto the purple target ring.
    • 点 A + B:逐分量相加之后,对比读数里"手(A) + 手(B)"和"手(A + B)"。Press A + B: after the component-wise sum, compare "hand(A) + hand(B)" with "hand(A + B)" in the readout.
    另一种看法:关节空间里的加法 vs 手的位置Another way to see it: sums in joint space vs. the hand's position

    \(\mathbf q\) 本身是向量,所以 \(\mathbf A+\mathbf B\) 完全合法,它就是"逐个关节把两个角度加起来"。但手的位置不是 \(\mathbf q\) 的线性函数:手(A + B) 一般不等于手(A) + 手(B)。从关节角到手的位置那条映射是弯的,把它在一点附近拉直成矩阵,就是雅可比(3.3 节)。另外,角度每 360° 绕一圈,严格地说构型空间是环面而不是平坦的 \(\R^n\)。这里先忽略,Lynch & Park 第 2 章有完整的讲法。

    \(\mathbf q\) is a vector, so \(\mathbf A+\mathbf B\) is perfectly legal: it adds the two angles joint by joint. But the hand's position is not a linear function of \(\mathbf q\): hand(A + B) is generally not hand(A) + hand(B). The map from joint angles to hand position is curved, and straightening it into a matrix near one point gives the Jacobian (Lesson 3.3). Also, angles wrap around every 360°, so strictly speaking the configuration space is a torus, not a flat \(\R^n\). We ignore that here; Lynch & Park Ch. 2 tells the full story.

    6再看数学The math

    提示:把鼠标悬停在公式中带底色的符号上,FIG 2 或 FIG 6 里对应的箭头会被加粗。Tip: hover the highlighted symbols in the formulas; the matching arrow in FIG 2 or FIG 6 gets thicker.

    记号Notation

    \(\R^n\) 是所有"n 个实数排成一列"的集合:\(\R^2\) 是平面,\(\R^3\) 是空间。本课程里向量一律写成列向量:

    \(\R^n\) is the set of all "n real numbers stacked in a column": \(\R^2\) is the plane and \(\R^3\) is space. In this course vectors are always column vectors:

    $$ \mathbf v = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix} \in \R^n, \qquad \mathbf v\T = \begin{bmatrix} v_1 & v_2 & \cdots & v_n \end{bmatrix}. $$

    \(\mathbf v\T\)(转置)是同一串数写成一行。为什么需要它,1.7 节会讲。

    \(\mathbf v\T\) (the transpose) is the same list written as a row. Why we need it comes in Lesson 1.7.

    加法、减法与数乘Addition, subtraction and scaling

    三种运算都是逐分量的:

    All three operations act component by component:

    $$ \begin{aligned} \hl{sum}{\mathbf a + \mathbf b} &= \begin{bmatrix} \cv{a_1} + \cw{b_1} \\ \cv{a_2} + \cw{b_2} \end{bmatrix}, & \hl{diff}{\mathbf a - \mathbf b} &= \begin{bmatrix} \cv{a_1} - \cw{b_1} \\ \cv{a_2} - \cw{b_2} \end{bmatrix}, & \hl{s}{s\,\mathbf a} &= \begin{bmatrix} s\,\cv{a_1} \\ s\,\cv{a_2} \end{bmatrix}. \end{aligned} $$

    几何上:加法是"首尾相接",数乘是"沿原方向缩放(\(s<0\) 时掉头)",减法是 \(\mathbf a + (-1)\,\mathbf b\),也就是从 \(\mathbf b\) 的尖指向 \(\mathbf a\) 的尖。所有分量都是 0 的向量叫零向量 \(\mathbf 0\)。

    Geometrically: addition is "tip to tail", scaling stretches along the original direction (and turns around when \(s<0\)), and subtraction is \(\mathbf a + (-1)\,\mathbf b\), the arrow from the tip of \(\mathbf b\) to the tip of \(\mathbf a\). The vector whose components are all 0 is the zero vector \(\mathbf 0\).

    运算法则The rules

    对任意向量 \(\mathbf a,\mathbf b,\mathbf c\) 和数 \(s,t\):

    For any vectors \(\mathbf a,\mathbf b,\mathbf c\) and numbers \(s,t\):

    $$ \begin{array}{ll} \mathbf a+\mathbf b=\mathbf b+\mathbf a & \text{(commutative)}\\ (\mathbf a+\mathbf b)+\mathbf c=\mathbf a+(\mathbf b+\mathbf c) & \text{(associative)}\\ \mathbf a+\mathbf 0=\mathbf a,\quad \mathbf a+(-\mathbf a)=\mathbf 0 & \\ s(\mathbf a+\mathbf b)=s\mathbf a+s\mathbf b,\quad (s+t)\mathbf a=s\mathbf a+t\mathbf a & \text{(distributive)}\\ (st)\,\mathbf a=s(t\,\mathbf a),\quad 1\,\mathbf a=\mathbf a & \end{array} $$

    每一条都可以逐分量验证,因为它们对普通的数成立。交换律的几何含义就是 FIG 2 里的平行四边形:两条路到同一点。结合律说的是:多段位移首尾相接,先把哪两段并起来都一样。这就是第 7 节里里程计可以"一步步累加"的原因。

    Each rule can be checked component by component, because it holds for ordinary numbers. The geometric content of commutativity is the parallelogram in FIG 2: two routes, one destination. Associativity says that when several displacements are chained, it does not matter which two you merge first. That is why odometry in Section 7 can simply accumulate step by step.

    长度(范数)Length (the norm)

    二维向量 \(\mathbf v=(x,y)\) 的长度,是以 \(x\) 和 \(y\) 为两条直角边的直角三角形的斜边,由勾股定理:

    The length of a 2D vector \(\mathbf v=(x,y)\) is the hypotenuse of the right triangle whose legs are \(x\) and \(y\). By Pythagoras:

    $$ |\hl{norm}{\cv{\mathbf v}}| = \sqrt{\hl{nx}{\ci{x^2}} + \hl{ny}{\cj{y^2}}}. $$
    FIG 6长度与单位向量Length and the unit vector
    拖动琥珀色的 \(\mathbf v\):接近整数格点或单位圆(青色虚线)时它会吸附。红、绿两条边是 \(x\)、\(y\) 分量;青色的 \(\hat{\mathbf u}\) 是和 \(\mathbf v\) 同方向、长度为 1 的向量。注意"两条边的长度之和"总是不小于斜边。 Drag the amber \(\mathbf v\): it snaps near integer lattice points and onto the unit circle (cyan, dashed). The red and green legs are the \(x\) and \(y\) components; the cyan \(\hat{\mathbf u}\) points the same way as \(\mathbf v\) with length 1. Notice that "sum of the two leg lengths" is never less than the hypotenuse.
    • 找一个整数分量、长度恰为 5 的向量,而且不在坐标轴上。Find a vector with integer components and length exactly 5 that is not on an axis.
    • 让 \(\mathbf v\) 成为单位向量,且不沿坐标轴方向。Make \(\mathbf v\) a unit vector that does not lie along an axis.

    三维时把勾股定理用两次:先在水平面里得到斜边 \(d=\sqrt{x^2+y^2}\),再把 \(d\) 和竖直分量 \(z\) 当作两条直角边:\(|\mathbf v|=\sqrt{d^2+z^2}\)。例如 \(\mathbf v=(2,3,6)\):\(d^2=2^2+3^2=13\),\(|\mathbf v|=\sqrt{13+36}=\sqrt{49}=7\)。同理对任意维数,分量的平方和再开方:

    In 3D, use Pythagoras twice: first get the hypotenuse in the horizontal plane, \(d=\sqrt{x^2+y^2}\), then treat \(d\) and the vertical component \(z\) as the two legs: \(|\mathbf v|=\sqrt{d^2+z^2}\). For \(\mathbf v=(2,3,6)\): \(d^2=2^2+3^2=13\) and \(|\mathbf v|=\sqrt{13+36}=\sqrt{49}=7\). In any dimension the rule is the same: square the components, add, take the root:

    $$ |\mathbf v| = \sqrt{v_1^2 + v_2^2 + \cdots + v_n^2}, \qquad |s\,\mathbf v| = |s|\,|\mathbf v|, \qquad |\mathbf a + \mathbf b| \le |\mathbf a| + |\mathbf b|. $$

    最右边是三角不等式:走两段位移,总位移不会比两段长度之和更长。只有当 \(\mathbf a\)、\(\mathbf b\) 同向(或其中一个为零)时才取等号。这正是 FIG 2 里第二个任务的答案。两点 \(P\)、\(Q\) 之间的距离就是位移的长度 \(|Q-P|\)。

    The rightmost rule is the triangle inequality: two displacements in a row never carry you farther than the sum of their lengths. Equality holds only when \(\mathbf a\) and \(\mathbf b\) point the same way (or one is zero). That is the answer to the second task in FIG 2. The distance between points \(P\) and \(Q\) is the length of the displacement, \(|Q-P|\).

    单位向量Unit vectors

    把非零向量除以自己的长度,就得到只保留方向的单位向量 (unit vector)。反过来,任何向量都是"长度 × 方向":

    Divide a nonzero vector by its own length and you keep only the direction: a unit vector. Conversely, every vector is "length × direction":

    $$ \hl{unit}{\hat{\mathbf u}} = \frac{\cv{\mathbf v}}{|\cv{\mathbf v}|}, \qquad |\hat{\mathbf u}|=1, \qquad \cv{\mathbf v} = |\cv{\mathbf v}|\,\hat{\mathbf u}. $$

    零向量没有方向,所以 \(\hat{\mathbf u}\) 对 \(\mathbf v=\mathbf 0\) 无定义,代码里要先判断长度不为 0。

    The zero vector has no direction, so \(\hat{\mathbf u}\) is undefined for \(\mathbf v=\mathbf 0\); in code, check that the length is nonzero first.

    点与向量的运算,用符号写一遍Points and vectors, written in symbols

    第 4 节的四条规则:\(Q-P=\mathbf d\)(向量),\(P+\mathbf d=Q\)(点),\(\mathbf d_1+\mathbf d_2\)(向量),\(P+Q\)(无意义)。有一种"点加点"是允许的:权重加起来等于 1 的组合,例如中点 \(\tfrac12(P+Q)=P+\tfrac12(Q-P)\)。它其实是"从 \(P\) 出发走一半位移",所以不依赖原点。

    The four rules of Section 4: \(Q-P=\mathbf d\) (a vector), \(P+\mathbf d=Q\) (a point), \(\mathbf d_1+\mathbf d_2\) (a vector), \(P+Q\) (meaningless). One kind of "point plus point" is allowed: a combination whose weights add up to 1, such as the midpoint \(\tfrac12(P+Q)=P+\tfrac12(Q-P)\). It is really "start at \(P\) and walk half the displacement", so it does not depend on the origin.

    7机器人中的它:里程计与航位推算In robotics: odometry and dead reckoning

    一个移动机器人不知道自己在哪,但每一步都能估计"这一步走了多少"(轮子编码器的计数、IMU 的积分)。每一步是一个位移向量 \(\mathbf s_i\)。把它们首尾相接,总位移就是向量之和;机器人现在的位置是"起点 + 总位移",正是第 4 节的"点 + 向量 = 点":

    A mobile robot does not know where it is, but at every step it can estimate how far it just moved (from wheel-encoder counts or integrated IMU data). Each step is a displacement vector \(\mathbf s_i\). Chain them tip to tail and the total displacement is the vector sum. The robot's current position is "start + total displacement", exactly the "point + vector = point" rule of Section 4:

    $$ \mathbf p_N = \mathbf p_0 + \sum_{i=1}^{N} \cv{\mathbf s_i}. $$

    这种只靠累加位移来估计位置的方法叫航位推算 (dead reckoning)。琥珀色是真实的路径,紫色虚线是带噪声的估计:每一步的估计 = 真值 + 一点随机误差。拖动任意一个节点,改变那一步的位移(后面的节点会整体平移);用滑块加噪声,看终点的误差怎么积累。

    Estimating position purely by accumulating displacements is called dead reckoning. The amber path is the truth; the dashed purple path is the noisy estimate: each estimated step is the true step plus a little random error. Drag any node to change that one step (the later nodes shift along with it), and use the slider to add noise and watch the end-point error build up.

    FIG 7步子连成路径Steps chained into a path
    不带颜色的虚线箭头是总位移 \(\sum\mathbf s_i\),它连接起点和真实终点,与中间怎么走无关。红色箭头是估计终点相对真实终点的误差,它等于各步噪声之和。 The uncoloured dashed arrow is the total displacement \(\sum\mathbf s_i\): it joins the start to the true end and does not care how the path wandered in between. The red arrow is the estimated end relative to the true end; it equals the sum of the per-step noises.
    • 拖动节点,让路径回到起点(总位移为零)。路径的总长度还能很长吗?Drag the nodes so the path returns to its start (zero total displacement). Can the path still be long?
    • 加大噪声,让终点误差超过 1 m。Raise the noise until the end-point error exceeds 1 m.
    真实系统里In real systems

    轮式里程计给出的步长是在机器人自己的坐标系里量的("向前 0.3 米"),累加之前要先用旋转矩阵换到世界坐标系(1.10 节、5.1 节),本图为了专注向量加法,假设步长已经在世界系里。误差会随步数越积越大,所以实际系统总要用 GPS、激光匹配或卡尔曼滤波(4.8 节)不断校正。

    Wheel odometry reports steps in the robot's own frame ("0.3 m forward"). Before accumulating they must be rotated into the world frame (Lessons 1.10 and 5.1); this figure assumes the steps are already in world coordinates so we can focus on vector addition. The error grows with every step, so real systems keep correcting it with GPS, lidar matching or a Kalman filter (Lesson 4.8).

    同一个想法还有一个更小的例子:速度也是向量,可以相加。一条船相对水以 3 m/s 向北开,水流以 1 m/s 向东,那么船相对地面的速度是 \((0,3)+(1,0)=(1,3)\) m/s,速率 \(\sqrt{10}\approx3.16\) m/s,航向偏离正北向东约 18.4°。无人机顶着侧风飞、传送带上的机械臂抓取,都是同样的加法。

    A smaller example of the same idea: velocities are vectors too, and they add. A boat heads north at 3 m/s relative to the water while the current flows east at 1 m/s. Its velocity over the ground is \((0,3)+(1,0)=(1,3)\) m/s, a speed of \(\sqrt{10}\approx3.16\) m/s, tilted about 18.4° east of north. A drone flying in a crosswind, or an arm grasping from a moving conveyor, uses the same addition.

    8常见误区Common pitfalls

    误区 1:把两个位置相加Pitfall 1: adding two positions

    "杯子在 (2, 1),盒子在 (4, 3),两者相加得 (6, 4)",这个数没有物理含义:换一个原点它就变了(FIG 4)。要加的是位移,要减的才是两个位置。想求"两者的中间",用 \(\tfrac12(P+Q)\),因为它的权重之和是 1。

    "The cup is at (2, 1), the box at (4, 3), so their sum is (6, 4)": that number has no physical meaning, because it changes when the origin changes (FIG 4). Add displacements; subtract positions. For "the point halfway between them" use \(\tfrac12(P+Q)\), whose weights add up to 1.

    误区 2:把不同单位的量放进同一个向量再算长度Pitfall 2: taking the length of a vector with mixed units

    机器人位姿常写成 \((x,y,\theta)\),前两项是米,第三项是弧度。\(\sqrt{x^2+y^2+\theta^2}\) 这个"长度"没有意义:把 \(\theta\) 改用度,数值就完全不同(见小练习第 5 题)。要比较两个位姿的"远近",必须先决定权重,例如乘上一个特征长度,把角度换成"弧长"。

    Robot poses are often written \((x,y,\theta)\): the first two in metres, the third in radians. The "length" \(\sqrt{x^2+y^2+\theta^2}\) is meaningless: switch \(\theta\) to degrees and the number is completely different (see quiz question 5). To compare how far apart two poses are, you must first choose weights, for example a characteristic length that turns the angle into an arc length.

    误区 3:长度等于分量之和Pitfall 3: the length is the sum of the components

    \(|(3,4)|=5\),不是 7。"分量之和"(7)是出租车走过的路程:先横着走 3 再竖着走 4。同样,\(|\mathbf a+\mathbf b|\) 一般小于 \(|\mathbf a|+|\mathbf b|\),只有同向时才相等(三角不等式)。

    \(|(3,4)|=5\), not 7. The "sum of components" (7) is the route of a taxi that drives 3 across and then 4 up. Likewise \(|\mathbf a+\mathbf b|\) is in general smaller than \(|\mathbf a|+|\mathbf b|\), with equality only for the same direction (the triangle inequality).

    误区 4:代码里的"行"与"列"Pitfall 4: "row" versus "column" in code

    本课程里向量是列向量,但 NumPy 的一维数组 v = np.array([1.0, 2.0, 3.0]) 的形状是 (3,),既不是行也不是列:对它做 v.T 什么也不会发生。a * b 是逐元素相乘,不是点积(点积用 a @ b,1.7 节);长度用 np.linalg.norm(v),不是 sum(v)。需要真正的列向量时写 v.reshape(3, 1)。

    In this course vectors are columns, but a NumPy 1-D array v = np.array([1.0, 2.0, 3.0]) has shape (3,): neither row nor column, so v.T does nothing. a * b multiplies element by element and is not the dot product (that is a @ b, Lesson 1.7); the length is np.linalg.norm(v), not sum(v). When you need a real column, write v.reshape(3, 1).

    9小练习Check yourself

    1. 机器人在坐标系 A 里测得杯子 \(P=(3,1)\)、盒子 \(Q=(7,4)\)。现在把坐标系的原点向右平移 1(物体都没动)。下面哪个量不会改变?1. In frame A a robot measures a cup \(P=(3,1)\) and a box \(Q=(7,4)\). Now the frame's origin is shifted 1 unit to the right (the objects did not move). Which quantity does not change?
    新读数是 \(P=(2,1)\)、\(Q=(6,4)\),每个坐标的 x 都减了 1。\(Q-P\) 仍是 \((4,3)\);\(P+Q\) 从 \((10,5)\) 变成 \((8,5)\);\(|Q|\) 从 \(\sqrt{65}\approx8.06\) 变成 \(\sqrt{52}\approx7.21\)。位移不依赖原点。The new readings are \(P=(2,1)\) and \(Q=(6,4)\): every x lost 1. \(Q-P\) is still \((4,3)\); \(P+Q\) went from \((10,5)\) to \((8,5)\); \(|Q|\) went from \(\sqrt{65}\approx8.06\) to \(\sqrt{52}\approx7.21\). Displacements do not depend on the origin.
    2. 设 \(\mathbf a=(2,-1)\),\(\mathbf b=(-3,4)\)。\(2\mathbf a-\mathbf b\) 等于?2. Let \(\mathbf a=(2,-1)\) and \(\mathbf b=(-3,4)\). What is \(2\mathbf a-\mathbf b\)?
    \(2\mathbf a=(4,-2)\),再减去 \(\mathbf b\):\((4+3,\,-2-4)=(7,-6)\)。其他选项分别是 \(2\mathbf a+\mathbf b=(1,2)\)、\(2\mathbf b-\mathbf a=(-8,9)\)、\(\mathbf a+\mathbf b=(-1,3)\),都是符号或系数弄错的结果。\(2\mathbf a=(4,-2)\), then subtract \(\mathbf b\): \((4+3,\,-2-4)=(7,-6)\). The other options are \(2\mathbf a+\mathbf b=(1,2)\), \(2\mathbf b-\mathbf a=(-8,9)\) and \(\mathbf a+\mathbf b=(-1,3)\): each comes from a slipped sign or coefficient.
    3. \(\mathbf v=(3,-4)\) 方向上的单位向量是?3. What is the unit vector in the direction of \(\mathbf v=(3,-4)\)?
    \(|\mathbf v|=\sqrt{9+16}=5\),所以 \(\hat{\mathbf u}=(3/5,-4/5)=(0.6,-0.8)\)。\((3/7,-4/7)\) 是除以了分量之和(长度 \(5/7\ne1\));\((3/25,-4/25)\) 是除以了长度的平方(长度 \(1/5\));\((0.8,-0.6)\) 长度虽是 1,方向却不对(它更偏向 x 轴)。\(|\mathbf v|=\sqrt{9+16}=5\), so \(\hat{\mathbf u}=(3/5,-4/5)=(0.6,-0.8)\). \((3/7,-4/7)\) divides by the sum of components (its length is \(5/7\ne1\)); \((3/25,-4/25)\) divides by the squared length (its length is \(1/5\)); \((0.8,-0.6)\) has length 1 but the wrong direction (it leans more toward the x-axis).
    4. 机器人依次向东走 3 m、向北走 4 m、向西走 3 m。它的总位移是?4. A robot drives 3 m east, then 4 m north, then 3 m west. What is its total displacement?
    \((3,0)+(0,4)+(-3,0)=(0,4)\),长度 4 m。路程(各步长度之和)是 \(3+4+3=10\) m,位移长度只有 4 m:\(|\sum\mathbf s_i|\le\sum|\mathbf s_i|\)。东西分量抵消了,但南北分量没有。\((3,0)+(0,4)+(-3,0)=(0,4)\), of length 4 m. The distance driven (the sum of the step lengths) is \(3+4+3=10\) m, while the displacement is only 4 m long: \(|\sum\mathbf s_i|\le\sum|\mathbf s_i|\). The east-west components cancel; the north-south one does not.
    5. 位姿 \((x,y,\theta)=(3\text{ m},\,4\text{ m},\,0.5\text{ rad})\)。有人算它的"长度" \(\sqrt{3^2+4^2+0.5^2}\approx5.02\)。这个做法如何?5. A pose is \((x,y,\theta)=(3\text{ m},\,4\text{ m},\,0.5\text{ rad})\). Someone computes its "length" \(\sqrt{3^2+4^2+0.5^2}\approx5.02\). What do you make of that?
    公式本身没错,问题在于米和弧度不是同一种量,加起来的结果取决于你选了什么单位:用度时 \(\sqrt{9+16+28.65^2}\approx29.08\)。"弧度"只是碰巧让数值看起来合理。要衡量位姿之间的差距,需要显式地加权,例如 \(\sqrt{x^2+y^2+(\ell\theta)^2}\),其中 \(\ell\) 是一个以米为单位的特征长度(比如机器人半径)。The formula is fine; the problem is that metres and radians are different kinds of quantity, so the sum depends on the units you pick: in degrees it is \(\sqrt{9+16+28.65^2}\approx29.08\). Radians only happen to give a number that looks reasonable. To measure how far apart poses are, weight explicitly, for instance \(\sqrt{x^2+y^2+(\ell\theta)^2}\) with a characteristic length \(\ell\) in metres (say the robot's radius).

    10延伸阅读Further reading

    • 3Blue1Brown, Essence of Linear Algebra — 第 1 章"向量究竟是什么?"(箭头、数字、抽象三种看法)Ch. 1 "Vectors, what even are they?" (the arrow, number-list and abstract views)
    • G. Strang, Introduction to Linear Algebra, Ch. 1 — 向量入门:线性组合与长度Introduction to vectors: linear combinations and lengths
    • K. Lynch & F. Park, Modern Robotics, Ch. 2 — 构型空间:为什么机器人的状态是 \(\R^n\) 中的一个点(以及它什么时候不是平坦的)Configuration space: why a robot's state is a point in \(\R^n\) (and when that space is not flat)
    • J. Ström et al., Immersive Linear Algebra, Ch. 2 — 带交互插图的向量一章The interactive chapter on vectors