Identity, Inverse & Determinant
The identity matrix (np.eye), the inverse of a matrix (np.linalg.inv) and the determinant (np.linalg.det) — what each means and how they connect.
With ordinary numbers, 1 is special (x × 1 = x) and every non-zero number has a reciprocal (5 × 1/5 = 1). Matrices have the same ideas: the identity matrix plays the role of 1, the inverse plays the role of the reciprocal, and the determinant tells you whether an inverse exists at all. These three are the foundation for solving equations in the next topic.
Definition
The identity matrix
Iis a square matrix with 1s on the main diagonal and 0s everywhere else. For any matrixAof matching size,A @ I = I @ A = A.
The inverse of a square matrix
A, writtenA⁻¹, is the matrix that "undoes" A:A @ A⁻¹ = A⁻¹ @ A = I.
The determinant of a square matrix,
det(A), is a single number computed from its entries. Ifdet(A) = 0the matrix is singular — it has no inverse. Otherwise it is invertible.
- Square — same number of rows and columns. Only square matrices have an identity partner, an inverse and a determinant.
- Main diagonal — positions
[0,0], [1,1], [2,2], …. - Singular — the matrix squashes space flat (one row is a combination of others), so information is lost and cannot be undone.
- For a 2×2 matrix
[[a, b], [c, d]]:det = a·d − b·candinverse = (1/det) × [[d, −b], [−c, a]].
Syntax
python
np.eye(N, M=None, k=0, dtype=float)
np.identity(n, dtype=float)
np.linalg.inv(a)
np.linalg.det(a)
np.allclose(a, b) # compare floats with a tolerance| Function | Parameter | Meaning | Default |
|---|---|---|---|
np.eye | N | Number of rows | — required |
np.eye | M | Number of columns | None → same as N |
np.eye | k | Which diagonal holds the 1s (0 = main, 1 = above, −1 = below) | 0 |
np.eye / identity | dtype | Data type of the result | float |
np.identity | n | Size of the square identity matrix | — required |
np.linalg.inv | a | Square, non-singular matrix | — required |
np.linalg.det | a | Square matrix | — required |
Example — identity matrix
python
import numpy as np
I = np.eye(3)
print(I)
print(np.identity(3, dtype=int))
A = np.array([[2, 1, 0], [1, 3, 1], [0, 1, 4]])
print(np.array_equal(A @ np.eye(3, dtype=int), A))
print(np.eye(2, 3, k=1, dtype=int))text
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
[[1 0 0]
[0 1 0]
[0 0 1]]
True
[[0 1 0]
[0 0 1]]How it works: np.eye returns floats by default (1.). Multiplying by I leaves A unchanged. np.eye is more flexible than np.identity — it can be rectangular and shift the diagonal with k.
Example — inverse, and the floating-point surprise
python
A = np.array([[4, 7],
[2, 6]])
A_inv = np.linalg.inv(A)
print(A_inv)
print(A @ A_inv)
print(np.allclose(A @ A_inv, np.eye(2)))
print((A @ A_inv).round(10))text
[[ 0.6 -0.7]
[-0.2 0.4]]
[[ 1.00000000e+00 0.00000000e+00]
[-2.22044605e-16 1.00000000e+00]]
True
[[ 1. 0.]
[-0. 1.]]How it works: by the 2×2 formula, det = 4·6 − 7·2 = 10, so A⁻¹ = (1/10) × [[6, −7], [−2, 4]] = [[0.6, −0.7], [−0.2, 0.4]] ✔. A @ A_inv should be exactly I, but computers store decimals in binary, so one cell is -2.22e-16 (= −0.000000000000000222) instead of 0. That is a tiny rounding error, not a bug. Always compare float matrices with np.allclose, never with ==. (-0. after rounding is just "negative zero", which equals 0.)
Example — determinant and a singular matrix
python
A = np.array([[4, 7],
[2, 6]])
print(np.linalg.det(A))
print(round(np.linalg.det(A), 6))
S = np.array([[1, 2],
[2, 4]])
print(np.linalg.det(S))
try:
np.linalg.inv(S)
except np.linalg.LinAlgError as e:
print("LinAlgError:", e)text
10.000000000000002
10.0
0.0
LinAlgError: Singular matrixdet(A) should be 10 — again a tiny float error, so round it for display. In S, the second row is exactly 2 × the first row, so det(S) = 1·4 − 2·2 = 0: the matrix is singular and inv raises LinAlgError: Singular matrix.
Example — a 3×3 matrix
python
M = np.array([[2, 1, 0],
[1, 3, 1],
[0, 1, 4]])
print(round(float(np.linalg.det(M)), 6))
print(np.linalg.inv(M).round(3))
print(np.allclose(M @ np.linalg.inv(M), np.eye(3)))text
18.0
[[ 0.611 -0.222 0.056]
[-0.222 0.444 -0.111]
[ 0.056 -0.111 0.278]]
TrueWhat the determinant tells you
| det(A) | Meaning |
|---|---|
0 | singular — no inverse; rows are linearly dependent; equations have no unique solution |
| non-zero | invertible — a unique inverse exists |
| negative | the transformation also "flips" orientation (mirror image) |
| large / small absolute value | how much the matrix stretches / shrinks area (2D) or volume (3D) |
det(I) = 1 | the identity changes nothing |
Common mistakes
- ❌
A ** -1or1 / Afor the inverse — these are element-wise reciprocals. ✅np.linalg.inv(A). - ❌ Inverting a non-square matrix →
LinAlgError: Last 2 dimensions of the array must be square. ✅ Only square matrices have inverses (look upnp.linalg.pinvfor the pseudo-inverse). - ❌
(A @ A_inv == np.eye(2)).all()→ oftenFalsebecause of rounding. ✅np.allclose(A @ A_inv, np.eye(2)). - ❌ Checking
det == 0exactly for float matrices. ✅ Checkabs(det) < 1e-10, or better, just try and catchLinAlgError.
Interview tip
Q: When does a matrix not have an inverse? A: When it is not square, or when its determinant is 0 (it is singular) — this happens when one row/column is a linear combination of the others, so the matrix loses information that cannot be recovered.
Key points to remember
- The identity matrix I has 1s on the diagonal and 0s elsewhere; A @ I = A. Create it with np.eye(n) or np.identity(n).
- The inverse A⁻¹ satisfies A @ A⁻¹ = I; compute it with np.linalg.inv(A).
- The determinant (np.linalg.det) is one number; det = 0 means the matrix is singular and has no inverse.
- For a 2×2 matrix [[a, b], [c, d]], det = ad − bc.
- Only square matrices have determinants and inverses.
- Floating-point results are slightly off (e.g. 10.000000000000002); compare with np.allclose and round for display.
- 1 / A and A ** -1 are element-wise, not the matrix inverse.
Try it yourself
Identity, inverse and check
Singular matrix: det 0, no inverse
Practice: 2x2 inverse by formula
Revision Flashcards
Tap a card to flip it and see the answer.
