Skip to content
Home » numpy Part 14 – The dot product

numpy Part 14 – The dot product

Spread the love

Today we’ll be talking about the dot function from the numpy module which is used to calculate the dot product.

The dot function returns a scalar or an array. It returns a scalar if we pass two scalars as arguments:

>>> print(np.dot(5, 3))
15

It also returns a scalar if we pass two 1-dimensional arrays as arguments:

>>> a = np.array([2, 4, 6])
>>> b = np.array([-1, 2, 3])
>>> print(np.dot(a, b))
24

This is how the dot product is calculated:

dot product

For 2-dimensional arrays the dot function returns the result of matrix multiplication. Just a quick reminder: We can only multiply two matrices if the first of them has the same number of columns as the number of rows of the other matrix. So, for example, we can multiply these two matrices:

A = [[4, 2, 3, 8],
     [2, 1, 3, 5],
     [1, 1, 2, 6]]

B = [[1, 3],
     [2, 2],
     [8, 2],
     [5, 3]]

because A has 4 columns and  has 4 rows.

Have a look:

Let’s create nested lists and convert them to arrays:

>>> a = [[4, 2, 3, 8],
...      [2, 1, 3, 5],
...      [1, 1, 2, 6]]
... 
>>> b = [[1, 3],
...      [2, 2],
...      [8, 2],
...      [5, 3]]
... 
>>> A = np.array(a)
>>> B = np.array(b)
>>> print(A)
[[4 2 3 8]
 [2 1 3 5]
 [1 1 2 6]]
>>> print(B)
[[1 3]
 [2 2]
 [8 2]
 [5 3]]

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

We can only multiply two matrices if the number of columns of the first matrix is equal to the number of rows of the second matrix. Or, more generally, the shape of the last dimension of the first matrix must be the same as the last but one dimension of the second matrix. In our case the two shapes are equal:

>>> A.shape[-1] == B.shape[-2]
True

So we can multiply these two matrices. Let’s use the dot function:

>>> print(np.dot(A, B))
[[72 46]
 [53 29]
 [49 27]]

If you don’t remember how matrices are multiplied, here’s a visualization of our example:

multiplication of matrices

If the number of columns of the first array is different than the number of rows of the second array, we’ll get an error, because it’s impossible to multiply such two matrices.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of this article:


Spread the love
Tags:

Leave a Reply