HOME Mathematics Computer Science
Matrix Multiplication
Prerequisites
Show All
Hide All
Matrices
Show/Hide
﻿
Definition
A matrix is a rectangular group of numbers (or symbols) with a given number of rows and columns.
Example

The following matrix has two rows and three columns, and is described as a "two by three" (2x3) matrix.

LaTeX
\begin{bmatrix}
3 & 6 & 2  \\
5 & 1 & 10
\end{bmatrix}

Notation
• There are a few different ways to represent a matrix. The easiest is with an bold uppercase letter: A
Code
A = [[3, 6, 2], [5, 1, 10]]

print(A)

Notes
• The plural of matrix is matrices.
• The size of a matrix is called its dimension or order. It is written with as rows x columns. A matrix with 2 rows and 3 columns is written as 2x3.
• Two matrices are considered equal if and only if:
• They have the same number of rows.
• They have the same number of columns.
• Each corresponding element is equal. i.e. x111 = x211
• The numpy.matrix is depreciated, in favour of regular arrays
Matrix Transposition
Show/Hide
﻿
Introduction
• Matrix Transposition is an operation that converts one matrix into another.
• Each row in the original matrix is used as a column in the transposed matrix.
Notation
There are two main notations used to show that a matrix is transposed:
• Using a prime: the transpose of matrix A is A'
• Using a T superscript: the tranpose of matrix A is AT
Example
Code
import numpy as np

a = np.matrix([[3, 6, 2], [5, 1, 10]])
print(a)

transposed = a.transpose()
print(transposed)

Notes
• The order (or dimension) of a matrix is switched when transposing, i.e. a 3x2 matrix creates a 2x3 matrix.
Row And Column Vectors
Show/Hide
﻿
Introduction
• Row and Column vectors are special types of matrices which have only one column or row.
Example
Notation
• A column vector is represented by a bold lowercase letter, e.g. a
• A row vector is a transposed column vector. It is represented by a bold lowercase letter with a prime, e.g. a'
Code
In python, both row and column vectors are represented as arrays.
a = [3, 6, 2]
print(a)

Dot product of row and column vectors
Show/Hide
﻿
Introduction
• The dot product is a method of multiplying two vectors and receiving a number as the result.
• The first vector must be a row vector, while the second must be a column vector.
• Both vectors must contain the same amount of elements.
• To calculate the vector inner product, multiply the corresponding elements and sum the results.
Mathematical Definition
a and b are column vectors.
Example
Code
import numpy as np

a = [3, 6, 2]
b = [1, 3, 8]

c = np.dot(a, b)
print(c)

Notes
• It is also known as the scalar product and vector inner product.

Matrix Multiplication
﻿
Introduction
• Matrix Multiplication, or matrix product, is a method of multiplying two matrices to produce a third matrix.
• Two matrices can only be multiplied together if the number of rows in the first matrix is equal to the number of columns in the second matrix.
• The result will be a matrix with the number of columns from the first matrix and the number of rows from the second matrix.
• A is a i x j matrix
• B is a j x k matrix
• C = AB
• C is a i x k matrix
• The values in the resulting matrix are calculated by finding the dot product, using the row from the first matrix and the column from the second:
Example
Find the result of multiplying the following two matrices:

Solution
1. First we position them to make it easier to see which vectors need to be multiplied:

2. Then we write the dot product of the corresponding vectors:

3. And finally solve:
Code
import numpy as np

a = [[1, 4], [3, 7], [8, 4]]
b = [[3, 6, 2], [5, 1, 10]]

c = np.matmul(a, b)
print(c)