Numpy provides the following functions to perform the different algebraic calculations on the input data.
SN | Function | Definition |
---|---|---|
1 | dot() | It is used to calculate the dot product of two arrays. |
2 | vdot() | It is used to calculate the dot product of two vectors. |
3 | inner() | It is used to calculate the inner product of two arrays. |
4 | matmul() | It is used to calculate the matrix multiplication of two arrays. |
5 | det() | It is used to calculate the determinant of a matrix. |
6 | solve() | It is used to solve the linear matrix equation. |
7 | inv() | It is used to calculate the multiplicative inverse of the matrix. |
This function is used to return the dot product of the two matrices. It is similar to the matrix multiplication. Consider the following example.
import numpy as np a = np.array([[100,200],[23,12]]) b = np.array([[10,20],[12,21]]) dot = np.dot(a,b) print(dot)
Output:
This function is used to calculate the dot product of two vectors. It can be defined as the sum of the product of corresponding elements of multi-dimensional arrays.
Consider the following example.
import numpy as np a = np.array([[100,200],[23,12]]) b = np.array([[10,20],[12,21]]) vdot = np.vdot(a,b) print(vdot)
Output:
This function returns the sum of the product of inner elements of the one-dimensional array. For n-dimensional arrays, it returns the sum of the product of elements over the last axis.
Consider the following example.
import numpy as np a = np.array([1,2,3,4,5,6]) b = np.array([23,23,12,2,1,2]) inner = np.inner(a,b) print(inner)
Output:
It is used to return the multiplication of the two matrices. It gives an error if the shape of both matrices is not aligned for multiplication. Consider the following example.
import numpy as np a = np.array([[1,2,3],[4,5,6],[7,8,9]]) b = np.array([[23,23,12],[2,1,2],[7,8,9]]) mul = np.matmul(a,b) print(mul)
The determinant of the matrix can be calculated using the diagonal elements. The determinant of following 2 X 2 matrix
A B
C D
can be calculated as AD - BC.
The numpy.linalg.det() function is used to calculate the determinant of the matrix. Consider the following example.
import numpy as np a = np.array([[1,2],[3,4]]) print(np.linalg.det(a))
Output:
This function is used to solve a quadratic equation where values can be given in the form of the matrix.
The following linear equations
3X + 2 Y + Z = 10 X + Y + Z = 5
can be represented by using three matrices as:
3 2 1 1 1 1 X Y Z and 10 5.
The two matrices can be passed into the numpy.solve() function given as follows.
import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[1,2],[3,4]]) print(np.linalg.solve(a, b))
Output:
This function is used to calculate the multiplicative inverse of the input matrix. Consider the following example.
import numpy as np a = np.array([[1,2],[3,4]]) print("Original array:\n",a) b = np.linalg.inv(a) print("Inverse:\n",b)
Output: