Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Multiplying Matrices and Vectors

We will see some very important concepts in this chapter. The dot product is used in every equation explaining data science algorithms so it’s worth the effort to understand it. Then we will see some properties of this operation. Finally, we will to get some intuition on the link between matrices and systems of linear equations.

Source

The standard way to multiply matrices is not to multiply each element of one with each elements of the other (this is the element-wise product) but to calculate the sum of the products between rows and columns. The matrix product, also called dot product, is calculated as following:

An example of how to calculate the dot product between a matrix and a vector

The dot product between a matrix and a vector

The number of columns of the first matrix must be equal to the number of rows of the second matrix. Thus, if the dimensions, or the shape of the first matrix, is (m×nm \times n) the second matrix need to be of shape (n×xn \times x). The resulting matrix will have the shape (m×xm \times x).

Example 1.

As a starter we will see the multiplication of a matrix and a vector.

A×b=C\boldsymbol{A} \times \boldsymbol{b} = \boldsymbol{C}

with $ \boldsymbol{A}=

[123456]\begin{bmatrix} 1 & 2\\\\ 3 & 4\\\\ 5 & 6 \end{bmatrix}

and and \boldsymbol{b}=\begin{bmatrix} 2\\ 4 \end{bmatrix}$. We saw that the formula is the following:

[A1,1A1,2A2,1A2,2A3,1A3,2]×[B1,1B2,1]=[A1,1B1,1+A1,2B2,1A2,1B1,1+A2,2B2,1A3,1B1,1+A3,2B2,1]\begin{align*} &\begin{bmatrix} A_{1,1} & A_{1,2} \\\\ A_{2,1} & A_{2,2} \\\\ A_{3,1} & A_{3,2} \end{bmatrix}\times \begin{bmatrix} B_{1,1} \\\\ B_{2,1} \end{bmatrix}=\\\\ &\begin{bmatrix} A_{1,1}B_{1,1} + A_{1,2}B_{2,1} \\\\ A_{2,1}B_{1,1} + A_{2,2}B_{2,1} \\\\ A_{3,1}B_{1,1} + A_{3,2}B_{2,1} \end{bmatrix} \end{align*}

So we will have:

[123456]×[24]=[1×2+2×43×2+4×45×2+6×4]=[102234]\begin{align*} &\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\\\ 5 & 6 \end{bmatrix}\times \begin{bmatrix} 2 \\\\ 4 \end{bmatrix}=\\\\ &\begin{bmatrix} 1 \times 2 + 2 \times 4 \\\\ 3 \times 2 + 4 \times 4 \\\\ 5 \times 2 + 6 \times 4 \end{bmatrix}= \begin{bmatrix} 10 \\\\ 22 \\\\ 34 \end{bmatrix} \end{align*}

It is a good habit to check the dimensions of the matrix so see what is going on. We can see in this example that the shape of A\boldsymbol{A} is (3×23 \times 2) and the shape of b\boldsymbol{b} is (2×12 \times 1). So the dimensions of C\boldsymbol{C} are (3×13 \times 1).

With Numpy

The Numpy function dot() can be used to compute the matrix product (or dot product). Let’s try to reproduce the last exemple:

array([[1, 2], [3, 4], [5, 6]])
array([[2], [4]])
array([[10], [22], [34]])

It is equivalent to use the method dot() of Numpy arrays:

array([[10], [22], [34]])

And also with @ operator:

array([[10], [22], [34]])

Example 2.

Multiplication of two matrices.

A×B=C\boldsymbol{A} \times \boldsymbol{B} = \boldsymbol{C}

with:

A=[123456789101112]\boldsymbol{A}=\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\\\ 10 & 11 & 12 \end{bmatrix}

and:

B=[271236]\boldsymbol{B}=\begin{bmatrix} 2 & 7 \\\\ 1 & 2 \\\\ 3 & 6 \end{bmatrix}

So we have:

[123456789101112]×[271236]=[2×1+1×2+3×37×1+2×2+6×32×4+1×5+3×67×4+2×5+6×62×7+1×8+3×97×7+2×8+6×92×10+1×11+3×127×10+2×11+6×12]=[132931744911967164]\begin{align*} &\begin{bmatrix} 1 & 2 & 3 \\\\ 4 & 5 & 6 \\\\ 7 & 8 & 9 \\\\ 10 & 11 & 12 \end{bmatrix}\times \begin{bmatrix} 2 & 7 \\\\ 1 & 2 \\\\ 3 & 6 \end{bmatrix}=\\\\ &\begin{bmatrix} 2 \times 1 + 1 \times 2 + 3 \times 3 & 7 \times 1 + 2 \times 2 + 6 \times 3 \\\\ 2 \times 4 + 1 \times 5 + 3 \times 6 & 7 \times 4 + 2 \times 5 + 6 \times 6 \\\\ 2 \times 7 + 1 \times 8 + 3 \times 9 & 7 \times 7 + 2 \times 8 + 6 \times 9 \\\\ 2 \times 10 + 1 \times 11 + 3 \times 12 & 7 \times 10 + 2 \times 11 + 6 \times 12 \\\\ \end{bmatrix}\\\\ &= \begin{bmatrix} 13 & 29 \\\\ 31 & 74 \\\\ 49 & 119 \\\\ 67 & 164 \end{bmatrix} \end{align*}

Let’s check the result with Numpy:

array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]])
array([[2, 7], [1, 2], [3, 6]])
array([[ 13, 29], [ 31, 74], [ 49, 119], [ 67, 164]])

It works!

Formalization of the dot product

Ci,j=Ai,kBk,j=∑kAi,kBk,jC_{i,j} = A_{i,k}B_{k,j} = \sum_{k}A_{i,k}B_{k,j}

You can find more examples about the dot product here.

Properties of the dot product

We will now see some interesting properties of the matrix multiplication. It will become useful as we move forward in the chapters. Using simple examples for each property will provide a way to check them while we get used to the Numpy functions.

Matrices mutliplication is distributive

A(B+C)=AB+AC\boldsymbol{A}(\boldsymbol{B}+\boldsymbol{C}) = \boldsymbol{AB}+\boldsymbol{AC}

Example 3.

A=[231476],B=[52],C=[43]\boldsymbol{A}=\begin{bmatrix} 2 & 3 \\\\ 1 & 4 \\\\ 7 & 6 \end{bmatrix}, \boldsymbol{B}=\begin{bmatrix} 5 \\\\ 2 \end{bmatrix}, \boldsymbol{C}=\begin{bmatrix} 4 \\\\ 3 \end{bmatrix}
A(B+C)=[231476]×([52]+[43])=[231476]×[95]=[2×9+3×51×9+4×57×9+6×5]=[332993]\begin{align*} \boldsymbol{A}(\boldsymbol{B}+\boldsymbol{C})&=\begin{bmatrix} 2 & 3 \\\\ 1 & 4 \\\\ 7 & 6 \end{bmatrix}\times \left(\begin{bmatrix} 5 \\\\ 2 \end{bmatrix}+ \begin{bmatrix} 4 \\\\ 3 \end{bmatrix}\right)= \begin{bmatrix} 2 & 3 \\\\ 1 & 4 \\\\ 7 & 6 \end{bmatrix}\times \begin{bmatrix} 9 \\\\ 5 \end{bmatrix}\\\\ &= \begin{bmatrix} 2 \times 9 + 3 \times 5 \\\\ 1 \times 9 + 4 \times 5 \\\\ 7 \times 9 + 6 \times 5 \end{bmatrix}= \begin{bmatrix} 33 \\\\ 29 \\\\ 93 \end{bmatrix} \end{align*}

is equivalent to

AB+AC=[231476]×[52]+[231476]×[43]=[2×5+3×21×5+4×27×5+6×2]+[2×4+3×31×4+4×37×4+6×3]=[161347]+[171646]=[332993]\begin{align*} \boldsymbol{A}\boldsymbol{B}+\boldsymbol{A}\boldsymbol{C} &= \begin{bmatrix} 2 & 3 \\\\ 1 & 4 \\\\ 7 & 6 \end{bmatrix}\times \begin{bmatrix} 5 \\\\ 2 \end{bmatrix}+ \begin{bmatrix} 2 & 3 \\\\ 1 & 4 \\\\ 7 & 6 \end{bmatrix}\times \begin{bmatrix} 4 \\\\ 3 \end{bmatrix}\\\\ &= \begin{bmatrix} 2 \times 5 + 3 \times 2 \\\\ 1 \times 5 + 4 \times 2 \\\\ 7 \times 5 + 6 \times 2 \end{bmatrix}+ \begin{bmatrix} 2 \times 4 + 3 \times 3 \\\\ 1 \times 4 + 4 \times 3 \\\\ 7 \times 4 + 6 \times 3 \end{bmatrix}\\\\ &= \begin{bmatrix} 16 \\\\ 13 \\\\ 47 \end{bmatrix}+ \begin{bmatrix} 17 \\\\ 16 \\\\ 46 \end{bmatrix}= \begin{bmatrix} 33 \\\\ 29 \\\\ 93 \end{bmatrix} \end{align*}
array([[2, 3], [1, 4], [7, 6]])
array([[5], [2]])
array([[4], [3]])

A(B+C)\boldsymbol{A}(\boldsymbol{B}+\boldsymbol{C}):

array([[33], [29], [93]])

is equivalent to AB+AC\boldsymbol{AB}+\boldsymbol{AC}:

array([[33], [29], [93]])

Matrices mutliplication is associative

A(BC)=(AB)C\boldsymbol{A}(\boldsymbol{BC}) = (\boldsymbol{AB})\boldsymbol{C}
array([[2, 3], [1, 4], [7, 6]])
array([[5, 3], [2, 2]])

A(BC)\boldsymbol{A}(\boldsymbol{BC}):

array([[100], [ 85], [287]])

is equivalent to (AB)C(\boldsymbol{AB})\boldsymbol{C}:

array([[100], [ 85], [287]])

Matrix multiplication is not commutative

AB≠BA\boldsymbol{AB} \neq \boldsymbol{BA}
array([[2, 3], [6, 5]])
array([[5, 3], [2, 2]])

AB\boldsymbol{AB}:

array([[16, 12], [40, 28]])

is different from BA\boldsymbol{BA}:

array([[28, 30], [16, 16]])

However vector multiplication is commutative

xTy=yTx\boldsymbol{x^{ \text{T}}y} = \boldsymbol{y^{\text{T}}x}
array([[2], [6]])
array([[5], [2]])

xTy\boldsymbol{x^\text{T}y}:

array([[22]])

is equivalent to yTx\boldsymbol{y^\text{T}x}:

array([[22]])

Simplification of the matrix product

(AB)T=BTAT(\boldsymbol{AB})^{\text{T}} = \boldsymbol{B}^\text{T}\boldsymbol{A}^\text{T}
array([[2, 3], [1, 4], [7, 6]])
array([[5, 3], [2, 2]])

(AB)T(\boldsymbol{AB})^{\text{T}}:

array([[16, 13, 47], [12, 11, 33]])

is equivalent to BTAT\boldsymbol{B}^\text{T}\boldsymbol{A}^\text{T}:

array([[16, 13, 47], [12, 11, 33]])

System of linear equations

This is an important part of why linear algebra can be very useful to solve variety of problems. Here we will see that it can be use to represent system of equations.

A system of equations is a set of multiple equations (at least 1). For instance we could have:

{y=2x+1y=72x+3\begin{cases} y = 2x + 1 \\\\ y = \frac{7}{2}x +3 \end{cases}

A system of equations is defined by its number of equations and its number of unknowns. In our example above, the system has 2 equations and 2 unknowns (xx and yy). In addition we call this a system of linear equations because each equations is linear. It is easy to see that in 2 dimensions: we will have one straight line per equation and the dimensions are the unknowns. Here is the plot of the first one:

Representation of a line from an equation

Representation of a linear equation

In our system of equations, the unknowns are the dimensions and the number of equations is the number of lines (in 2D) or $n$-dimensional planes.

Using matrices to describe the system

Matrices can be used to describe a system of linear equations of the form Ax=b\boldsymbol{Ax}=\boldsymbol{b}. Here is such a system:

A1,1x1+A1,2x2+A1,nxn=b1A2,1x1+A2,2x2+A2,nxn=b2⋯Am,1x1+Am,2x2+Am,nxn=bnA_{1,1}x_1 + A_{1,2}x_2 + A_{1,n}x_n = b_1 \\\\ A_{2,1}x_1 + A_{2,2}x_2 + A_{2,n}x_n = b_2 \\\\ \cdots \\\\ A_{m,1}x_1 + A_{m,2}x_2 + A_{m,n}x_n = b_n

The unknowns (what we want to find to solve the system) are the variables x1x_1 and x2x_2 corresponding to the previous variables xx and yy. It is exactly the same form as with the last example but with all the variables on the same side. y=2x+1y = 2x + 1 becomes −2x+y=1-2x + y = 1 with xx corresponding to x1x_1 and yy corresponding to x2x_2. We will have nn unknowns and mm equations.

The variables are named x1,x2,⋯ ,xnx_1, x_2, \cdots, x_n by convention because we will see that it can be summarised in the vector x\boldsymbol{x}.

Left hand side

The left hand term can considered as the product of a matrix A\boldsymbol{A} containing weights for each variable (nn columns) and each equation (mm rows):

A=[A1,1A1,2⋯A1,nA2,1A2,2⋯A2,n⋯⋯⋯⋯Am,1Am,2⋯Am,n]\boldsymbol{A}= \begin{bmatrix} A_{1,1} & A_{1,2} & \cdots & A_{1,n} \\\\ A_{2,1} & A_{2,2} & \cdots & A_{2,n} \\\\ \cdots & \cdots & \cdots & \cdots \\\\ A_{m,1} & A_{m,2} & \cdots & A_{m,n} \end{bmatrix}

with a vector x\boldsymbol{x} containing the nn unknowns

x=[x1x2⋯xn]\boldsymbol{x}= \begin{bmatrix} x_1 \\\\ x_2 \\\\ \cdots \\\\ x_n \end{bmatrix}

The dot product of A\boldsymbol{A} and x\boldsymbol{x} gives a set of equations. Here is a simple example:

Matrix form of a system of linear equation

Matrix form of a system of linear equations

We have a set of two equations with two unknowns. So the number of rows of A\boldsymbol{A} gives the number of equations and the number of columns gives the number of unknowns.

Both sides

The equation system can be wrote like that:

[A1,1A1,2⋯A1,nA2,1A2,2⋯A2,n⋯⋯⋯⋯Am,1Am,2⋯Am,n]×[x1x2⋯xn]=[b1b2⋯bm]\begin{bmatrix} A_{1,1} & A_{1,2} & \cdots & A_{1,n} \\\\ A_{2,1} & A_{2,2} & \cdots & A_{2,n} \\\\ \cdots & \cdots & \cdots & \cdots \\\\ A_{m,1} & A_{m,2} & \cdots & A_{m,n} \end{bmatrix} \times \begin{bmatrix} x_1 \\\\ x_2 \\\\ \cdots \\\\ x_n \end{bmatrix} = \begin{bmatrix} b_1 \\\\ b_2 \\\\ \cdots \\\\ b_m \end{bmatrix}

Or simply:

Ax=b\boldsymbol{Ax}=\boldsymbol{b}

Example 4.

We will try to convert the common form of a linear equation y=ax+by=ax+b to the matrix form. If we want to keep the previous notation we will have instead:

x2=ax1+bx_2=ax_1+b

Don’t confuse the variable x1x_1 and x2x_2 with the vector x\boldsymbol{x}. This vector contains actually all the variables of our equations. Here we have:

x=[x1x2]\boldsymbol{x} = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}

In this example we will use the following equation:

x2=2x1+1⇔2x1−x2=−1\begin{align*} &x_2=2x_1+1\\\\ \Leftrightarrow& 2x_1-x_2=-1 \end{align*}

In order to end up with this system when we multiply A\boldsymbol{A} and x\boldsymbol{x} we need A\boldsymbol{A} to be a matrix containing the weights of each variable. The weight of x1x_1 is 2 and the weights of x2x_2 is -1:

A=[2−1]\boldsymbol{A}= \begin{bmatrix} 2 & -1 \end{bmatrix}

So we have

[2−1][x1x2]=[2x1−1x2]\begin{bmatrix} 2 & -1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} 2x_1-1x_2 \end{bmatrix}

To complete the equation we have

b=[−1]\boldsymbol{b}= \begin{bmatrix} -1 \end{bmatrix}

which gives

[2−1][x1x2]=[−1]\begin{bmatrix} 2 & -1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} -1 \end{bmatrix}

This system of equations is thus very simple and contains only 1 equation (A\boldsymbol{A} has 1 row) and 2 variables (A\boldsymbol{A} has 2 columns).

To summarise, A\boldsymbol{A} will be the matrix of dimensions m×nm\times n containing scalars multiplying these variables (here x1x_1 is multiplied by 2 and x2x_2 by -1). The vector x\boldsymbol{x} contains the variables x1x_1 and x2x_2. And the right hand term is the constant b\boldsymbol{b}:

A=[2−1]\boldsymbol{A}= \begin{bmatrix} 2 & -1 \end{bmatrix}
x=[x1x2]\boldsymbol{x}= \begin{bmatrix} x_1\\\\ x_2 \end{bmatrix}
b=[−1]\boldsymbol{b}= \begin{bmatrix} -1 \end{bmatrix}

We can write this system

Ax=b\boldsymbol{Ax}=\boldsymbol{b}

We will see at the end of the next chapter that this compact way of writing sets of linear equations can be very usefull. It provides actually a way to solve the equations.