Deep (learning) like Jacques Cousteau – Part 6 – Dot products

(This article was first published on Embracing the Random | R, and kindly contributed to R-bloggers)

(TL;DR: Start with two vectors with equal numbers of elements. Multiply
them element-wise. Sum the results. This is the dot product.)

LaTeX and MathJax warning for those viewing my feed: please view
directly on website!

Kendrick Lamar 2011

Hmmm…this is a tricky one!

Uhhh…did you know that Kendrick Lamar’s stage name used to be “K.Dot”?


we learnt how to add vectors. It’s time to learn about dot products!

Today’s topic: dot products

Let’s define two vectors:

Let’s multiply these vectors element-wise. We’ll take the first
elements of our vectors and multiply them:

Let’s take the second elements and multiply them:

Now add the element-wise products:

This, my friends, is the dot product of our vectors.

More generally, if we have an arbitrary vector of
elements and another arbitrary vector also of
elements, then the dot product is:

The dot product is equivalent to
. Let’s come back to this next time
when we talk about matrix multiplication.

What is that angular ‘E’ looking thing?

For anyone who doesn’t know how to read the dot product equation, let’s
dissect its right-hand side!

is the uppercase form of the Greek letter ‘sigma’. In this
context, means ‘sum’. So we know that we’ll need to add some

We have and . In an earlier
post, we learnt that this refers to the th element of some vector. So
we can refer to the first element of our vector as
. We notice that also shares the same subscript . So
we know that whenever we refer to the second element in (i.e.
), we will be referring to the second element in (i.e.

We notice that is next to . So
we’re going to be multiplying elements of our vectors which occur in the
same position, .

We see that below our uppercase sigma there is a little . We also
notice that there is a little above it. These mean “Let
. Keep incrementing until you reach ”.

What is ? It’s the number of elements in our vectors!

If we expand the right-hand side, we get:

This looks somewhat similar to the equation from the example earlier:

Easy! These are the mechanics of dot products.

What the hell does this all mean anyway?

For a deeper understanding of dot products (which is unfortunately
beyond me right at this moment!) please refer to this video:

The entire series in the playlist is so beautifully done. They are

How can we perform dot products in R?

Let’s define two vectors:

x <- c(1, 2, 3)
y <- c(4, 5, 6)

We can find the dot product of these two vectors using the %*%

x %*% y
##      [,1]
## [1,]   32

What does R do if we simply multiply one vector by the other?

x * y
## [1]  4 10 18

This is the element-wise product! If the dot product is simply the sum
of the element-wise product, then x %*% y is equivalent to doing this:

sum(x * y)
## [1] 32

In our previous posts, R allowed us to multiply vectors of different
lengths. Notice how R doesn’t allow us to calculate the dot product of
vectors with different lengths:

x <- c(1, 2)
y <- c(3, 4, 5)

x %*% y

This is the exception that gets raised:

Error in x %*% y : non-conformable arguments


We have learnt the mechanics of calculating dot products. We can now
finally move onto matrices. Ooooooh yeeeeeah.


To leave a comment for the author, please follow the link and comment on their blog: Embracing the Random | R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)