Velamuvi AIAcademy

Lesson 7 of 16 · Matrices

Identity and Inverse

Understand the identity matrix, the inverse as an undo button, the 2 x 2 inverse formula, singular matrices that cannot be undone, and why np.linalg.solve beats inverting in practice.

  • 9 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • What the identity matrix is and why it is the 'do nothing' matrix
  • What the inverse of a matrix is
  • How to find the inverse of a 2 × 2 matrix
  • What a singular matrix is and why it has no inverse
  • Why programmers rarely compute inverses directly

The matrix that does nothing

With ordinary numbers, multiplying by 1 changes nothing: 7 × 1 = 7. Matrices have their own "1".

     [ 1  0  0 ]
I₃ = [ 0  1  0 ]
     [ 0  0  1 ]
Try it yourselfPython
import numpy as np

I = np.eye(3)            # "eye" sounds like I
print(I)

x = np.array([4, 7, 2])
print(I @ x)             # unchanged

The inverse: an undo button

With numbers, 5 × (1/5) = 1. The number 1/5 "undoes" multiplying by 5. Some matrices have an undo button too.

Try it yourselfPython
import numpy as np

A = np.array([[2.0, 1.0],
              [1.0, 1.0]])
A_inv = np.linalg.inv(A)

print(A_inv)
print(np.round(A @ A_inv, 10) + 0.0)  # the identity (rounding hides tiny float errors)

x = np.array([3.0, 5.0])
y = A @ x                              # apply A
print("y =", y)
print("back to x:", A_inv @ y)        # undo it

The 2 × 2 inverse formula

For a 2 × 2 matrix there is a neat formula:

A = [ a  b ]          A⁻¹ =    1      [  d  -b ]
    [ c  d ]                ------- × [ -c   a ]
                            ad - bc

In words: swap a and d, flip the signs of b and c, then divide everything by ad - bc.

When there is no inverse

The number ad - bc is so important it has a name, the determinant (its own lesson comes later). If it is 0, the formula would divide by zero.

Try it yourselfPython
import numpy as np

S = np.array([[1.0, 2.0],
              [2.0, 4.0]])        # the second row is just double the first

det = np.linalg.det(S)
print("determinant:", round(det, 6) + 0.0)

# Always check before inverting
if abs(det) < 1e-10:
    print("No inverse: this matrix is singular")
else:
    print(np.linalg.inv(S))

Why can it not be undone? S turns many different vectors into the same result, so you cannot tell which one you started with:

Try it yourselfPython
import numpy as np

S = np.array([[1, 2],
              [2, 4]])
print(S @ np.array([2, 0]))
print(S @ np.array([0, 1]))      # a different input, the same output

Like a blender: once two different fruits come out as the same smoothie, you cannot un-blend them.

Solving equations: use solve, not inv

Inverses are the key to solving equations of the form A x = b ("which x does A turn into b?"): mathematically, x = A⁻¹b. In code, there is a better way:

Try it yourselfPython
import numpy as np

A = np.array([[2.0, 1.0],
              [1.0, 3.0]])
b = np.array([5.0, 10.0])

x = np.linalg.solve(A, b)        # faster and more accurate than inv(A) @ b
print(x)
print(A @ x)                     # check: gives back b

Common mistakes

Practice exercise

Write your own 2 × 2 inverse, and check it the safe way.

Your solutionPython
import numpy as np

A = np.array([[4.0, 7.0],
              [2.0, 6.0]])

# 1. Write inverse_2x2(M) using the formula: swap a and d, flip the signs of b and c,
#    divide by ad - bc. Return a NumPy array.
# 2. Print the inverse of A, rounded to 2 decimal places.
# 3. Check that A @ inverse is the identity with np.allclose, and print the result.

# Output must be exactly:
# [[ 0.6 -0.7]
#  [-0.2  0.4]]
# Is identity: True
Knowledge check

Which matrix has no inverse?

Summary

  • The identity matrix I has 1s on the main diagonal and leaves vectors unchanged.
  • The inverse A⁻¹ undoes A: A⁻¹ @ A = I.
  • For 2 × 2: swap a and d, flip the signs of b and c, divide by ad - bc.
  • A singular matrix has determinant 0, squashes information and has no inverse.
  • To solve A x = b in code, use np.linalg.solve, not inv.

Next: the transpose, which flips a matrix over its diagonal.

Finished reading? Tick it off to track your progress.