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

import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) print(np.dot(a,b))

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

import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) # vdot flattens the arrays and calculates the inner product print (np.vdot(a,b))

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

import numpy as np print (np.inner(np.array([1,2,3]),np.array([0,1,0]))) # Equivalent to 1*0+2*1+3*0

The output result is:

2

Multi-dimensional array example

import numpy as np a = np.array([[1,2], [3,4]]) print ('Array a:') print (a) b = np.array([[11, 12], [13, 14]]) print ('Array b:') print (b) print ('Inner product:') print (np.inner(a,b))

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

import numpy.matlib import numpy as np a = [[1,0],[0,1]] b = [[4,1],[2,2]] print (np.matmul(a,b))

The output result 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] print (np.matmul(a,b)) print (np.matmul(b,a))

The output result is:

[1  2] 
[1  2]

Arrays with dimensions greater than two:

Example

import numpy.matlib import numpy as np a = np.arange(8).reshape(2,2,2) b = np.arange(4).reshape(2,2) print (np.matmul(a,b))

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

import numpy as np a = np.array([[1,2], [3,4]]) print (np.linalg.det(a))

The output result is:

-2.0

Example

import numpy as np b = np.array([[6,1,1], [4, -2, 5], [2,8,7]]) print (b) print (np.linalg.det(b)) print (6*(-2*7 - 5*8) - 1*(4*7 - 5*2) + 1*(4*8 - -2*2))

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

import numpy as np x = np.array([[1,2],[3,4]]) y = np.linalg.inv(x) print (x) print (y) print (np.dot(x,y))

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

import numpy as np a = np.array([[1,1,1],[0,2,5],[2,5,-1]]) print ('Array a:') print (a) ainv = np.linalg.inv(a) print ('Inverse of a:') print (ainv) print ('Matrix b:') b = np.array([[6],[-4],[27]]) print (b) print ('Calculate: A^(-1)B:') x = np.linalg.solve(a,b) print (x) # This is the solution of the linear equations x = 5, y = 3, z = -2

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