Gaussian elimination, inverses and determinants by hand
Ten problems on solving linear systems by row reduction: a 2×2 and a 3×3 system on the augmented matrix, a zero pivot that needs a row swap, a free variable and an inconsistent row, the 2×2 inverse formula, a 3×3 inverse by Gauss–Jordan on [A | I], a determinant by row reduction, the rules for det(cA), det(AB) and det(A⁻¹), an LU factorization with forward and back substitution, and a singular matrix whose null space is a redundant feature, with worked solutions and the mistakes of updating only the left side, dropping 1/det and forgetting the sign of a swap.
Before you start
Almost every computation with matrices that is done by hand, and most of the ones a linear algebra library does for you, is Gaussian elimination underneath: solving ,Ax=b, inverting ,A, computing ,detA, finding a rank or a null space. The method is three row operations used to clear the entries below each pivot, followed by back substitution. It goes wrong in a few predictable places: an operation applied to the coefficients but not to the right-hand side, a zero pivot divided by instead of swapped away, a row of zeros misread, the det1 dropped from the 2×2 inverse, and a row swap that silently flips the sign of a determinant. These ten problems cover each of them, and end with the singular matrix you meet in machine learning when one feature is the sum of two others.
The augmented matrix. The system Ax=b is written :[A∣b]: the coefficients of each equation in a row, with its right-hand side after the bar. A row operation changes the whole row, right-hand side included.
Row operations. (1) Add a multiple of one row to another, written .R2→R2−3R1. (2) Swap two rows, .R1↔R2. (3) Multiply a row by a nonzero number. None of them changes the solutions of the system.
Pivots and echelon form. Working left to right, the first nonzero entry of each row is its pivot; elimination uses each pivot to clear the entries below it. A zero where a pivot should be is fixed by swapping with a lower row. In echelon form each pivot is to the right of the one above, and any rows of zeros are at the bottom.
Reading the result. A pivot in every column of :A: one solution, found by back substitution from the last row up. A column without a pivot: its variable is free, and there are infinitely many solutions. A row [0⋯0∣c] with c=0 says :0=c: no solution. A row of all zeros, ,0=0, says nothing.
Inverses.A−1 satisfies ,AA−1=A−1A=I, and exists exactly when .detA=0. For :2×2:.[acbd]−1=ad−bc1[d−c−ba]. In general, row reduce [A∣I] until the left half is ;I; the right half is then .A−1.
Determinants. For ,2×2,.ad−bc. Adding a multiple of one row to another leaves det unchanged, a swap multiplies it by ,−1, and scaling a row by c multiplies it by .c. The determinant of a triangular matrix is the product of its diagonal entries. For n×n matrices, ,det(AB)=detAdetB,det(A⊤)=detA and .det(cA)=cndetA.
LU.,A=LU, with U the echelon form from elimination without swaps and L lower triangular with 1s on its diagonal and the multipliers ℓij below it, where Ri→Ri−ℓijRj was the operation used.
Problems
·
Solve ,x+2y=5,3x+4y=6 by row reducing the augmented matrix.
·
Solve the system ,x+y+z=6,,2x+y−z=1,.x−2y+3z=6.
··
Solve ,2y+z=1,,x+y+z=4,.2x+3y+2z=7.
··
Solve ,x+2y+z=4,,2x+4y+3z=11,.x+2y+2z=7. Then say what happens if the last right-hand side is 8 instead of .7.
·
Find the inverse of A=[4276] and use it to solve .Ax=(1,2)⊤.
··
Find the inverse of A=105216340 by row reducing .[A∣I].
··
Compute detA for A=01410−3238 by row reduction, and check it by cofactor expansion.
··
A and B are 3×3 matrices with detA=3 and .detB=−2. Find ,det(2A),,det(−A),,det(A−1),det(A⊤A) and .det(A2B). Can det(A+B) be found from this information?
···
Factor A=248137139 as ,A=LU, use it to solve ,Ax=(4,10,24)⊤, and read off .detA.
···
A design matrix has rows ,(1,1,2),(1,2,k) and ,(1,3,4), one row per example and one column per feature. For which k is it singular? For that ,k, find its null space, solve ,Ax=(2,3,4)⊤, and show that Ax=(1,0,0)⊤ has no solution.
Answers
,x=−4,y=29
(x,y,z)=(1,2,3)
(x,y,z)=(2,−1,3)
(x,y,z)=(1−2t,t,3) for every ;t; with 8 the last row reads 0=1 and there is no solution
Singular for ;k=3; null space ;t(1,1,−1)⊤;x=(t,t,1−t)⊤ for ;(2,3,4)⊤; no solution for (1,0,0)⊤
Worked solutions
Problem 1
Solve ,x+2y=5,3x+4y=6 by row reducing the augmented matrix.
[132456]Each equation is a row; the right-hand sides go after the bar.
:R2→R2−3R1:[102−25−9]The multiplier is ,3=13, the entry to clear over the pivot. The right side changes too: .6−3⋅5=−9.
,−2y=−9, so y=29The last row is one equation in one unknown.
,x+2⋅29=5, so x=−4Back substitution into the first row.
,x=−4,y=29Check in the second equation: .3(−4)+4⋅29=−12+18=6.
Problem 2
Solve the system ,x+y+z=6,,2x+y−z=1,.x−2y+3z=6.
12111−21−13616The augmented matrix.
,R2→R2−2R1,:R3→R3−R1:1001−1−31−326−110Clear the first column below the pivot .1.
:R3→R3−3R2:1001−101−3116−1133The second pivot is ,−1, and the multiplier is .−1−3=3. Check the new row: 2−3(−3)=11 and .0−3(−11)=33.
,11z=33, so ;z=3;,−y−9=−11, so ;y=2;,x+2+3=6, so x=1Back substitution from the bottom row up.
(x,y,z)=(1,2,3)Check all three: ,1+2+3=6,2+2−3=1 and .1−4+9=6.
Problem 3
Solve ,2y+z=1,,x+y+z=4,.2x+3y+2z=7.
012213112147The first equation has no ,x, so the top-left entry is 0 and cannot be a pivot.
:R1↔R2:102123112417Swap in a row with a nonzero first entry. Swapping equations does not change the solutions.
:R3→R3−2R1:10012111041−1Clear the first column.
,R2↔R3, then :R3→R3−2R2:1001101014−13The swap is optional, but it makes the pivot 1 and avoids fractions; R3→R3−21R2 without it gives the same answer.
(x,y,z)=(2,−1,3),z=3,,y=−1, and .x=4−(−1)−3=2. Check: ,2(−1)+3=1,2−1+3=4 and .4−3+6=7.
Problem 4
Solve ,x+2y+z=4,,2x+4y+3z=11,.x+2y+2z=7. Then say what happens if the last right-hand side is 8 instead of .7.
,R2→R2−2R1,:R3→R3−R1:100200111433Clearing the first column also clears the second: the y coefficients are twice the x coefficients in every row.
:R3→R3−R2:100200110430The last row says ,0=0, which is always true and adds no information.
The pivots are in the x and z columns, so y is free: y=tNo pivot in the y column means nothing pins y down.
z=3 and x=4−2t−3=1−2tBack substitution with .y=t.
With :8: the same steps give the last row [000∣1]R3−R1 now ends in ,8−4=4, and R3−R2 in .4−3=1.
(x,y,z)=(1−2t,t,3) for every ;t; with 8 the last row reads 0=1 and there is no solutionCheck :t=1:(−1,1,3) gives ,−1+2+3=4,−2+4+9=11 and .−1+2+6=7.
Problem 5
Find the inverse of A=[4276] and use it to solve .Ax=(1,2)⊤.
detA=4⋅6−7⋅2=10Not zero, so A is invertible.
A−1=101[6−2−74]Swap the diagonal entries, negate the off-diagonal ones, divide by the determinant.
AA−1=101[24−1412−12−28+28−14+24]=ICheck before using it.
x=A−1b=101[6−14−2+8]=101[−86]Multiply both sides of Ax=b on the left by .A−1.
,A−1=101[6−2−74],x=(−0.8,0.6)⊤Check: 4(−0.8)+7(0.6)=1 and .2(−0.8)+6(0.6)=2.
Problem 6
Find the inverse of A=105216340 by row reducing .[A∣I].
:R3→R3−5R1:10021−434−1510−5010001Every operation acts on all six columns.
:R3→R3+4R2: third row [001∣−541],−15+16=1, so the third pivot is already .1.
R2→R2−4R3 and :R1→R1−3R3: rows [010∣20−15−4] and [120∣16−12−3]Now clear above the pivots, starting from the last column: ,0−4(−5)=20,,1−16=−15,.1+15=16.
:R1→R1−2R2: first row [100∣−24185],16−40=−24,,−12+30=18,.−3+8=5.
A−1=−2420−518−1545−41Check one entry of AA−1 per row: ,(1,2,3)⋅(−24,20,−5)=−24+40−15=1,,(0,1,4)⋅(18,−15,4)=1,.(5,6,0)⋅(5,−4,1)=1. No row was scaled and no rows were swapped, so detA is the product of the pivots, ,1, which is why the inverse has integer entries.
Problem 7
Compute detA for A=01410−3238 by row reduction, and check it by cofactor expansion.
:R1↔R2:,10401−3328, and the determinant changes signThe top-left entry is ,0, so swap first. Keep a running factor of .−1.
:R3→R3−4R1: third row (0,−3,−4)Adding a multiple of a row does not change the determinant.
:R3→R3+3R2: third row (0,0,2);−4+6=2; again no change.
detA=−1⋅(1⋅1⋅2)=−2The echelon form is triangular, so its determinant is the product of its diagonal; the one swap contributes the .−1.
detA=−2Cofactor expansion along the first row: .0−1(1⋅8−3⋅4)+2(1⋅(−3)−0⋅4)=4−6=−2.
Problem 8
A and B are 3×3 matrices with detA=3 and .detB=−2. Find ,det(2A),,det(−A),,det(A−1),det(A⊤A) and .det(A2B). Can det(A+B) be found from this information?
det(2A)=23detA=242A doubles every one of the three rows, and each doubled row doubles the determinant.
det(−A)=(−1)3detA=−3The same rule with ;c=−1; for an even size the sign would not change.
det(A⊤A)=detA⊤detA=9 and det(A2B)=3⋅3⋅(−2)=−18The product rule, with .detA⊤=detA.
,24,,−3,,31,,9,;−18;det(A+B) cannot be foundThe determinant is not additive. With A=diag(3,1,1) and ,B=diag(−2,1,1),;det(A+B)=1⋅2⋅2=4; with B=diag(1,−2,1) instead, .det(A+B)=4⋅(−1)⋅2=−8. Same determinants, different answers.
Problem 9
Factor A=248137139 as ,A=LU, use it to solve ,Ax=(4,10,24)⊤, and read off .detA.
,R2→R2−2R1,:R3→R3−4R1: rows (0,1,1) and (0,3,5)Multipliers ℓ21=2 and .ℓ31=4.
:R3→R3−3R2: row ,(0,0,2), so U=200110112Multiplier .ℓ32=3.
L=124013001The multipliers go below the diagonal, in the positions they cleared, with their signs as used in .Ri→Ri−ℓRj.
:Lc=b:,c1=4,,c2=10−2⋅4=2,c3=24−4⋅4−3⋅2=2Forward substitution, top row first. This repeats on b what elimination did to .A.
,L=124013001,,U=200110112,,x=(1,1,1)⊤,detA=2⋅1⋅2=4detA=detLdetU and .detL=1. Once L and U are known, each new right-hand side costs only the two triangular solves, which is why libraries factor once and reuse it.
Problem 10
A design matrix has rows ,(1,1,2),(1,2,k) and ,(1,3,4), one row per example and one column per feature. For which k is it singular? For that ,k, find its null space, solve ,Ax=(2,3,4)⊤, and show that Ax=(1,0,0)⊤ has no solution.
detA=1(8−3k)−1(4−k)+2(3−2)=6−2kCofactor expansion along the first row of .A=1111232k4.
detA=0 exactly when k=3Then the third column is :(2,3,4)⊤=(1,1,1)⊤+(1,2,3)⊤: the third feature is the sum of the first two.
Row reduce with :k=3:,100110210, so ,x3=t,,x2=−t,;x1=−t; null space spanned by (1,1,−1)⊤R2−R1 and R3−R1 give (0,1,1) and ,(0,2,2), and R3−2R2 gives zeros. Check: .A(1,1,−1)⊤=(0,0,0)⊤.
(2,3,4)⊤ is the third column, so x=(0,0,1)⊤ works, and every solution is (0,0,1)⊤+t(1,1,−1)⊤One solution plus anything in the null space.
For (1,0,0)⊤ the same operations end in the row [000∣1]The right side becomes (1,−1,−1) after R2−R1 and ,R3−R1, and R3−2R2 turns its last entry into .−1−2(−1)=1. That row says .0=1.
Singular for ;k=3; null space ;t(1,1,−1)⊤;x=(t,t,1−t)⊤ for ;(2,3,4)⊤; no solution for (1,0,0)⊤In regression terms: weights w and w+t(1,1,−1)⊤ give identical predictions, so X⊤X is singular and the least-squares weights are not unique until a feature is dropped or a penalty such as ridge is added.
Where this goes wrong
1. Row operation applied to the coefficients only
The coefficients sit in a neat block, and the column after the bar is easy to leave behind.
[132456]Right so far: Problem 1.
“R2→R2−3R1 turns (3,4) into .(0,−2).”The habit that causes the mistake: the operation is done on the coefficients and stops at the bar.
,[102−256], so y=−3 and x=11The right side must become .6−3⋅5=−9. The check exposes it: ,3⋅11+4(−3)=21, not .6. The solution is ,x=−4,.y=29.
2. Dropping the 1/det from the 2×2 inverse
The swap-and-negate pattern is the memorable part of the formula, and the scalar in front is not.
,A=[4276], with [6−2−74] from swapping and negatingRight so far: Problem 5.
“So .A−1=[6−2−74].”The step that causes the mistake: the division by detA=10 is skipped.
x=[6−2−74](1,2)⊤=(−8,6)⊤Then ,Ax=(−32+42,−16+36)⊤=(10,20)⊤, ten times :b: the matrix used is ,10A−1, and A times it is ,10I, not .I. Divide by :detA:.x=(−0.8,0.6)⊤.
3. A row of zeros read as no solution
A row with nothing left in it looks like a system that has broken down.
100200110430Right so far: Problem 4.
“The last row has no variables left.”True, and harmless: it says .0=0.
“So the system has no solution.”No solution needs a row [000∣c] with .c=0. A row of zeros on both sides of the bar only says one equation was a combination of the others: here the third equation is the second minus the first. The y column has no pivot, so there are infinitely many solutions, ,(1−2t,t,3), such as (1,0,3) and .(−1,1,3).
4. A row swap that keeps the determinant's sign
Row operations are thought of as harmless to the determinant, and for the most common one they are.
,A=01410−3238, swap R1↔R2 to get a nonzero pivotRight so far: Problem 7.
Eliminate to 100010322Also right: the two row additions do not change the determinant.
detA=1⋅1⋅2=2A swap multiplies the determinant by ,−1, so ,detA=−2, as cofactor expansion confirms. Two swaps would cancel; an odd number leaves a minus sign.
5. det(2A) taken as 2 det A
Scalars come out of most things linearly, so the 2 is pulled out once.
detA=3 for a 3×3 matrix ARight so far: Problem 8.
“2A is A times .2.”True; the question is what that does to the determinant.
det(2A)=2⋅3=62A multiplies each of the three rows by ,2, and the determinant is linear in each row separately, so the factor comes out three times: .det(2A)=23⋅3=24. A quick test: det(2I)=8 for the 3×3 identity, not .2.