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.ndarrayLsuch thatA = 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).eigenvaluesis a one-dimensional array of lengthN;eigenvectorsis anN-by-Narray 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, orrange.axis – optional axis along which the norm is computed. If
None(the default), the norm is taken over the flattened input and a singlefloatis returned. Otherwise the norm is reduced along that axis and anumpy.ndarrayis returned.
- Returns:
either a Python
float(when axis isNoneor when x is an iterable other than an ndarray) or anumpy.ndarraywith 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,Qhas shape(M, M)andRhas shape(M, N). In'reduced'mode,QandRare truncated to the leadingmin(M, N)columns and rows respectively.
- Returns:
a 2-tuple
(Q, R)ofnumpy.ndarrayobjects such thatm == Q @ R, withQorthonormal andRupper-triangular.- Raises:
TypeError – if m is not an
numpy.ndarray.ValueError – if m is not two-dimensional or mode is not one of the accepted values.
The decomposition is implemented via successive Givens rotations.