{"id":38991,"date":"2022-12-29T13:42:39","date_gmt":"2022-12-29T13:42:39","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38991"},"modified":"2022-12-29T13:42:40","modified_gmt":"2022-12-29T13:42:40","slug":"numpy-linalg-det","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-det","title":{"rendered":"NumPy linalg.det &#8211; Compute the determinant of the given array"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on\u00a0<strong>Numpy linalg.det<\/strong>. In this tutorial, we will be learning about the\u00a0<strong>NumPy linalg.det()\u00a0<\/strong>method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<p><strong><em>Also check: <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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is numpy.linalg.det?<\/h2>\n\n\n\n<p>The <code>numpy.linalg.det()<\/code> method in NumPy is used to compute the determinant of a given square matrix.<\/p>\n\n\n\n<p>If we have a 2&#215;2 matrix of the form:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"347\" height=\"295\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/2-d-array.png\" alt=\"2 D Array\" class=\"wp-image-38992\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/2-d-array.png 347w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/2-d-array-300x255.png 300w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><figcaption class=\"wp-element-caption\">2&#215;2 Array<\/figcaption><\/figure>\n\n\n\n<p>Its determinant is calculated as:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"283\" height=\"157\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/2x2-array-determinant.png\" alt=\"2x2 Array Determinant\" class=\"wp-image-38993\"\/><figcaption class=\"wp-element-caption\">2&#215;2 Array Determinant<\/figcaption><\/figure>\n\n\n\n<p>For a 3&#215;3 matrix like<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"316\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array.png\" alt=\"3x3 Array\" class=\"wp-image-38994\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array.png 452w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array-300x210.png 300w\" sizes=\"auto, (max-width: 452px) 100vw, 452px\" \/><figcaption class=\"wp-element-caption\">3&#215;3 Array<\/figcaption><\/figure>\n\n\n\n<p>The determinant is computed as:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array-determinant.png\" alt=\"3x3 Array Determinant\" class=\"wp-image-38995\" width=\"561\" height=\"61\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array-determinant.png 1121w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array-determinant-300x33.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array-determinant-1024x111.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/3x3-array-determinant-768x84.png 768w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><figcaption class=\"wp-element-caption\">3&#215;3 Array Determinant<\/figcaption><\/figure>\n\n\n\n<p>Similarly, we can calculate the determinant of higher-order arrays.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy linalg.det<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.linalg.det(a)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Parameter:<\/strong> <em>a<\/em>, an MxM array. Input array to compute the determinant of.<\/li>\n\n\n\n<li><strong>Returns:<\/strong> Determinant of <em>a<\/em>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of NumPy linalg.det<\/h2>\n\n\n\n<p>Let&#8217;s look at some examples of the NumPy linalg.det function to learn how it works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using NumPy linalg.det on a 2&#215;2 array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;2, 1], &#x5B;3, 5]]\n# using np.linalg.det() method to compute the determinant\nprint(&quot;array = &quot;, arr)\nprint(&quot;Determinant of array = &quot;, np.linalg.det(arr))\ndet = 5*2 - 3*1\nprint(&quot;Determinant of array using manual calculation = &quot;, det)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narray =  &#x5B;&#x5B;2, 1], &#x5B;3, 5]]\nDeterminant of array =  6.999999999999999\nDeterminant of array using manual calculation =  7\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using NumPy linalg.det on a 2&#215;2 array with negative numbers<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;-5, 2], &#x5B;-4, 8]]\n# using np.linalg.det() method to compute the determinant\nprint(&quot;array = &quot;, arr)\nprint(&quot;Determinant of array = &quot;, np.linalg.det(arr))\ndet = (-5*8) - (-4*2)\nprint(&quot;Determinant of array using manual calculation = &quot;, det)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narray =  &#x5B;&#x5B;-5, 2], &#x5B;-4, 8]]\nDeterminant of array =  -32.0\nDeterminant of array using manual calculation =  -32\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy linalg.det calculation for a 3&#215;3 array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;2, 1, 3], &#x5B;5, 3, 4], &#x5B;1, 0, 1]]\n# using np.linalg.det() method to compute the determinant\nprint(&quot;array = &quot;, arr)\nprint(&quot;Determinant of array = &quot;, np.linalg.det(arr))\ndet = 2*(3*1 -0*4) -1*(5*1 - 1*4) + 3*(5*0 - 3*1)\nprint(&quot;Determinant of array using manual calculation = &quot;, det)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narray =  &#x5B;&#x5B;2, 1, 3], &#x5B;5, 3, 4], &#x5B;1, 0, 1]]\nDeterminant of array =  -4.0\nDeterminant of array using manual calculation =  -4\n<\/pre><\/div>\n\n\n<p>In all the above examples, we have used the <code>numpy.linalg.det<\/code> method to compute the determinant as well as calculated the determinant using the manual method i.e. through the formulas discussed above. Through this, we can conclude that both methods return the same answer.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That\u2019s all! In this tutorial, we learned about the&nbsp;<strong>Numpy linalg.det&nbsp;<\/strong>method and practiced different types of examples using the same. &nbsp;<\/p>\n\n\n\n<p>If you want to learn more about NumPy, feel free to go through our&nbsp;<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy tutorials<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.linalg.det.html\" target=\"_blank\" rel=\"noreferrer noopener\">numpy.linalg.det Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on\u00a0Numpy linalg.det. In this tutorial, we will be learning about the\u00a0NumPy linalg.det()\u00a0method and also seeing a lot of examples regarding the same. So let us begin! Also check: Numpy linalg.eig \u2013 Compute the eigenvalues and right eigenvectors of a square array What is numpy.linalg.det? The numpy.linalg.det() method in NumPy [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":38996,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38991","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\/38991","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\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38991"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38991\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38996"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}