{"id":38717,"date":"2022-12-26T15:13:10","date_gmt":"2022-12-26T15:13:10","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38717"},"modified":"2023-01-28T15:07:23","modified_gmt":"2023-01-28T15:07:23","slug":"numpy-vector-dot","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-vector-dot","title":{"rendered":"How to Find Vector Dot Product Using Numpy?"},"content":{"rendered":"\n<p>Vectors are the physical quantities possessing both magnitude and direction in the direction of movement of the object. They are predominantly used for navigating around different spaces &#038; planes in the field of mathematics. While there are umpteen operations that can be carried out with the vectors, in this article, we will be exploring one such operation using an in-built function within the\u00a0<em>numpy\u00a0<\/em>library \u2013 the\u00a0vector dot product! <\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-dot\" data-type=\"post\" data-id=\"23933\">Numpy dot() \u2013 A Complete Guide to Vectors, Numpy, And Calculating Dot Products<\/a><\/em><\/strong><\/p>\n\n\n\n<p>The function that is to be used from the <em>numpy<\/em>&nbsp;library for calculating the vector dot product is the <em>vdot( )<\/em> function. Let\u2019s start things by importing the<em>&nbsp;numpy<\/em>&nbsp;library using the below code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>We shall further explore the&nbsp;<em>vdot( )&nbsp;<\/em>function through each of the following sections.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Syntax of&nbsp;<em>vdot( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Calculating Vector Dot Product for N-Dimensional Arrays<\/strong><\/li>\n\n\n\n<li><strong>Calculating Vector Dot Product for Complex Numbers<\/strong><\/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\">Syntax of\u00a0vdot( )\u00a0function<\/h2>\n\n\n\n<p>One can wonder why bother with the <em>vdot( ) <\/em>function when there is already a <em>dot( ) <\/em>function within the <em>numpy <\/em>library that serves the same purpose. Though these may seem synonymous initially, the devil is in the details.<\/p>\n\n\n\n<p>The <em>vdot( ) <\/em>function deploys the complex conjugate technique if the inputs provided are complex in nature. The function utilizes the complex conjugate of the first input parameter to calculate the vector dot product of the given two vectors.<\/p>\n\n\n\n<p>But the real difference kicks in when the N-dimensional arrays come into the picture. While the <em>dot( ) <\/em>function uses the matrix multiplication technique for calculating the dot product of the N-dimensional arrays, the <em>vdot( ) <\/em>function flattens the given N-dimensional arrays into their one-dimensional equivalents to calculate the dot product. <\/p>\n\n\n\n<p>All this happens within the <em>vdot( ) <\/em>function whose syntax is as follows,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.vdot(a, b)\n<\/pre><\/div>\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>a \u2013&nbsp;<\/em><\/strong>n-dimensional array or complex number for the first input vector<\/li>\n\n\n\n<li><strong><em>b \u2013<\/em><\/strong>&nbsp;n-dimensional array or complex number for the second input vector<\/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\">Calculating Vector Dot Product for N-Dimensional Arrays<\/h2>\n\n\n\n<p>After importing the&nbsp;<em>numpy&nbsp;<\/em>library let us find the dot product of two vectors using a couple of two-dimensional arrays as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;&#x5B;12, 5],\n                &#x5B;5, 9]], dtype = int)\nar2 = np.array(&#x5B;&#x5B;21, 50],\n                &#x5B;8, 6]], dtype = int)\nnp.vdot(ar1, ar2)\n<\/pre><\/div>\n\n\n<p>Once the above code is run, the following computation happens in the back end for calculating the dot product of the given two vectors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First element of the first input array gets multiplied with the first element of the second input array, such as \u201812&#215;21\u2019.<\/li>\n\n\n\n<li>The above step is repeated till each element in the first input array is multiplied with their corresponding elements in the second input array, such as \u20185&#215;50\u2019, \u20185&#215;8\u2019, \u20189&#215;6\u2019.<\/li>\n\n\n\n<li>The results of all these products are then added to print the vector dot product of the given two N- dimensional arrays viz. (12&#215;21)+(5&#215;50)+(5&#215;8)+(9&#215;6) = 596<\/li>\n<\/ul>\n\n\n\n<p>The final answer deduced in the last step as stated above can also be seen as the result in the below image when the code is run.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"116\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Dot-Product-Calculated-for-N-Dimensional-Arrays.jpg\" alt=\"Dot Product Calculated For N Dimensional Arrays\" class=\"wp-image-38749\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Dot-Product-Calculated-for-N-Dimensional-Arrays.jpg 343w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Dot-Product-Calculated-for-N-Dimensional-Arrays-300x101.jpg 300w\" sizes=\"auto, (max-width: 343px) 100vw, 343px\" \/><figcaption class=\"wp-element-caption\">Dot Product Calculated For N-Dimensional Arrays<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Calculating Vector Dot Product for Complex Numbers<\/h2>\n\n\n\n<p>This section shall elaborate on the usage of complex numbers with the<em> vdot( )<\/em> function. Let us assign a couple of variables &#038; then use them for calculating the vector dot product as shown in the below code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar3 = &#x5B;&#x5B;2+21j, 3-16j]]\nar4 = &#x5B;&#x5B;6-17j, 18+6j]]\nnp.vdot(ar3, ar4)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"289\" height=\"84\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/12\/Vector-Dot-Product-Calculated-for-Complex-Numbers.jpg\" alt=\"Vector Dot Product Calculated For Complex Numbers\" class=\"wp-image-38752\"\/><figcaption class=\"wp-element-caption\">Vector Dot Product Calculated For Complex Numbers<\/figcaption><\/figure>\n\n\n\n<p>The same technique stated in the syntax section is followed here multiplying the conjugate of the complex numbers to deduce the final result.<\/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>Now that we have reached the end of this article, hope it has elaborated on how to use the&nbsp;<em>vdot( )&nbsp;<\/em>function from the&nbsp;<em>numpy&nbsp;<\/em>library to calculate the dot product of the given two vectors. Here\u2019s another article that explains <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-outer\" target=\"_blank\" type=\"URL\" id=\"https:\/\/www.askpython.com\/python\/how-to-use-numpy-outer\" rel=\"noreferrer noopener\">how to find the outer product of the given vectors using&nbsp;<em>numpy<\/em><\/a><em>&nbsp;<\/em>in Python. There are numerous other enjoyable and equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" rel=\"noreferrer noopener\">AskPython<\/a> that might be of great help to those who are looking to level up in Python. Whilst you enjoy those,&nbsp;<em>hasta luego<\/em>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vectors are the physical quantities possessing both magnitude and direction in the direction of movement of the object. They are predominantly used for navigating around different spaces &#038; planes in the field of mathematics. While there are umpteen operations that can be carried out with the vectors, in this article, we will be exploring one [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":38718,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38717","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\/38717","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\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38717"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38717\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38718"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}