{"id":38726,"date":"2023-01-24T15:03:44","date_gmt":"2023-01-24T15:03:44","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38726"},"modified":"2023-01-30T15:52:21","modified_gmt":"2023-01-30T15:52:21","slug":"numpy-linalg-eigvals","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-eigvals","title":{"rendered":"Numpy linalg.eigvals &#8211; Compute the eigenvalues of a general matrix"},"content":{"rendered":"\n<p>Matrices in mathematics are rectangular arrangements of an array of homogeneous data types such as numbers, symbols, or expressions. They are arranged in the form of rows and columns which represents objects or some specific properties of that object. Eigenvalues are values that are scalar in type and are associated with linear matrix equations. They are used to compress dimensional space.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-inv\" data-type=\"post\" data-id=\"39878\">Numpy linalg.inv \u2013 Compute the (multiplicative) inverse of a matrix<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Numpy linalg.eigvals?<\/h2>\n\n\n\n<p>Numpy is the numerical python package\/library that contains various functions used for scientific and complex mathematical computations involving linear algebra, matrices, and many more. Its built-in functions and libraries help us to calculate complicated equations easily.  The numpy linalg.eigvals() computes the eigenvalues of a general matrix.<\/p>\n\n\n\n<p>The required matrix should be a NxN matrix, that is, the number of rows and the number of columns of the matrix should be equal.<\/p>\n\n\n\n<p>To use any numpy function you have to have the NumPy package installed in your system. Run the following code in your command prompt to install numpy in your system. Make sure you run the command prompt in administrator mode.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install numpy\n<\/pre><\/div>\n\n\n<p>The only difference between the two functions is that the former returns only one singular array containing the eigenvalues whereas the latter returns two arrays, one containing the eigenvalues and the other containing the eigenvectors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax and parameters of the linalg.eigvals() function:<\/h2>\n\n\n\n<p>The syntax of the numpy linalg.eigvals() function is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nlinalg.eigvals(m)\n<\/pre><\/div>\n\n\n<p>The parameters of the function are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the input: m: the required matrix(array-like)-> The matrix whose eigenvalues we have to compute.<\/li>\n\n\n\n<li>the output: x: array or ndarray -&gt; The array of eigenvalues is unordered but repeated according to their multiplicities. Most of the time these values are complex, but in case the complex part is zero, the value returned is real.<\/li>\n\n\n\n<li>Error raised: In case the matrix is not a square matrix or if the eigenvalues diverge, it raises a <strong>LinAlgError<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-eig\" data-type=\"post\" data-id=\"38694\">Numpy linalg.eig \u2013 Compute the eigenvalues and right eigenvectors of a square array<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use the function? (with examples)<\/h2>\n\n\n\n<p>Let&#8217;s now take a look at 3 examples of using the numpy eigvals() function<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Compute the eigenvalues of a general predefined matrix<\/h3>\n\n\n\n<p>A simple program to compute the eigenvalues of a general predefined matrix. This is the most simple form of using the linalg.eigvals() function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#from numpy calling the function\nfrom numpy import linalg as L\n  \nm=&#x5B;&#x5B;4,2],&#x5B;1,3]] #pre defined matrix\nprint(&quot;the initial matrix is=&quot;)\nprint(m) #displaying the matrix\nresult = L.eigvals(m) #compute result\n#display result  \nprint(&quot;the computed eigenvalues of the matrix is=&quot;)\nprint(result)\n<\/pre><\/div>\n\n\n<p>The output of the above code is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nthe initial matrix is=\n&#x5B;&#x5B;4, 2], &#x5B;1, 3]]\nthe computed eigenvalues of the matrix is=\n&#x5B;5. 2.]\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"445\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-33-1024x445.png\" alt=\"The code and the output of 1st example.\" class=\"wp-image-40400\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-33-1024x445.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-33-300x130.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-33-768x333.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-33.png 1366w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">The code and the output of 1st example.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Take user input for the matrix<\/h3>\n\n\n\n<p>This example illustrates how you can take user input for the matrix and every time you run the code, the user can modify the values, hence, this program can be used as an eigenvalue calculator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as py\nfrom numpy import linalg as L\n#taking user input\nrow = int(input(&quot;Enter the number of rows:&quot;))\ncol= int(input(&quot;Enter the number of columns:&quot;))\nprint(&quot;NOTE!! The number of rows should be equal to the number of columns&quot;)\n\n  \n# Initializing the required matrix\nm = &#x5B;]\nprint(&quot;enter the values rowwise:&quot;)\n  \n# For user input\nfor i in range(row):          # loop for row entries\n    b =&#x5B;]\n    for j in range(col):      # loop for column entries\n         b.append(int(input()))\n    m.append(b)\n  \n# For displaying the matrix\nprint(&quot;The matrix is as follows:&quot;)\nprint(&quot;&#x5B;&quot;)\nfor i in range(row):\n    for j in range(col):\n        print(m&#x5B;i]&#x5B;j], end = &quot; &quot;)\n    print()\nprint(&quot;]&quot;)\nresult= L.eigvals(m) #computing the values and vectors\n\n#displaying the result\nprint(&quot;the eigenvalues are:&quot;,result)\n\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEnter the number of rows:2\nEnter the number of columns:2\nNOTE!! The number of rows should be equal to the number of columns\nenter the values rowwise:\n6\n5\n4\n1\nThe matrix is as follows:\n&#x5B;\n6 5\n4 1\n]\nthe eigenvalues are: &#x5B; 8.62347538 -1.62347538]\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-34-1024x439.png\" alt=\"The code and output of the 2nd example.\" class=\"wp-image-40405\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-34-1024x439.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-34-300x128.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-34-768x329.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-34.png 1366w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">The code and output of the 2nd example.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Generating a random matrix<\/h3>\n\n\n\n<p>Another example can be illustrated by generating a random matrix using the numpy.random() function and then using the linalg.eigvals() function. Here the matrix generated has random numbers and is not predefined or given by the user as input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#from numpy calling the function\nimport numpy as py\nfrom numpy import linalg as L\n  \nnum=py.random.random() #generating random number using the function random\n#creating the random matrix\nm = py.array(&#x5B;&#x5B;py.sin(num), -py.cos(num)], &#x5B;py.cos(num), py.sin(num)]])\nprint(&quot;the initial matrix is=&quot;)\nprint(m) #displaying the matrix\nresult = L.eigvals(m) #compute result\n#display result  \nprint(&quot;the computed eigenvalues of the matrix is=&quot;)\nprint(result)\n<\/pre><\/div>\n\n\n<p> The output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nthe initial matrix is=\n&#x5B;&#x5B; 0.81665335 -0.5771285 ]\n &#x5B; 0.5771285   0.81665335]]\nthe computed eigenvalues of the matrix is=\n&#x5B;0.81665335+0.5771285j 0.81665335-0.5771285j]\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"436\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-35-1024x436.png\" alt=\"The code and output for example 3.\" class=\"wp-image-40407\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-35-1024x436.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-35-300x128.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-35-768x327.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/image-35.png 1366w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">The code and output for example 3.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This is a basic tutorial on how to use the eigvals() function, which involves three examples of different types of usage of the function. The only thing that needs to be in check is the shape of the matrix. It must be a square matrix so that the function doesn&#8217;t throw an error. Numpy also has many other linear algebraic functions which makes computation easy and less tedious for the user. For more info on numpy linear algebraic functions, <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.linalg.eigvals.html\" data-type=\"URL\" data-id=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.linalg.eigvals.html\" target=\"_blank\" rel=\"noopener\">click here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Matrices in mathematics are rectangular arrangements of an array of homogeneous data types such as numbers, symbols, or expressions. They are arranged in the form of rows and columns which represents objects or some specific properties of that object. Eigenvalues are values that are scalar in type and are associated with linear matrix equations. They [&hellip;]<\/p>\n","protected":false},"author":49,"featured_media":38743,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38726","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/49"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38726"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38726\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38743"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}