Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to the fourth lesson in our course on the mathematical foundations of AI.
In our previous lessons, we built up a solid understanding of vector spaces, culminating in the crucial concepts of basis and dimension. We established that a basis provides a unique coordinate system for a vector space, and its size, the dimension, is a fundamental property of that space.
Today, we shift from the objects within these spaces (vectors) to the operators that act upon them: matrices. This lesson will cover the fundamental rules of matrix algebra. Our learning outcome is to perform matrix operations including addition, multiplication, and transposition.
While you may have encountered these operations in your practical work with NumPy, our focus here will be on the formal definitions, their algebraic properties, and the deeper geometric meaning behind them. This theoretical understanding is precisely what's needed to grasp why algorithms in machine learning are structured the way they are.
1. Matrix Addition and Scalar Multiplication
The most straightforward matrix operations are addition and scalar multiplication. They work exactly as you might expect: element by element. For two matrices to be added, they must have the exact same dimensions.
To see the formal definitions and a list of their algebraic properties, please review the following section from a set of linear algebra notes.
MATH 233 - Linear Algebra I Lecture Notes
These lecture notes provide a clear and formal definition of matrix addition and scalar multiplication. They also list the key algebraic properties that govern these operations.
Please read Section 9.1, 'Sums of Matrices' (pages 74-75). Pay close attention to Definition 9.1 and 9.2, and review the properties listed in Theorem 9.4.
As you read, note that these properties (commutativity, associativity, etc.) are very similar to the properties of real number addition and multiplication. This is because these matrix operations are direct extensions of their scalar counterparts.
To summarize the properties from Theorem 9.4:
- Commutativity of Addition:
- Associativity of Addition:
- Distributive Properties: and
2. Matrix Multiplication: The Core Operation
Matrix multiplication is by far the most complex and powerful of these operations. It is not performed element-wise. The reason for its specific definition is that it represents something profound: the composition of linear transformations.
The Geometric "Why": Composition of Transformations
Before we dive into the formula, it's essential to understand what matrix multiplication does. In the next lesson, we will formally define linear transformations, which are functions that move vectors around in space (e.g., rotations, shears, projections). A matrix is a way to encode such a transformation.
Multiplying two matrices, and , to get a new matrix , corresponds to finding a single transformation that has the same effect as applying transformation first, and then applying transformation .
This video provides an outstanding geometric intuition for this concept.
Matrix multiplication as composition | Chapter 4, Essence of linear algebra
From the 'Essence of linear algebra' series by 3Blue1Brown, this video masterfully explains matrix multiplication as the composition of two successive linear transformations.
Watch the video from the beginning to 07:04. Focus on these key ideas: A quick recap of how a matrix represents a transformation by showing where the basis vectors land. The central idea: matrix multiplication is about finding a single matrix that represents the total effect of applying one transformation after another. How the columns of the product matrix are computed by tracking where the basis vectors land after being put through both transformations.
The Formal Definition and Computational Rule
The video provides the intuition. Now, let's formalize it. The notes from MATH 233 define matrix multiplication in exactly this way—as the standard matrix of a composite mapping.
MATH 233 - Linear Algebra I Lecture Notes
This section formalizes the geometric intuition from the video. It defines the product AB as the matrix corresponding to the composition of the linear maps represented by A and B.
Read the first part of Section 9.2 'Matrix Multiplication' (pages 75-76), up to Definition 9.5. This connects the abstract idea of composing mappings (TA ◦ TB) to the concrete definition of the matrix product AB.
This definition leads to the standard computational rule you may have seen before: the entry in the -th row and -th column of the product is the dot product of the -th row of and the -th column of .
Let be an matrix and be an matrix. Their product will be an matrix, where each element is given by:
Notice that for the product to be defined, the number of columns in must match the number of rows in .
Properties of Matrix Multiplication
Matrix multiplication has several important properties, some of which are non-intuitive.
1. Non-Commutativity: Order Matters!
In general, . Applying a rotation then a shear is not the same as applying a shear then a rotation. The 3Blue1Brown video explains this geometrically.
Matrix multiplication as composition | Chapter 4, Essence of linear algebra
This short segment provides a clear visual demonstration of why changing the order of matrix multiplication leads to a different result.
Watch the segment from 07:28 to 08:21.
2. Associativity:
While the order of matrices cannot be changed, the order of operations doesn't matter. Thinking in terms of transformations makes this property obvious: applying C, then B, then A is the same regardless of how you group the operations.
Matrix multiplication as composition | Chapter 4, Essence of linear algebra
Here, 3Blue1Brown provides a simple, yet profound, intuitive proof for the associativity of matrix multiplication by framing it in terms of transformations.
Watch from 08:21 to 09:38. This is a perfect example of how thinking geometrically can make a property that is algebraically tedious to prove become self-evident.
Other key properties include distributivity over addition () and the existence of an identity matrix such that . These are summarized in Theorem 9.8 of the MATH 233 notes.
Test your understanding!
Let and .
- Compute .
- Compute .
- What do these results tell you about matrix multiplication? (Can you interpret matrix B geometrically?)
Show answer
-
Compute AB:
-
Compute BA:
-
Interpretation:
Clearly, , confirming that matrix multiplication is not commutative.Geometrically, matrix swaps the basis vectors and . Applying it to a vector results in — it reflects the vector across the line .
- represents first swapping the columns of .
- represents first swapping the rows of .
These are different operations, leading to different results.
3. The Matrix Transpose
The final fundamental operation is the transpose, which flips a matrix along its main diagonal, turning its rows into columns and vice-versa. We denote the transpose of as .
Definition and Basic Properties
If is an matrix, then is an matrix where .
Properties of the transpose of a matrix, linear algebra tutorial
This video by blackpenredpen provides a clear definition of the transpose and lists its main properties.
Watch from the beginning to 06:26. Pay attention to the definition, the notation for entries, and the list of properties presented.
The simple properties are:
The Transpose of a Product: A Crucial Identity
The most important and non-obvious property concerns the transpose of a product:
Note the reversal of order. This identity is extremely important and appears constantly in machine learning derivations, such as the normal equations for linear regression. Given your goal of understanding the theory with mathematical rigor, watching its proof is essential.
Properties of the transpose of a matrix, linear algebra tutorial
The remainder of the blackpenredpen video is dedicated to a formal, step-by-step proof of the identity (AB)ᵀ = BᵀAᵀ. This is a perfect example of the kind of rigorous derivation you're looking for.
Watch from 06:26 to the end. Follow the logic as it uses the summation definition of matrix multiplication to show that the (i, j)-th entry of the left side is equal to the (i, j)-th entry of the right side.
This proof, which manipulates indices and summations, is a great example of the mathematical machinery used to formalize linear algebra.
Conclusion
In this lesson, we have formally defined the fundamental operations of matrix algebra. More importantly, we've explored the "why" behind them.
Key Takeaways:
- Matrix Addition and Scalar Multiplication are element-wise operations that follow familiar algebraic rules like commutativity and associativity.
- Matrix Multiplication is defined to represent the composition of linear transformations. Its row-by-column computational rule is a consequence of this definition.
- Matrix multiplication is associative but not commutative. The order of application matters.
- The Transpose of a matrix flips its rows and columns.
- A critical identity to remember and understand is the transpose of a product: , where the order of the matrices is reversed.
The concept of matrix multiplication as a representation of composite transformations serves as a direct bridge to our next topic. In the upcoming lesson, we will formally define linear transformations, explore their properties, and solidify the connection between the abstract transformation and its concrete matrix representation.