NumPy Linear Algebra
NumPy provides a linear algebra function librarylinalg, which contains all the functions required for linear algebra. You can see the description below:
| Function | Description |
|---|---|
dot |
Dot product of two arrays, i.e., corresponding elements are multiplied. |
vdot |
Dot product of two vectors |
inner |
Inner product of two arrays |
matmul |
Matrix product of two arrays |
determinant |
Determinant of an array |
solve |
Solve linear matrix equations |
inv |
Compute the multiplicative inverse of a matrix |
numpy.dot()
numpy.dot() For two one-dimensional arrays, it calculates the sum of products of the elements at corresponding indices of the two arrays (mathematically called the vector dot product); for two-dimensional arrays, it calculates the matrix product of the two arrays; for multi-dimensional arrays, its general calculation formula is as follows, that is, each element in the resulting array is: the sum of products of all elements on the last dimension of array a and all elements on the second-to-last dimension of array b:dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])。
numpy.dot(a, b, out=None)
Parameter description:
- a: ndarray array
- b: ndarray array
- out: ndarray, optional, used to store the calculation result of dot()
Example
The output result is:
[[37 40] [85 92]]
The calculation formula is:
[[1*11+2*13, 1*12+2*14],[3*11+4*13, 3*12+4*14]]
numpy.vdot()
The numpy.vdot() function is the dot product of two vectors. If the first argument is a complex number, its complex conjugate is used for the calculation. If the argument is a multi-dimensional array, it is flattened.
Example
The output result is:
130
The calculation formula is:
1*11 + 2*12 + 3*13 + 4*14 = 130
numpy.inner()
The numpy.inner() function returns the vector inner product of one-dimensional arrays. For higher dimensions, it returns the sum-product over the last axes.
Example
The output result is:
2
Multi-dimensional array example
The output result is:
数组 a: [[1 2] [3 4]] 数组 b: [[11 12] [13 14]] 内积: [[35 41] [81 95]] 数组 a: [[1 2] [3 4]] 数组 b: [[11 12] [13 14]] 内积: [[35 41] [81 95]]
The inner product calculation formula is:
1*11+2*12, 1*13+2*14 3*11+4*12, 3*13+4*14
numpy.matmul
The numpy.matmul function returns the matrix product of two arrays. Although it returns the normal product of two-dimensional arrays, if either argument has a dimension greater than 2, it is treated as a stack of matrices existing in the last two indices, and broadcasting is performed accordingly.
On the other hand, if either argument is a one-dimensional array, it is promoted to a matrix by appending 1 to its dimensions, and is removed after the multiplication.
For two-dimensional arrays, it is matrix multiplication:
Example
The output result is:
[[4 1] [2 2]]
Two-dimensional and one-dimensional operations:
Example
The output result is:
[1 2] [1 2]
Arrays with dimensions greater than two:
Example
The output result is:
[[[ 2 3] [ 6 11]] [[10 19] [14 27]]]
numpy.linalg.det()
The numpy.linalg.det() function calculates the determinant of the input matrix.
The determinant is a very useful value in linear algebra. It is calculated from the diagonal elements of a square matrix. For a 2×2 matrix, it is the difference between the product of the top-left and bottom-right elements and the product of the other two.
In other words, for the matrix [[a, b], [c, d]], the determinant is calculated as ad-bc. Larger square matrices are considered as combinations of 2×2 matrices.
Example
The output result is:
-2.0
Example
The output result is:
[[ 6 1 1] [ 4 -2 5] [ 2 8 7]] -306.0 -306
numpy.linalg.solve()
The numpy.linalg.solve() function gives the solution of linear equations in matrix form.
Consider the following linear equations:
x + y + z = 6 2y + 5z = -4 2x + 5y - z = 27
It can be expressed in matrix form as:

If the matrices are A, X, and B, the equation becomes:
AX = B 或 X = A^(-1)B
numpy.linalg.inv()
The numpy.linalg.inv() function computes the multiplicative inverse of a matrix.
Inverse matrix: Suppose A is an n-order matrix over a number field. If there exists another n-order matrix B over the same number field such that: AB=BA=E, then we call B the inverse matrix of A, and A is called an invertible matrix. Note: E is the identity matrix.
Example
The output result is:
[[1 2] [3 4]] [[-2. 1. ] [ 1.5 -0.5]] [[1.0000000e+00 0.0000000e+00] [8.8817842e-16 1.0000000e+00]]
Now create the inverse matrix of matrix A:
Example
The output result is:
数组 a: [[ 1 1 1] [ 0 2 5] [ 2 5 -1]] a 的逆: [[ 1.28571429 -0.28571429 -0.14285714] [-0.47619048 0.14285714 0.23809524] [ 0.19047619 0.14285714 -0.0952381 ]] 矩阵 b: [[ 6] [-4] [27]] 计算:A^(-1)B: [[ 5.] [ 3.] [-2.]]
The result can also be obtained using the following function:
x = np.dot(ainv,b)Other extensions