# PyTorch – How to compute the eigenvalues and eigenvectors of a square matrix?

PyTorchServer Side ProgrammingProgramming

<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(&quot;Eigen Values: &quot;, eigenvalues) print(&quot;Eigen Vectors: &quot;, 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(&quot;Matrix: &quot;, A) # compute the Eigen values and vectors of the matrix eigenvalues, eigenvectors = torch.linalg.eig(A) print(&quot;Eigen Values: &quot;, eigenvalues) print(&quot;Eigen Vectors: &quot;, eigenvectors)</pre><h2>Output</h2><p>It will produce the following output &minus;</p><pre class="result notranslate" style="">Matrix: &nbsp; &nbsp;tensor([[-0.7412, 0.6472, -0.4741], &nbsp; &nbsp; &nbsp; [ 1.8981, 0.2936, -1.9471], &nbsp; &nbsp; &nbsp; [-0.1449, 0.0327, -0.8543]]) Eigen Values: &nbsp; &nbsp;tensor([ 1.0190+0.j, -1.3846+0.j, -0.9364+0.j]) Eigen Vectors: &nbsp; &nbsp;tensor([[-0.3476+0.j, -0.7716+0.j, 0.5184+0.j], &nbsp; &nbsp; &nbsp; [-0.9376+0.j, 0.5862+0.j, 0.3982+0.j], &nbsp; &nbsp; &nbsp; [ 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(&quot;Matrix: &quot;, A) # computet the eigen values and vectors of the matrix eigenvalues, eigenvectors = torch.linalg.eig(A) print(&quot;Eigen Values: &quot;, eigenvalues) print(&quot;Eigen Vectors: &quot;, eigenvectors)</pre><h2>Output</h2><p>It will produce the following output &minus;</p><pre class="result notranslate" style="">Matrix: &nbsp; &nbsp;tensor([[-0.1068-0.0045j, 0.7061-0.5698j], &nbsp; &nbsp; &nbsp; [-0.2521-1.1166j, 0.6921+1.4637j]]) Eigen Values: &nbsp; &nbsp;tensor([0.3194-0.3633j, 0.2659+1.8225j]) Eigen Vectors: &nbsp; &nbsp;tensor([[ 0.8522+0.0000j, -0.2012-0.3886j], &nbsp; &nbsp; &nbsp; [ 0.5231-0.0109j, 0.8992+0.0000j]])</pre>
Updated on 07-Jan-2022 06:08:11