{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"identity-inverse-and-solving","title":"Undoing a matrix, and why you rarely should","url":"https://addaly.com/learn/maths-you-need/identity-inverse-and-solving","position":13,"of":76,"minutes":9,"answer":"The inverse exists only when a matrix loses no information, and even then solving a system directly is faster and far more numerically stable than forming the inverse.","keyIdea":"The inverse exists only when a matrix loses no information, and even then solving a system directly is faster and far more numerically stable than forming the inverse."},"body":{"format":"markdown","text":"## The matrix that does nothing\n\nThe identity matrix has 1s down the diagonal and 0s everywhere else:\n\n```\nI = [ 1  0  0 ]\n    [ 0  1  0 ]\n    [ 0  0  1 ]\n```\n\nMultiply any vector by it and you get the vector back. Multiply any matrix by it and you get that matrix back. It is the matrix equivalent of the number 1, and it exists so that \"undoing\" has something to mean.\n\n## The inverse\n\nThe inverse of A, written `A^-1`, is the matrix that undoes A: `A^-1 A = I`. If A rotates, `A^-1` rotates back. If A stretches by 3, `A^-1` shrinks by 3.\n\nFor a 2×2 there is a formula worth seeing once, because it exposes the whole story:\n\n```\nA = [ a  b ]      A^-1 =    1     [  d  -b ]\n    [ c  d ]              ad - bc [ -c   a ]\n```\n\nThe quantity `ad − bc` is the determinant. When it is zero, the formula divides by zero and no inverse exists. Worked:\n\n```\nA = [ 2  1 ]   det = 2*3 - 1*1 = 5\n    [ 1  3 ]\nA^-1 = (1/5) [  3  -1 ]  =  [  0.6  -0.2 ]\n             [ -1   2 ]     [ -0.2   0.4 ]\ncheck: A A^-1 = [1 0; 0 1]   (multiply it out; it does)\n```\n\n## When there is no inverse\n\nA matrix has no inverse when it destroys information: when two different inputs produce the same output. Consider\n\n```\nA = [ 1  2 ]      A [1, 0] = [1, 2]     A [0, 1] = [2, 4]\n    [ 2  4 ]\n```\n\nThe second row is twice the first, so every output lands on a single line. Given an output you cannot recover which input produced it, because infinitely many did. Such a matrix is called singular, and its determinant is `1*4 − 2*2 = 0`.\n\nThis matters more than it first appears. A layer whose weight matrix is singular has thrown information away permanently, and no later layer can retrieve it. Real weight matrices are almost never exactly singular, but many are *close*, which is the practically important case.\n\n## Nearly singular: the condition number\n\nThe useful measure is the condition number, roughly the ratio of the largest to the smallest singular value. A well-conditioned matrix has a condition number in the tens. A badly conditioned one has it in the millions, and it means a tiny change in the input produces an enormous change in the output, or, in the direction that hurts, that a tiny rounding error in your data becomes an enormous error in the solution.\n\nA rough rule: with 32-bit floats you have about 7 decimal digits of precision, so a condition number of `10^6` leaves you roughly one meaningful digit. That single fact explains most of the \"why is my linear solve producing nonsense\" problems in practice.\n\n## Why you should not compute the inverse\n\nHere is a habit that separates people who have been burnt from people who have not. To solve `Ax = b`, do **not** compute `A^-1` and multiply.\n\n```python\nimport numpy as np\nx = np.linalg.inv(A) @ b     # slower, less accurate, avoid\nx = np.linalg.solve(A, b)    # do this instead\n```\n\nThree reasons. Forming the inverse costs roughly three times the work of solving directly. It is measurably less accurate, because it computes an intermediate object with its own rounding errors before using it. And it is unnecessary: `solve` uses LU decomposition, which never builds the inverse at all.\n\nThe rule generalises. In numerical code, the answer to \"how do I undo this matrix\" is almost always \"solve the system\", not \"invert the matrix\". Library authors know this, which is why `np.linalg.solve` exists and why the documentation for `np.linalg.inv` gently steers you away.\n\n## Where this shows up in machine learning\n\nExplicit matrix inversion is rare in deep learning, which is worth saying plainly: nothing in transformer training inverts a matrix. It appears in three neighbouring places.\n\n- **Linear regression's closed form** involves `(X^T X)^-1`, and this is exactly where the advice bites. When two features are highly correlated, `X^T X` is close to singular, the condition number explodes, and the fitted coefficients become huge with opposite signs. This is multicollinearity, and adding a ridge penalty (`(X^T X + λI)^-1`) fixes it precisely by pushing the smallest singular values away from zero.\n- **Gaussian processes and Kalman filters** invert covariance matrices, and practitioners of both spend real effort on numerical conditioning, usually via Cholesky decomposition rather than an inverse.\n- **Second-order optimisers** would like the inverse Hessian and cannot afford it; every practical method, from L-BFGS to Adam's diagonal approximation, is a way of avoiding that inversion.\n\n## The rule to keep\n\n`solve`, never `inv`. And when a solve returns absurd numbers, check the condition number before you check anything else: `np.linalg.cond(A)` is one call, and it usually names the problem immediately."},"check":{"question":"A linear regression fitted with the closed-form solution returns coefficients like +48,200 and -48,190 on two features that are almost perfectly correlated. What is the mechanism, and what does adding a small ridge penalty do?","options":[{"text":"The features were not standardised, so their scales differ by four orders of magnitude; ridge rescales them.","correct":false,"tempting":"Scaling changes coefficient magnitudes but does not produce the huge equal-and-opposite pair, and ridge does not rescale features."},{"text":"The model is overfitting the training data; ridge reduces capacity by removing features.","correct":false,"tempting":"Ridge shrinks coefficients rather than removing them, and the equal-and-opposite signature points at conditioning, not capacity."},{"text":"There are more features than examples, so the system is underdetermined; ridge selects one of the infinitely many solutions.","correct":false,"tempting":"A real situation with its own remedy, but the described symptom is the correlated-pair one and can happen with plenty of rows."},{"text":"The correlated features make X-transpose-X nearly singular so its smallest singular value is close to zero; the ridge term adds lambda to every eigenvalue, pushing it away from zero and shrinking the solution.","correct":true}]},"prev":{"slug":"shapes-broadcasting-and-batches","title":"Shapes, batches, and the broadcasting rules","url":"https://addaly.com/learn/maths-you-need/shapes-broadcasting-and-batches"},"next":{"slug":"determinants-and-volume","title":"The determinant, and what it says about collapse","url":"https://addaly.com/learn/maths-you-need/determinants-and-volume"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Undoing a matrix, and why you rarely should', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/identity-inverse-and-solving (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Undoing a matrix, and why you rarely should","courseTitle":"The Maths You Actually Need","module":{"index":2,"slug":"matrices-and-layers","title":"Matrices, and what a layer really does"},"answer":null,"keyIdea":"The inverse exists only when a matrix loses no information, and even then solving a system directly is faster and far more numerically stable than forming the inverse.","hero":{"kind":"plate","image":"https://addaly.com/art/modules/maths-you-need/matrices-and-layers.webp","alt":"Three blocks of cells joined by arrows: a narrow column, a large square grid and a single tall bar.","caption":"A column of values entering a grid of weights and leaving as a shorter column, in that order: the shape change is what a layer does, and the grid’s rank decides whether two small matrices could stand in for it."},"blocks":[{"t":"h2","id":"the-matrix-that-does-nothing","runs":[{"text":"The matrix that does nothing"}]},{"t":"p","runs":[{"text":"The identity matrix has 1s down the diagonal and 0s everywhere else:"}]},{"t":"code","lang":"","label":"Text","text":"I = [ 1  0  0 ]\n    [ 0  1  0 ]\n    [ 0  0  1 ]"},{"t":"p","runs":[{"text":"Multiply any vector by it and you get the vector back. Multiply any matrix by it and you get that matrix back. It is the matrix equivalent of the number 1, and it exists so that “undoing” has something to mean."}]},{"t":"h2","id":"the-inverse","runs":[{"text":"The inverse"}]},{"t":"p","runs":[{"text":"The inverse of A, written "},{"text":"A^-1","code":true},{"text":", is the matrix that undoes A: "},{"text":"A^-1 A = I","code":true},{"text":". If A rotates, "},{"text":"A^-1","code":true},{"text":" rotates back. If A stretches by 3, "},{"text":"A^-1","code":true},{"text":" shrinks by 3."}]},{"t":"p","runs":[{"text":"For a 2×2 there is a formula worth seeing once, because it exposes the whole story:"}]},{"t":"code","lang":"","label":"Text","text":"A = [ a  b ]      A^-1 =    1     [  d  -b ]\n    [ c  d ]              ad - bc [ -c   a ]"},{"t":"p","runs":[{"text":"The quantity "},{"text":"ad − bc","code":true},{"text":" is the determinant. When it is zero, the formula divides by zero and no inverse exists. Worked:"}]},{"t":"code","lang":"","label":"Text","text":"A = [ 2  1 ]   det = 2*3 - 1*1 = 5\n    [ 1  3 ]\nA^-1 = (1/5) [  3  -1 ]  =  [  0.6  -0.2 ]\n             [ -1   2 ]     [ -0.2   0.4 ]\ncheck: A A^-1 = [1 0; 0 1]   (multiply it out; it does)"},{"t":"h2","id":"when-there-is-no-inverse","runs":[{"text":"When there is no inverse"}]},{"t":"p","runs":[{"text":"A matrix has no inverse when it destroys information: when two different inputs produce the same output. Consider"}]},{"t":"code","lang":"","label":"Text","text":"A = [ 1  2 ]      A [1, 0] = [1, 2]     A [0, 1] = [2, 4]\n    [ 2  4 ]"},{"t":"p","runs":[{"text":"The second row is twice the first, so every output lands on a single line. Given an output you cannot recover which input produced it, because infinitely many did. Such a matrix is called singular, and its determinant is "},{"text":"1*4 − 2*2 = 0","code":true},{"text":"."}]},{"t":"p","runs":[{"text":"This matters more than it first appears. A layer whose weight matrix is singular has thrown information away permanently, and no later layer can retrieve it. Real weight matrices are almost never exactly singular, but many are "},{"text":"close","i":true},{"text":", which is the practically important case."}]},{"t":"h2","id":"nearly-singular-the-condition-number","runs":[{"text":"Nearly singular: the condition number"}]},{"t":"p","runs":[{"text":"The useful measure is the condition number, roughly the ratio of the largest to the smallest singular value. A well-conditioned matrix has a condition number in the tens. A badly conditioned one has it in the millions, and it means a tiny change in the input produces an enormous change in the output, or, in the direction that hurts, that a tiny rounding error in your data becomes an enormous error in the solution."}]},{"t":"p","runs":[{"text":"A rough rule: with 32-bit floats you have about 7 decimal digits of precision, so a condition number of "},{"text":"10^6","code":true},{"text":" leaves you roughly one meaningful digit. That single fact explains most of the “why is my linear solve producing nonsense” problems in practice."}]},{"t":"h2","id":"why-you-should-not-compute-the-inverse","runs":[{"text":"Why you should not compute the inverse"}]},{"t":"p","runs":[{"text":"Here is a habit that separates people who have been burnt from people who have not. To solve "},{"text":"Ax = b","code":true},{"text":", do "},{"text":"not","b":true},{"text":" compute "},{"text":"A^-1","code":true},{"text":" and multiply."}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\nx = np.linalg.inv(A) @ b     # slower, less accurate, avoid\nx = np.linalg.solve(A, b)    # do this instead"},{"t":"p","runs":[{"text":"Three reasons. Forming the inverse costs roughly three times the work of solving directly. It is measurably less accurate, because it computes an intermediate object with its own rounding errors before using it. And it is unnecessary: "},{"text":"solve","code":true},{"text":" uses LU decomposition, which never builds the inverse at all."}]},{"t":"p","runs":[{"text":"The rule generalises. In numerical code, the answer to “how do I undo this matrix” is almost always “solve the system”, not “invert the matrix”. Library authors know this, which is why "},{"text":"np.linalg.solve","code":true},{"text":" exists and why the documentation for "},{"text":"np.linalg.inv","code":true},{"text":" gently steers you away."}]},{"t":"h2","id":"where-this-shows-up-in-machine-learning","runs":[{"text":"Where this shows up in machine learning"}]},{"t":"p","runs":[{"text":"Explicit matrix inversion is rare in deep learning, which is worth saying plainly: nothing in transformer training inverts a matrix. It appears in three neighbouring places."}]},{"t":"ul","items":[[{"text":"Linear regression’s closed form","b":true},{"text":" involves "},{"text":"(X^T X)^-1","code":true},{"text":", and this is exactly where the advice bites. When two features are highly correlated, "},{"text":"X^T X","code":true},{"text":" is close to singular, the condition number explodes, and the fitted coefficients become huge with opposite signs. This is multicollinearity, and adding a ridge penalty ("},{"text":"(X^T X + λI)^-1","code":true},{"text":") fixes it precisely by pushing the smallest singular values away from zero."}],[{"text":"Gaussian processes and Kalman filters","b":true},{"text":" invert covariance matrices, and practitioners of both spend real effort on numerical conditioning, usually via Cholesky decomposition rather than an inverse."}],[{"text":"Second-order optimisers","b":true},{"text":" would like the inverse Hessian and cannot afford it; every practical method, from L-BFGS to Adam’s diagonal approximation, is a way of avoiding that inversion."}]]},{"t":"h2","id":"the-rule-to-keep","runs":[{"text":"The rule to keep"}]},{"t":"p","runs":[{"text":"solve","code":true},{"text":", never "},{"text":"inv","code":true},{"text":". And when a solve returns absurd numbers, check the condition number before you check anything else: "},{"text":"np.linalg.cond(A)","code":true},{"text":" is one call, and it usually names the problem immediately."}]}],"check":{"question":[{"text":"A linear regression fitted with the closed-form solution returns coefficients like +48,200 and -48,190 on two features that are almost perfectly correlated. What is the mechanism, and what does adding a small ridge penalty do?"}],"options":[{"text":[{"text":"The features were not standardised, so their scales differ by four orders of magnitude; ridge rescales them."}],"correct":false,"tempting":[{"text":"Scaling changes coefficient magnitudes but does not produce the huge equal-and-opposite pair, and ridge does not rescale features."}]},{"text":[{"text":"The model is overfitting the training data; ridge reduces capacity by removing features."}],"correct":false,"tempting":[{"text":"Ridge shrinks coefficients rather than removing them, and the equal-and-opposite signature points at conditioning, not capacity."}]},{"text":[{"text":"There are more features than examples, so the system is underdetermined; ridge selects one of the infinitely many solutions."}],"correct":false,"tempting":[{"text":"A real situation with its own remedy, but the described symptom is the correlated-pair one and can happen with plenty of rows."}]},{"text":[{"text":"The correlated features make X-transpose-X nearly singular so its smallest singular value is close to zero; the ridge term adds lambda to every eigenvalue, pushing it away from zero and shrinking the solution."}],"correct":true,"tempting":null}],"order":[2,3,1,0],"why":null},"prev":{"slug":"shapes-broadcasting-and-batches","title":"Shapes, batches, and the broadcasting rules","minutes":9},"next":{"slug":"determinants-and-volume","title":"The determinant, and what it says about collapse","minutes":8}}}