点积The Dot Product
点积把两个向量变成一个数,回答一个简单的问题:这两支箭头有多同向?正数表示大致同向,零表示垂直,负数表示相反。障碍物在前还是在后,目标在不在视野里,速度有多少真正朝着目标,一个力做了多少功,甚至一句话和一张图像配不配,用的都是这一个运算。 The dot product turns two vectors into one number that answers a simple question: how much do the two arrows agree? Positive means roughly the same direction, zero means perpendicular, negative means opposed. Whether an obstacle is ahead or behind, whether a target is in view, how much of the velocity points at the goal, how much work a force does, even whether a sentence matches an image: it is all this one operation.
1为什么机器人需要它Why robots need this
机器人每时每刻都在回答一类很小的问题:
- 前方检测到一个障碍物,它在我的前面还是后面?
- 那个人在不在我的相机视野里?
- 我现在的速度,有多少是真正朝着目标的?
- 机械臂推着一个箱子,我的力里有多少在做功,有多少只是白白顶着墙?
- 这句指令和这张图像不像?(CLIP 这类模型正是这样把语言和图像配对的,见 Part V。)
这些问题看上去毫不相干,答案却都来自同一个运算:点积 (dot product)。它还有一个隐藏身份:1.3 节里矩阵乘向量的"行视角",每一个分量就是一次点积。这一节把它的两种写法、两种读法,以及机器人最常用的几个场景,一次讲清楚。
A robot is constantly answering a family of tiny questions:
- An obstacle was detected: is it in front of me or behind me?
- Is that person inside my camera's field of view?
- How much of my current velocity actually points toward the goal?
- The arm pushes a box: how much of my force does work, and how much just presses against the wall?
- Does this instruction match this image? (Models like CLIP pair language with images exactly this way; see Part V.)
These questions look unrelated, yet every answer comes from one operation: the dot product. It also has a hidden second identity: the "row view" of a matrix–vector product from Lesson 1.3 computes each entry as a dot product. This lesson covers its two formulas, its two readings, and the places robots use it most.
2直觉:两支箭头有多"一致"Intuition: how much do two arrows agree?
取两支箭头 \(\mathbf a\) 和 \(\mathbf b\),夹角为 \(\theta\)。把 \(\mathbf b\) 垂直地"投"到 \(\mathbf a\) 所在的直线上,得到一段影子,它的带符号长度是 \(|\mathbf b|\cos\theta\)。再乘上 \(\mathbf a\) 自己的长度,就是点积:
Take two arrows \(\mathbf a\) and \(\mathbf b\) with angle \(\theta\) between them. Drop \(\mathbf b\) perpendicularly onto the line through \(\mathbf a\) to get a shadow whose signed length is \(|\mathbf b|\cos\theta\). Multiply by the length of \(\mathbf a\) itself and you have the dot product:
影子和 \(\mathbf a\) 同向,结果为正;影子反向,结果为负;\(\mathbf b\) 垂直于 \(\mathbf a\) 时,影子缩成一个点,结果为 0。
最具体的例子是推车。小车只能沿导轨走,你推它的力 \(\mathbf F\) 却常常是斜的。真正推动小车的只有沿导轨的那一份,垂直于导轨的那一份只是顶着导轨。物理里"力做的功"正是 \(W = \mathbf F\cdot\mathbf s\),\(\mathbf s\) 是小车的位移。拖动力的箭头,看看什么时候做正功、零功、负功。
A shadow pointing the same way as \(\mathbf a\) gives a positive result, an opposing shadow gives a negative one, and when \(\mathbf b\) is perpendicular to \(\mathbf a\) the shadow shrinks to a point and the result is 0.
The most concrete example is pushing a cart. The cart can only roll along its rail, but your force \(\mathbf F\) is often slanted. Only the part of the force along the rail moves the cart; the part across the rail just presses on it. In physics the work done by a force is exactly \(W = \mathbf F\cdot\mathbf s\), where \(\mathbf s\) is the cart's displacement. Drag the force arrow and see when the work is positive, zero, or negative.
- 用一个不小的力(\(|\mathbf F| \ge 2\) N)推车,但让它做的功几乎为 0。力应该朝哪?Push with a sizeable force (\(|\mathbf F| \ge 2\) N) that does almost no work. Which way must it point?
- 让这个力做负功(\(W < -2\) J),也就是在给小车刹车。Make the force do negative work (\(W < -2\) J): it brakes the cart.
点积 = 一个向量中沿另一个向量方向的那一部分,乘上另一个向量的长度。符号告诉你方向关系:正(夹角小于 90°)、零(垂直)、负(夹角大于 90°)。它不关心垂直于那个方向的部分。
The dot product is the part of one vector that lies along the other vector, times the length of the other. Its sign tells you the relationship: positive (angle under 90°), zero (perpendicular), negative (angle over 90°). It ignores everything perpendicular to that direction.
3动手玩:点积实验室Play: the dot-product lab
拖动琥珀色 a 和紫色 b 的箭头尖。紫色虚线从 b 的尖端垂直落到 a 所在的直线上,落点与原点之间的粗线就是 b 的影子:为正时绿色,为负时红色。淡绿、淡红的底色标出 b 落在哪一侧时点积为正、为负。右边用两个公式各算一遍同一个数,看看它们是否永远一致。
Drag the tips of the amber a and the purple b. The purple dashed line drops perpendicularly from the tip of b onto the line through a; the thick band between the foot and the origin is the shadow of b: green when positive, red when negative. The faint green and red background marks where b must lie for the dot product to be positive or negative. On the right, two formulas compute the same number; see whether they ever disagree.
- 让 \(\mathbf a\cdot\mathbf b = 0\),而且两个向量都不是零向量。此时影子是什么样?Make \(\mathbf a\cdot\mathbf b = 0\) with both vectors non-zero. What does the shadow look like?
- 两个向量的长度不变,让点积尽可能小(最负)。这时夹角是多少?Keep the lengths as they are and make the dot product as negative as possible. What is the angle?
- 让 \(\mathbf a\cdot\mathbf b = |\mathbf a|^2\),但 \(\mathbf b \ne \mathbf a\)。提示:影子要和 \(\mathbf a\) 一样长。Make \(\mathbf a\cdot\mathbf b = |\mathbf a|^2\) while \(\mathbf b \ne \mathbf a\). Hint: the shadow must be exactly as long as \(\mathbf a\).
- 打开"等值线",保持 a 不动,把 b 沿那条线拖出至少 1.5 的距离:点积一动不动。为什么?Turn on "Level line", keep a fixed and drag b at least 1.5 units along that line: the dot product does not budge. Why?
点积只看 b 在 a 方向上的分量(影子)。沿着垂直于 a 的方向移动 b,影子的长度完全不变,所以点积也不变。于是 \(\{\mathbf x : \mathbf a\cdot\mathbf x = c\}\) 是一条垂直于 \(\mathbf a\) 的直线(三维里是垂直于 \(\mathbf a\) 的平面)。"平面方程 \(\mathbf n\cdot\mathbf x = d\)"就是这个事实:\(\mathbf n\) 是法向量。另外两个极端也值得记住:垂直时点积为 0;反向(\(\theta = 180°\))时点积取到最小值 \(-|\mathbf a||\mathbf b|\)。
The dot product only looks at the component of b along a (the shadow). Moving b in a direction perpendicular to a leaves the shadow's length untouched, so the dot product stays put. Hence \(\{\mathbf x : \mathbf a\cdot\mathbf x = c\}\) is a line perpendicular to \(\mathbf a\) (a plane perpendicular to \(\mathbf a\) in 3D). The "plane equation \(\mathbf n\cdot\mathbf x = d\)" is exactly this fact, with \(\mathbf n\) the normal. Two extremes are worth remembering too: perpendicular vectors give 0, and opposed vectors (\(\theta = 180°\)) give the minimum \(-|\mathbf a||\mathbf b|\).
4第二个视角:两个公式为什么是同一个数A second view: why the two formulas agree
实验室右边有两个公式。代数公式 \(a_1b_1 + a_2b_2\) 要用到坐标,换一套坐标轴,每个分量都变了。几何公式 \(|\mathbf a||\mathbf b|\cos\theta\) 只用到长度和夹角,而把整幅图转个角度,长度和夹角都不会变。两者要想永远相等,分量的乘积就必须"此消彼长",使总和保持不变。
下面把 \(\mathbf a\) 和 \(\mathbf b\) 当作一个刚性的整体一起转动,用三根柱子看这件事。
The lab shows two formulas. The algebraic formula \(a_1b_1 + a_2b_2\) uses coordinates: pick different axes and every component changes. The geometric formula \(|\mathbf a||\mathbf b|\cos\theta\) uses only lengths and the angle, and rotating the whole picture changes neither. For the two to agree forever, the component products must trade off against each other so that their sum stays put.
Below, \(\mathbf a\) and \(\mathbf b\) are rotated together as one rigid pair. Watch the three bars.
- 找一个角度,让红柱 \(a_1b_1\) 变成负数,而青柱依然是同一个正数。Find an angle where the red bar \(a_1b_1\) goes negative while the cyan bar is still the same positive number.
- 转到 a 正好躺在 x 轴上(\(a_2 \approx 0\))。这时绿柱消失,点积就只剩 \(a_1b_1\)。Rotate until a lies along the x-axis (\(a_2 \approx 0\)). The green bar vanishes and the dot product reduces to \(a_1b_1\).
点积是一个不依赖坐标轴朝向的量:它只由两个长度和一个夹角决定。这正是它在机器人里如此好用的原因:不管你用世界坐标系、机器人坐标系还是相机坐标系,只要两个向量在同一个坐标系里写出来,点积都是同一个数(1.10 节会把这一点升级成"正交矩阵保持点积")。
The dot product is independent of how the axes are oriented: it is fixed by two lengths and one angle. That is why it is so useful in robotics: whether you use the world frame, the robot frame or the camera frame, as long as both vectors are written in the same frame, the dot product is the same number (Lesson 1.10 upgrades this to "orthogonal matrices preserve dot products").
5第三个视角:矩阵乘向量的行视角A third view: the row view of a matrix–vector product
1.3 节按列读矩阵乘向量:\(A\mathbf v\) 是 \(A\) 各列的线性组合。同一个乘积还有第二种读法,也是代码里真正在算的那一种:\(A\mathbf v\) 的第 \(i\) 个分量,是 \(A\) 的第 \(i\) 行与 \(\mathbf v\) 的点积。
可以把每一行想成一个"探测器",它有自己的朝向 \(\mathbf r_i\),读出的是 \(\mathbf v\) 在自己方向上有多少。下面左图画出两行 \(\mathbf r_1, \mathbf r_2\) 和 \(\mathbf v\),右图用列视角拼出同一个 \(A\mathbf v\)。两边的结果必须一样。
Lesson 1.3 read a matrix–vector product by columns: \(A\mathbf v\) is a combination of the columns of \(A\). The same product has a second reading, the one code actually computes: the \(i\)-th entry of \(A\mathbf v\) is the dot product of row \(i\) of \(A\) with \(\mathbf v\).
Think of each row as a "detector" with its own direction \(\mathbf r_i\); it reads out how much of \(\mathbf v\) lies along that direction. On the left, the two rows \(\mathbf r_1, \mathbf r_2\) and \(\mathbf v\); on the right, the column view assembles the same \(A\mathbf v\). Both sides must agree.
- 让第一行与 \(\mathbf v\) 垂直。右图里 \(A\mathbf v\) 的 x 分量会变成 0,箭头落到 y 轴上。Make row 1 perpendicular to \(\mathbf v\). On the right, the x-entry of \(A\mathbf v\) becomes 0 and the arrow lands on the y-axis.
- 让 \(A\mathbf v = \mathbf 0\),而 \(\mathbf v \ne \mathbf 0\),两行也都不是零向量。两行之间必须是什么关系?Make \(A\mathbf v = \mathbf 0\) with \(\mathbf v \ne \mathbf 0\) and both rows non-zero. What must the two rows look like relative to each other?
第二个任务的答案:\(\mathbf v\) 同时垂直于两行,所以两行只能互相平行(在平面里垂直于 \(\mathbf v\) 的方向只有一条线)。这样的 \(\mathbf v\) 被矩阵压成 \(\mathbf 0\),所有这样的 \(\mathbf v\) 组成零空间 (null space),1.6 节会专门讲。行视角还有一个后果:矩阵 \(A^\top A\) 的第 \((i,j)\) 项,是 \(A\) 的第 \(i\) 列与第 \(j\) 列的点积,1.12 和 1.13 节里会反复出现。
Answer to the second task: \(\mathbf v\) must be perpendicular to both rows, so the two rows can only be parallel to each other (in the plane there is just one line perpendicular to \(\mathbf v\)). Such a \(\mathbf v\) is crushed to \(\mathbf 0\), and all such vectors form the null space, the topic of Lesson 1.6. The row view has another consequence: entry \((i,j)\) of \(A^\top A\) is the dot product of column \(i\) with column \(j\) of \(A\), which keeps reappearing in Lessons 1.12 and 1.13.
6再看数学The math
提示:把鼠标悬停在公式中带底色的符号上,FIG 2 中对应的元素会被高亮。Tip: hover the highlighted symbols in the formulas; the matching element in FIG 2 lights up.
定义Definition
两个同维度的向量 \(\mathbf a, \mathbf b\in\R^n\),点积是对应分量乘积之和,也可以写成行向量乘列向量:
For two vectors \(\mathbf a, \mathbf b\in\R^n\) of the same dimension, the dot product is the sum of products of matching entries, which is also a row vector times a column vector:
例如 \((1,2,3)\cdot(4,-5,6) = 4 - 10 + 18 = 12\)。结果是一个标量,不是向量。
For example, \((1,2,3)\cdot(4,-5,6) = 4 - 10 + 18 = 12\). The result is a scalar, not a vector.
性质Properties
- 对称:\(\mathbf a\cdot\mathbf b = \mathbf b\cdot\mathbf a\)。
- 线性(对每个位置分别线性):\(\mathbf a\cdot(\mathbf b + \mathbf c) = \mathbf a\cdot\mathbf b + \mathbf a\cdot\mathbf c\),\((s\mathbf a)\cdot\mathbf b = s\,(\mathbf a\cdot\mathbf b)\)。
- 与长度的关系:\(\mathbf a\cdot\mathbf a = |\mathbf a|^2\),所以 \(|\mathbf a| = \sqrt{\mathbf a\cdot\mathbf a}\)。
- Symmetric: \(\mathbf a\cdot\mathbf b = \mathbf b\cdot\mathbf a\).
- Linear (in each slot separately): \(\mathbf a\cdot(\mathbf b + \mathbf c) = \mathbf a\cdot\mathbf b + \mathbf a\cdot\mathbf c\), \((s\mathbf a)\cdot\mathbf b = s\,(\mathbf a\cdot\mathbf b)\).
- Link to length: \(\mathbf a\cdot\mathbf a = |\mathbf a|^2\), so \(|\mathbf a| = \sqrt{\mathbf a\cdot\mathbf a}\).
几何形式从哪里来Where the geometric form comes from
把差向量 \(\mathbf a-\mathbf b\) 当作三角形的第三条边。几何上,余弦定理说:
Treat \(\mathbf a-\mathbf b\) as the third side of a triangle. Geometrically, the law of cosines says:
代数上,用上面的对称性与线性把同一个量展开:
Algebraically, expand the same quantity with symmetry and linearity:
两式相比,\(|\mathbf a|^2 + |\mathbf b|^2\) 消掉,剩下:
Compare the two: the terms \(|\mathbf a|^2 + |\mathbf b|^2\) cancel and what remains is
这就是 FIG 3 里"代数 = 几何"的证明。用到了余弦定理,所以它在任意维度都成立:任意两个向量总落在同一个平面里,夹角 \(\theta\) 就是那个平面里的夹角。
That is the proof behind "algebra = geometry" in FIG 3. It uses the law of cosines, so it holds in any dimension: any two vectors lie in a common plane, and \(\theta\) is the angle within that plane.
夹角、正交与影子长度Angle, orthogonality and shadow length
"clamp"把括号里的数夹在 \([-1, 1]\) 之内(原因见"常见误区")。例:\(\mathbf a = (3,1)\),\(\mathbf b = (1,2)\),则 \(\mathbf a\cdot\mathbf b = 5\),\(|\mathbf a||\mathbf b| = \sqrt{10}\sqrt{5} = \sqrt{50}\),\(\cos\theta = 5/\sqrt{50} = \tfrac{\sqrt2}{2}\),\(\theta = 45°\)。
当 \(\mathbf u\) 是单位向量时,\(\mathbf u\cdot\mathbf b\) 就是 \(\mathbf b\) 在 \(\mathbf u\) 方向上的带符号长度。把这个长度再乘回 \(\mathbf u\),得到完整的"投影向量" \((\mathbf u\cdot\mathbf b)\,\mathbf u\),那是 1.9 节的主题。
"Clamp" squeezes the quantity in the parentheses into \([-1, 1]\) (see the pitfalls for why). Example: \(\mathbf a = (3,1)\), \(\mathbf b = (1,2)\) give \(\mathbf a\cdot\mathbf b = 5\), \(|\mathbf a||\mathbf b| = \sqrt{10}\sqrt{5} = \sqrt{50}\), \(\cos\theta = 5/\sqrt{50} = \tfrac{\sqrt2}{2}\), so \(\theta = 45°\).
When \(\mathbf u\) is a unit vector, \(\mathbf u\cdot\mathbf b\) is the signed length of \(\mathbf b\) along \(\mathbf u\). Multiply it back onto \(\mathbf u\) and you get the full "projection vector" \((\mathbf u\cdot\mathbf b)\,\mathbf u\), the subject of Lesson 1.9.
Cauchy–Schwarz 与余弦相似度Cauchy–Schwarz and cosine similarity
因为 \(|\cos\theta|\le 1\),立刻得到
Since \(|\cos\theta|\le 1\), we immediately get
第一个不等式叫 Cauchy–Schwarz 不等式。它回答了"固定长度时点积最大能到多少":当两者同向时取到 \(|\mathbf a||\mathbf b|\)。第二个量是余弦相似度,先把两个向量都除以自己的长度,再做点积:它只比较方向,不受长度影响,是机器学习里比较 embedding 的标准做法。
The inequality is the Cauchy–Schwarz inequality. It answers "how large can the dot product get at fixed lengths": the maximum \(|\mathbf a||\mathbf b|\) is reached when the two point the same way. The second quantity is cosine similarity: divide each vector by its own length, then take the dot product. It compares directions only, ignores lengths, and is the standard way to compare embeddings in machine learning.
矩阵写法与旋转不变性Matrix notation and rotation invariance
\(\mathbf a^\top\mathbf b\) 是 \(1\times n\) 乘 \(n\times 1\),得到 \(1\times 1\),也就是一个数;反过来 \(\mathbf a\mathbf b^\top\) 是 \(n\times n\) 的矩阵(外积,1.12 节的"一层")。行视角则写成 \((A\mathbf v)_i = \mathbf r_i^\top\mathbf v\)。若 \(R\) 是旋转矩阵,满足 \(R^\top R = I\)(1.10 节),那么 FIG 3 的观察就是一个等式:
\(\mathbf a^\top\mathbf b\) is \(1\times n\) times \(n\times 1\), giving \(1\times 1\), a number; the other order, \(\mathbf a\mathbf b^\top\), is an \(n\times n\) matrix (the outer product, one "layer" in Lesson 1.12). The row view reads \((A\mathbf v)_i = \mathbf r_i^\top\mathbf v\). If \(R\) is a rotation matrix with \(R^\top R = I\) (Lesson 1.10), the observation in FIG 3 becomes an identity:
7机器人中的它:视野判断与朝向目标的速度In robotics: field-of-view tests and speed toward the goal
视野判断:目标在不在视野里?Field-of-view test: is the target in view?
机器人的朝向是单位向量 \(\hat{\mathbf h}\),相机的视野半角是 \(\alpha\)。物体相对机器人的位移是 \(\mathbf d = \mathbf p_{\text{obj}} - \mathbf p_{\text{robot}}\)。物体可见,当且仅当 \(\mathbf d\) 与 \(\hat{\mathbf h}\) 的夹角 \(\varphi\le\alpha\)。因为 \(\cos\) 在 \([0,\pi]\) 上递减,这等价于
The robot's heading is a unit vector \(\hat{\mathbf h}\) and the camera's field of view has half-angle \(\alpha\). The object's displacement from the robot is \(\mathbf d = \mathbf p_{\text{obj}} - \mathbf p_{\text{robot}}\). The object is visible exactly when the angle \(\varphi\) between \(\mathbf d\) and \(\hat{\mathbf h}\) satisfies \(\varphi\le\alpha\). Since \(\cos\) is decreasing on \([0,\pi]\), this is equivalent to
右边的 \(\cos\alpha\) 只算一次,每个物体只需要一次点积和一次开方,不需要 \(\operatorname{atan2}\) 或 \(\arccos\)。拖动机器人的朝向(红色箭头尖),再调节视野半角,看看右侧表格里每个物体的"点积 ≥ 阈值"是怎么变的。
\(\cos\alpha\) is computed once, and each object needs one dot product and one square root, with no \(\operatorname{atan2}\) or \(\arccos\). Drag the robot's heading (the red arrow tip), adjust the half-angle, and watch the "dot ≥ threshold" comparison for each object in the table on the right.
- 把视野半角调到 30° 或更小(窄镜头),转动机器人,让至少 3 个物体同时在视野里。Set the half-angle to 30° or less (a narrow lens) and turn the robot so that at least 3 objects are in view at once.
- 让某个物体恰好卡在视野边缘(夹角与 \(\alpha\) 相差不到 1.5°,它的圆环会变成琥珀色)。这时不等式两边几乎相等。Park an object exactly on the edge of the view (angle within 1.5° of \(\alpha\); its ring turns amber). The two sides of the inequality are then almost equal.
朝向目标的速度:贪心控制器的核心Speed toward the goal: the core of a greedy controller
机器人在原点,目标在 \(\mathbf G\),指向目标的单位向量是 \(\hat{\mathbf g} = \mathbf G/|\mathbf G|\)。速度 \(\mathbf v\) 中朝向目标的那一份,就是逼近速度 \(s = \mathbf v\cdot\hat{\mathbf g}\):每秒大约离目标近多少米。假设速度大小不能超过 \(v_{\max}\),什么方向最好?Cauchy–Schwarz 给出答案:\(s\le|\mathbf v||\hat{\mathbf g}|\le v_{\max}\),当且仅当 \(\mathbf v = v_{\max}\hat{\mathbf g}\) 时取等号。
很多局部规划器的做法与此一脉相承:生成一批候选速度(图中速度圆上的小点),给每个候选打分,选分数最高的。点积 \(\mathbf v\cdot\hat{\mathbf g}\) 就是最基本的"朝向目标"得分。
The robot sits at the origin, the goal is at \(\mathbf G\), and the unit direction to the goal is \(\hat{\mathbf g} = \mathbf G/|\mathbf G|\). The part of the velocity \(\mathbf v\) that points at the goal is the closing speed \(s = \mathbf v\cdot\hat{\mathbf g}\): roughly how many meters per second you get closer. If the speed cannot exceed \(v_{\max}\), which direction is best? Cauchy–Schwarz answers: \(s\le|\mathbf v||\hat{\mathbf g}|\le v_{\max}\), with equality exactly when \(\mathbf v = v_{\max}\hat{\mathbf g}\).
Many local planners follow the same idea: generate candidate velocities (the dots on the speed circle in the figure), score each one, and pick the best. The dot product \(\mathbf v\cdot\hat{\mathbf g}\) is the most basic "toward the goal" score.
- 让逼近速度达到上限的 97% 以上(\(s \ge 1.94\) m/s)。v 应该指向哪里?Reach a closing speed above 97% of the limit (\(s \ge 1.94\) m/s). Where must v point?
- 保持几乎全速(\(|\mathbf v| \ge 1.7\)),但让逼近速度几乎为 0(\(|s| < 0.08\))。机器人这时在做什么?Keep nearly full speed (\(|\mathbf v| \ge 1.7\)) while the closing speed is almost zero (\(|s| < 0.08\)). What is the robot doing?
第二个任务的答案:\(\mathbf v\perp\hat{\mathbf g}\),机器人绕着目标转圈,既不靠近也不远离,一直在白费力气。
Answer to the second task: \(\mathbf v\perp\hat{\mathbf g}\). The robot circles the goal, neither approaching nor receding, spending effort for nothing.
功率。力与速度的点积 \(P = \mathbf F\cdot\mathbf v\) 是瞬时功率;对关节,\(P = \boldsymbol\tau\cdot\dot{\mathbf q}\),即关节力矩向量与关节速度向量的点积。\(P>0\) 说明电机在向机械臂输出能量,\(P<0\) 说明外力在拖着电机转,能量流回驱动器(再生制动)。能耗估计、电机发热和功率限制都从这个点积出发。
余弦相似度。CLIP 这样的模型把图像和文字都映射成同一空间中的向量(embedding),归一化后,匹配分数就是点积,也就是余弦相似度。机器人用它判断"拿起红色的杯子"这句话和相机画面里哪个区域最匹配。对比学习的细节见 Part V。
Power. The dot product of force and velocity, \(P = \mathbf F\cdot\mathbf v\), is instantaneous power; for joints, \(P = \boldsymbol\tau\cdot\dot{\mathbf q}\), the joint-torque vector dotted with the joint-velocity vector. \(P>0\) means the motor is delivering energy to the arm; \(P<0\) means external forces are dragging the motor and energy flows back into the drive (regenerative braking). Energy estimates, motor heating and power limits all start from this dot product.
Cosine similarity. Models like CLIP map images and text into vectors (embeddings) in a shared space; after normalization the matching score is a dot product, that is, the cosine similarity. A robot can use it to decide which region of the camera image best matches "pick up the red cup". Contrastive learning itself is covered in Part V.
8常见误区Common pitfalls
点积同时包含长度和夹角。取 \(\mathbf a = (1,0)\),\(\mathbf b = (3,3)\),\(\mathbf c = (1, 0.1)\):\(\mathbf a\cdot\mathbf b = 3\),\(\mathbf a\cdot\mathbf c = 1\),\(\mathbf b\) "得分更高"。但 \(\mathbf b\) 与 \(\mathbf a\) 相差 45°,\(\mathbf c\) 只差约 5.7°。要比较方向,必须先除以长度,用余弦相似度(\(\cos 45° \approx 0.71\),而 \(\cos 5.7° \approx 0.995\))。
The dot product mixes length and angle. Take \(\mathbf a = (1,0)\), \(\mathbf b = (3,3)\), \(\mathbf c = (1, 0.1)\): \(\mathbf a\cdot\mathbf b = 3\) and \(\mathbf a\cdot\mathbf c = 1\), so \(\mathbf b\) "scores higher". Yet \(\mathbf b\) is 45° away from \(\mathbf a\) while \(\mathbf c\) is only about 5.7° away. To compare directions, divide by the lengths first and use cosine similarity (\(\cos 45° \approx 0.71\) versus \(\cos 5.7° \approx 0.995\)).
数学上 \(\cos\theta\in[-1,1]\),但浮点误差可能让归一化后的点积变成 \(1.0000000000000002\)。例如 NumPy 里 a = np.array([0.4, 0.06, 0.3]),计算 a @ a / (np.linalg.norm(a) * np.linalg.norm(a)),结果就是这个数,np.arccos 对它返回 nan。机器人代码里一个 NaN 会顺着控制链一路传下去。永远写成 np.arccos(np.clip(c, -1.0, 1.0))。
Mathematically \(\cos\theta\in[-1,1]\), but floating-point error can push a normalized dot product to \(1.0000000000000002\). In NumPy, with a = np.array([0.4, 0.06, 0.3]), the expression a @ a / (np.linalg.norm(a) * np.linalg.norm(a)) gives exactly that, and np.arccos returns nan for it. In robot code a single NaN propagates down the whole control chain. Always write np.arccos(np.clip(c, -1.0, 1.0)).
\(\sum a_ib_i\) 只有在标准正交基(轴互相垂直、长度为 1)里写出的坐标上,才等于几何点积。反例:取斜的基 \(\mathbf f_1 = (1,0)\),\(\mathbf f_2 = (1,1)\)。坐标 \((1,1)\) 与 \((1,-1)\) 的"分量乘积和"是 \(1 - 1 = 0\),好像垂直;但真实向量是 \(\mathbf a = \mathbf f_1 + \mathbf f_2 = (2,1)\),\(\mathbf b = \mathbf f_1 - \mathbf f_2 = (0,-1)\),真正的点积是 \(-1 \ne 0\)。同理,不同单位的量不能放在一起点乘:位姿 \((x, y, \theta)\) 里米和弧度混在一起,它的"点积"没有几何意义。
\(\sum a_ib_i\) equals the geometric dot product only for coordinates written in an orthonormal basis (perpendicular axes of length 1). Counterexample: take the skewed basis \(\mathbf f_1 = (1,0)\), \(\mathbf f_2 = (1,1)\). The coordinate lists \((1,1)\) and \((1,-1)\) have "sum of products" \(1 - 1 = 0\), as if perpendicular; but the actual vectors are \(\mathbf a = \mathbf f_1 + \mathbf f_2 = (2,1)\) and \(\mathbf b = \mathbf f_1 - \mathbf f_2 = (0,-1)\), whose true dot product is \(-1 \ne 0\). By the same logic, quantities in different units cannot be dotted: in a pose \((x, y, \theta)\) meters and radians are mixed, and its "dot product" has no geometric meaning.
\(\hat{\mathbf h}\cdot\mathbf d>0\) 只说明夹角小于 90°,也就是"在我前方的半个平面里"。真实相机的视野半角通常只有 30° 到 60°,必须和阈值比较:\(\hat{\mathbf h}\cdot\mathbf d\ge|\mathbf d|\cos\alpha\)。把 \(\alpha\) 当成 90° 写代码,会把相机侧面的物体也当成"看得见"。反过来,若 \(\mathbf h\) 没有归一化,阈值那一侧也要乘上 \(|\mathbf h|\)。
\(\hat{\mathbf h}\cdot\mathbf d>0\) only says the angle is below 90°, i.e. "somewhere in the half-plane ahead". A real camera's half-angle is usually 30° to 60°, so you must compare against a threshold: \(\hat{\mathbf h}\cdot\mathbf d\ge|\mathbf d|\cos\alpha\). Coding with \(\alpha = 90°\) treats objects far off to the camera's side as visible. Conversely, if \(\mathbf h\) is not normalized, the threshold side must be multiplied by \(|\mathbf h|\) too.
9小练习Check yourself
10延伸阅读Further reading
- 3Blue1Brown, Essence of Linear Algebra — 第 9 章(点积与对偶性)Ch. 9 (Dot products and duality)
- G. Strang, Introduction to Linear Algebra, Ch. 1.2 — 长度与点积Lengths and dot products
- J. Ström et al., Immersive Linear Algebra, Ch. 3 — 点积(带交互插图)The dot product (with interactive figures)
- A. Radford et al., Learning Transferable Visual Models From Natural Language Supervision (CLIP), ICML 2021 — 余弦相似度在图文匹配中的应用cosine similarity at work for matching images and text