第 02 章
消元法:从方程组到上三角矩阵
消元是所有软件真正使用的解法:用主元消去下方的系数,再回代求解;把每一步写成矩阵,就得到 E 与 U,以及判断矩阵是否可逆的方法。
Elimination: From Equations to an Upper Triangular Matrix
Elimination is what every software package actually runs: use pivots to clear the entries below, then back-substitute. Written in matrix form, it produces E and U and a test for invertibility.
篇内目录
- What this lecture does
- Timeline
- A example
- Step one: clear x from row two
- Step two: clear y from row three
- Back substitution
- When elimination fails
- Case one: zero in the pivot position, but a nonzero below it
- Case two: zero pivot with only zeros below
- A by-product: the determinant is the product of pivots
- Rewriting elimination in matrix language
- Left versus right multiplication
- The first step:
- The second step:
- Putting them together
- Permutation matrices
- Inverses: undoing one elimination step
- Takeaways
- Check your understanding
- How it connects
这一讲要做什么
上一讲用三种视角理解方程组,但没有给出通用的求解方法。这一讲给出消元法:它不是行列式,而是每个数值软件实际使用的算法。目标是把 变成上三角矩阵 ,然后从最后一行开始回代。
整个流程分两半:
- 前向消元:用主元把下方的系数消成 0,得到 ;
- 回代:从最后一个方程开始,自下而上求出每个未知数。
同时回答一个问题:消元什么时候会失败?
时间轴
| 时间 | 内容 |
|---|---|
| 0:44 | 消元法:本讲使用的方法 |
| 3:26 | 乘数:用一行消去另一行 |
| 4:01 | 乘数是什么 |
| 7:38 | 消去 位置 |
| 10:44 | 消元失败:主元为零 |
| 14:40 | 把右端项一起消元 |
| 17:04 | 回代求解 |
| 20:50 | 用矩阵重写行操作 |
| 21:55 | 行操作 |
| 27:50 | 检查乘积的单个元素 |
| 30:26 | 消元矩阵 |
| 36:58 | 排列矩阵:交换行 |
| 43:24 | 逆矩阵:把一步消元倒回去 |
| 44:31 | 求逆的例子 |
例子:一个 方程组
写成方程就是
第一步:消去第二行的 x
第一个主元是左上角的 。要用它消掉第二行的 ,乘数是 :第二行减去 倍第一行。
矩阵上就是第二行变成 ,右端 。
第三行的第一个系数本来就是 ,乘数为 ,这一步不用做。
第二步:消去第三行的 y
现在第二行的第一个非零元是 ,这是第二个主元。第三行在 方向是 ,乘数是 :第三行减去 倍第二行。
于是
三个主元是 。它们都在对角线上,没有一个是 。
一句话总结: 消元就是不断用当前主元消掉它下方的系数,直到矩阵变成上三角。
回代
写回方程:
从最后一行开始:
代入第二行:
再代入第一行:
解是 。代回原方程可验证:
消元什么时候会失败
失败的定义很具体:拿不到足够的非零主元。
情况一:主元位置是 0,但下面有非零元
如果第一个数是 ,不能直接用它作主元。但也不代表无解:交换两行,把一个非零元换到主元位置即可。
课堂上的第二个例子:如果把第二行的 改成 ,第一步消元后第二行变成 ,第二个主元成了 。但第三行的 系数是 ,交换第二、三行后仍然可以继续。这叫临时失败,可以通过行交换挽救。
情况二:主元位置是 0,且下面全是 0
如果主元位置是 ,下方也没有可交换的非零元,就真的没有这个主元了。矩阵是奇异的,不可逆,方程组要么无解,要么有无穷多解。
课堂上的例子:把第三行第三列的 改成 。前两步不变,最后一行会变成 ,第三个主元消失。
三个主元只剩两个,矩阵不可逆。
一个附带结论:行列式等于主元之积
对没有行交换的情形, 等于各主元之积。这个例子中
这条结论会在行列式那一讲正式证明,这里先把它当作消元的副产品。
用矩阵语言重写消元
到目前为止,消元是「对行做操作」。线性代数的关键一步,是把这些操作本身写成矩阵。
先复习:左乘与右乘的区别
- 矩阵乘向量:结果是矩阵各列的线性组合。
- 行向量乘矩阵:结果是矩阵各行的线性组合。
这两句话是理解消元矩阵的钥匙。课堂上的说法是:
左乘做行操作,右乘做列操作。
因为消元只动行,所以消元矩阵都放在左边。
第一步的矩阵:
第一步是「第二行减去 3 倍第一行」,第一、三行不变。这个矩阵是
验证第二行:它是 倍第一行加 倍第二行:
这正是消元后的第二行。第一行由 取出,第三行由 取出,都不变。
第二步的矩阵:
第二步是「第三行减去 2 倍第二行」,所以
合起来
矩阵乘法的结合律允许改变括号:
但交换律不成立:。矩阵必须保持在「高斯给定的顺序」里。
这一点在工程上很重要:消元的顺序不能随便换,否则得到的是另一个变换。
排列矩阵
行交换也需要一个矩阵。把单位矩阵的两行交换,就得到排列矩阵 。对 情形:
因为左乘做行操作,所以 放在左边交换行。
如果想交换列, 必须放在右边:
这条对比把「左行右列」讲透了。
逆矩阵:把一步消元倒回去
做的是「第二行减去 3 倍第一行」。要撤销它,就「第二行加上 3 倍第一行」:
验证:
这个例子说明逆矩阵的直觉:它就是那个把操作倒着做一遍的矩阵。更一般的求逆方法下一讲展开。
本讲结论
- 消元把 变成上三角 ,右端向量同时变成 。
- 主元不能为 0;主元为 0 时先尝试行交换,换不到就说明矩阵奇异。
- 回代从最后一行开始,逐层向上。
- 每一步消元都是一个初等矩阵左乘;整体写作 。
- 矩阵乘法满足结合律,不满足交换律。
- 左乘做行操作,右乘做列操作;排列矩阵交换行或列。
- 初等矩阵的逆就是把该操作反过来。
理解检验
1. 对上面的例子,写出第二次消元的乘数,并说明为什么是它。
参考答案:乘数是 。第二次消元要消掉第三行的 系数,而当前主元是第二行第二列的 ,第三行的对应系数是 。乘数定义为「要消去的数 ÷ 主元」,所以是 :第三行减去 倍第二行,
右端同时变成 。
2. 为什么把第二行的 8 改成 6 只是「临时失败」,而把右下角的 1 改成 是「真正失败」?
参考答案:临时失败指主元位置暂时为 ,但下方还有非零元可以用行交换把它换上来。把 改成 后,第一步消元把第二行变成 ,第二行第二列是 ;但第三行的 系数是 ,交换第二、三行后主元就位,消元能继续。真正失败指主元位置为 ,且下方也全是 0,换行也换不出非零主元。把右下角的 改成 后,,第三行整行归零,第三个主元彻底消失,矩阵奇异。
3. 写出交换 矩阵两行的排列矩阵,并验证它左乘、右乘分别做什么。
参考答案:。左乘交换行:
右乘交换列:
原因还是「左乘做行操作、右乘做列操作」: 的每一行从原矩阵取出哪一行,决定的是行;每一列从原矩阵取出哪一列,决定的是列。
4. 为什么 的逆是「加上 3 倍第一行」而不是「减去 3 倍第一行」?
参考答案: 执行的动作是「第二行 第二行 第一行」。逆矩阵要做的正是把这个动作倒回去,即「第二行 第二行 第一行」,所以
直接验证:
若也用「减 3 倍」,等于把同一操作又做了一遍,只会把第二行变成 倍第一行,离单位阵更远。
5. 用主元之积计算本例的行列式,并与直接展开的结果比较。
参考答案:主元是 ,所以 。直接按第一行展开:
两者一致。注意「行列式等于主元之积」这条结论在没有行交换时成立;一旦用过行交换,每交换一次还要乘 。
与前后课的关系
这一讲给出了算法与矩阵语言。下一讲处理矩阵乘法与逆矩阵的一般理论:如何判断一个矩阵可逆,以及如何系统地求出 。再往后,消元会被重写成 的分解。
What this lecture does
The previous lecture gave three ways to see a system but no general method to solve one. This lecture supplies elimination, the algorithm every numerical package uses instead of determinants. The goal is to turn into an upper triangular matrix , then solve from the bottom up.
The process has two halves:
- Forward elimination: use each pivot to clear the entries below it, producing .
- Back substitution: start at the last equation and work upward.
It also answers a second question: when does elimination fail?
Timeline
| Time | Topic |
|---|---|
| 0:44 | Elimination as the method |
| 3:26 | The multiplier |
| 4:01 | What the multiplier is |
| 7:38 | Wiping out the entry |
| 10:44 | When elimination fails: a zero pivot |
| 14:40 | Carrying the right-hand side through elimination |
| 17:04 | Back substitution |
| 20:50 | Writing row operations as matrices |
| 21:55 | Row operations only |
| 27:50 | Checking a single entry of a product |
| 30:26 | The elimination matrix |
| 36:58 | Permutation matrices |
| 43:24 | The inverse of an elimination step |
| 44:31 | An example of computing an inverse |
A example
As equations:
Step one: clear x from row two
The first pivot is the in the top-left corner. To eliminate the below it, the multiplier is : subtract three times row one from row two.
In matrix terms row two becomes , and the right-hand side becomes .
The first entry of row three is already , so its multiplier is and the step is skipped.
Step two: clear y from row three
The first nonzero entry of row two is : the second pivot. Row three has in the position, so the multiplier is : subtract twice row two from row three.
So
The pivots are : all on the diagonal, none zero.
One-sentence summary: Elimination repeatedly uses the current pivot to clear the entries beneath it, until the matrix is upper triangular.
Back substitution
Write back as equations:
From the last row:
Substitute into the second:
Then the first:
The solution is . Check against the original second equation:
When elimination fails
Failure has a precise meaning: you cannot produce enough nonzero pivots.
Case one: zero in the pivot position, but a nonzero below it
If the first entry is , it cannot serve as a pivot. That does not mean the system is unsolvable: exchange rows and bring a nonzero entry into the pivot position.
In the lecture's variant, changing the in row two to makes row two after the first step, so the second pivot is . But row three has in the position, so swapping rows two and three rescues the process. This is a temporary failure, fixed by a row exchange.
Case two: zero pivot with only zeros below
If the pivot position is and nothing below it is nonzero, the pivot simply does not exist. The matrix is singular, not invertible, and the system has either no solution or infinitely many.
The lecture's variant changes the bottom-right to . The first two steps are unchanged, but the last row becomes , so the third pivot vanishes:
Two pivots instead of three: the matrix is singular.
A by-product: the determinant is the product of pivots
When no row exchanges are needed,
The determinant lecture proves this properly; here it arrives as a by-product of elimination.
Rewriting elimination in matrix language
So far elimination is "operations on rows". The key move in linear algebra is to write the operations themselves as matrices.
Left versus right multiplication
- Matrix times vector: a linear combination of the columns.
- Row vector times matrix: a linear combination of the rows.
These two sentences explain elimination matrices. The lecture's rule:
Left multiplication does row operations; right multiplication does column operations.
Elimination touches rows only, so every elimination matrix sits on the left.
The first step:
"Subtract three times row one from row two, leaving rows one and three alone" is
Check row two: it is times row one plus times row two:
That is exactly the eliminated row. Row one is picked out by and row three by , so both are unchanged.
The second step:
"Subtract twice row two from row three" gives
Putting them together
The associative law lets you move the parentheses:
The commutative law fails: . The matrices must stay in their Gaussian order.
This matters in practice: the order of elimination steps is part of the transformation and cannot be rearranged freely.
Permutation matrices
Row exchanges also need a matrix. Swap two rows of the identity and you get a permutation matrix . For :
Because left multiplication acts on rows, goes on the left to swap rows.
To swap columns, must go on the right:
That contrast is the whole "left rows, right columns" rule in one example.
Inverses: undoing one elimination step
subtracts three times row one from row two. To undo it, add three times row one back:
Check:
The intuition is direct: an inverse is the matrix that performs the step backwards. The general method for finding inverses is the next lecture.
Takeaways
- Elimination turns into upper triangular and simultaneously turns into .
- A pivot cannot be zero; try a row exchange, and if none is possible the matrix is singular.
- Back substitution starts at the last row and works upward.
- Each elimination step is a left multiplication by an elementary matrix, so .
- Matrix multiplication is associative but not commutative.
- Left multiplication acts on rows, right multiplication on columns; permutations swap rows or columns.
- The inverse of an elementary matrix reverses its operation.
Check your understanding
1. For the example, write the multiplier of the second elimination step and explain why it is that value.
Reference answer: The multiplier is . The step must clear the -entry in row three; the current pivot is the in position , and the entry to clear is . The multiplier is "entry to clear ÷ pivot", so : row three minus twice row two,
with the right-hand side becoming .
2. Why is changing the to only a "temporary failure", while changing the bottom-right to is a real failure?
Reference answer: A temporary failure means the pivot slot is zero but something nonzero lies below it, so a row swap can bring it up. With the changed to , the first step makes row two , so position is zero; but row three still has -entry , and swapping rows two and three restores a pivot. A real failure means the pivot slot is zero and everything below is zero too. With the changed to , : the third row becomes entirely zero, the third pivot disappears, and the matrix is singular.
3. Write the permutation matrix that swaps rows, and verify what left and right multiplication each do.
Reference answer: . Left multiplication swaps rows:
Right multiplication swaps columns:
The reason is again "left acts on rows, right acts on columns".
4. Why is the inverse of "add three times row one" rather than "subtract three times row one"?
Reference answer: performs "row two row two row one". Its inverse must undo exactly that, hence "row two row two row one":
Check:
Subtracting again would repeat the operation, leaving row two as times row one — further from the identity, not closer.
5. Compute the determinant of the example from the pivots and compare with direct expansion.
Reference answer: The pivots are , so . Expanding along the first row:
The two agree. The pivot-product rule holds when no row exchanges were used; each swap would multiply the determinant by .
How it connects
This lecture supplies both the algorithm and the matrix language. The next lecture develops matrix multiplication and inverses in general: how to recognize an invertible matrix and how to compute systematically. Later, elimination is rewritten as the factorization .