NumPy Linear Algebra

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.

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

snippet
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:

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

snippet
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:

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

snippet
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:

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

snippet
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)

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

snippet
import numpy as np
a = np.array([[1,2],[3,4]])
print(np.linalg.det(a))

Output:

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

snippet
3X + 2 Y + Z = 10 
X + Y + Z = 5

can be represented by using three matrices as:

snippet
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.

Example

snippet
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:

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

snippet
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:

Output
Original array: [[1 2] [3 4]] Inverse: [[-2. 1. ] [ 1.5 -0.5]]
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +