Calculus, Linear Algebra & Discrete Math Matrices & Linear Algebra Gauss-Jordan elimination with partial pivoting

Matrix Inverse Calculator

Enter a square matrix and this calculator inverts it by row-reducing the augmented array [A | I] to [I | A⁻¹], the same Gauss-Jordan procedure you would run by hand. You get the inverse entry by entry, the adjugate for matrices up to 3×3, the determinant that decides whether an inverse exists at all, the ∞-norm condition number that tells you how much precision a solve will cost, and the largest entry of A·A⁻¹ − I as an arithmetic check on the answer.

Calculator

This calculator runs in your browser. Enable JavaScript for live results — the inputs, formula and worked example below remain fully readable without it.

Inputs this calculator takes, with typical values
InputWhat to enterExample
Row 1Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix is smaller.2, -1, 0, 0
Row 2Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix is smaller.-1, 2, -1, 0
Row 3Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix is smaller.0, -1, 2, -1
Row 4Separate entries with commas or spaces. Fractions such as 3/4 are accepted. Leave a row blank if your matrix is smaller.0, 0, -1, 2

It returns

  • Determinant det A — The inverse exists if and only if this is not zero.
  • Condition number κ∞(A) — ‖A‖∞ · ‖A⁻¹‖∞. Roughly log10 of this is the number of digits a solve can lose.
  • Trace of A⁻¹
  • Determinant of A⁻¹
  • Largest entry of A·A⁻¹ − I — Zero to rounding error confirms the inverse is correct.

The formula

A1=adj(A)det(A)
[abcd]1=1adbc[dbca]

In plain text: A⁻¹ = adj(A) / det(A), and A·A⁻¹ = A⁻¹·A = I

  • A⁻¹The inverse: the unique matrix with A·A⁻¹ = I (—)
  • adj(A)The adjugate — the transpose of the matrix of cofactors (—)
  • det(A)The determinant; the inverse exists exactly when this is non-zero (—)
  • κ∞(A)Condition number ‖A‖∞·‖A⁻¹‖∞, where ‖·‖∞ is the largest absolute row sum (—)

The adjugate formula is how you prove the inverse exists. Gauss-Jordan elimination is how you actually compute it.

Updated Category Matrices & Linear Algebra Verified against published test cases Reading time 10 min

What an inverse matrix is

The inverse of A is the matrix that undoes it. If A rotates and stretches space, A⁻¹ unrotates and unstretches, and applying them in sequence leaves everything exactly where it started — that is what A·A⁻¹ = I means. The identity matrix I is the do-nothing map, with ones on the diagonal and zeros everywhere else.

Not every matrix has one. If A flattens three dimensions onto a plane, information is gone for good and no matrix can restore it. That collapse is detected by the determinant: A has an inverse if and only if det(A) ≠ 0, and the calculator refuses, with an explanation, when it is zero. A matrix with no inverse is called singular; one with an inverse is invertible or non-singular.

The inverse is what makes matrix algebra feel like ordinary algebra. To solve Ax = b you would like to divide by A, and x = A⁻¹b is as close as matrices get — with one important caveat covered further down, which is that computing the inverse is almost never the right way to solve the system.

Gauss-Jordan: row-reduce [A | I] to [I | A⁻¹]

Write A next to the identity matrix, giving an n×2n array. Now row-reduce the left half to the identity, applying every operation to the whole row so the right half comes along. When the left half becomes I, the right half has become A⁻¹.

The reason is short. Every elementary row operation is itself a matrix multiplication on the left. If a chain of them Ek…E₁ turns A into I, then Ek…E₁A = I, so Ek…E₁ is A⁻¹ — and applying that same chain to I produces exactly that product. You are not doing two calculations; you are recording one.

The calculator uses partial pivoting: at each column it swaps the largest available entry into the pivot position before eliminating. That is not for elegance. Dividing by a pivot much smaller than the entries beneath it amplifies rounding error, and pivoting keeps every multiplier at magnitude one or less.

The adjugate formula, A⁻¹ = adj(A)/det(A), is the other classical route and the one that proves the inverse exists. The adjugate is the transpose of the cofactor matrix: entry (i, j) of adj(A) is (−1)i+j times the minor Mji. For a 2×2 it collapses to the familiar swap-and-negate rule. For a 3×3 it means nine 2×2 determinants, which is tedious but doable; for anything larger it is hopeless, because it needs n² minors of size n−1. The calculator shows the adjugate for matrices up to 3×3 because it makes the fractional structure of the answer visible.

Worked example: inverting [[2, −1, 0], [−1, 2, −1], [0, −1, 2]]

This is the 3×3 second-difference matrix, the discrete version of a second derivative and one of the most common matrices in numerical analysis.

  1. Determinant first. Expand along the first row: 2 × det[[2,−1],[−1,2]] − (−1) × det[[−1,−1],[0,2]] + 0 = 2(4 − 1) + 1(−2 − 0) = 6 − 2 = 4. Non-zero, so the inverse exists.
  2. Set up the augmented array. [[2,−1,0 | 1,0,0], [−1,2,−1 | 0,1,0], [0,−1,2 | 0,0,1]].
  3. Clear column 1. Divide row 1 by 2 to make the pivot 1: [1, −0.5, 0 | 0.5, 0, 0]. Add row 1 to row 2: [0, 1.5, −1 | 0.5, 1, 0]. Row 3 already has a zero there.
  4. Clear column 2. Divide row 2 by 1.5: [0, 1, −2/3 | 1/3, 2/3, 0]. Add half of it to row 1: [1, 0, −1/3 | 2/3, 1/3, 0]. Add it to row 3: [0, 0, 4/3 | 1/3, 2/3, 1].
  5. Clear column 3. Multiply row 3 by 3/4: [0, 0, 1 | 0.25, 0.5, 0.75]. Add ⅓ of it to row 1 and ⅔ of it to row 2.
  6. Read off the right half. A⁻¹ = ¼ × [[3, 2, 1], [2, 4, 2], [1, 2, 3]] = [[0.75, 0.5, 0.25], [0.5, 1, 0.5], [0.25, 0.5, 0.75]].

Check one entry by multiplication: row 1 of A times column 1 of A⁻¹ is 2(0.75) + (−1)(0.5) + 0(0.25) = 1.5 − 0.5 = 1, which is the (1,1) entry of the identity. Row 2 of A times column 1 of A⁻¹ is (−1)(0.75) + 2(0.5) + (−1)(0.25) = −0.75 + 1 − 0.25 = 0. Correct.

The condition number follows from the two row-sum norms: the largest absolute row sum of A is 1 + 2 + 1 = 4 (the middle row), and of A⁻¹ is 0.5 + 1 + 0.5 = 2 (also the middle row), so κ∞ = 4 × 2 = 8. That is a very well-conditioned matrix.

How to read the condition number

The condition number, not the determinant, tells you how trustworthy a solve will be. κ(A) = ‖A‖·‖A⁻¹‖ measures how much a relative error in your data can be amplified in the answer: a relative perturbation of size ε in b can produce a relative change of up to κ·ε in x. The rule of thumb is that you lose about log₁₀(κ) significant digits. With double precision giving you roughly 16 digits, κ = 10⁸ leaves you 8, and κ = 10¹⁶ leaves you none.

The best possible value is κ = 1, achieved by the identity and by any scaled orthogonal matrix. The example above at κ∞ = 8 is excellent. The calculator warns above 10⁶.

Contrast that with the determinant, which is a poor measure of near-singularity because it scales as the n-th power of the entries. Multiply a well-conditioned 4×4 by 0.01 and its determinant falls by 10⁸ while κ does not move at all — the matrix is exactly as invertible as before.

The residual output is your proof. A·A⁻¹ − I should be zero. For an integer matrix with a modest condition number it comes out at 10⁻¹⁶ or exactly zero. A residual around 10⁻⁸ on a matrix you expected to be clean means the elimination lost half its digits, and κ will confirm why.

Inverses of small matrices worth recognising

Every row can be checked in the calculator above. The second-difference family is the one that appears in every finite-difference scheme.
MatrixdetInverseκ∞
[[4,7],[2,6]]10[[0.6,−0.7],[−0.2,0.4]]14.3
[[1,0],[0,1]]1itself1
[[0,1],[1,0]]−1itself1
[[2,−1],[−1,2]]3⅓[[2,1],[1,2]]3
[[2,−1,0],[−1,2,−1],[0,−1,2]]4¼[[3,2,1],[2,4,2],[1,2,3]]8
diag(a, b)abdiag(1/a, 1/b)max(|a|,|b|)/min(|a|,|b|)
[[1,2],[2,4]]0none — singular

The n×n second-difference matrix has determinant exactly n+1, and its inverse has entries min(i,j)·(n+1−max(i,j))/(n+1) — which is why the 4×4 default above inverts to fifths.

Things that go wrong with inverses

  • Inverting to solve a system. Computing A⁻¹ and multiplying costs about three times as much work as solving Ax = b directly by elimination, and it is less accurate. Form the inverse only when you genuinely need the matrix itself.
  • Assuming (AB)⁻¹ = A⁻¹B⁻¹. The order reverses: (AB)⁻¹ = B⁻¹A⁻¹. Think of dressing — socks then shoes — and undressing in the opposite order.
  • Treating a tiny determinant as the danger signal. It is not; the condition number is. Scaling changes one and not the other.
  • Rounding the entries before inverting. On an ill-conditioned matrix, rounding your inputs to four decimals can change the inverse in the first digit. Enter exact fractions where you have them.
  • Expecting an inverse for a rectangular matrix. Only square matrices have true inverses. Rectangular ones can have a left or right pseudo-inverse, which solves a least-squares problem rather than an exact one.
  • Forgetting that the inverse of an integer matrix is rarely an integer matrix. It is integer exactly when the determinant is ±1, which is why the adjugate table is the more readable form: integer entries over one common denominator.

The inverse is a description, not an algorithm

Numerical analysts have a standing rule: never compute an inverse if what you want is a solution. Solving Ax = b by elimination costs about n³/3 operations; forming A⁻¹ costs about n³ and then multiplying costs another n². The direct solve is also backward-stable, while multiplying by a computed inverse is not. Use the inverse when the matrix itself is the object of interest — a covariance precision matrix, a transformation you need to store and reuse, or a proof — and use elimination when you want an answer.

Check invertibility first with the determinant calculator, which also reports the rank so you can see how far a singular matrix has collapsed. If you have an actual system to solve, go straight to the RREF calculator, which handles the augmented matrix and tells you whether the solution is unique, absent or a family. For a 2×2 or 3×3 system with a non-zero determinant, Cramer's rule gives each unknown as a ratio of determinants.

To verify an inverse by hand, multiply it back with the matrix multiplication calculator and confirm you get the identity — that is exactly the residual this page reports. If you need repeated solves with the same matrix and different right-hand sides, an LU decomposition is the professional answer: factor once, then each new solve costs only n² instead of n³.

The condition number is a spectral quantity in disguise. In the 2-norm it is the ratio of the largest to the smallest singular value, and for a symmetric matrix that is the ratio of the largest to the smallest absolute eigenvalue — which is why a matrix with an eigenvalue near zero is close to singular.

Frequently asked questions

When does a matrix have no inverse?

Exactly when its determinant is zero. Equivalently: when its rows are linearly dependent, when its rank is less than its size, when zero is one of its eigenvalues, or when the system Ax = 0 has a solution other than x = 0. These are all the same condition stated differently, and the calculator reports the determinant so you can see it immediately.

What is the fastest way to invert a 2x2 matrix by hand?

Swap the two diagonal entries, negate the two off-diagonal entries, and divide everything by ad − bc. For [[4,7],[2,6]] the determinant is 24 − 14 = 10, and the inverse is one tenth of [[6,−7],[−2,4]] = [[0.6,−0.7],[−0.2,0.4]]. It is worth memorising; the 3×3 version has no comparable shortcut.

Why does the calculator show an adjugate as well as an inverse?

Because the adjugate is the inverse with the fractions cleared. For an integer matrix, adj(A) has integer entries and A⁻¹ = adj(A)/det(A), so the adjugate shows the numerators and the determinant is the single common denominator. That is the form most textbooks want in an answer, and it is far easier to check by hand than a grid of decimals.

What condition number is too large?

As a rule of thumb you lose about log₁₀(κ) significant digits, so with double precision's roughly 16 digits a κ of 10⁸ still leaves you eight good digits, and 10¹⁴ leaves you two. The calculator warns above 10⁶. What counts as too large depends on how accurate your input data is: if your entries are only good to three digits, a κ of 10³ has already consumed all the accuracy you had.

Can this invert a 5x5 or larger matrix?

Not here — four rows is the limit, which covers the sizes people work through by hand and the transformation matrices used in graphics. For larger systems the answer is usually not to form the inverse at all: factor the matrix with LU and solve, or use a specialist numerical library. The mathematics is identical; only the bookkeeping grows.

Is the inverse of a symmetric matrix symmetric?

Yes, whenever it exists. Transposing A·A⁻¹ = I gives (A⁻¹)ᵀAᵀ = I, and if A = Aᵀ that says (A⁻¹)ᵀ is also an inverse of A — and inverses are unique, so (A⁻¹)ᵀ = A⁻¹. You can see it in the default matrix above: both it and its inverse are symmetric about the diagonal.

Why is the residual not exactly zero?

Because the elimination is done in floating-point arithmetic, where numbers like one third have no exact representation. A residual around 10⁻¹⁶ is the expected size of that rounding for a well-conditioned matrix. A residual much larger than that, say 10⁻⁸, is a real signal: the condition number will be large, and the inverse has lost roughly half its digits.

How do I invert a diagonal matrix?

Take the reciprocal of each diagonal entry and leave the zeros alone. diag(2, 5, 0.5) inverts to diag(0.5, 0.2, 2). If any diagonal entry is zero the matrix is singular and there is no inverse, which matches the determinant rule since the determinant of a diagonal matrix is the product of its entries.

References

  • Introduction to Linear Algebra, 5th ed., §2.5 (Inverse Matrices) — Gilbert Strang, Wellesley-Cambridge Press
  • Matrix Computations, 4th ed., chapter 3 (General Linear Systems) — Gene H. Golub and Charles F. Van Loan, Johns Hopkins University Press
  • Accuracy and Stability of Numerical Algorithms, 2nd ed., chapter 14 (Matrix Inversion) — Nicholas J. Higham, Society for Industrial and Applied Mathematics