{"id":24095,"date":"2021-11-23T13:04:11","date_gmt":"2021-11-23T13:04:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=24095"},"modified":"2021-11-23T13:04:12","modified_gmt":"2021-11-23T13:04:12","slug":"numpy-cross-product","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-cross-product","title":{"rendered":"Numpy Cross Product &#8211; A Complete Guide"},"content":{"rendered":"\n<p>A cross product is a mathematical tool to find the perpendicular vector component of two vector coordinates. <\/p>\n\n\n\n<p><strong>Suppose in a 3D space, there are two points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&#8216;a&#8217; with coordinates (1,2,3)  <\/li><li>&#8216;b&#8217; with coordinates (4,5,6). <\/li><\/ul>\n\n\n\n<p>So the vector component of the two coordinates will be the cross-product of the determinant of this vector matrix.<\/p>\n\n\n\n<p>The cross product will be a non-commutative perpendicular vector product of the two matrix points.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Numpy Cross Product<\/h2>\n\n\n\n<p>The numpy.cross() is a mathematical function in the Python library that finds out the cross product between two arrays (Dimension of 2&#038;3) and the result can be displayed with the <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-print-function\" data-type=\"post\" data-id=\"2517\">print function<\/a>. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax of Numpy Cross Product<\/h3>\n\n\n\n<p>The basic syntax for implementing cross product is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.cross&#x5B;M,N]\n<\/pre><\/div>\n\n\n<p>where M and N are array variables storing vector coordinates, but we can specify certain parameters as per our suitability and needs.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/numpy.crossa-b-axisa-1-axisb-1-axisc-1-axisNone-1024x512.png\" alt=\"Numpy Crossa B Axisa 1 Axisb 1 Axisc 1 AxisNone\" class=\"wp-image-24109\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/numpy.crossa-b-axisa-1-axisb-1-axisc-1-axisNone-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/numpy.crossa-b-axisa-1-axisb-1-axisc-1-axisNone-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/numpy.crossa-b-axisa-1-axisb-1-axisc-1-axisNone-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/numpy.crossa-b-axisa-1-axisb-1-axisc-1-axisNone.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Numpy Cross Syntax<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">How To Calculate Cross Product Using Numpy Python?<\/h2>\n\n\n\n<p>Let\u2019s look at a functional code over how cross-product is found in python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Cross product of 2X2 matrix<\/h3>\n\n\n\n<p>Let\u2019s suppose there are two arrays, X= [2,3], and Y= [4,3]. To find the vector product, we need to find the difference between the product of i1-j2 and i2-j1. The vector-product of two 2-Dimensional arrays will always be a single-dimensional integer.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/unnamed-file-1024x512.png\" alt=\"cross_product_2x2.png\" class=\"wp-image-24097\" width=\"1024\" height=\"512\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/unnamed-file-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/unnamed-file-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/unnamed-file-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/unnamed-file.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>2X2 cross product<\/figcaption><\/figure><\/div>\n\n\n\n<p>The final result is (3*2) &#8211; (4*3) = -6.<\/p>\n\n\n\n<p>Note: In this case, X and Y dimensions are defined while the z component is not there, so the final output is scalar.<\/p>\n\n\n\n<p><strong>Example code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as pr \n\n#initialize arrays \n\nX = pr.array(&#x5B;2, 3]) \nY = pr.array(&#x5B;4, 3]) \n\n#calculating cross product \n\nvector_result= pr.cross(X,Y) \nprint(vector_result)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Cross product of a 2X3 array<\/h3>\n\n\n\n<p>Let\u2019s take two 3-Dimensional arrays and find the cross-product of it.<\/p>\n\n\n\n<p>Let\u2019s take X= [1,3,5] and Y= [1,2,1]<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/1-1024x512.png\" alt=\"cross-product-2x3matrix.png\" class=\"wp-image-24096\" width=\"1024\" height=\"512\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/1-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/1-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/1-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/11\/1.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>2X3 matrix cross product<\/figcaption><\/figure><\/div>\n\n\n\n<p>Here, the final output will be = (-7, 4, -1)<\/p>\n\n\n\n<p><strong>Example code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as pr\n\n#initialize arrays\n\nX = pr.array(&#x5B;1, 3, 5])\nY = pr.array(&#x5B;1, 2, 1])\n\n#calculating cross product\ncross_product= pr.cross(X,Y)\n\nprint(cross_product)\n<\/pre><\/div>\n\n\n<p>Note: The numpy cross-product supports matrices of 2 &amp; 3 dimensions and any matrix with the higher dimension will throw error outputs.<\/p>\n\n\n\n<p>Let&#8217;s take another example where, let&#8217;s suppose, M=[5,6,4] and N=[2,1]<\/p>\n\n\n\n<p><strong>Example code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as pr\n\n#initialize arrays\n\nX = pr.array(&#x5B;5, 6, 4])\nY = pr.array(&#x5B;2, 1])\n\n#calculating cross product\ncross_product= pr.cross(X,Y)\n\nprint(cross_product)\n<\/pre><\/div>\n\n\n<p>Here, the compiler automatically assigns the z component of array N as zero and calculates the final output based on that parameter.<br>Final result = [-4, 8, -7]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article we learned how to find the cross product of two vector arrays by using the python mathematical function &#8216;numpy.cross&#8217;. We also learned about different case scenarios and parameters through which numpy.cross can be implemented on different sets of array values.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A cross product is a mathematical tool to find the perpendicular vector component of two vector coordinates. Suppose in a 3D space, there are two points: &#8216;a&#8217; with coordinates (1,2,3) &#8216;b&#8217; with coordinates (4,5,6). So the vector component of the two coordinates will be the cross-product of the determinant of this vector matrix. The cross [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":24118,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-24095","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\/24095","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\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=24095"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/24095\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/24118"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=24095"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=24095"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=24095"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}