Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Dot Products and Matrix Multiplication

Recommended resources

Welcome back to our course on computer vision!

In our first lesson, you learned how to represent data using vectors and matrices and how to perform basic element-wise operations like addition and scalar multiplication. These are the fundamental data structures of linear algebra.

Today, we move on to two of the most powerful and consequential operations: the dot product and matrix multiplication. This lesson will allow you to compute the dot product of two vectors and the product of two matrices, and interpret their meaning. Unlike the operations you've learned so far, these are not simple element-wise calculations. Their definitions are more complex but unlock a much deeper understanding of the relationships between data points and the transformations that can be applied to them. These operations are the computational workhorses behind nearly every machine learning model.

This lesson is divided into two parts, each designed to fit into a 30-minute study session.

1. The Vector Dot Product: Measuring Similarity

We know how to add and scale vectors, but how do we quantify the relationship between two vectors? For example, do they point in similar directions? Are they perpendicular? The dot product provides the answer.

Algebraic and Geometric Definitions

The dot product has two equivalent definitions.

1. Algebraic Definition:
The dot product (also called the inner product) of two vectors and in is the sum of the products of their corresponding components. The result is a single number (a scalar).

For example, if and , their dot product is:
.

2. Geometric Definition:
Geometrically, the dot product is defined as the product of the magnitudes (lengths) of the two vectors and the cosine of the angle between them.

Geometric Interpretation of the Dot Product
This image illustrates the geometric formula for the dot product. The value of the dot product depends on the vectors' lengths (magnitudes) and the angle \(\theta\) between them.

Interpreting the Dot Product

This geometric formula is key to interpretation. The value of tells us about the alignment of the vectors:

  • If , the vectors are parallel and point in the same direction. , so . The dot product is at its maximum positive value.
  • If , the vectors are orthogonal (perpendicular). , so . A zero dot product is a crucial indicator of orthogonality.
  • If , the vectors are parallel and point in opposite directions. , so . The dot product is at its maximum negative value.

In short, the dot product measures the "similarity" or "agreement" between two vectors. A large positive value means they are long and point in a similar direction, while a large negative value means they are long and point in opposite directions.

Another powerful interpretation is that of projection. The term is the length of the orthogonal projection of vector onto vector . So, the dot product can be seen as the length of the projection of onto , scaled by the length of .

To dive deeper into these interpretations, and to see the derivation that connects the algebraic and geometric definitions (which I think you'll find interesting given your preference for first principles), let's turn to a curated discussion.

What does the dot product of two vectors represent?

The following resource from Stack Exchange features several excellent explanations of the dot product. The top answers provide intuitive geometric interpretations, while one answer provides a full mathematical derivation.

First, read the top answer under 'Geometric Meaning' (starts with 'As other answers have pointed out...') to solidify the concept of projection. Also read the short answer that begins 'I think of dot product as the "same-ness" of two vectors.' Then, focus on the section titled 'Derivation'. This is the most important part for you. It rigorously proves that the algebraic definition (sum of products) is equivalent to the geometric one. Follow the steps that use the Law of Cosines (via the Pythagorean theorem) on a triangle formed by vectors.

The derivation you just read is a beautiful piece of mathematics. It shows that the simple algebraic operation of inherently contains geometric information about the angle between the vectors, all without ever explicitly calculating an angle.

The Dot Product in Python

In NumPy, the @ operator is used for dot products (and matrix multiplication, as we'll see).

import numpy as np

u = np.array([1, 2, 3])
v = np.array([4, -5, 6])

# Calculate the dot product
dot_product = u @ v  # or np.dot(u, v)
print(f"The dot product of u and v is: {dot_product}")
Test your understanding!

Consider the vectors and .

  1. Calculate their dot product .
  2. Calculate the magnitude of each vector, and . (Hint: The magnitude is , which is also ).
  3. Using the results from (1) and (2), find the angle between them.
Show answer
  1. Dot Product:

  2. Magnitudes:

  3. Angle: We use the formula .


    The angle for which is or radians.



2. Matrix Multiplication: Composing Transformations

Just as the dot product was not an element-wise operation, neither is matrix multiplication. Its definition is specifically designed to represent the composition of linear transformations.

Imagine a vector . Applying a matrix to it gives a new vector . If we then apply another matrix to , we get a final vector . Matrix multiplication defines a single matrix that accomplishes this entire two-step transformation in one go: .

Matrix-Vector Multiplication

Let's start with multiplying a matrix (with rows, columns) by a vector (with elements). The result is a new vector with elements. There are two vital ways to view this:

  1. Row View: Each element of the output vector is the dot product of the corresponding row of matrix with the vector .
  2. Column View: The output vector is a linear combination of the columns of matrix , where the weights are the elements of vector .

The row view is useful for computation, but the column view is arguably more powerful for interpretation, as it shows how the matrix transforms the input vector into an output vector within the "space" defined by its columns.

Matrix-Matrix Multiplication

Now we can extend this to multiplying two matrices, () and (). The product will be an matrix.

Rule: The element at row and column of the product matrix is the dot product of the -th row of and the -th column of .

Notice the dimension constraint: For the product to be defined, the number of columns in must match the number of rows in .

To solidify these concepts, let's read a clear, geometrically-motivated explanation.

A Geometric Review of Linear Algebra

The following text, 'A Geometric Review of Linear Algebra', explains matrix multiplication from the perspective of composing linear systems. This matches the deep, conceptual understanding you prefer.

Please read the section 'Linear Systems & Matrices' (page 5). Focus on the two interpretations of matrix-vector multiplication (dot product of rows vs. linear combination of columns) and how matrix-matrix multiplication is defined as the 'sequential application of two linear systems'.

Properties and Python Implementation

One of the most important properties of matrix multiplication is that it is not commutative: in general, . The order matters, just as the order of applying transformations matters.

In NumPy, the @ operator also performs matrix multiplication.

A = np.array([[1, 2],
              [3, 4]])

B = np.array([[5, 6],
              [7, 8]])

# Matrix Multiplication
C = A @ B
print("A @ B =\n", C)

# Let's check the first element C[0, 0] manually
# It's the dot product of row 0 of A and column 0 of B
# C[0, 0] = (1 * 5) + (2 * 7) = 5 + 14 = 19
# This matches the output.

# Let's check non-commutativity
D = B @ A
print("\nB @ A =\n", D)
# As you can see, A @ B is not the same as B @ A.
Test your understanding!

Given the matrices and .

Calculate the product by hand.

Show answer

The resulting matrix will be a 2x2 matrix.

  • = (row 1 of ) (col 1 of ) =
  • = (row 1 of ) (col 2 of ) =
  • = (row 2 of ) (col 2 of ) =
  • = (row 2 of ) (col 2 of ) =

So, the final matrix is:


Conclusion

Fantastic work today! You've tackled two of the most fundamental operations in linear algebra, moving beyond simple element-wise arithmetic into more abstract and powerful concepts.

Here are the key takeaways:

  • The dot product of two vectors can be computed algebraically () or geometrically (). It measures the similarity or alignment between vectors and is zero for orthogonal vectors.
  • Matrix-vector multiplication can be viewed as applying dot products between rows and the vector, or as creating a linear combination of the matrix's columns.
  • Matrix-matrix multiplication is defined by dot products between the rows of the first matrix and columns of the second. It represents the composition of linear transformations and is not commutative.

Preview of the Next Lesson:
Now that we understand how matrices transform vectors, a natural question arises: can we "undo" a transformation? This leads to the concepts of the determinant and inverse of a matrix. You will learn how to calculate these values and understand what they tell us about the properties of a matrix and the transformation it represents.