

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
PyTorch – How to compute the eigenvalues and eigenvectors of a square matrix?
<p style=""><strong>torch.linalg.eig()</strong> computes the Eigen value decomposition of a square matrix or a batch of square matrices. It accepts matrix and batch of matrices of <strong>float, double, cfloat</strong> and <strong>cdouble</strong> data types. It returns a named tuple (eigenvalues, eigenvectors). The eigenvalues and eigenvectors are always complex valued. The eigenvectors are given by columns of <strong>eigenvectors</strong>.</p><h2>Syntax</h2><pre class="just-code notranslate language-python" data-lang="python" style="">(eigenvalues, eigenvectors) = torch.linalg.eig(A)</pre><p>Where A is a square matrix or a batch of square matrices. It returns a named tuple (eigenvalues, eigenvectors).</p><h2 style="">Steps</h2><ul class="list"><li><p>Import the required library. In all the following examples, the required Python library is <strong>torch</strong>. Make sure you have already installed it.</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">import torch</pre><ul class="list"><li><p>Create a square matrix or batch of square matrices. Here we define a square matrix (a 2D torch tensor) of size [3, 3].</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">A = torch.randn(3,3)</pre><ul class="list"><li><p>Compute Eigen value decomposition of square matrix or batch of square matrices using <strong>torch.linalg.eig(A)</strong>. Here A is square matrix.</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">eigenvalues, eigenvectors = torch.linalg.eig(A)</pre><ul class="list"><li><p>Display eigenvalues and eigenvectors.</p></li></ul><pre class="just-code notranslate language-python" data-lang="python" style="">print("Eigen Values: ", eigenvalues) print("Eigen Vectors: ", eigenvectors)</pre><h2>Example 1</h2><p style="">In this program, we compute the eigenvalues and eigenvectors of a square matrix.</p><pre class="just-code notranslate language-python" data-lang="python"># 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)</pre><h2>Output</h2><p>It will produce the following output −</p><pre class="result notranslate" style="">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]])</pre><h2>Example 2</h2><p>In this program, we compute eigenvalues and eigenvectors of a square complex matrix.</p><pre class="just-code notranslate language-python" data-lang="python"># 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)</pre><h2>Output</h2><p>It will produce the following output −</p><pre class="result notranslate" style="">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]])</pre>
- Related Questions & Answers
- How to find the Eigenvalues and Eigenvectors of a square matrix using SciPy?
- PyTorch – How to compute the determinant of a square matrix?
- PyTorch – How to compute the inverse of a square matrix?
- PyTorch – How to compute the pseudoinverse of a matrix?
- PyTorch – How to compute QR decomposition of a matrix?
- Compute the eigenvalues of a complex Hermitian or real symmetric matrix in Python
- PyTorch – How to compute the norm of a vector or matrix?
- PyTorch – How to compute Singular Value Decomposition (SVD) of a matrix?
- How to compute the histogram of a tensor in PyTorch?
- How to compute the mean and standard deviation of a tensor in PyTorch?
- PyTorch – How to compute the error function of a tensor?
- How to compute the Jacobian of a given function in PyTorch?
- How to compute the sine of elements of a tensor in PyTorch?
- How to compute the Logarithm of elements of a tensor in PyTorch?
- How to compute gradients in PyTorch?
Advertisements