Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

11.2: Matrix Multiply

( \newcommand{\kernel}{\mathrm{null}\,}\)

Read Section 10.1 of the Stephen Boyd and Lieven Vandenberghe Applied Linear algebra book which covers Matrix Multiplication. Here is a quick review:

Two matrices A and B can be multiplied together if and only if their “inner dimensions” are the same, i.e. A is n×d and B is d×m (note that the columns of A and the rows of B are both d). Multiplication of these two matrices results in a third matrix C with the dimension of n×m. Note that C has the same first dimension as A and the same second dimension as B. i.e n×m.

The (i,j) element in C is the dot product of the ith row of A and the jth column of B.

The ith row of A is:

[ai1,ai2,,aid],

and the jth column of B is:

[b1jb2jbdj]

So, the dot product of these two vectors is:

cij=ai1b1j+ai2b2j++aidbdj

Consider the simple 2×2 example below:

[abcd][wxyz]=[aw+byax+bzcw+dycx+dz]

Let’s do an example using numpy and show the results using sympy:

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True) # Trick to make matrixes look nice in jupyter
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True) # Trick to make matrixes look nice in jupyter

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

A = np.matrix([[1,1], [2,2]])
sym.Matrix(A)
# 'Run' this cell to see the output
A = np.matrix([[1,1], [2,2]])
sym.Matrix(A)
# 'Run' this cell to see the output

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

B = np.matrix([[3,4], [3,4]])
sym.Matrix(B)
# 'Run' this cell to see the output
B = np.matrix([[3,4], [3,4]])
sym.Matrix(B)
# 'Run' this cell to see the output

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

sym.Matrix(A*B)
# 'Run' this cell to see the output
sym.Matrix(A*B)
# 'Run' this cell to see the output
Do This

Given two matrices; A and B, show that order matters when doing a matrix multiply. That is, in general, ABBA. Show this with an example using two 3×3 matrices and numpy.

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

# Put your code here.
# Put your code here.

Now consider the following set of linear equations:

3x13x2+9x3= 24

2x12x2+7x3= 17

x1+2x24x3=11

We typically write this in the following form:

[339227124][x1x2x3]=[241711]

Notice how doing the matrix multiplication results back into the original system of equations. If we rename the three matrices from above to A, x, and b (note x and b are lowercase because they are column vectors) then we get the main equation for this class, which is:

Ax=b

Note the information about the equation doesn’t change when you change formats. For example, the equation format, the augmented format and the Ax=b format contain the same information. However, we use the different formats for different applications. Consider the numpy.linalg.solve function which assumes the format Ax=b

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

A = np.matrix([[3, -3,9], [2, -2, 7], [-1, 2, -4]])
sym.Matrix(A)
# 'Run' this cell to see the output
A = np.matrix([[3, -3,9], [2, -2, 7], [-1, 2, -4]])
sym.Matrix(A)
# 'Run' this cell to see the output

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

b = np.matrix([[24], [17], [-11]])
sym.Matrix(b)
# 'Run' this cell to see the output
b = np.matrix([[24], [17], [-11]])
sym.Matrix(b)
# 'Run' this cell to see the output

Login with LibreOne to run this code cell interactively.

If you have already signed in, please refresh the page.

#Calculate answer to x using numpy
x = np.linalg.solve(A,b)
sym.Matrix(x)
# 'Run' this cell to see the output
#Calculate answer to x using numpy
x = np.linalg.solve(A,b)
sym.Matrix(x)
# 'Run' this cell to see the output
Question

What is the size of the matrix resulting from multiplying a 10×40 matrix with a 40×3 matrix?


This page titled 11.2: Matrix Multiply is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?