Skip to content

Commit f7a5306

Browse files
committed
Modified chapter to include singular systems
1 parent 5e34439 commit f7a5306

File tree

2 files changed

+110
-30
lines changed

2 files changed

+110
-30
lines changed

chapters/matrix_methods/gaussian_elimination/code/haskell/gaussianElimination.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ swapRows r1 r2 m
1717
multRow :: (Num a) => Int -> a -> Matrix a -> Matrix a
1818
multRow r a m = m // [((r, k), a * m ! (r, k)) | k <- cols m]
1919

20-
combRows ::
20+
combineRows ::
2121
(Eq a, Fractional a) => (Int, Int) -> a -> Int -> Matrix a -> Matrix a
22-
combRows (r, c) a t m
22+
combineRows (r, c) a t m
2323
| m ! (t, c) == 0 = m
2424
| otherwise =
2525
m // [((t, k), a * m ! (t, k) / (m ! (t, c)) - m ! (r, k)) | k <- cols m]
@@ -33,7 +33,7 @@ toEchelon mat = go (r1, c1) mat
3333
| pivot == 0 = go (r, c + 1) m
3434
| otherwise =
3535
go (r + 1, c + 1) $
36-
foldr (combRows (r, c) pivot) (swapRows r target m) [r + 1 .. rn]
36+
foldr (combineRows (r, c) pivot) (swapRows r target m) [r + 1 .. rn]
3737
where
3838
(pivot, target) = maximum [(m ! (k, c), k) | k <- [r .. rn]]
3939

@@ -46,7 +46,10 @@ toReducedEchelon mat = foldr go mat (echelonPath (r1, c1))
4646
| mat ! (r, c) == 0 = echelonPath (r, c + 1)
4747
| otherwise = (r, c) : echelonPath (r + 1, c + 1)
4848
go (r, c) m =
49-
foldr (combRows (r, c) 1) (multRow r (1 / (m ! (r, c))) m) [r1 .. r - 1]
49+
foldr
50+
(combineRows (r, c) 1)
51+
(multRow r (1 / (m ! (r, c))) m)
52+
[r1 .. r - 1]
5053

5154
printM :: (Show a) => Matrix a -> String
5255
printM m =

chapters/matrix_methods/gaussian_elimination/gaussian_elimination.md

Lines changed: 103 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,7 @@ $$
8080
\right]
8181
$$
8282

83-
84-
and it has a particular name: _Row Echelon Form_. Basically, any matrix can be considered in row echelon form if
85-
86-
1. All non-zero rows are above rows of all zeros
87-
2. The leading coefficient or _pivot_ (the first non-zero element in every row when reading from left to right) is right of the pivot of the row above it.
88-
89-
Now, Row Echelon Form is nice, but wouldn't it be even better if our system of equations looked simply like this
83+
Remember this one, we'll come back to it. Now, this is nice, but wouldn't it be even better if our system of equations looked simply like this
9084

9185

9286
$$
@@ -111,10 +105,91 @@ $$
111105
\right]
112106
$$
113107

108+
And that's where we really want to get to for obvious reasons.
109+
110+
Remember the earlier matrix form? It has a particular name: _Row Echelon Form_. Basically, any matrix can be considered in row echelon form if
111+
112+
1. All non-zero rows are above rows of all zeros
113+
2. The leading coefficient or _pivot_ (the first non-zero element in every row when reading from left to right) is right of the pivot of the row above it.
114+
115+
All the following examples are in the Row Echelon Form:
116+
117+
$$
118+
\left[
119+
\begin{array}{ccc|c}
120+
2 & 3 & 4 & 6 \\
121+
0 & 1 & 2 & 2 \\
122+
0 & 0 & 11 & 18
123+
\end{array}
124+
\right]
125+
\;,\;
126+
\left[
127+
\begin{array}{ccc|c}
128+
5 & 4 & 0 & 10 \\
129+
0 & 0 & 5 & 7 \\
130+
0 & 0 & 0 & 1
131+
\end{array}
132+
\right]
133+
\;,\;
134+
\left[
135+
\begin{array}{ccccc}
136+
1 & -3 & 4 & 1 & 6 \\
137+
0 & 3 & 3 & 5 & 0 \\
138+
0 & 0 & 0 & 2 & 0
139+
\end{array}
140+
\right]
141+
\;,\;
142+
\left[
143+
\begin{array}{cc}
144+
0 & 0 \\
145+
0 & 0 \\
146+
0 & 0
147+
\end{array}
148+
\right]
149+
$$
150+
151+
Out of all of these, only the first two one make sense if you're talking about a system of linear equations, as the last two don't even have the right dimensions. Additionally, if you translate the last row of second matrix into a system, you get $$0=1$$, which is a contradiction. This is due to the fact that the matrix is singular, and there are no solutions to this particular system. Nevertheless, all of these are in Row Echelon Form.
152+
153+
Now, it seems obvious to point out that if we ignore the last column, Row Echelon Form is an upper triangular matrix. This might not be important now, but it will play an important role in future discussions, so keep it buzzing in the back of your brain.
154+
155+
As we discussed before, Row Echelon Form is not the terminus. Cue the * **Reduced** Row Echelon Form*. A matrix is in Reduced Row Echelon form if it satisfies the following conditions:
114156

115-
And again has a special name * **Reduced** Row Echelon Form*. Now, it seems obvious to point out that if we remove the values to the right of the equals sign \($$=$$\), Row Echelon Form is an upper triangular matrix. This might not be important now, but it will play an important role in future discussions, so keep it buzzing in the back of your brain.
157+
1. It is in row echelon form.
158+
2. Every pivot is 1 and is the only nonzero entry in its column.
116159

117-
For now, I hope the motivation is clear: we want to convert a matrix into Row Echelon and (potentially) Reduced Row Echelon Form to make large systems of equations trivial to solve, so we need some method to do that. What is that method called? \(Hint: It's the title of this section\)
160+
All the following examples are in the Reduced Row Echelon Form:
161+
162+
$$
163+
\left[
164+
\begin{array}{ccc|c}
165+
1 & 0 & 0 & 8 \\
166+
0 & 1 & 0 & -3 \\
167+
0 & 0 & 1 & 9
168+
\end{array}
169+
\right]
170+
\;,\;
171+
\left[
172+
\begin{array}{ccc|c}
173+
1 & 4 & 0 & 9 \\
174+
0 & 0 & 1 & 7 \\
175+
0 & 0 & 0 & 1
176+
\end{array}
177+
\right]
178+
\;,\;
179+
\left[
180+
\begin{array}{cc}
181+
0 & 0 \\
182+
0 & 0 \\
183+
0 & 0
184+
\end{array}
185+
\right]
186+
$$
187+
188+
Again, only the first one (the identity matrix looking guy) is desirable in the context of solving a system of equations, but transforming a matrix in this form gives us an immediate and definitive answer at the question: can I solve my system?
189+
190+
Beyond solving a system, reshaping a matrix in this form makes it very easy to deduce other properties of the matrix, such as the rank.
191+
192+
For now, I hope the motivation is clear: we want to convert a matrix into Row Echelon and then Reduced Row Echelon Form to make large systems of equations trivial to solve, so we need some method to do that. What is that method called? \(Hint: It's the title of this section\)
118193

119194
That's right! _Gaussian Elimination_
120195

@@ -150,7 +225,7 @@ $$
150225

151226
There are plenty of different strategies you could use to do this, and no one strategy is better than the rest. Personally, I usually try to multiply each row in the matrix by different values and add rows together until the first column is all the same value, and then I subtract the first row from all subsequent rows. I then do the same thing for the following columns.
152227

153-
After you get an upper triangular matrix, the next step is diagonalizing to create the Reduced Row Echelon Form. In other words, we do the following:
228+
After you get an upper triangular matrix, the next step is creating the Reduced Row Echelon Form. In other words, we do the following:
154229

155230
$$
156231
\left[
@@ -176,19 +251,22 @@ Here, the idea is similar to above. The strategy is the same as before, but star
176251

177252
Now, the analytical method may seem straightforward, but the algorithm does not obviously follow from the game we were playing before, so we'll go through it step-by-step.
178253

179-
In general, do the following process:
254+
Row by row, do the following process:
180255

181-
1. For each column `col`, find the highest value
256+
1. Find the highest value in the column below the pivot candidate
182257
$$
183258
\left[
184259
\begin{array}{ccc|c}
185-
2 & 3 & 4 & 6 \\
260+
\mathbf{2} & 3 & 4 & 6 \\
186261
1 & 2 & 3 & 4 \\
187262
\mathbf{3} & -4 & 0 & 10
188263
\end{array}
189264
\right]
190265
$$
191-
2. Swap the row with the highest valued element with the `col`th row.
266+
267+
If that value is $$0$$, the matrix is singular and the system has no solutions. Feel free to exit here, but I'm powering through by moving on to the next column, baby!
268+
269+
2. Swap the row with the highest valued element with the current row.
192270
$$
193271
\left[
194272
\begin{array}{ccc|c}
@@ -206,7 +284,7 @@ $$
206284
\end{array}
207285
\right]
208286
$$
209-
3. For all remaining rows, find a fraction that corresponds to the ratio of the lower value in that column to the central pivot \(the one you swapped to the top\)
287+
3. For all remaining rows below the pivot, find a fraction that corresponds to the ratio of the lower value in that column to the pivot \(the one you swapped to the top\)
210288
$$
211289
\rightarrow
212290
\left[
@@ -223,7 +301,7 @@ $$
223301
$$
224302
4. Set all values in the corresponding rows to be the value they were before $$-$$ the top row $$\times$$ the fraction. This is essentially performing move 3 from above, except with an optimal multiplicative factor.
225303
$$
226-
A(\text{curr_row}_{\text{row}}, \text{curr_col}_{\text{col}}) \mathrel{+}= A(\text{pivot_row}_{\text{row}}, \text{pivot_row}_{\text{curr_col}} \times f) \\
304+
A(\text{curr_row}_{\text{row}}, \text{curr_col}_{\text{col}}) \mathrel{-}= f \times A(\text{pivot_row}_{\text{row}}, \text{pivot_row}_{\text{curr_col}}) \\
227305
\left[
228306
\begin{array}{ccc|c}
229307
3 & -4 & 0 & 10 \\
@@ -235,22 +313,21 @@ A(\text{curr_row}_{\text{row}}, \text{curr_col}_{\text{col}}) \mathrel{+}= A(\te
235313
\left[
236314
\begin{array}{ccc|c}
237315
3 & -4 & 0 & 10 \\
238-
\mathbf{\frac{1}{3}} & \mathbf{\frac{2}{3}} & \mathbf{1} & \mathbf{\frac{4}{3}} \\
316+
\mathbf{1-\frac{3}{3}} & \mathbf{2-\frac{-4}{3}} & \mathbf{3 -\frac{0}{3}} & \mathbf{4-\frac{10}{3}} \\
239317
2 & 3 & 4 & 6
240318
\end{array}
241319
\right]
242-
$$
243-
5. Set the value of that row's pivot column to 0.
244-
$$
320+
\rightarrow
245321
\left[
246322
\begin{array}{ccc|c}
247323
3 & -4 & 0 & 10 \\
248-
0 & 2 & 3 & 4 \\
324+
\mathbf{0} & \mathbf{\frac{10}{3}} & \mathbf{3} & \mathbf{\frac{2}{3}} \\
249325
2 & 3 & 4 & 6
250326
\end{array}
251327
\right]
252-
253328
$$
329+
You may set the values below the pivot to 0 for numerical reasons.
330+
254331

255332
In code, this looks like:
256333

@@ -261,13 +338,13 @@ In code, this looks like:
261338
[import:4-33, lang:"haskell"](code/haskell/gaussianElimination.hs)
262339
{% endmethod %}
263340

264-
As with all code, it takes time to fully absorb what is going on and why everything is happening; however, I have tried to comment the above pseudocode with the necessary steps. Let me know if anything is unclear!
341+
As with all code, it takes time to fully absorb what is going on and why everything is happening; however, I have tried to comment the above code with the necessary steps. Let me know if anything is unclear!
265342

266-
Now, to be clear: this algorithm creates an upper-triangular matrix. In other words, it only creates a matrix in *Row Echelon Form*, not * **Reduced** Row Echelon Form*! So what do we do from here? Well, we could create another step to further reduce the matrix, but another method would be to use *Back-Substitution*.
343+
Now, to be clear: this algorithm creates an upper-triangular matrix. In other words, it only creates a matrix in *Row Echelon Form*, not * **Reduced** Row Echelon Form*! So what do we do from here? Well, we continue further reducing the matrix, but with a twist: using the *Back-Substitution*.
267344

268-
The back-substitution method is precisely what we said above.
345+
The back-substitution method is precisely what we said above, but for every pivot starting from the bottom right one.
269346
If we have a matrix in Row Echelon Form, we can directly solve for $$z$$, and then plug that value in to find $$y$$ and then plug both of those values in to find $$x$$!
270-
Even though this seems straightforward, the pseudocode might not be as simple as you thought!
347+
Even though this seems straightforward, the code might not be as simple as you thought, especially if your matrix is singular.
271348

272349
{% method %}
273350
{% sample lang="jl" %}

0 commit comments

Comments
 (0)