Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 49 ยท Linear Algebra

Matrix Operations

Learn NumPy matrix operations: transpose, trace, determinant, inverse, matrix_power, block matrices and kron, and why you should rarely invert a matrix.

A matrix in NumPy is just a two-dimensional array, and the numpy.linalg module plus a handful of array methods give you everything you need to work with it. In the previous lesson you multiplied vectors and matrices with @. This lesson covers the next layer: flipping a matrix, summarizing it with a single number, undoing it, raising it to a power and gluing matrices together.

You will meet two of the most famous matrix quantities, the determinant and the inverse, and, just as important, learn when not to use the inverse. Many beginners compute inv(A) @ b because that is what the textbook formula says. In practice there is a faster and more accurate tool, which the next lesson covers in depth.

Always use plain ndarray objects with @. NumPy also has an old np.matrix class, but it is deprecated in spirit, always 2D and behaves differently from arrays. Everything on this page uses regular arrays.

Transpose, trace and identity

The transpose swaps rows and columns: element (i, j) becomes (j, i). Use A.T. As you saw in reshaping, a transpose returns a view, so it costs nothing and no data is copied.

The trace is the sum of the main diagonal, np.trace(A). The identity matrix np.eye(n) has ones on the diagonal and zeros elsewhere; multiplying by it changes nothing.

Python Transpose, trace and identity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two properties are worth remembering, because they come up constantly:

  • (A @ B).T equals B.T @ A.T: the order reverses.
  • np.trace(A @ B) equals np.trace(B @ A) even when A @ B and B @ A are different matrices.

The determinant

The determinant of a square matrix is a single number, np.linalg.det(A). Geometrically, it is the factor by which the matrix scales areas (in 2D) or volumes (in 3D). A negative value means the matrix also flips orientation, like a mirror.

The most useful fact: a matrix has an inverse only if its determinant is not zero. A zero determinant means the matrix squashes space onto a line or a point, and that cannot be undone.

flowchart LR
    A["Square matrix A"] --> D{"det(A) is zero?"}
    D -- no --> I["Invertible:<br/>one unique solution"]
    D -- yes --> S["Singular:<br/>no inverse"]

The determinant decides whether a square matrix can be inverted.

Python Determinants as area scaling

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Floating-point arithmetic means the determinant of a singular matrix can come out as something like 1e-17 instead of exactly 0, which is why the code above rounds. Never test det(A) == 0 directly. For a real check, use the condition number from the next lesson or np.linalg.matrix_rank.

Huge or tiny determinants: slogdet

The determinant is a product of many numbers, so for a large matrix it easily overflows (becomes inf) or underflows (becomes 0.0) even when the matrix is perfectly healthy. np.linalg.slogdet(A) avoids that by returning two numbers: the sign of the determinant (1.0, -1.0 or 0.0) and logabsdet, the natural logarithm of its absolute value. Together they describe det(A) = sign * exp(logabsdet) without ever building the huge number. Statistics code uses it for the log-likelihood of multivariate normal distributions.

Python det versus slogdet

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a singular matrix slogdet returns sign 0.0 and logabsdet of -inf, so check the sign before you use the logarithm.

The inverse

The inverse Aโปยน is the matrix that undoes A: A @ Aโปยน gives the identity. NumPy computes it with np.linalg.inv(A). Because of rounding, the product is close to the identity, so compare with np.allclose, never ==.

If the matrix is singular, inv raises numpy.linalg.LinAlgError.

Python Inverse and the identity check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why you rarely need the inverse

Suppose you have A @ x = b and want x. The textbook answer is x = inv(A) @ b. Do not write that. Computing the full inverse takes more work than solving for one x, and it is usually less accurate. np.linalg.solve(A, b) finds x directly, and it is the right tool whenever the inverse is only a stepping stone. The next lesson, Solving Linear Systems, shows the details.

Reach for inv only when you truly need the inverse matrix itself, for example to inspect it or to reuse it on many right-hand sides in a formula you cannot rewrite.

Matrix powers

np.linalg.matrix_power(A, n) multiplies a square matrix by itself n times, using repeated squaring so it stays fast. Negative powers use the inverse. Note the difference from A ** 2, which squares every element separately.

A classic use: the Fibonacci sequence hides inside powers of a small matrix.

Python matrix_power versus elementwise power

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Integer matrices stay integer under matrix_power, so they can overflow for large n (see data types). Use dtype=object or floats when numbers can grow very large.

Block matrices and Kronecker products

Sometimes you build a big matrix out of smaller ones. np.block assembles a matrix from nested lists of blocks, and np.kron(A, B) computes the Kronecker product: every element of A multiplied by the whole of B, laid out as a bigger matrix.

Python np.block and np.kron

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

flowchart TD
    K["np.kron(A, B)"] --> E["each element a_ij of A"]
    E --> S["multiplied by the whole matrix B"]
    S --> R["result shape = A.shape * B.shape<br/>(2x2 and 2x2 give 4x4)"]

The Kronecker product scales a copy of B by every element of A, so shapes multiply.

Matrix rank

The rank is the number of independent rows. A square n x n matrix is invertible exactly when its rank is n. NumPy gives it with np.linalg.matrix_rank, which uses a tolerance, so it is more trustworthy than checking the determinant.

Python Rank tells you about invertibility

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a determinant of zero tell you about a square matrix?

    A zero determinant means the matrix collapses space and cannot be undone, so np.linalg.inv raises LinAlgError.
  2. 2. Which expression multiplies a square matrix A by itself, as matrix multiplication?

    A ** 2 and np.square(A) square each element separately. matrix_power(A, 2) is A @ A.
  3. 3. Why should you usually avoid inv(A) @ b to solve A x = b?

    Computing the whole inverse does extra work and accumulates more rounding error. solve finds x directly.
  4. 4. How do you correctly test that A @ A_inv is the identity?

    Floating-point rounding makes the product only approximately the identity, so compare with a tolerance using allclose.

Summary

  • A.T transposes without copying; np.trace sums the diagonal; np.eye(n) is the identity.
  • np.linalg.det measures how a matrix scales area or volume; zero means singular.
  • np.linalg.inv undoes a matrix but raises LinAlgError for singular input. Check results with np.allclose.
  • Do not use the inverse to solve equations; np.linalg.solve is faster and more accurate.
  • np.linalg.matrix_power is repeated matrix multiplication, unlike elementwise **.
  • np.block assembles matrices from blocks; np.kron multiplies every element by a whole matrix.
  • np.linalg.matrix_rank is the reliable way to detect a singular matrix.

Next, put these ideas to work in Solving Linear Systems and Least Squares.