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,

A = 3i + 5j + 4k,

and

B = 2i + 7j + 5k

Dot Product = 3 * 2 + 5 * 7 + 4 * 5 = 6 + 35 + 20 + 61

#### Computing Dot Product in R

R language provides a very efficient method to calculate the dot product of two vectors. By using **dot()** method which is available in the **geometry** library one can do so.

Syntax:dot(x, y, d = NULL)

Parameters:

x:Matrix of vectors

y:Matrix of vectors

d:Dimension along which to calculate the dot product

Return:Vector with length of dth dimension

**Example 1:**

## R

`# R Program illustrating ` `# dot product of two vectors ` ` ` `# Import the required library ` `library` `(geometry) ` ` ` `# Taking two scalar values ` `a = 5 ` `b = 7 ` ` ` `# Calculating dot product using dot() ` `print` `(` `dot` `(a, b, d = ` `TRUE` `))` |

*chevron_right*

*filter_none*

**Output:**

[1] 35

**Example 2:**

## R

`# R Program illustrating ` `# dot product of two vectors ` ` ` `# Import the required library ` `library` `(geometry) ` ` ` `# Taking two complex values ` `a = 3 + 1i ` `b = 7 + 6i ` ` ` `# Calculating dot product using dot() ` `print` `(` `dot` `(a, b, d = ` `TRUE` `))` |

*chevron_right*

*filter_none*

**Output:**

[1] 15+25i

**Example 3:**

## R

`# R Program illustrating ` `# dot product of two vectors ` ` ` `# Import the required library ` `library` `(geometry) ` ` ` `# Taking two simple vectors ` `a = ` `c` `(1, 4) ` `b = ` `c` `(7, 4) ` ` ` `# Calculating dot product using dot() ` `print` `(` `dot` `(a, b, d = ` `TRUE` `))` |

*chevron_right*

*filter_none*

**Output:**

[1] 23

**Example 4:**

In this following example let’s take two 2D arrays and calculate the dot product of these two. To create a 2D array in R please refer Multidimensional Array in R.

## R

`# R Program illustrating ` `# dot product of two vectors ` ` ` `# Import the required library ` `library` `(geometry) ` ` ` `# Taking two 2D array ` `vector1 = ` `c` `(2, 1) ` `vector2 = ` `c` `(0, 3) ` `a = ` `array` `(` `c` `(vector1, vector2), dim = ` `c` `(2, 2)) ` `vector1 = ` `c` `(4, 2) ` `vector2 = ` `c` `(9, 3) ` `b = ` `array` `(` `c` `(vector1, vector2), dim = ` `c` `(2, 2)) ` ` ` `# Calculating dot product using dot() ` `print` `(` `dot` `(a, b, d = ` `TRUE` `))` |

*chevron_right*

*filter_none*

**Output:**

[1] 10 9

## Recommended Posts:

- Cross Product of Vectors in R Programming
- Create Dot Charts in R Programming - dotchart () Function
- Getting and Setting Length of the Vectors in R Programming - length() Function
- Create a Data Frame of all the Combinations of Vectors passed as Argument in R Programming - expand.grid() Function
- Compute the beta value of Non-Negative Numeric Vectors in R Programming - beta() Function
- Compute the Covariance between Two Vectors in R Programming - cov() Function
- Compute the Correlation Coefficient Value between Two Vectors in R Programming - cor() Function
- Creating a Data Frame from Vectors in R Programming
- Generating sequenced Vectors in R Programming - sequence() Function
- Find positions of Matching Elements between Vectors in R Programming - match() Function
- Generate Color Vectors of desired Length in R Programming - rainbow() Function
- Compute the Parallel Minima and Maxima between Vectors in R Programming - pmin() and pmax() Functions
- Assigning Vectors in R Programming
- Types of Vectors in R Programming
- Append Operation on Vectors in R Programming
- Operations on Vectors in R
- List of Vectors in R
- Create Matrix from Vectors in R
- Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
- Combine Vectors, Matrix or Data Frames by Rows in R Language - rbind() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.