A matrix warps the plane: in P1.B5 a square grid turned into a slanted one. This lesson asks two questions that sound different and are really one. Can the warp be run backwards? And: if you are told where a point ended up, where did it start? The first question leads to the inverse of a matrix. The second is a linear system , one of the most common problems in all of numerical computing. We also learn why good code almost never computes an inverse, even though the inverse is the idea that explains everything.
Undoing a transformation
Take the matrix from P1.B8, with columns and :
It sends the point to . Now we ask the reverse question: is there a matrix that takes back to , and in the same way every other output back to its input? There is, and it is
(the formula previewed in P1.B8; the next sections explain it). Check on our point: . It works.
The inverse of a square matrix is the matrix that undoes it:
Here is the identity matrix, the map that does nothing (P1.B5): every point stays where it is. By P1.B6, means “first , then ”, so the equation says that warping and then unwarping leaves every point where it started. The second equation says the same the other way round: unwarping and then warping.
The widget lets you watch this. Slide Step from to and the grid and the F are warped by . Keep going to and carries everything back.
Things to try:
- Drag Step from to . At the grid, the F and the test point are exactly where they started. The readout shows and the check says is the identity.
- Press Rotate 30°. The inverse is a rotation by the other way, and its entries are the same numbers with the signs of the off-diagonal entries swapped.
- Press Stretch (the matrix ). The inverse shrinks by half and stretches by : it is .
- Press Shear. The inverse is the shear the other way, . Press Reflect: the inverse is the same matrix, because mirroring twice puts everything back.
- Press Squash or Projection. The Undo button is switched off and the slider stops at . A second point appears, a different point from , and both dots land in the same place. Which of the two would the inverse send it back to? It cannot be both, so there is no inverse.
- Press Nearly squashed (the matrix ). An inverse exists, but its entries are and , and the status warns about the condition number. We come back to this at the end.
Intuition
Think of rewinding a film. If the film (the warp) lost nothing, you can play it backwards and get the original frame. The inverse is that backwards film. It exists exactly when each output frame came from only one input frame.
The analogy breaks when something is thrown away. Scrambling an egg cannot be rewound: many different arrangements of the egg lead to the same scramble. A matrix that squashes the plane onto a line is like that, and it is the case in which an inverse does not exist.
Math
For a square matrix , an inverse is a matrix with . It is unique: if and both undo , then
That is why we say “the” inverse. The inverse is itself a matrix, so it is a linear map too. Taking the determinant of and using from P1.B8 gives , so . For our this is , and indeed .
Code
import numpy as np
A = np.array([[2.0, -1.0], [1.0, 2.0]])
A_inv = np.linalg.inv(A)
print(A_inv) # [[ 0.4 0.2]
# [-0.2 0.4]]
print(A_inv @ A) # the identity (up to rounding)np.linalg.inv returns the inverse of a square array. The NumPy section below shows how to check it, and why you will rarely need it.
Where the formula comes from
How do you find when you only know ? Use what a matrix is (P1.B5): its columns tell where and go. Undoing must send the landing points back, so:
The first column of is the input that sends to . The second column is the input that sends to .
(The columns of are and , and undoes .) For our , find with and . The second equation gives , then , so and . The first column of is , as above, and one checks . In the same way the second column, , is the input that lands on .
Do the same for a general . The input satisfies
and gives in the same way. Putting these two columns side by side:
In words: swap the two diagonal entries, change the sign of the two off-diagonal entries, and divide everything by the determinant. For our : swap and (nothing changes), flip to and to , divide by .
The division by has a picture. multiplies every area by (P1.B8). To undo that, must multiply every area by . For the matrix of the lesson, areas grow five times going forward and shrink to a fifth coming back.
Check with the numbers: .
This formula is a special case. For larger matrices the inverse is found by elimination (see the Go deeper box below), and in practice by a library.
When there is no inverse
Look at the projection of P1.B5 that drops every point onto the line :
It sends to , and it also sends to . The input goes to , and so does . Different inputs, same output. Given only the output , there is no way to say whether the input was , or itself. An inverse would have to be a rule that sends back to one point, so it cannot exist. The inverse widget shows it: choose Projection and the second point lands where does.
The formula agrees. For we have , and would be a division by zero. The formula is not failing; it is telling you that there is nothing to find.
This is exactly the situation of P1.B7 and P1.B8. A non-zero vector with (here ) is in the null space. Then for every , so each output comes from a whole line of inputs. A square matrix has an inverse exactly when this never happens, and then every output is reached too. The whole picture fits in one statement:
Two clarifications. First, singular does not mean small or zero: has no zero entry, and is tiny but has the inverse (P1.B8). Second, “different inputs give different outputs” and “every output is reached” are two separate things, and an inverse needs both. For a square matrix each one implies the other, which is why a single number, the determinant, decides everything. For matrices that are not square, they come apart.
Go deeper If a square matrix undoes A on one side, it undoes it on the other
The definition asks for both and . For square matrices one is enough. Suppose . If then , so the null space is just and the columns of the square matrix are independent. independent vectors in span the whole space (P1.B7), so for each there is a with ; put these as the columns of a matrix , so that . Then , and is a two-sided inverse.
Only square matrices have an inverse
If is it turns numbers into numbers, and its inverse would have to turn the numbers back into the . A two-sided inverse needs . For a tall matrix (, more outputs than inputs) the output is a flat sheet inside a bigger space, so most points of the big space are not outputs of anything. For a wide matrix () some information must be lost, so different inputs share outputs (P1.B7: rank at most , so the null space is not just ). Such a matrix cannot have a two-sided inverse, and the three equivalent statements above are about square matrices.
Go deeper Left and right inverses of tall and wide matrices
A tall matrix with independent columns can still be undone from one side. Take (columns and from P1.B7) and
Then : recovers the two input numbers from the three output numbers. But is with rank at most , so it cannot be . is a left inverse. It is not unique: is another one. The particular above is the pseudo-inverse of , which P1.B13 explains. A wide matrix with independent rows can have right inverses () in the same way. In this course we need the pseudo-inverse only as a name.
Rules for inverses
Assume all the matrices below are square and invertible.
| Rule | In words |
|---|---|
| undo the last step first | |
| transposing and inverting can be done in either order | |
| for a number | scaling by is undone by scaling by |
| undoing the undo gives the original | |
| from the last section |
The first rule is the one to understand. If means “first , then ” (P1.B6), then to undo it you must undo first and then . You put on socks, then shoes; to undo that, you take off the shoes first, then the socks. In symbols, multiply by the claimed inverse:
The inside pair cancels first, which is why the order has to be reversed. Take as above and from P1.B8, with and . Then has , so by the formula. And
the same matrix. The wrong order gives , which is a different matrix. For three matrices, . Also, for square matrices, a product is invertible exactly when every factor is: if one factor squashes the plane, the product does too.
The other rules check out on . The transpose is , whose inverse is , which is . For , , the inverse is . (The transpose rule is stated here and explained in P1.B10.)
What is not a rule. There is no simple sum or product rule for , and certainly : , but . Also is not the matrix of reciprocals . For our the reciprocals are , which is not the inverse. For a diagonal matrix, though, the inverse is found by taking reciprocals of the diagonal entries, as the next table shows.
Inverses you can write down at once
| Matrix | Inverse, and why |
|---|---|
| Identity | : it does nothing |
| Diagonal with every | : a coordinate stretched by is stretched back by . With a on the diagonal that coordinate is flattened and there is no inverse. |
| Scaling every direction by , that is | (a special diagonal matrix) |
| Rotation by an angle | the rotation by : turn back by the same angle. It also equals the transpose; P1.B10 explains why and makes it a general rule for orthogonal matrices. |
| Shear | : slide back by the same amount |
| Reflection or the swap | itself: doing it twice gives the identity |
| Projection onto a line | none: many points land on one |
For example , and the rotation by is undone by the rotation by , whose matrix is the transpose. You saw each of these in the inverse widget.
Cancelling is allowed now
P1.B6 showed that does not give when squashes the plane. If is invertible, it does: multiply both sides from the left by to get . The side matters, because matrices do not commute:
- gives (multiply on the left of both sides);
- gives (multiply on the right of both sides).
Never write : it would not say which of these you mean.
Linear systems:
A linear system is a set of equations in unknowns where each unknown only appears multiplied by a number and added up. Take the system
In matrix form this is
which we write as . Here and are known and we look for . There are three ways to read this one equation, and each of the three pictures is useful:
- Row view: lines that meet. Each equation is a line in the plane: is one line and another. A solution is a point on both lines. (In 3-D each equation is a plane, and in dimensions a flat slice; a solution lies on all of them.)
- Column view: a combination of the columns. Because (P1.B5), the system asks: how much of column 1 and how much of column 2 must be added to make ? Here , so .
- Transformation view: find the input. Which input does send to ? If can be undone, apply the undo: .
All three give the same answer, , and the system-lines widget draws the first two at once.
Things to try:
- At the start, the lines cross at and the thin arrows go once along column 1 and then once along column 2, and land on . The status says there is one solution.
- Press Parallel lines (the matrix with ). The lines never meet. In the column view both columns lie on one dashed line through the origin, and is off that line: no combination of the columns reaches it. The status says there is no solution.
- Press Same line (the same matrix with ). Now the two equations are the same line, drawn on top of each other, and is on the dashed line. Every point of the line is a solution: infinitely many.
- Press A row of zeros. The second equation reads , which is impossible. Change to and it reads , which says nothing, and the first equation alone leaves a whole line of solutions.
- Press Nearly parallel (the matrix with ). The lines meet at , but at a tiny angle. Now move from to : the crossing point jumps to , a big move for a change of .
Intuition
Think of as a machine that takes a recipe (how much of each ingredient) and produces a result . Solving means: given the result, find the recipe. If the machine loses nothing, there is exactly one recipe. If it squashes, there may be many recipes for one result, or results that no recipe makes.
Math
The system has a solution exactly when is in the column space of (P1.B7). If is square and invertible, the solution is unique and equals : multiply both sides on the left by to get , and substitute back to see it works. Uniqueness: two solutions differ by a vector in the null space, which is .
Code
import numpy as np
A = np.array([[2.0, -1.0], [1.0, 2.0]])
b = np.array([1.0, 3.0])
x = np.linalg.solve(A, b)
print(x) # [1. 1.]np.linalg.solve(A, b) finds directly, without building . More on this below.
One solution, none, or infinitely many
A system of two equations in two unknowns has exactly three possible outcomes, and the pictures show which:
- One solution. Row view: the lines cross at one point. Column view: the columns point in different directions, so they reach every , in exactly one way. Algebra: and rank .
- No solution. Row view: parallel lines that never meet. Column view: the columns lie on one line and is off that line. Algebra: and is not in the column space.
- Infinitely many solutions. Row view: the two equations are the same line. Column view: the columns lie on one line and is on it. Algebra: and is in the column space.
Notice what decides the case. When , the column space is the whole plane (P1.B7), so is always in it and the answer is “one solution” for every . When , the column space is only a line (or a point), and then it depends on : on the line gives infinitely many, off the line gives none. It is never exactly two or three solutions. The usual rank statement of this (stated here, standard in any linear algebra text): has a solution exactly when (adding the column does not add a new direction), and the solution is unique when that rank also equals the number of unknowns. The system-lines widget prints both ranks.
The example of P1.B7. For (column 2 is twice column 1), the column space is the line . The vector is on it: works, since . The vector is off it: there is no solution.
All the solutions, when there are many. If is one solution and is in the null space (P1.B7), then . Conversely two solutions differ by a null-space vector. So the full set of solutions is plus the null space. For the example, spans the null space ( and ), and the solutions of are
for any number . This is the line , which is exactly the line in the row view. The special system always has the solution ; it has other solutions exactly when the null space is bigger than , which for a square matrix means .
Systems that are not square. With more equations than unknowns (a tall matrix) there is usually no solution, because many conditions have to hold at once; that is the situation of fitting a line to many data points, and the closest we can get is the job of least squares (P1.B11, P3.A2). With fewer equations than unknowns (a wide matrix) there are usually infinitely many solutions, or none. In 3-D with three equations each equation is a plane: three planes usually meet at one point, but they can also share a line (infinitely many solutions) or have no common point at all, for example when two are parallel or when each pair meets in a line and the three lines run side by side like the edges of a prism (no solution).
Solving by elimination
Computers do not find first. They use the row operations of P1.B8: you may swap two equations, multiply an equation by a number, and add a multiple of one equation to another, and the solutions do not change. The plan is to use them to reach a triangular system, whose last equation has one unknown, and then work upwards (back substitution). Carry along as an extra column: the augmented matrix .
For , :
The second row now says , so . The first row says , so . The first non-zero entry of each row ( and ) is a pivot, the number you divide by.
A example with the matrix from P1.B8 and :
(first and , then ; these are the same steps as in P1.B8, now also applied to ). Back substitution: gives ; then gives ; then gives . The solution is . The pivots are the ones whose product gave in P1.B8: the same elimination both solves the system and computes the determinant.
When things go wrong, elimination tells you which case you are in:
- A pivot position holds a . Swap with a row below that has a non-zero entry. For , swapping the two rows gives and , so . (Computers swap even when the pivot is not zero: they move the largest available entry of the column into the pivot position, because dividing by a small pivot magnifies rounding errors.)
- A row becomes all zeros on the left. Look at its right side. For and , gives : the last row reads , which is always true and adds no information. One equation for two unknowns: is free and each choice gives a solution (infinitely many). For , gives : the last row reads , which is never true. No solution.
- The number of pivots is the rank of (P1.B7). A square system has exactly one solution when there are pivots.
Cost. Elimination takes a number of steps that grows like (about multiplications, as the next section says) and back substitution only about . If you have many right-hand sides for the same , the elimination part can be kept and reused.
Go deeper Finding the whole inverse by elimination
The inverse itself can be found with elimination: write next to the identity and row-reduce until the left half becomes . The right half is then . (Solving for each column of at once, which is exactly the first-columns idea above.) For our :
The right half is . If a pivot cannot be found, no inverse exists, which agrees with the list of equivalent statements.
Go deeper LU factorization: remember the elimination
The steps of elimination can be recorded as a matrix. For our , one step () produced the triangular matrix . The step is stored in (the multiplier goes below the diagonal), and indeed . Solving is then two easy triangular solves: (going down), then (going up). The expensive part, finding and , is done once and reused for every new . With row swaps it is called LU factorization with pivoting, and it is what NumPy and most libraries use.
Solve, do not invert
Since , it is tempting to write np.linalg.inv(A) @ b. Numerical programmers advise against it. In maths, “” is the right way to write the answer; in code, it should mean “solve ”. There are three reasons.
- It takes more work. An LU solve needs about multiplications for a dense matrix, while building the whole inverse needs about , around three times more (the usual textbook counts). You then still have to multiply by .
- It is usually less accurate. The inverse is a big matrix with its own rounding errors, and multiplying by it adds more. A solve works on the equations directly. The second NumPy cell below shows it on a famously difficult matrix.
- It throws away structure. The inverse of a sparse matrix (mostly zeros) is usually dense, so you give up memory and speed that a solver would keep. And you almost never need the inverse itself, only its effect on one or a few vectors.
np.linalg.solve(A, b) calls the LAPACK routine gesv, which is an LU factorization with row swaps. If you have several right-hand sides, pass them as the columns of a matrix, and the factorization is done once.
| In the maths | In the code |
|---|---|
np.linalg.solve(A, b) | |
np.linalg.solve(A, B) | |
| (P3.A2) | np.linalg.solve(X.T @ X, X.T @ y), or a least-squares routine (P1.B11) |
The only time to form an inverse is when you really need its entries, for example to read off a covariance-like quantity, and even then it should be a conscious choice.
When answers are fragile
Some systems are solvable and unique but badly behaved. P1.B7 met one: the basis and needs weights and to reach the modest point . As a system:
has the solution .
Change the target to and the answer is . Nudge by only , to , and the answer becomes . The target moved by of its size and the answer moved by about . The reason is in the inverse: , which multiplies any error in by .
The condition number (previewed in P1.B8) measures this. It is the largest stretch of the matrix divided by the smallest. For this matrix it is about . A rule of thumb, with proof in the Go deeper box: an error in can be magnified in by up to about the condition number (measured as relative errors). The “up to” is real: some errors are magnified fully and some are not. A matrix with condition number , like our rotation-and-scale , never magnifies anything. A matrix that is exactly singular has condition number .
In the system-lines widget, press Nearly parallel. In the row view the two lines nearly coincide, so a small shove of one line slides the crossing point a long way. In the inverse widget the Nearly squashed preset shows the same thing from the other side: the inverse exists, but its entries are and and its condition number is about .
Some consequences:
- Digits. Floating-point numbers carry about 16 significant digits (P1.B7 and P1.B8 met their rounding). As a rule of thumb, a condition number near leaves you with roughly trustworthy digits. At you cannot trust any digit.
- It is the problem, not the method. Rounding errors in the data are unavoidable, and a badly conditioned system turns them into big errors in the answer, whichever method you use. A better algorithm avoids adding more error, but it cannot make the data more informative.
- Singular versus nearly singular. On a computer, a matrix that is singular on paper may look slightly non-singular after rounding, and one that is nearly singular may look fine. So never test exactly; look at the rank with a tolerance (P1.B7) or at the condition number,
np.linalg.cond. - In machine learning, features that are nearly copies of each other give a nearly singular matrix, and the learned weights swing wildly with tiny changes in the data. This is why regression with correlated features is unstable (P3.A2) and why regularization helps (P3.A6).
Go deeper Why the condition number bounds the error
Let and perturb the right side to , with solution . Subtracting the equations gives , so . The largest stretch of is one over the smallest stretch of , so where is the smallest stretch of . Also with the largest stretch. The second inequality gives a lower bound on , so dividing the first by and using that bound gives
The ratio is the condition number. (Here “stretch” means the factor by which a vector’s length changes at most or at least; the exact definition by singular values is in P1.B13.) The bound is reached when points along the direction that stretches most and along the one that stretches most.
Inverses and systems in NumPy
np.linalg.inv(A) returns the inverse, np.linalg.solve(A, b) solves the system, A @ B multiplies, and np.linalg.cond(A) gives the condition number. The first cell goes through the lesson: the inverse and its check, solving and systems, several right-hand sides at once, and what happens for a singular matrix.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
What to expect:
- Check an inverse with
np.allclose, not==. The product is the identity only up to rounding, and an exact comparison can fail even though the answer is right. On the machine where this lesson was checked,==gaveFalsefor (the bottom-left entry came out as about instead of ) whilenp.allclosegaveTrue. Your last digits may differ; the point is to never rely on exact equality of floating-point results (P1.B7 and P1.B8 made the same point about ranks and determinants). solvetakes a vector or a matrix. With a matrix on the right, each column is a separate system. Here the columns and give the solutions and , which come back as the columns of the result.- A singular matrix raises an error. Without the
try, the last line of the error message readsnumpy.linalg.LinAlgError: Singular matrix. The exact wording and the class name path can differ between NumPy versions.np.linalg.solve(S, b)raises the same error. NumPy raises it when elimination meets an exact zero pivot, as for here. - A nearly singular matrix usually does not raise an error.
Ndiffers from in one entry by , so it is not singular, and NumPy happily returns an inverse with entries of size and a condition number of about . A singular-on-paper matrix whose rounding leaves a tiny pivot can also fail to raise (P1.B8 met this with its integer matrix ). So no error does not mean the answer is trustworthy: look atnp.linalg.cond.
The second cell compares solve and inv on the Hilbert matrix, whose entries are and which is a famous badly conditioned matrix, and then repeats the nearly parallel system from the last section.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
What to expect (the exact digits depend on the machine, the pattern does not usually):
- The condition number of is about , so we expect to lose about 13 of the 16 digits. On the machine where this lesson was checked, the residual of
solvewas about (it satisfies the equations to rounding), while the residual ofinv(H) @ bwas about . - The error in is much larger than the residual for both, about for
solveand forinv. That large error is the condition number at work, and no method could avoid it. But the inverse adds extra error on top, as the text said. - The three solves of the nearly parallel system print , and , the numbers from the last section, and the condition number rounds to .
Where you will meet it next
- Linear regression (P3.A2). Fitting a line or a plane by least squares leads to the normal equations (derived in P1.B11 and P3.A2), a linear system for the weights . You solve it with
np.linalg.solve, not by inverting . If one feature is a copy of another, is singular and there is no single best (infinitely many solutions, as in this lesson). For instance has the second column equal to twice the first, and has determinant . - Ridge regression (P3.A6). Adding a multiple of the identity fixes this: is invertible for every . In the example with it is , with determinant . It also improves the condition number, which tames the wild weights of the last section. (Why it always works is explained in P3.A6 and uses eigenvalues, P1.B12.)
- Newton’s method (P1.F6). To minimize a function, Newton’s method steps by an amount that solves , where is the gradient and is the Hessian matrix of second derivatives (P1.C8). It is a linear system, solved with
solveand not by inverting . For millions of parameters this solve is too expensive, which is one reason it is rarely used at scale. - Invertible layers and flows (P8.6). A normalizing flow is built from steps that can be run backwards, so that the model can map data to noise and noise to data. That needs each step to be invertible and, ideally, its inverse and determinant to be cheap (P1.B8). A general weight matrix is invertible if its determinant is not (which is almost certainly the case for a random square matrix), but a ReLU layer is not invertible, because it sends every negative number to .
- Orthogonal matrices (P1.B10). A rotation is undone by its transpose. Matrices with this property, called orthogonal, have the cheapest possible inverse: .
- Gaussians (P1.D7). The multivariate Gaussian density uses the inverse covariance matrix in its exponent; code computes by solving .
Common mistakes
Check your understanding
What is the inverse of ?
The determinant is . Swap the diagonal entries (3 and 4), change the sign of the other two (1 and 2), and divide by 10. Forgetting the signs, using 12 (the product of the diagonal) or forgetting the division all give a matrix whose product with the original is not the identity.
Which of these matrices has no inverse?
Its determinant is (column 2 is twice column 1), so it squashes the plane onto a line. The others have determinants , and . A zero entry does not make a matrix singular: has none and has two zeros, yet both are invertible.
and are invertible matrices. Which expression is always equal to ?
means first , then , so the undo is first , then , which is the product . The product is equal to it only in special cases where and commute, and inverses do not respect sums.
For , which right-hand side gives a system with infinitely many solutions?
The column space of is the line of multiples of . is on it, so solutions exist, and because the null space is not just there are infinitely many. The other three are off the line, so the lines in the row view are parallel and there is no solution.
You need with for an invertible square . Which call do numerical programmers prefer?
solvefactorizes the matrix and solves directly: less work, and usually more accurate.inv(A) @ bcomputes the same mathematical answer but builds the whole inverse first.np.linalg.det(A) * bandb / Acompute something else entirely: the determinant times , and an entry-by-entry division.A system has a matrix with condition number . What does that tell you?
The condition number is the largest stretch divided by the smallest stretch, and it bounds how much a relative error in can grow in . It is a worst-case bound, not an exact factor. The matrix is invertible (the condition number is finite), so a unique solution exists, and the errors can grow, not shrink.
What is the inverse of the rotation of the plane by ?
Turning back by the same angle undoes a rotation, and its matrix equals the transpose (P1.B10 explains why). Rotating by again would give . A rotation keeps all areas (det 1) and loses nothing, so it has an inverse.
Exercises
- Find the inverse of , and check it by multiplying.
Show answer
. Swap the diagonal and change the signs of the others: . Check: the first row times the first column is , and the first row times the second column is , and so on, giving . - For which does have no inverse?
Show answer
, so there is no inverse for , when the second column is twice the first column (equivalently, row 2 is three times row 1). For every other the inverse exists. - Find the inverse of the rotation and compare it with its transpose.
Show answer
, so the inverse is , the rotation by . This is exactly the transpose of the original matrix. - Solve and by elimination.
Show answer
gives , so , and then gives . The solution is . Check: . - Say whether each system has one solution, none or infinitely many, and why: (a) and ; (b) and ; (c) and .
Show answer
(a) Infinitely many: the second equation is twice the first, so they are the same line. (b) None: the lines are parallel, since doubling the first equation gives , not (elimination ends with ). (c) One solution, : the lines have different directions (). - Write in terms of the inverses of the three matrices, and explain with shoes.
Show answer
. applies first, then , then (socks, shoes, then boots). To undo it, you undo the last step first: , then , then , and a product applies its rightmost factor first. - A matrix has . What is ?
Show answer
(P1.B8), and the determinant of the inverse is the reciprocal, so . Equivalently has . - Solve by elimination the system , , .
Show answer
gives and gives . Then subtracting these two, , so ; then and . The solution is , and the pivots are . - Solve and then with the right-hand side . What does the change tell you?
Show answer
The first system has the solution . Subtracting the first equation from the second leaves for the first system and for the second, so the second solution is and , that is . A change of in (about of the size of ) moved the solution from to , a change of of its size: a magnification of about . The condition number of the matrix is about , which allows this. - For , compute and its determinant, then do the same for .
Show answer
with , because column 2 of is twice column 1. Adding gives with , so it is invertible. This is the idea of ridge regression. - Code: without running it, predict what
np.linalg.inv(np.array([[1, 2], [2, 4]]))does, then run it.Show answer
The matrix is singular (), so NumPy raisesnumpy.linalg.LinAlgError: Singular matrix. (The elimination meets an exact zero here. For a matrix that is singular on paper but has rounding in it, the behaviour can differ, as in P1.B8.)