# Python Scipy Linalg Eig

We’ll look over “Python Scipy Linalg Eig,” which can compute a matrix’s or band matrix eigenvalues or left or right eigenvectors, as well as how to sort these values. Additionally, we will cover the following topics.

• Python Scipy Linalg Eig
• Python Scipy Linalg Eig Banded
• Python Scipy Linalg Eig Vs Eigh
• How to sort the eigenvalues and eigenvectors of Python Scipy
• How to get the Python Scipy Eig Left Eigenvector
• How to get the Python Scipy Eig Right Eigenvector

## Python Scipy Linalg Eig

The method `eig()` of Python Scipy that exists in a module `scipy.linalg` is used to identify and resolve a square matrix’s ordinary or generalised eigenvalue problem or find the right or left eigenvectors and eigenvalues (w) of a generic matrix.

The syntax is given below.

``scipy.linalg.eig(a, b=None, left=False, right=True, overwrite_a=False, overwrite_b=False, check_finite=True, homogeneous_eigvals=False)``

Where parameters are:

• a(array_data (M,M)): A real or complex matrix whose eigenvalues and eigenvectors have to be determined.
• b(array_data (M,M)): Matrix on the right side of a generalised eigenvalue issue. The identity matrix is presumed when None is the default.
• left(boolean): Whether to calculate the left eigenvectors and return them. By default, It is False.
• right(boolean): Whether to calculate the right eigenvectors and return them. By default, It is True.
• overwrite_a(boolean): Whether or not to replace data in a.
• overwrite_b(boolean): Whether or not to replace data in b.
• check_finite(boolean): if it is necessary to verify that the input matrices only contain finite numbers. Disabling may improve performance, but if the inputs do contain infinities or NaNs, it may cause issues (crashes, non-termination).
• homogeneous_egvals(boolean): The eigenvalues will be returned in homogeneous coordinates if f True.

The method `eig()` returns `w` (The eigenvalues, are repeated for each in the appropriate number of times. Unless homogeneous eigvals=True, the shape is (M,)), `vl` (The column vl[:,i] contains the normalized left eigenvector that corresponds to the eigenvalue w[i]. Only given back if the left is True) and vr (The column vr[:,i] contains the normalized right eigenvector that corresponds to the eigenvalue w[i]. Only given back when right=True).

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

``````from scipy.linalg import eig
import numpy as np``````

Create an array containing the longitude and latitude of USA cities like `Greenfield, MA, USA : La = 42.587334, Lo = -72.603416`, `Cambridge, MD, USA : La = 38.563461, Lo = -76.085251`, using the below code.

``usa_city_lat_long = np.array([[42.58, -72.60.], [38.56, -76.08]])``

Now pass the above-created data to the method `eig()` using the below code.

From the output, we can see the computed eigenvectors and eigenvalues of the USA city’s latitude and longitude using the method `eig()` of Python Scipy.

## Python Scipy Linalg Eig Banded

The method `eig_banded()` of Python Scipy of module `scipy.linalg` identifies the eigenvalues of a real symmetric or complex Hermitian band matrix.

A band matrix, also known as a banded matrix, is a sparse matrix with non-zero entries that is restricted to a diagonal band made up of the main diagonal and zero or more diagonals on either side in mathematics.

The syntax is given below.

``scipy.linalg.eig_banded(a_band, lower=False, eigvals_only=False, overwrite_a_band=False, select='a', select_range=None, max_ev=0, check_finite=True)``

Where parameters are:

• a_band(): The bands of the matrix `a` of size M by M.
• lower(boolean): Is the lower form of the matrix. (Upper form is the default).
• eigvals_only(boolean): Do not compute the eigenvectors, only the eigenvalues. (By default, eigenvectors are also calculated).
• overwrite_a_band(boolean): Remove the data from a_band.
• select_range(min, max): Selection of an eigenvalue range.
• select(v, i , a): Which eigenvalues should be computed, a for all eigenvalues, i for eigenvalues with min <= i <= max indices and v for eigenvalues within the range [(min, max].
• max_ev(int): A maximum number of eigenvalues is anticipated for select==’v’. has no relevance for any other selection values. If in doubt, ignore this argument.
• check_finite(boolean): Whether or not to make sure the input matrix only has finite numbers. Disabling may improve performance, but if the inputs do contain infinities or NaNs, it may cause issues (crashes, non-termination).

The method `eig_banded()` returns `w` (The eigenvalues are repeated in increasing sequence according to their multiplicity) and `v` (The column v[:,i] contains the normalised eigenvector that corresponds to the eigenvalue w[i]).

Let’s take an example and compute the eigenvalues of the band matrix by following the below steps:

Import the required libraries or methods using the below python code.

``````from scipy.linalg import eig
import numpy as np``````

Create a band matrix of lower form using the below code.

``````band_mat = np.array([[3, 1, 2, 4], [8, 8, 8, 0], [1, 1, 0, 0]])
print(band_mat)``````

Compute the eigenvalues or eigenvectors of the band matrix using the below code.

``````w_, v_ = eig_banded(band_mat, lower=True)
print("Eigenvalues",w_)
print("Eigenvectors",v_)``````

This is how to compute the eigenvalues or eigenvectors of the real symmetric or complex hermitian band matrix using the method `eig_banded()` of Python Scipy.

## Python Scipy Linalg Eig Vs Eigh

The eigenvalues are guaranteed to be sorted by “eigh()” which also employs a quicker algorithm that benefits from the symmetric nature of the matrix. Use this function if you are certain that your matrix is symmetric.

We should be aware that “eigh()” only uses the lowest triangular component of your matrix by default, assuming that the upper triangular part is determined by the symmetry of the matrix.

We may verify that “eig()” uses a slower algorithm and so works for generic matrices by using, for instance, IPython’s magic command `%timeit`. We will also see that, in general, the eigenvalues are not sorted here if We test with larger matrices.

## Python Scipy Linalg Eig Sort

The method `eig()` hasn’t any attribute to sort the returned values eigenvector and eigenvalues. To sort the eigenvalues or eigenvectors, we will utilize the NumPy method `numpy.argsort`.

For example, we will use the same example that we have done in the above subsection “Python Scipy Lialg Eig”.

Import the required libraries or methods using the below python code.

``````from scipy.linalg import eig
import numpy as np``````

Create an array containing the longitude and latitude of USA cities like `Greenfield, MA, USA : La = 42.587334, Lo = -72.603416`, `Cambridge, MD, USA : La = 38.563461, Lo = -76.085251`, using the below code.

``usa_city_lat_long = np.array([[42.58, -72.60.], [38.56, -76.08]])``

Now pass the above-created data to the method `eig()` using the below code.

``eigval, eigvect = eig(usa_city_lat_long)``

Sort the eigenvalues and eigenvectors using the below code.

``````indx = eigval.argsort()[::-1]
eigenValues_ = eigval[indx]
eigenVectors_ = eigvect[:,indx]

print("Eigenvalues",eigenValues_)
print("Eigenvectors",eigenVectors_)
``````

The two variable `Eigenvalues and Eigenvectors` shows the sorted values in the above output.

This is how to sort the eigenvalues and eigenvectors by applying the method `argsort()` of NumPy.

## Python Scipy Linalg Eig Left Eigenvector

The method `eig()` of Python Scipy accepts a parameter `left` of type boolean, if this parameter is set to True, then the `eig()` returns the normalized left eigenvectors.

The syntax is given below.

``scipy.linalg.eig(a, left=False)``

Where parameters are:

• a(array_data (M,M)): A real or complex matrix whose eigenvalues and eigenvectors have to be determined.
• left(boolean): Whether to calculate the left eigenvectors and return them. By default, It is False.

Let’s take an example and compute the left eigenvectors of the given matrix by following the below steps:

Import the required libraries or methods using the below python code.

``````from scipy.linalg import eig
import numpy as np``````

Create a matrix using the below code.

``mat = np.array([[1.5, -2.], [2., 0.]])``

Now pass the above matrix to a method `eig()` with `left` parameter equal to `True` to compute the left eigenvectors using the below code.

``eig(mat, left=True, right=False)[1]``

This is how to compute the left eigenvectors of the matrix using the method `eig()` of Python Scipy with parameter `left`.

## Python Scipy Linalg Eig Right Eigenvector

The method `eig()` of Python Scipy accepts a parameter `right` of type boolean, if this parameter is set to True, then the `eig()` returns the normalized right eigenvectors.

The syntax is given below.

``scipy.linalg.eig(a, right=False)``

Where parameters are:

• a(array_data (M,M)): A real or complex matrix whose eigenvalues and eigenvectors have to be determined.
• right(boolean): Whether to calculate the right eigenvectors and return them. By default, It is True.

Let’s take an example and compute the right eigenvectors of the given matrix by following the below steps:

Import the required libraries or methods using the below python code.

``````from scipy.linalg import eig
import numpy as np``````

Create a matrix using the below code.

``mat = np.array([[4., -3.], [6., 0.1]])``

Now pass the above matrix to a method `eig()` with `right` parameter equal to `True` to compute the left eigenvectors using the below code.

``eig(mat, left=False, right=True)[1]``

This is how to compute the right eigenvectors of the matrix using the method `eig()` of Python Scipy with parameter `right`.

In addition to learning how to compute and sort eigenvalues and eigenvectors of real symmetric or Hermitian or band matrices, we also learned how to determine the precise left and right eigenvectors of matrices with the following topics.

• Python Scipy Linalg Eig
• Python Scipy Linalg Eig Banded
• Python Scipy Linalg Eig Vs Eigh
• How to sort the eigenvalues and eigenvectors of Python Scipy
• How to get the Python Scipy Eig Left Eigenvector
• How to get the Python Scipy Eig Right Eigenvector

You may like the following Python Scipy tutorials: