linalg (Linear Algebra) (NumPy)

np.linalg is NumPy's linear algebra module, providing functions to invert matrices, compute determinants, solve systems of equations, and find eigenvalues, norms, and decompositions. It wraps the battle-tested LAPACK library.

NumPy

The everyday tools live here: np.linalg.inv for the matrix inverse, np.linalg.det for the determinant, np.linalg.solve to solve Ax = b, np.linalg.eig for eigenvalues and eigenvectors, and np.linalg.norm for vector and matrix norms. Decompositions like SVD (svd), QR (qr), and Cholesky (cholesky) round it out.

For solving linear systems, prefer np.linalg.solve(A, b) over computing the inverse and multiplying — it is both faster and numerically more stable. Computing inv just to multiply is a common but avoidable mistake.

Because it calls optimized LAPACK routines, linalg is fast even on large matrices, and it underpins much of the math in machine learning, statistics, and graphics.

Syntax

np.linalg.solve(A, b)  |  np.linalg.inv(A)  |  np.linalg.det(A)

Examples

Python — Solves Ax = b for x.

import numpy as np
A = np.array([[2, 0], [0, 4]])
b = np.array([2, 8])
print(np.linalg.solve(A, b))

Output:

[1. 2.]

Learn it in a lesson

See also

Back to the Dictionary