
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute Eigenvalues and Eigenvectors of a Square Matrix in PyTorch
torch.linalg.eig() computes the Eigen value decomposition of a square matrix or a batch of square matrices. It accepts matrix and batch of matrices of float, double, cfloat and cdouble data types. It returns a named tuple (eigenvalues, eigenvectors). The eigenvalues and eigenvectors are always complex valued. The eigenvectors are given by columns of eigenvectors.
Syntax
(eigenvalues, eigenvectors) = torch.linalg.eig(A)
Where A is a square matrix or a batch of square matrices. It returns a named tuple (eigenvalues, eigenvectors).
Steps
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Create a square matrix or batch of square matrices. Here we define a square matrix (a 2D torch tensor) of size [3, 3].
A = torch.randn(3,3)
Compute Eigen value decomposition of square matrix or batch of square matrices using torch.linalg.eig(A). Here A is square matrix.
eigenvalues, eigenvectors = torch.linalg.eig(A)
Display eigenvalues and eigenvectors.
print("Eigen Values:
", eigenvalues) print("Eigen Vectors:
", eigenvectors)
Example 1
In this program, we compute the eigenvalues and eigenvectors of a square matrix.
# import required library import torch # create a 3x3 square matrix A = torch.randn(3,3) # print the above created matrix print("Matrix:
", A) # compute the Eigen values and vectors of the matrix eigenvalues, eigenvectors = torch.linalg.eig(A) print("Eigen Values:
", eigenvalues) print("Eigen Vectors:
", eigenvectors)
Output
It will produce the following output −
Matrix: tensor([[-0.7412, 0.6472, -0.4741], [ 1.8981, 0.2936, -1.9471], [-0.1449, 0.0327, -0.8543]]) Eigen Values: tensor([ 1.0190+0.j, -1.3846+0.j, -0.9364+0.j]) Eigen Vectors: tensor([[-0.3476+0.j, -0.7716+0.j, 0.5184+0.j], [-0.9376+0.j, 0.5862+0.j, 0.3982+0.j], [ 0.0105+0.j, -0.2469+0.j, 0.7568+0.j]])
Example 2
In this program, we compute eigenvalues and eigenvectors of a square complex matrix.
# import required library import torch # create a 2x2 square complex matrix A = torch.randn(2,2, dtype = torch.cfloat ) # print the above created matrix print("Matrix:
", A) # computet the eigen values and vectors of the matrix eigenvalues, eigenvectors = torch.linalg.eig(A) print("Eigen Values:
", eigenvalues) print("Eigen Vectors:
", eigenvectors)
Output
It will produce the following output −
Matrix: tensor([[-0.1068-0.0045j, 0.7061-0.5698j], [-0.2521-1.1166j, 0.6921+1.4637j]]) Eigen Values: tensor([0.3194-0.3633j, 0.2659+1.8225j]) Eigen Vectors: tensor([[ 0.8522+0.0000j, -0.2012-0.3886j], [ 0.5231-0.0109j, 0.8992+0.0000j]])