Linear algebra#
The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficientlow level implementations of standard linear algebra algorithms. Thoselibraries may be provided by NumPy itself using C versions of a subset of theirreference implementations but, when possible, highly optimized libraries thattake advantage of specialized processor functionality are preferred. Examplesof such libraries areOpenBLAS, MKL (TM), and ATLAS. Because those librariesare multithreaded and processor dependent, environmental variables and externalpackages such asthreadpoolctl may be needed to control the number of threadsor specify the processor architecture.
The SciPy library also contains alinalg submodule, and there isoverlap in the functionality provided by the SciPy and NumPy submodules. SciPycontains functions not found innumpy.linalg, such as functions related toLU decomposition and the Schur decomposition, multiple ways of calculating thepseudoinverse, and matrix transcendentals such as the matrix logarithm. Somefunctions that exist in both have augmented functionality inscipy.linalg.For example,scipy.linalg.eig can take a second matrix argument for solvinggeneralized eigenvalue problems. Some functions in NumPy, however, have moreflexible broadcasting options. For example,numpy.linalg.solve can handle“stacked” arrays, whilescipy.linalg.solve accepts only a single squarearray as its first argument.
Note
The termmatrix as it is used on this page indicates a 2dnumpy.arrayobject, andnot anumpy.matrix object. The latter is no longerrecommended, even for linear algebra. Seethe matrix object documentation formore information.
The@ operator#
Introduced in NumPy 1.10.0, the@ operator is preferable toother methods when computing the matrix product between 2d arrays. Thenumpy.matmul function implements the@ operator.
Matrix and vector products#
| Dot product of two arrays. |
| Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order. |
| Return the dot product of two vectors. |
| Vector dot product of two arrays. |
| Computes the vector dot product. |
| Inner product of two arrays. |
| Compute the outer product of two vectors. |
| Compute the outer product of two vectors. |
| Matrix product of two arrays. |
| Computes the matrix product. |
| Matrix-vector dot product of two arrays. |
| Vector-matrix dot product of two arrays. |
| Compute tensor dot product along specified axes. |
| Compute tensor dot product along specified axes. |
| Evaluates the Einstein summation convention on the operands. |
| Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays. |
| Raise a square matrix to the (integer) powern. |
| Kronecker product of two arrays. |
| Returns the cross product of 3-element vectors. |
Decompositions#
| Cholesky decomposition. |
| Compute the qr factorization of a matrix. |
| Singular Value Decomposition. |
| Returns the singular values of a matrix (or a stack of matrices) |
Matrix eigenvalues#
| Compute the eigenvalues and right eigenvectors of a square array. |
| Return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix. |
Compute the eigenvalues of a general matrix. | |
| Compute the eigenvalues of a complex Hermitian or real symmetric matrix. |
Norms and other numbers#
| Matrix or vector norm. |
| Computes the matrix norm of a matrix (or a stack of matrices) |
| Computes the vector norm of a vector (or batch of vectors) |
| Compute the condition number of a matrix. |
| Compute the determinant of an array. |
| Return matrix rank of array using SVD method |
Compute the sign and (natural) logarithm of the determinant of an array. | |
| Return the sum along diagonals of the array. |
| Returns the sum along the specified diagonals of a matrix (or a stack of matrices) |
Solving equations and inverting matrices#
| Solve a linear matrix equation, or system of linear scalar equations. |
| Solve the tensor equation |
| Return the least-squares solution to a linear matrix equation. |
| Compute the inverse of a matrix. |
| Compute the (Moore-Penrose) pseudo-inverse of a matrix. |
| Compute the 'inverse' of an N-dimensional array. |
Other matrix operations#
| Return specified diagonals. |
| Returns specified diagonals of a matrix (or a stack of matrices) |
| Transposes a matrix (or a stack of matrices) |
Exceptions#
Generic Python-exception-derived object raised by linalg functions. |
Linear algebra on several matrices at once#
Several of the linear algebra routines listed above are able tocompute results for several matrices at once, if they are stacked intothe same array.
This is indicated in the documentation via input parameterspecifications such asa:(...,M,M)array_like. This means thatif for instance given an input arraya.shape==(N,M,M), it isinterpreted as a “stack” of N matrices, each of size M-by-M. Similarspecification applies to return values, for instance the determinanthasdet:(...) and will in this case return an array of shapedet(a).shape==(N,). This generalizes to linear algebraoperations on higher-dimensional arrays: the last 1 or 2 dimensions ofa multidimensional array are interpreted as vectors or matrices, asappropriate for each operation.