NumPy Linear Algebra
NumPy provides a linear algebra function library linalg, which contains all the functions needed for linear algebra. You can see the following descriptions:
| function | Description |
|---|---|
dot |
The dot product of two arrays, i.e. the multiplication of corresponding elements. |
vdot |
The dot product of two vectors |
inner |
The inner product of two arrays |
matmul |
The matrix product of two arrays |
determinant |
The 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 computes 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 computes the matrix product of the two arrays; for multidimensional arrays, its general calculation formula is as follows, i.e. each element in the result 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
import
numpy
.
matlib
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
]
)
b
=
np
.
array
(
[
[
11
,
12
]
,
[
13
,
14
]
]
)
(
np
.
dot
(
a
,
b
)
)
The output 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 conjugate is used in the calculation. If the argument is a multidimensional array, it is flattened.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
]
)
b
=
np
.
array
(
[
[
11
,
12
]
,
[
13
,
14
]
]
)
vdot flattens arrays to compute the inner product
(
np
.
vdot
(
a
,
b
)
)
The output 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 product of sums along the last axis.
Example
import
numpy
as
np
(
np
.
inner
(
np
.
array
(
[
1
,
2
,
3
]
)
,
np
.
array
(
[
0
,
1
,
0
]
)
)
)
Equivalent to 10+21+3*0
The output is:
2
Multidimensional array example
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
]
)
(
'
Array a:
'
)
(
a
)
b
=
np
.
array
(
[
[
11
,
12
]
,
[
13
,
14
]
]
)
(
'
Array b:
'
)
(
b
)
(
'
Inner product:
'
)
(
np
.
inner
(
a
,
b
)
)
The output 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 is computed as:
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. While it returns the normal product for two-dimensional arrays, if either argument has more than 2 dimensions, it is treated as a stack of matrices residing in the last two indices and broadcast accordingly.
On the other hand, if either argument is a one-dimensional array, it is promoted to a matrix by appending a 1 to its dimensions, which is removed after multiplication.
For two-dimensional arrays, it is matrix multiplication:
Example
import
numpy
.
matlib
import
numpy
as
np
a
=
[
[
1
,
0
]
,
[
0
,
1
]
]
b
=
[
[
4
,
1
]
,
[
2
,
2
]
]
(
np
.
matmul
(
a
,
b
)
)
The output is:
[[4 1]
[2 2]]
Two-dimensional and one-dimensional operations:
Example
import
numpy
.
matlib
import
numpy
as
np
a
=
[
[
1
,
0
]
,
[
0
,
1
]
]
b
=
[
1
,
2
]
(
np
.
matmul
(
a
,
b
)
)
(
np
.
matmul
(
b
,
a
)
)
The output is:
[1 2]
[1 2]
Arrays with more than two dimensions:
Example
import
numpy
.
matlib
import
numpy
as
np
a
=
np
.
arange
(
8
)
.
reshape
(
2
,
2
,
2
)
b
=
np
.
arange
(
4
)
.
reshape
(
2
,
2
)
(
np
.
matmul
(
a
,
b
)
)
The output is:
[[[ 2 3]
[ 6 11]]
[[10 19]
[14 27]]]
numpy.linalg.det()
The numpy.linalg.det() function computes the determinant of the input matrix.
The determinant is a very useful value in linear algebra. It is computed from the diagonal elements of a square matrix. For a 2x2 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 computed as ad-bc. Larger square matrices are considered combinations of 2x2 matrices.
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
]
)
(
np
.
linalg
.
det
(
a
)
)
The output is:
-2.0
Example
import
numpy
as
np
b
=
np
.
array
(
[
[
6
,
1
,
1
]
,
[
4
, -
2
,
5
]
,
[
2
,
8
,
7
]
]
)
(
b
)
(
np
.
linalg
.
det
(
b
)
)
(
6
(
2
7
5
8
)
1
(
4
7
5
2
)
1
(
4
8
2
2
)
)
The output is:
[[ 6 1 1]
[ 4 -2 5]
[ 2 8 7]]
-306.0
-306
numpy.linalg.solve()
The numpy.linalg.solve() function gives the solution to a system of linear equations in matrix form.
Consider the following linear equations:
x + y + z = 6
2y + 5z = -4
2x + 5y - z = 27
These can be represented 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 (inverse matrix): Let A be an n-order matrix over a number field. If there exists another n-order matrix B over the same field such that AB=BA=E, then B is called the inverse of A, and A is called an invertible matrix. Note: E is the identity matrix.
Example
import
numpy
as
np
x
=
np
.
array
(
[
[
1
,
2
]
,
[
3
,
4
]
]
)
y
=
np
.
linalg
.
inv
(
x
)
(
x
)
(
y
)
(
np
.
dot
(
x
,
y
)
)
The output 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 of matrix A:
Example
import
numpy
as
np
a
=
np
.
array
(
[
[
1
,
1
,
1
]
,
[
0
,
2
,
5
]
,
[
2
,
5
,-
1
]
]
)
(
'
Array a:
'
)
(
a
)
ainv
=
np
.
linalg
.
inv
(
a
)
(
'
Inverse of a:
'
)
(
ainv
)
(
'
Matrix b:
'
)
b
=
np
.
array
(
[
[
6
]
,
[
4
]
,
[
27
]
]
)
(
b
)
(
'
Compute: A^(-1)B:
'
)
x
=
np
.
linalg
.
solve
(
a
,
b
)
(
x
)
This is the solution to the linear equations x = 5, y = 3, z = -2
The output 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)