Linear algebra (numpy.linalg)

The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient low level implementations of standard linear algebra algorithms. Those libraries may be provided by NumPy itself using C versions of a subset of their reference implementations but, when possible, highly optimized libraries that take advantage of specialized processor functionality are preferred. Examples of such libraries are OpenBLASopen in new window, MKL (TM), and ATLAS. Because those libraries are multithreaded and processor dependent, environmental variables and external packages such as threadpoolctlopen in new window may be needed to control the number of threads or specify the processor architecture.

Matrix and vector products

methoddescription
dotopen in new window(a, b[, out])Dot product of two arrays.
linalg.multi_dotopen in new window(arrays)Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order.
vdotopen in new window(a, b)Return the dot product of two vectors.
inneropen in new window(a, b)Inner product of two arrays.
outeropen in new window(a, b[, out])Compute the outer product of two vectors.
matmulopen in new window(x1, x2, /[, out, casting, order, …])Matrix product of two arrays.
tensordotopen in new window(a, b[, axes])Compute tensor dot product along specified axes.
einsumopen in new window(subscripts, *operands[, out, dtype, …])Evaluates the Einstein summation convention on the operands.
einsum_pathopen in new window(subscripts, *operands[, optimize])Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays.
linalg.matrix_poweropen in new window(a, n)Raise a square matrix to the (integer) power n.
kronopen in new window(a, b)Kronecker product of two arrays.

Decompositions

methoddescription
linalg.choleskyopen in new window(a)Cholesky decomposition.
linalg.qropen in new window(a[, mode])Compute the qr factorization of a matrix.
linalg.svdopen in new window(a[, full_matrices, compute_uv, …])Singular Value Decomposition.

Matrix eigenvalues

methoddescription
linalg.eigopen in new window(a)Compute the eigenvalues and right eigenvectors of a square array.
linalg.eighopen in new window(a[, UPLO])Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.
linalg.eigvalsopen in new window(a)Compute the eigenvalues of a general matrix.
linalg.eigvalshopen in new window(a[, UPLO])Compute the eigenvalues of a complex Hermitian or real symmetric matrix.

Norms and other numbers

methoddescription
linalg.normopen in new window(x[, ord, axis, keepdims])Matrix or vector norm.
linalg.condopen in new window(x[, p])Compute the condition number of a matrix.
linalg.detopen in new window(a)Compute the determinant of an array.
linalg.matrix_rankopen in new window(M[, tol, hermitian])Return matrix rank of array using SVD method
linalg.slogdetopen in new window(a)Compute the sign and (natural) logarithm of the determinant of an array.
traceopen in new window(a[, offset, axis1, axis2, dtype, out])Return the sum along diagonals of the array.

Solving equations and inverting matrices

methoddescription
linalg.solveopen in new window(a, b)Solve a linear matrix equation, or system of linear scalar equations.
linalg.tensorsolveopen in new window(a, b[, axes])Solve the tensor equation a x = b for x.
linalg.lstsqopen in new window(a, b[, rcond])Return the least-squares solution to a linear matrix equation.
linalg.invopen in new window(a)Compute the (multiplicative) inverse of a matrix.
linalg.pinvopen in new window(a[, rcond, hermitian])Compute the (Moore-Penrose) pseudo-inverse of a matrix.
linalg.tensorinvopen in new window(a[, ind])Compute the ‘inverse’ of an N-dimensional array.

Exceptions

methoddescription
linalg.LinAlgErroropen in new windowGeneric Python-exception-derived object raised by linalg functions.

Linear algebra on several matrices at once

New in version 1.8.0.

Several of the linear algebra routines listed above are able to compute results for several matrices at once, if they are stacked into the same array.

This is indicated in the documentation via input parameter specifications such as a : (..., M, M) array_like. This means that if for instance given an input array a.shape == (N, M, M), it is interpreted as a “stack” of N matrices, each of size M-by-M. Similar specification applies to return values, for instance the determinant has det : (...) and will in this case return an array of shape det(a).shape == (N,). This generalizes to linear algebra operations on higher-dimensional arrays: the last 1 or 2 dimensions of a multidimensional array are interpreted as vectors or matrices, as appropriate for each operation.