NumPy Linear Algebraic functions to know!

NumPy Linear Algebraic Functions

Hello, readers! In this article, we will be focusing on NumPy Linear Algebraic functions in Python. So, let us get started! 🙂

The NumPy module offers us various functions to deal with and manipulate data. It enables us to create and store data in an array data structure. Moving ahead, it offers us various functions to analyze and manipulate the data values.

List of NumPy Linear Algebraic functions

1. Matrix functions offered by NumPy module

With NumPy module, we can perform the linear algebraic matrix functions on the array structure.

In the course of this topic, we would be having a look at the below functions–

  1. Rank of the matrix: We can calculate the rank of the array using numpy.linalg.matrix_rank() function.
  2. Determinant: The numpy.linalg.det() function helps us calculate the determinant of the array treating it as a matrix.
  3. Inverse: The inv() function enables us to calculate the inverse of the array.
  4. Exponent: Using numpy.linalg.matrix_power() function, we can raise a power value to the matrix and fetch the results.

Example:

In the below example, we have created an array using numpy.array() function. Further, we have performed the above mentioned linear algebraic operations on the array and printed the results.

import numpy

x = numpy.array([ [2, 8, 7],
                 [6, 1, 1],
                [4, -2, 5]])
 
print("Rank: ", numpy.linalg.matrix_rank(x))
det_mat = numpy.linalg.det(x) 
print("\nDeterminant: ",det_mat)
inv_mat = numpy.linalg.inv(x)
print("\nInverse: ",inv_mat) 
print("\nMatrix raised to power y:\n",
           numpy.linalg.matrix_power(x, 8))

Output:

Rank:  3

Determinant:  -306.0

Inverse:  [[-0.02287582  0.17647059 -0.00326797]
 [ 0.08496732  0.05882353 -0.13071895]
 [ 0.05228758 -0.11764706  0.1503268 ]]

Matrix raised to power y:
 [[ 85469036  43167250 109762515]
 [ 54010090  32700701  75149010]
 [ 37996120  22779200  52792281]]

2. Eigen value with NumPy Array

NumPy Linear Algebraic functions have the linalg class that has eigh() function to calculate the eigenvalue from the array elements passed to it.

Have a look at the below syntax!

Syntax:

numpy.linalg.eigh(array)

The eigh() function returns the eigenvalues as well as the eigenvectors of a complex or a real symmetric matrix.

Example:

from numpy import linalg as li

x = numpy.array([[2, -4j], [-2j, 4]])

res = li.eigh(x)
 
print("Eigen value:", res)

Output:

Eigen value: (array([0.76393202, 5.23606798]), array([[-0.85065081+0.j        ,  0.52573111+0.j        ],
       [ 0.        -0.52573111j,  0.        -0.85065081j]]))


3. Dot Product

With NumPy Linear Algebraic functions, we can perform dot operations on scalar as well as multi-dimensional values. It performs scalar multiplication for single dimensional vector values.

For multi-dimensional arrays/matrices, it performs matrix multiplication on the data values.

Syntax:

numpy.dot()

Example:

import numpy as np

sc_dot = np.dot(10,2)
print("Dot Product: ", sc_dot)

vectr_x = 1 + 2j
vectr_y = 2 + 4j
 
vctr_dot = np.dot(vectr_x, vectr_y)
print("Dot Product: ", vctr_dot)

Output:

Dot Product:  20
Dot Product:  (-6+8j)

4. Solving Linear equations with NumPy module

With NumPy Linear Algebraic functions, we can even perform the calculations and solve the linear algebraic scalar equations. The numpy.linalg.solve() function solves for the array values with the equation ax=b.

Example:

import numpy as np

x = np.array([[2, 4], [6, 8]])
 
y = np.array([2, 2])
 
print(("Solution of linear equations:", 
      np.linalg.solve(x, y)))

Output:

('Solution of linear equations:', array([-1.,  1.]))

Conclusion

Feel free to comment below, in case you come across any question. For more such posts related to Python programming, stay tune with us. Till then, happy learning!! 🙂