How to Calculate the Scalar Product in R

Scalar product, sometimes known as the dot product, is an algebraic operation that returns a single integer from two numbers of equal length.

Let’s say we have two vectors x and y and we need to get the dot product of the two.

Scalar Product

Given that,

x = [x1, x2, x3] When vector y = [y1, y2, y3], the dot product of vectors x and y, abbreviated as x.y, is calculated as follows:

x · y = x1 * y1 + x2 * y2 + x3 * y3

For example, if x = [12, 15, 16] and y = [14, 13, 12], then the dot product of x and y would be equal to:

x. y = 12*14 + 15*13 + 16*12

x·y = 168 + 195 + 192

x ·y = 555

The sum of the products of the corresponding entries in two vectors is the dot product/scalar product.

In R, how do you calculate the dot product?

In R, there are two simple techniques to calculate the dot product of two vectors.

Approach 1: Use %*%

The following code demonstrates how to create the dot product between two vectors in R using the percent * percent function:

Let’s create a two vectors

a <- c(12, 15, 16)
b <- c(14, 13, 12)

Now we calculate the dot product between the above vectors

a %*% b
    [,1]
[1,]  555

The result of the dot product is 555.

This function is also applicable to data frame columns:

The above data we can assign to “data” as a data frame.

data<- data.frame(a=c(12, 15, 16), b=c(14, 13, 12))

To calculate the dot product between columns ‘a’ and ‘b’ of a data frame, we can use the same percent * percent function.

data$a %*% data$b
     [,1]
[1,]   555

Approach 2: Use the dot() function

The dot() method from the pracma library can also be used to calculate the dot product between two vectors.

library(pracma)

We can use the same vectors mentioned in approach1.

a <- c(12, 15, 16)
b <- c(14, 13, 12)

Now we can calculate the dot product between vectors based on dot function.

dot(a, b)
[1] 555

The dot product of the two vectors comes out to be 555 once again.

Approach 3: Use the dot() function

R provides a very efficient way of computing the dot product of two vectors. This may be accomplished by utilizing the dot() method from the geometry library.

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

Parameters:

x: Matrix of vectors

y: Matrix of vectors

d: The dimension along which the dot product is calculated

library(geometry)
dot(a, b, d = TRUE)
[1] 555

The answer is the same as in approaches 1 and 2.

Approach 4: Use the sum() function

a <- c(12, 15, 16)
b <- c(14, 13, 12)
sum(a*b)
[1] 555

Yes, simple and powerful method.

Approach 5: Use own function

Let’s create the function and calculate the dot product.

mydot <- function(x, y){   # x and y can be vectors or matrices
  result <- t(x)%*%y   # %*% is the matrix multiplication operator
  print(result)        # t(x) denotes the transpose of x
}
mydot(a,b)
    [,1]
[1,]  555

In all the approaches the answer is the same.

Binomial Distribution in R-Quick Guide »

Subscribe to our newsletter!

[newsletter_form type=”minimal”]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

2 × 4 =