Site icon DataFlair

NumPy Linear Algebra and Matrix Functions

Free NumPy course with real-time projects Start Now!!

NumPy has a separate module for linear algebra. The module contains all the functions necessary for linear algebra. numpy.linalg is the package in NumPy for NumPy Linear Algebra. Linear Algebra is the branch of mathematics concerned with vector spaces and mapping amongst the spaces.

NumPy Linear Algebra

Let us see various Matrix and Vector Products in NumPy:

Matrix and Vector Products

For matrix and vector computations it has the following functions:

NumPy dot and vdot functions

The dot function gives the dot product of two matrices. It is similar to matrix multiplication.

The vdot function, on the other hand, is used for the dot product of two or more vectors. It is equivalent to the sum of the array elements.

import numpy as np  
arr1 = np.array([[1,2],[3,4]])  
arr2 = np.array([[5,6],[7,8]])  
print(np.dot(arr1,arr2))  
print(np.vdot(arr1,arr2))

Output

[[19 22]
[43 50]]
70

NumPy inner and outer functions

The inner function gives the sum of the product of the inner elements of the array. In the case of n-dimensional arrays, it gives the output over the last axis only.

The outer function returns the sum of the product of the outer array elements.

import numpy as np  
a = np.array([1,2,3])  
b = np.array([7,8,9])  
#inner function
i=np.inner(a,b)
print(i)
#outer function
o= np.outer(a,b)  
print(o)    

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Output

50
[[ 7 8 9]
[14 16 18]
[21 24 27]]

NumPy Matrix Functions

1. The matrix multiplication function gives the multiplication of two matrices of the same shape. If the shape is not the same, then it gives error.

import numpy as np  
a = np.array([4,5,6])  
b = np.array([7,8,9])  
#matrix multiplication function
i=np.matmul(a,b)
print(i)

Output

122

2. The determinant function uses the diagonal elements for determinant calculation.

import numpy as np  
a = np.array(([4,5],[2,3])) 
#determinant function
m=(np.linalg.det(a))
print(m)

Output

2.0

3. The solve function is used to evaluate quadratic equations.

import numpy as np  
a = np.array([[7,8],[5,6]])  
b = np.array([[1,2],[3,4]])  
#solve function
s=np.linalg.solve(a, b)
print(s)

Output

[[ -9. -10.]
[ 8. 9.]]

4. The inverse function returns the multiplicative inverse of the input matrix.

import numpy as np  
a = np.array([[7,8],[5,6]])  
 #inverse function
inver=np.linalg.inv(a)
print(inver)

Output

[[ 3. -4. ]
[-2.5 3.5]]

5. The trace function returns the sum of all the diagonal values

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

Output

5

6. The rank function returns the rank of the matrix

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

Output

2

NumPy Matrix Eigenvalue Functions

This function computes complex conjugate or real symmetric matrices. The function returns two objects for eigenvalues and eigenvectors respectively. The output consists of:

import numpy as np
A = np.array([[1,2,3],[3,2,-1],[4,0,-4]])
#function to calculate eigenvalues and vector
val, vect = np.linalg.eig(A)
print(val)
print(vect)
 

Output

[-6.05798024 4.4666812 0.59129904]
[[ 0.44047381 -0.66326042 0.49735565]
[-0.27023519 -0.67962995 -0.75158903]
[-0.85612836 -0.31335084 0.43330277]]

1. The np.linspace(, ,) function outputs an equally spaced array. We use it for array generation.

np.linspace(0, 10, num=4)

Output

array([ 0. , 3.33333333, 6.66666667, 10. ])

2. The np.logspace() return numbers spaced evenly on a log scale. We use it for array generation.

np.logspace(0, 10, num=4)

Output

array([1.00000000e+00, 2.15443469e+03, 4.64158883e+06, 1.00000000e+10])

3. For 1-D arrays the most common function is np.arange(..), passing any value create an array from 0 to that number.

import numpy as np
            array=np.arange(20)
            array

Output

array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19])

We can check the dimensions by using array.shape.

4. The np.asarray() is to convert the input to an array. It is helpful in array creation.

np.asarray((1,2,3,4))

Output

array([1, 2, 3, 4])

Summary

NumPy linear algebra functions are beneficial for advanced scientific computations. It has functions and modules for matrix and vector processing. It is useful for concepts like eigenvalues and vectors evaluation. These functions make use of the NumPy functionalities to its full capacity.

Exit mobile version