# Numpy linalg.eig – Compute the eigenvalues and right eigenvectors of a square array The numpy linalg.eig function is a powerful mathematical tool that enables users to calculate the eigenvalues and right eigenvectors of a square array. This function is used by scientists and mathematicians in a variety of fields, from physics and engineering to economics and finance.

By utilizing this function, users can gain insight into the underlying structure of a system and discover relationships between variables. In this article, we will explore what an eigenvalue and eigenvector are, and how the numpy linalg.eig function works in order to calculate them.

Also read: Numpy fabs- Compute the absolute value element wise.

## What are eigenvalues and eigenvectors?

Eigenvalues are unique scalar values associated with the linear equations of a matrix. Eigenvalues tell us how much variance there is in a specific data value in a specific direction. The directions are mainly given by the eigenvectors. Eigenvectors are non-zero vectors that can be atmost changed by a scalar quantity after they undergo linear transformations.

### What is the use of eigenvalues and eigenvectors?

Eigenvalues and eigenvectors are required to compress data and remove or reduce dimensional spaces.

### Required conditions for calculating eigenvalues and eigenvectors

The matrix whose eigenvalues and eigenvectors are to be calculated must be a square matrix, that is, the dimensions of the matrix must be in the form of “nXn”, where n=number of rows=number of columns.

## Numpy linalg library and the linalg.eig() function

The numpy linalg that is the numpy linear algebra library contains a range of functions that makes complex linear algebra computing much easier.

### Syntax of linalg.eig() function

The syntax of the function is given below where x is the initial argument or input:

```linalg.eig(x)
```

Parameters of the numpy linalg.eig() function

Given below are the required parameters of the function:

Input `x : array` -> The initial square matrix whose eigenvalues and right eigenvectors are to be calculated.

Output

`y : array` -> The eigenvalues unordered repeated according to their multiplicities. The type of the array is complex unless the complex part is zero, in which case the return type is real.

`z : array` -> The unit eigenvectors such that column z[: , i] corresponds to eigenvalue y[i].

## How to use the numpy linalg.eig() function

The first step would be to install the numpy package if you dont already have it installed. Run the following code to install numpy in your system.

```pip install numpy
```

Next, import the library in your project and then follow through the given examples.

### Example 1 – Computes Eigenvalues and Eigenvectors of a Pre-Defined Matrix

This code computes the eigenvalues and eigenvectors of a pre-defined matrix. It first imports the necessary modules, numpy (as py) and linalg from numpy. Then, it defines a matrix as x. Next, it uses the linalg.eig() function to compute the eigenvalues and eigenvectors of the matrix and store them in the variables y and z. Finally, it displays the computed eigenvalues and eigenvectors.

```import numpy as py #importing required modules
from numpy import linalg as L #importing the linalg function
x=[[1,6],[4,9]] #pre defined matrix
y, z = L.eig(x) #computing the eigenvalues and eigenvectors
#displaying the values
print("the eigenvalues are:",y)
print("the eigenvectors are:",z)
```

Output:

```the eigenvalues are: [-1.32455532 11.32455532]
the eigenvectors are: [[-0.93246475 -0.50245469]
[ 0.36126098 -0.86460354]]
```

### Example 2 – Calculate Eigenvalues and Eigenvectors of Matrices

To take a matrix user input and calculate the eigenvalues and eigenvectors. This code is designed to take a user-input matrix, compute its eigenvalues and eigenvectors, and then display the results. It uses the NumPy and linalg modules to carry out the calculations. The user is first asked to enter the number of rows and columns for the matrix. If the number of rows is not equal to the number of columns, an error message is displayed. The user is then prompted to enter the values for the matrix row by row. Once the matrix has been entered, it is displayed, and then the linalg.eig() function is used to calculate the eigenvalues and eigenvectors. Finally, the results are displayed.

```#importing the required modules
import numpy as py
from numpy import linalg as L
#taking user input
row = int(input("Enter the number of rows:"))
col= int(input("Enter the number of columns:"))
print("NOTE!! The number of rows should be equal to the number of columns")

# Initializing the required matrix
x = []
print("enter the values rowwise:")

# For user input
for i in range(row):          # loop for row entries
b =[]
for j in range(col):      # loop for column entries
b.append(int(input()))
x.append(b)

# For displaying the matrix
print("The matrix is as follows:")
print("[")
for i in range(row):
for j in range(col):
print(x[i][j], end = " ")
print()
print("]")
y, z = L.eig(x) #computing the values and vectors

#displaying the result
print("the eigenvalues are:",y)
print("the eigenvectors are:",z)
```

Output:

```Enter the number of rows:2
Enter the number of columns:2
NOTE!! The number of rows should be equal to the number of columns
enter the values rowwise:
1
-1
-1
1
The matrix is as follows:
[
1 -1
-1 1
]
the eigenvalues are: [2. 0.]
the eigenvectors are: [[ 0.70710678  0.70710678]
[-0.70710678  0.70710678]]
```

## Shortcomings of eigenvalues and eigenvectors

1. Eigenvalues and eigenvectors are only applicable to linear transformations, so they cannot be used to solve non-linear problems.
2. They can only be used to study linear transformations, and cannot be used to study non-linear transformations.
3. It can be difficult to compute the eigenvalues and eigenvectors of a given matrix, and the calculations can be time-consuming.
4. Eigenvectors often do not represent the most intuitive or meaningful information about the data set.

In case the eigenvectors of a matrix are linearly dependent, it signifies that there is an eigenvalue whose algebraic multiplicity is greater than its geometric multiplicity. In such situations, there is going to be a defect in the results. To know more about multiplicities of eigenvalues, click here.

## Conclusion:

The numpy linalg.eig function is a powerful tool for computing the eigenvalues and right eigenvectors of a square matrix. By using this function, scientists and mathematicians can gain insight into the underlying structure of a system and discover relationships between variables.

This can help them to compress data and reduce dimensional spaces. However, the eigenvalues and eigenvectors are only applicable to linear transformations and can be difficult to compute, so users must be sure to understand their limitations when using this function.