{"id":30451,"date":"2024-06-28T17:36:21","date_gmt":"2024-06-28T12:06:21","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=30451"},"modified":"2024-06-28T17:36:22","modified_gmt":"2024-06-28T12:06:22","slug":"einstein-summation-in-python-numpy-einsum","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/einstein-summation-in-python-numpy-einsum\/","title":{"rendered":"How to Perform Einstein Summation in Python Using numpy.einsum()"},"content":{"rendered":"\n<p>In Python, the concept of Einstein summation helps us simplify the calculations of arrays. It provides a great help in solving matrices, particularly calculating their cross-product, dot product, sum of diagonals, etc. In this article, let us explore this concept and understand the <strong>numpy.einsum()<\/strong> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding Einstein Summation<\/h2>\n\n\n\n<p><strong>Einstein summation<\/strong> is something that helps us to work with <strong>arrays and tensors<\/strong>. It helps us to simplify expressions such as<strong> matrices, tensors <\/strong>and <strong>summation of vectors. <\/strong><\/p>\n\n\n\n<p>There are three vital <strong>rules <\/strong>about this convection to keep in mind:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When an index is repeated, it is summed over automatically, without any need to write the summation symbol.<\/li>\n\n\n\n<li>Each index can only repeat itself twice in any term.<\/li>\n\n\n\n<li>Each term must have the same non-repeated indices.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"254\" height=\"97\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-71.png\" alt=\"Einstein Summation\" class=\"wp-image-30455\"\/><\/figure>\n\n\n\n<p>I see you are a bit perplexed by reading the theory. I agree that Einstein gives us headaches all the time, but do not worry, you will see how this equation works as magic in our code problems. Let&#8217;s discuss its Numpy function and then we will jump into some interesting examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The numpy.einsum() Function<\/h2>\n\n\n\n<p>The <strong>numpy.einsum()<\/strong> function performs the <strong>Einstein summation<\/strong> on the provided input. <strong>Matrix multiplication <\/strong>of multidimensional arrays is made easy using this function. Let&#8217;s discuss it in detail.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.einsum(subscripts, *operands)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Parameters:<\/h3>\n\n\n\n<p>Here is what each parameter in the given syntax means:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>subscripts:<\/strong> This is a string which is used to specify the operation.<\/li>\n\n\n\n<li><strong>operands<\/strong>: This is the collection of arrays you want to operate on.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Return Value:<\/h3>\n\n\n\n<p>This method returns Einstein\u2019s summation convention for the operand arrays according to the provided subscripts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working of numpy.einsum<\/h2>\n\n\n\n<p>Let us understand the<strong> <\/strong>workings of numpy.einsum() function with an example. Consider that there are two arrays A and B and we need to multiply them.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nA = np.array(&#x5B;&#x5B;4, 2], &#x5B;2, 4]])\nB = np.array(&#x5B;&#x5B;3, 6], &#x5B;6, 3]])\n\nresult = np.einsum(&#039;ij,jk-&gt;ik&#039;, A, B)\nprint(&quot;The Multiplication of A and B is &quot;,result)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"249\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-1-1024x249.png\" alt=\"Working of numpy.einsum\" class=\"wp-image-30582\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-1-1024x249.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-1-300x73.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-1-768x187.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-1.png 1032w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the line <strong>ij,jk-&gt;ik<\/strong>, <strong>ij <\/strong>represents the axes of the <strong>array A,<\/strong> where <strong>i is for the rows<\/strong> and<strong> j is for the columns<\/strong>. <strong>jk<\/strong> represent the axes of <strong>array B,<\/strong> where <strong>j is for the rows<\/strong> and <strong>k is for the columns.<\/strong> We see that <strong>j is being repeated<\/strong>, which means that<strong> rows of A<\/strong> are being <strong>multiplied<\/strong> by <strong>columns of B<\/strong>, and since j is not included in the output, it means the <strong>product obtained is summed up<\/strong>. And output will be in the form of a <strong>2D array, i.e., ik<\/strong>. This is how we obtain the matrix multiplication of two arrays and the output clearly justifies it:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"538\" height=\"120\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image.png\" alt=\"Output (How it works)\" class=\"wp-image-30581\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image.png 538w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-300x67.png 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Use Cases of numpy.einsum<\/h2>\n\n\n\n<p>Now that we know that einsum helps us to find matrix multiplication with ease, it is not the only thing that einsum helps us with, it can be used in various ways. Anything that involves combinations of multiplying and summing axes can be written using&nbsp;einsum. Let&#8217;s see how.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1:<\/strong><\/h3>\n\n\n\n<p>We can find out the element-wise multiplication of two array inputs. This is how einsum helps us with it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nA = np.array(&#x5B;4, 4, 4])\nB = np.array(&#x5B;2 ,2 ,2])\n\nresult = np.einsum(&#039;i,i-&gt;i&#039;, A, B)\nprint(&quot;The Element -wise Multiplication of A and B is &quot;, result)\n<\/pre><\/div>\n\n\n<p>In this case, <strong>&#8216;i,i-&gt;i&#8217;<\/strong> means that we take elements of index <strong>&#8216;i&#8217; (rows)<\/strong> from array A. Similarly, we take elements from array B in the <strong>same fashion<\/strong>, as it is also labeled<strong> &#8216;i&#8217;<\/strong>. Then, we <strong>multiply the corresponding elements<\/strong> together to obtain the result.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"540\" height=\"93\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-2.png\" alt=\"Output (Example 1)\" class=\"wp-image-30583\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-2.png 540w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-2-300x52.png 300w\" sizes=\"(max-width: 540px) 100vw, 540px\" \/><\/figure>\n\n\n\n<p>Also, learn how to compute the product of a 1-D array in Python by <a href=\"https:\/\/codeforgeek.com\/find-the-product-of-array-elements-in-python\/\">clicking here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2:<\/strong><\/h3>\n\n\n\n<p>If we need to calculate the dot product, we can use einsum in this way:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nA = np.array(&#x5B;4, 3, 2])\nB = np.array(&#x5B;6 ,7 ,8])\n\nresult = np.einsum(&#039;i,i-&gt;&#039;, A, B)\nprint(&quot;The Dot product of A and B is &quot;, result)\n<\/pre><\/div>\n\n\n<p>Here, <strong>&#8216;i,i-&gt;&#8217;<\/strong> means that from both array A and array B, we take row elements <strong>labelled as i<\/strong> and multiply the corresponding elements together. But, instead of keeping each individual result, just <strong>sum them up<\/strong>. Therefore, we get the dot product.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"447\" height=\"91\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-3.png\" alt=\"Output (Example 2)\" class=\"wp-image-30584\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-3.png 447w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-3-300x61.png 300w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3:<\/strong> <\/h3>\n\n\n\n<p>Einsum can also be used to find the sum of the main diagonal. Here is the code where we find the sum of diagonal elements using einsum:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nA = np.array(&#x5B;&#x5B;4, 1, 1],\n           &#x5B;2, 4, 2],\n           &#x5B;5, 5, 4]])\n\nresult = np.einsum(&#039;ii&#039;, A)\nprint(&quot;The sum of diagonal elements of A is &quot;, result)\n<\/pre><\/div>\n\n\n<p>In this code,<strong> &#8216;ii<\/strong>&#8216; means we <strong>select and sum only<\/strong> the elements that have the <strong>same indices<\/strong> for both rows and columns, which is nothing but the <strong>diagonal elements<\/strong>, hence the function returns the sum of all diagonal elements.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"493\" height=\"94\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-4.png\" alt=\"Output (Example 3)\" class=\"wp-image-30585\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-4.png 493w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/image-4-300x57.png 300w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>I hope this article clears your mind about <strong>Einstein summation<\/strong> in Python. The <strong>numpy.einsum() <\/strong>function simplifies array and tensor calculations, making operations like matrix multiplication, dot products, and diagonal sums easy to perform. With practical examples, you can now use this function to speed up your numerical computations.<\/p>\n\n\n\n<p><em>Do you know how to find the cumulative sum of a number in Python? <a href=\"https:\/\/codeforgeek.com\/numpy-cumsum-python\/\">Read here<\/a><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/26089893\/understanding-numpys-einsum\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/26089893\/understanding-numpys-einsum<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the concept of Einstein summation helps us simplify the calculations of arrays. It provides a great help in solving matrices, particularly calculating their cross-product, dot product, sum of diagonals, etc. In this article, let us explore this concept and understand the numpy.einsum() function. Understanding Einstein Summation Einstein summation is something that helps us [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":31002,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-30451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation.png",1200,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation-300x150.png",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation-768x384.png",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation-1024x512.png",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation.png",1200,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/06\/Einstien-summation.png",1200,600,false]},"uagb_author_info":{"display_name":"Snigdha Keshariya","author_link":"https:\/\/codeforgeek.com\/author\/snigdha\/"},"uagb_comment_info":0,"uagb_excerpt":"In Python, the concept of Einstein summation helps us simplify the calculations of arrays. It provides a great help in solving matrices, particularly calculating their cross-product, dot product, sum of diagonals, etc. In this article, let us explore this concept and understand the numpy.einsum() function. Understanding Einstein Summation Einstein summation is something that helps us&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/30451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=30451"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/30451\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/31002"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=30451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=30451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=30451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}