 # How to calculate dot product of two vectors in Python?

## How to calculate dot product of two vectors in Python?

In mathematics, the dot product or also known as the scalar product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number. Let us given two vectors A and B, and we have to find the dot product of two vectors.

Given that, and, Where,

i: the unit vector along the x directions

j: the unit vector along the y directions

k: the unit vector along the z directions

Then the dot product is calculated as: Example:

Given two vectors A and B as, ## Dot Product of Two Vectors in Python

Python provides a very efficient method to calculate the dot product of two vectors. By using numpy.dot() method which is available in the NumPy module one can do so.

Syntax:

numpy.dot(vector_a, vector_b, out = None)

Parameters:

vector_a: [array_like] if a is complex its complex conjugate is used for the calculation of the dot product.

vector_b: [array_like] if b is complex its complex conjugate is used for the calculation of the dot product.

out: [array, optional] output argument must be C-contiguous, and its dtype must be the dtype that would be returned for dot(a,b).

Return:

Dot Product of vectors a and b. if vector_a and vector_b are 1D, then scalar is returned

Example 1:

 `# Python Program illustrating` `# dot product of two vectors` `# Importing numpy module` `import` `numpy as np` `# Taking two scalar values` `a ``=` `5` `b ``=` `7` `# Calculating dot product using dot()` `print``(np.dot(a, b))`

Output:

`35`

Example 2:

 `# Python Program illustrating` `# dot product of two vectors` `# Importing numpy module` `import` `numpy as np` `# Taking two 1D array` `a ``=` `3` `+` `1j` `b ``=` `7` `+` `6j` `# Calculating dot product using dot()` `print``(np.dot(a, b))`

Output:

`(15+25j)`

Example 3:

 `# Python Program illustrating` `# dot product of two vectors` `# Importing numpy module` `import` `numpy as np` `# Taking two 2D array` `# For 2-D arrays it is the matrix product` `a ``=` `[[``2``, ``1``], [``0``, ``3``]]` `b ``=` `[[``1``, ``1``], [``3``, ``2``]]` `# Calculating dot product using dot()` `print``(np.dot(a, b))`

Output:

```[[5 4]
[9 6]]```

Example 4:

 `# Python Program illustrating` `# dot product of two vectors` `# Importing numpy module` `import` `numpy as np` `# Taking two 2D array` `# For 2-D arrays it is the matrix product` `a ``=` `[[``2``, ``1``], [``0``, ``3``]]` `b ``=` `[[``1``, ``1``], [``3``, ``2``]]` `# Calculating dot product using dot()` `# Note that here I have taken dot(b, a)` `# Instead of dot(a, b) and we are going to` `# get a different output for the same vector value` `print``(np.dot(b, a))`

Output:

```[[2 4]
[6 9]]```

Last Updated on March 17, 2022 by admin

## How to save a Python Dictionary to a CSV File?How to save a Python Dictionary to a CSV File?

How to save a Python Dictionary to a CSV File? CSV (comma-separated values) files are one

## Matplotlib.pyplot.savefig() in PythonMatplotlib.pyplot.savefig() in Python

Matplotlib.pyplot.savefig() in Python Matplotlib is highly useful visualization library in Python. It is a multi-platform data

## Difference between Pandas VS NumPyDifference between Pandas VS NumPy

Difference between Pandas VS NumPy Pandas is an open-source, BSD-licensed library written in Python Language. Pandas provide high performance, fast,

## numpy.cov() functionnumpy.cov() function

Python | numpy.cov() function Covariance provides the a measure of strength of correlation between two

## numpy.argsort() in Pythonnumpy.argsort() in Python

numpy.argsort() in Python numpy.argsort() function is used to perform an indirect sort along the given axis

## Pandas dataframe.interpolate()Pandas dataframe.interpolate()

Python | Pandas dataframe.interpolate() Python is a great language for doing data analysis, primarily because

## Create pong game using Python – TurtleCreate pong game using Python – Turtle

Create pong game using Python – Turtle Pong is one of the most famous arcade

## Python – Display images with PyGamePython – Display images with PyGame

Python | Display images with PyGame Pygame is a cross-platform set of Python modules designed for