NumPy Linear Algebra

Numpy provides the following functions to perform the different algebraic calculations on the input data.

SNFunctionDefinition
1dot()It is used to calculate the dot product of two arrays.
2vdot()It is used to calculate the dot product of two vectors.
3inner()It is used to calculate the inner product of two arrays.
4matmul()It is used to calculate the matrix multiplication of two arrays.
5det()It is used to calculate the determinant of a matrix.
6solve()It is used to solve the linear matrix equation.
7inv()It is used to calculate the multiplicative inverse of the matrix.

numpy.dot() function

This function is used to return the dot product of the two matrices. It is similar to the matrix multiplication. Consider the following example.

Example

Output:

[[3400 6200]
 [ 374  712]]


The dot product is calculated as:

[100 * 10 + 200 * 12, 100 * 20 + 200 * 21] [23*10+12*12, 23*20 + 12*21] 

numpy.vdot() function

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.

Example

Output:

5528


np.vdot(a,b) = 100 *10 + 200 * 20 + 23 * 12 + 12 * 21 = 5528 

numpy.inner() function

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.

Example

Output:

130

numpy.matmul() function

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.

Example

numpy determinant

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.

Example

Output:

-2.0000000000000004

numpy.linalg.solve() function

This function is used to solve a quadratic equation where values can be given in the form of the matrix.

The following linear equations

can be represented by using three matrices as:

The two matrices can be passed into the numpy.solve() function given as follows.

Example

Output:

[[1. 0.]
 [0. 1.]]

numpy.linalg.inv() function

This function is used to calculate the multiplicative inverse of the input matrix. Consider the following example.

Example

Output:

Original array:
 [[1 2]
 [3 4]]
Inverse:
 [[-2.   1. ]
 [ 1.5 -0.5]]





Latest Courses