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

In math, the speck item or otherwise called the scalar item is an arithmetical activity that takes two equivalent length groupings of numbers and returns a solitary number. Let us given two vectors An and B, and we need to track down the dab result of two vectors

This instructional exercise acquaints the various ways with work out the speck result of two clusters or vectors in Python. Before we continue on to the various techniques to carry out this, we will initially find out with regards to the dab item in Python. As you might know, a spot item, now and again even alluded to as a scalar item, is an arithmetical activity done between any two determined clusters; they could be scalar or vector. The result is consistently a solitary number. Python permits the computation of the speck result of two exhibits, gave the length-groupings of both the clusters are comparable. the Dot item (in some cases known as scalar item) is a mathematical activity that profits a solitary worth from two equivalent length groupings of numbers.

This single worth is determined as the amount of the results of the comparing components from the two successions. These successions may be single-layered vectors, complex vectors, or just numbers.
How to work out dab result of two vectors in Python?

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]]
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://eevibes.com/mathematics/how-to-calculate-dot-product-of-two-vectors-in-python/", "fetch_time": 1723392377000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-33/segments/1722641002566.67/warc/CC-MAIN-20240811141716-20240811171716-00401.warc.gz", "warc_record_offset": 166881871, "warc_record_length": 14719, "token_count": 849, "char_count": 3504, "score": 4.34375, "int_score": 4, "crawl": "CC-MAIN-2024-33", "snapshot_type": "latest", "language": "en", "language_score": 0.8996405601501465, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00028-of-00064.parquet"}