{"id":13883,"date":"2021-03-31T16:25:13","date_gmt":"2021-03-31T16:25:13","guid":{"rendered":"https:\/\/www.askpython.com\/?p=13883"},"modified":"2023-02-16T19:56:53","modified_gmt":"2023-02-16T19:56:53","slug":"numpy-linear-algebraic-functions","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/numpy-linear-algebraic-functions","title":{"rendered":"NumPy Linear Algebraic functions to know!"},"content":{"rendered":"\n<p> Hello, readers! In this article, we will be focusing on <strong>NumPy Linear Algebraic functions<\/strong> in Python. So, let us get started! \ud83d\ude42<\/p>\n\n\n\n<p>The <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">NumPy module<\/a> offers us various functions to deal with and manipulate data. It enables us to create and store data in an array data structure. Moving ahead, it offers us various functions to analyze and manipulate the data values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-1\"><\/span>List of NumPy Linear Algebraic functions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-2\"><\/span><span id=\"eh-1\"><\/span>1. Matrix functions offered by NumPy module<\/h3>\n\n\n\n<p>With NumPy module, we can perform the linear algebraic matrix functions on the <a href=\"https:\/\/www.askpython.com\/python\/array\/python-array-declaration\" class=\"rank-math-link\">array structure<\/a>.<\/p>\n\n\n\n<p>In the course of this topic, we would be having a look at the below functions&#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Rank of the matrix<\/strong>: We can calculate the rank of the array using numpy.linalg.matrix_rank() function.<\/li><li><strong>Determinant<\/strong>: The numpy.linalg.det() function helps us calculate the determinant of the array treating it as a matrix.<\/li><li><strong>Inverse<\/strong>: The inv() function enables us to calculate the inverse of the array.<\/li><li><strong>Exponent<\/strong>: Using numpy.linalg.matrix_power() function, we can raise a power value to the matrix and fetch the results.<\/li><\/ol>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In the below example, we have created an array using numpy.array() function. Further, we have performed the above mentioned linear algebraic operations on the array and printed the results.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n\nx = numpy.array(&#x5B; &#x5B;2, 8, 7],\n                 &#x5B;6, 1, 1],\n                &#x5B;4, -2, 5]])\n \nprint(&quot;Rank: &quot;, numpy.linalg.matrix_rank(x))\ndet_mat = numpy.linalg.det(x) \nprint(&quot;\\nDeterminant: &quot;,det_mat)\ninv_mat = numpy.linalg.inv(x)\nprint(&quot;\\nInverse: &quot;,inv_mat) \nprint(&quot;\\nMatrix raised to power y:\\n&quot;,\n           numpy.linalg.matrix_power(x, 8))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nRank:  3\n\nDeterminant:  -306.0\n\nInverse:  &#x5B;&#x5B;-0.02287582  0.17647059 -0.00326797]\n &#x5B; 0.08496732  0.05882353 -0.13071895]\n &#x5B; 0.05228758 -0.11764706  0.1503268 ]]\n\nMatrix raised to power y:\n &#x5B;&#x5B; 85469036  43167250 109762515]\n &#x5B; 54010090  32700701  75149010]\n &#x5B; 37996120  22779200  52792281]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-3\"><\/span><span id=\"eh-2\"><\/span>2. Eigen value with NumPy Array<\/h3>\n\n\n\n<p>NumPy Linear Algebraic functions have the linalg class that has <strong>eigh() function<\/strong> to calculate the eigenvalue from the array elements passed to it.<\/p>\n\n\n\n<p>Have a look at the below syntax!<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.linalg.eigh(array)\n<\/pre><\/div>\n\n\n<p>The eigh() function returns the eigenvalues as well as the eigenvectors of a complex or a real symmetric matrix.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom numpy import linalg as li\n\nx = numpy.array(&#x5B;&#x5B;2, -4j], &#x5B;-2j, 4]])\n\nres = li.eigh(x)\n \nprint(&quot;Eigen value:&quot;, res)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEigen value: (array(&#x5B;0.76393202, 5.23606798]), array(&#x5B;&#x5B;-0.85065081+0.j        ,  0.52573111+0.j        ],\n       &#x5B; 0.        -0.52573111j,  0.        -0.85065081j]]))\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-4\"><\/span><span id=\"eh-3\"><\/span>3. Dot Product <\/h3>\n\n\n\n<p>With NumPy Linear Algebraic functions, we can perform dot operations on scalar as well as multi-dimensional values. It performs scalar multiplication for single dimensional vector values. <\/p>\n\n\n\n<p>For multi-dimensional arrays\/matrices, it performs matrix multiplication on the data values.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.dot()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nsc_dot = np.dot(10,2)\nprint(&quot;Dot Product: &quot;, sc_dot)\n\nvectr_x = 1 + 2j\nvectr_y = 2 + 4j\n \nvctr_dot = np.dot(vectr_x, vectr_y)\nprint(&quot;Dot Product: &quot;, vctr_dot)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nDot Product:  20\nDot Product:  (-6+8j)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-5\"><\/span><span id=\"eh-4\"><\/span>4. Solving Linear equations with NumPy module<\/h3>\n\n\n\n<p>With NumPy Linear Algebraic functions, we can even perform the calculations and solve the linear algebraic scalar equations. The <strong>numpy.linalg.solve() function<\/strong> solves for the array values with the equation ax=b.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nx = np.array(&#x5B;&#x5B;2, 4], &#x5B;6, 8]])\n \ny = np.array(&#x5B;2, 2])\n \nprint((&quot;Solution of linear equations:&quot;, \n      np.linalg.solve(x, y)))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n(&#039;Solution of linear equations:&#039;, array(&#x5B;-1.,  1.]))\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Feel free to comment below, in case you come across any question. For more such posts related to Python programming, stay tune with us. Till then, happy learning!! \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will be focusing on NumPy Linear Algebraic functions in Python. So, let us get started! \ud83d\ude42 The NumPy module offers us various functions to deal with and manipulate data. It enables us to create and store data in an array data structure. Moving ahead, it offers us various functions [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":14416,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,1],"tags":[],"class_list":["post-13883","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13883","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=13883"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/13883\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14416"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=13883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=13883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=13883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}