numpy.linalg — Linear algebra routines

The numpy.linalg submodule provides a small selection of linear-algebra routines that operate on numpy.ndarray objects. Functions that operate on matrices require two-dimensional arrays; passing arrays of a different dimensionality raises a ValueError. Complex dtypes are not supported by this submodule.

Functions

numpy.linalg.cholesky(A: ndarray) ndarray

Compute the Cholesky decomposition of a positive-definite, symmetric square matrix.

Parameters:

A – a positive-definite, symmetric, two-dimensional square matrix.

Returns:

a lower-triangular numpy.ndarray L such that A = L @ L.T.

Raises:

ValueError – if A is not square, not symmetric, or not positive definite.

numpy.linalg.det(m: ndarray) float

Compute the determinant of a square matrix.

Parameters:

m – a two-dimensional square matrix.

Returns:

the determinant of m as a Python float. The return value is always a float, even when the input is an integer array.

Raises:

ValueError – if m is not square.

The computation is performed by Gaussian elimination with partial pivoting.

numpy.linalg.eig(m: ndarray) tuple[ndarray, ndarray]

Compute the eigenvalues and eigenvectors of a real symmetric square matrix.

Parameters:

m – a two-dimensional, real, symmetric square matrix.

Returns:

a 2-tuple (eigenvalues, eigenvectors). eigenvalues is a one-dimensional array of length N; eigenvectors is an N-by-N array whose columns are the corresponding eigenvectors.

Raises:

ValueError – if m is not square, not symmetric, or if the Jacobi-rotation iteration fails to converge.

numpy.linalg.inv(m: ndarray) ndarray

Compute the inverse of a square matrix.

Parameters:

m – a two-dimensional square matrix.

Returns:

the inverse of m as a float numpy.ndarray.

Raises:

ValueError – if m is not square or is singular.

The inversion is performed by Gauss-Jordan elimination.

numpy.linalg.norm(x: ndarray, axis: int | None = None) float | ndarray

Compute the 2-norm of a vector or matrix.

Parameters:
  • x – an numpy.ndarray, tuple, list, or range.

  • axis – optional axis along which the norm is computed. If None (the default), the norm is taken over the flattened input and a single float is returned. Otherwise the norm is reduced along that axis and a numpy.ndarray is returned.

Returns:

either a Python float (when axis is None or when x is an iterable other than an ndarray) or a numpy.ndarray with that axis removed.

The 2-norm is computed as sqrt(sum(x*x)) using a numerically stable running mean, so the operation does not require additional storage proportional to the size of the input.

numpy.linalg.qr(m: ndarray, mode: str = 'reduced') tuple[ndarray, ndarray]

Factor a matrix as the product of an orthonormal matrix and an upper-triangular matrix.

Parameters:
  • m – a two-dimensional matrix of shape (M, N).

  • mode – either 'reduced' (default) or 'complete'. In 'complete' mode, Q has shape (M, M) and R has shape (M, N). In 'reduced' mode, Q and R are truncated to the leading min(M, N) columns and rows respectively.

Returns:

a 2-tuple (Q, R) of numpy.ndarray objects such that m == Q @ R, with Q orthonormal and R upper-triangular.

Raises:

The decomposition is implemented via successive Givens rotations.