Numpy Linear Algebra
NumPy provides a linear algebra library called **linalg**, which contains all the functions needed for linear algebra. You can see the description below:
| 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 a linear matrix equation. |
| `inv` | Compute the multiplicative inverse of a matrix. |
### numpy.dot()
For two one-dimensional arrays, numpy.dot() calculates the sum of the products of corresponding elements (mathematically known as the vector dot product); for two-dimensional arrays, it calculates the matrix product of the two arrays; for multi-dimensional arrays, the general formula is as follows, meaning each element in the resulting array is the sum of products of all elements from the last dimension of array a and 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 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 is:
[ ]
The calculation 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 parameter is a complex number, its complex conjugate is used for the calculation. If the parameter 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]])print(np.vdot(a,b))
The output is:
130
The calculation 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 axis.
## Example
import numpy as np print(np.inner(np.array([1,2,3]),np.array([0,1,0])))
The output 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 is:
Array a:[ ]Array b:[ ]Inner product:[ ]Array a:[ ]Array b:[ ]Inner product:[ ]
The inner product calculation 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 for two-dimensional arrays, if either argument has a dimension greater than 2, it is treated as a stack of matrices residing in the last two indices and is broadcast accordingly.
On the other hand, if either argument is a one-dimensional array, it is promoted to a matrix by prepending a 1 to its dimensions, and after multiplication, the prepended 1 is removed.
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 is:
[ ]
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 is:
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 is:
[[ ] [ ]]
### 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 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 combinations of 2Γ2 matrices.
## Example
import numpy as np a = np.array([[1,2], [3,4]])print(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]])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 is:
[ ]-306.0-306
### numpy.linalg.solve()
The numpy.linalg.solve() function gives the solution to a linear matrix equation in matrix form.
Consider the following linear equations:
x + y + z = 62y + 5z = -42x + 5y - z = 27
These can be represented using matrices 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-th order matrix over a number field. If there exists another n-th 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 is:
[ ][[-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([,,])print(b)print('Calculation: A^(-1)B:')x = np.linalg.solve(a,b)print(x)
The output is:
Array a:[ ] Inverse of a:[[ 1.28571429 -0.28571429 -0.14285714] [-0.47619048 0.14285714 0.23809524] [ 0.19047619 0.14285714 -0.0952381 ]]Matrix b:[ ]Calculation: A^(-1)B:[[ 5.] [ 3.] [-2.]]
The result can also be obtained using the following function:
x = np.dot(ainv,b)
YouTip