{"id":25704,"date":"2021-12-04T09:19:26","date_gmt":"2021-12-04T03:49:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=25704"},"modified":"2021-12-04T09:19:26","modified_gmt":"2021-12-04T03:49:26","slug":"python-program-to-calculate-the-dot-product","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-the-dot-product\/","title":{"rendered":"Python Program to Calculate the Dot Product?"},"content":{"rendered":"
The dot product, commonly known as the scalar product in mathematics, is an algebraic operation that takes two equal-length sequences of numbers and produces a single number.<\/p>\n
Given two vectors A and B, we must find the dot product of the two vectors.<\/p>\n
For Example :<\/strong><\/p>\n Let A = a1i+a2j+a3k<\/p>\n B= b1i+b2j+b3k<\/p>\n Where<\/p>\n i<\/strong> is the unit vector along the x-axis.<\/p>\n j<\/strong> is the unit vector along the y-axis.<\/p>\n k<\/strong> is the unit vector along the z-axis.<\/p>\n The numpy.dot() method of the numpy library provides an efficient way to find the dot product of two sequences.<\/p>\n numpy.dot() is a method that accepts two sequences as arguments, whether they are vectors or multidimensional arrays, and displays the output, which is the dot product. To use this approach, we must first import Python’s numpy module.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n vector_a:<\/strong> It is an array. If a<\/strong> is complex, its complex conjugate is utilized to compute the dot product.<\/p>\n vector_b: <\/strong>It is an array. If b\u00a0<\/strong>is complex, its complex conjugate is utilized to compute the dot product.<\/p>\n out: <\/strong>It is an array. This is optional. The output parameter must be C-contiguous, and its datatype must be the same as the datatype\u00a0returned by dot (a,b).<\/p>\n Return Value:<\/strong><\/p>\n Returns the Dot Product of given vectors a and b. Scalar is returned if vector a and vector b are both 1D.<\/p>\n Given two scalar values and use numpy.dot() function to display their dot product.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Given two arrays, and the task is to calculate the dot product of the given two vectors.<\/p>\n These arrays might be one-dimensional, two-dimensional, or multi-dimensional. And we’ll compute their dot product with the help of dot(). For the dot product, we are evaluating two 2-D arrays.<\/p>\n Matrix multiplication is used to compute the dot product for 2-D arrays.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n The dot product is not commutative for two-dimensional or multi-dimensional arrays. That means a.b does not<\/strong> equal b.a.<\/p>\n If we calculated dot product as a.b rather than b.a, the output is not the same for both of them. It is different for both.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Dot Product: The dot product, commonly known as the scalar product in mathematics, is an algebraic operation that takes two equal-length sequences of numbers and produces a single number. Given two vectors A and B, we must find the dot product of the two vectors. For Example : Let A = a1i+a2j+a3k B= b1i+b2j+b3k Where …<\/p>\nThe Formula to calculate the dot product<\/h4>\n
Dot product = a1*b1+a2*b2+a3*b3<\/pre>\n
Calculating Dot Product of Two Vectors in Python<\/h4>\n
numpy.dot(vector_a, vector_b, out = None )<\/pre>\n
How to find the Dot Product of Scalars?<\/h4>\n
\n
# Import numpy library using the import keyword\r\nimport numpy as np\r\n# Give the first scalar value as static input and store it in a variable.\r\nx = 4\r\n# Give the second scalar value as static input and store it in another variable.\r\ny = 6\r\n# Calculate the dot product of the given two scalar values using the numpy.dot()\r\n# function by passing the given first and second scalar values as the arguments.\r\n# Store it in another variable.\r\ndot_prdct = np.dot(x, y)\r\n# Print the dot product of the given two scalar values.\r\nprint(\"The dot product of given scalars x{\", x, \"} and y {\", y, \"} = \")\r\nprint(dot_prdct)\r\n<\/pre>\n
The dot product of given scalars x{ 4 } and y { 6 } = \r\n24<\/pre>\n
How to find the Dot Product of Arrays?<\/h4>\n
\n
# Import numpy library using the import keyword\r\nimport numpy as np\r\n# Give the 2- Dimensional array as static input and store it in a variable.\r\nx = [[3, 1], [5, 2]]\r\n# Give the other 2- Dimensional array as static input and store it in another\r\n# variable.\r\ny = [[8, 5], [2, 4]]\r\n# Calculate the dot product of the given two arrays using the numpy.dot()\r\n# function by passing the given two 2-D arrays as the arguments.\r\n# Store it in another variable.\r\ndot_prdct = np.dot(x, y)\r\n# Print the dot product of the given two 2- Dimensional arrays\r\nprint(\"The dot product of given two 2-D arrays x \", x, \" and y \", y, \" = \")\r\nprint(dot_prdct)\r\n<\/pre>\n
The dot product of given two 2-D arrays x [[3, 1], [5, 2]] and y [[8, 5], [2, 4]] = \r\n[[26 19]\r\n[44 33]]<\/pre>\n