LINEAR ALGEBRA IN NUMPY
In this tutorial, we are going to learn about the linear algebra in numpy and explore methods like dot, vdot, inner, matmul etc. We will be looking at some handy examples to understand these methods in more depth.
What is Linear Algebra?
Linear Algebra is the study of linear equations and their properties in mathematics. It is widely used in engineering mainly covering subjects like Physics and Mathematics. You can view the full reference of Linear Algebra here
What is linear algebra in Numpy?
Numpy has a built in linear algebra module which is used for doing linear algebra. Let’s look at some of the functions of linear algebra.
Dot function in Numpy
This function returns the scalar dot product of two arrays. This function is similar to the matrix multiplication Let’s look at a quick example to understand more in detail:
In order to understand dot product multiplication, view this tutorial here. Let’s look at a numpy example here:
import numpy as np x = np.array([[10,20],[30,40]]) y = np.array([[10,20],[30,40]]) print(x) print(y) print(np.dot(x,y))
Output:
[[10 20] [30 40]] [[10 20] [30 40]] [[ 700 1000] [1500 2200]]
V-Dot function in Numpy
This function returns the vector dot product of two arrays. This function is similar to the matrix multiplication. Let’s look at a quick example to understand more in detail:
import numpy as np x = np.array([[10,20],[30,40]]) y = np.array([[10,20],[30,40]]) print(x) print(y) print(np.vdot(x,y))
Output:
[[10 20] [30 40]] [[10 20] [30 40]] 3000
Inner function in Numpy
This function gives the product of single dimensional arrays. For more dimensions, this function returns the sum of product of elements over the last axis. For example:
import numpy as np print(np.inner(np.array([10,20,30]),np.array([0,10,0])))
Output:
200
Matmul Function in Numpy
This function is used for multiplying two matrices. If two shapes of the matrices aren’t the same then an error will erupt. Let’s have an example:
import numpy as np x = [[2,4],[2,3]] y = [[5,6],[4,1]] print(np.matmul(x,y))
Output:
[[26 16] [22 15]]
Determinant function in Numpy
The determinant function is used to perform calculations diagonally in a matrix. The linalg.set() is used for calculating the determinant of a matrix. Let’s look at an example:
import numpy as np arr = np.array([[10,20],[30,40]]) print(np.linalg.det(arr))
Output:
-200.0000000000001
Linear Algebra Solve in Numpy
This method is used for solving an algebraic quadratic equation where we provide values in the form of a matrix. For example: