NumPy Linear Algebra
NumPy provides a linear algebra function library linalg, which contains all the functionalities needed for linear algebra. Here are some descriptions:
Function | Description |
---|---|
dot | Dot product of two arrays, i.e., element-wise multiplication. |
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() computes the dot product of two one-dimensional arrays, which is the sum of the products of corresponding elements (mathematically known as the inner product). For two-dimensional arrays, it computes the matrix product. For multi-dimensional arrays, the general formula is as follows: each element in the result array is the sum of the 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
- b: ndarray
- out: ndarray, optional, used to store the 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))
Output:
[[37 40]
[85 92]]
Calculation:
[[1*11+2*13, 1*12+2*14],[3*11+4*13, 3*12+4*14]]
numpy.vdot()
numpy.vdot() computes the dot product of two vectors. If the first parameter is complex, its conjugate will be used in the calculation. If the parameters are multi-dimensional arrays, they will be 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 computes the inner product
print (np.vdot(a,b))
Output:
130
Calculation:
1*11 + 2*12 + 3*13 + 4*14 = 130
numpy.inner()
numpy.inner() returns the inner product of vectors for one-dimensional arrays. For higher dimensions, it returns the sum of products on the last axis.
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
Output:
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))
Output:
Array a:
[[1 2]
[3 4]]
Array b:
[[11 12]
[13 14]]
Inner product:
[[35 41]
[81 95]]
Array a:
[[1 2]
[3 4]]
Array b:
[[11 12]
[13 14]]
Inner product:
[[35 41]
[81 95]]
Inner product calculation:
1*11+2*12, 1*13+2*14
3*11+4*12, 3*13+4*14
numpy.matmul
numpy.matmul returns the matrix product of two arrays. Although it returns the normal product for two-dimensional arrays, if either parameter has more than two dimensions, it is treated as a stack of matrices residing in the last two indices and is broadcast accordingly.
On the other hand, if either parameter is a one-dimensional array, it is promoted to a matrix by appending 1 to its dimension, which is removed after the multiplication.
For two-dimensional arrays, it is the 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))
Output:
[[4 1]
[2 2]]
Two-dimensional and one-dimensional operation:
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))
Output:
[1 2]
[1 2]
For 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))
Output:
[[[ 2 3]
[ 6 11]]
[[10 19]
[14 27]]]
numpy.linalg.det()
The numpy.linalg.det() function computes the determinant of an input matrix.
The determinant is a useful value in linear algebra. It is calculated from the diagonal elements of a square matrix. For a 2x2 matrix, it is the product of the top-left and bottom-right elements minus the product of the other two.
In other words, for a matrix [[a, b], [c, d]], the determinant is calculated as ad-bc. Larger square matrices are considered combinations of 2x2 matrices.
Example
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(np.linalg.det(a))
Output:
-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))
Output:
[[ 6 1 1]
[ 4 -2 5]
[ 2 8 7]]
-306.0
-306
numpy.linalg.solve()
The numpy.linalg.solve() function provides the solution to 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
or
X = A^(-1)B
numpy.linalg.inv()
The numpy.linalg.inv() function computes the multiplicative inverse of a matrix.
Inverse Matrix: Let A be an n-by-n matrix over a field K. If there exists another n-by-n matrix B over K such that AB = BA = I, then B is the inverse of A, and A is invertible. Here, I 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))
Output:
[[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]])
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 for x = 5, y = 3, z = -2
Output:
Array a:
[[ 1 1 1]
[ 0 2 5]
[ 2 5 -1]]
Inverse of a:
[[ 1.28571429 -0.28571429 -0.14285714]
[-0.47619048 0.14285714 0.23809524]
[ 0.19047619 0.14285714 -0.0952381 ]]
Matrix b:
[[ 6]
[-4]
[27]]
Calculate A^(-1)B:
[[ 5.]
[ 3.]
[-2.]]
The result can also be obtained using:
x = np.dot(ainv, b)