Matrix operations
We spent some time learning about
vectors and
operations on vectors: scalar product, vector sum and dot
product. Then we learned about
matrices, and
the matrix-vector product operation, which connects matrices and
vectors. This led to several lessons on matrix-vector-product
equations, row echelon form and gaussian elimination. Now it's
time to look at operations on the matrices themselves. Can
we add two matrices? Can we multiply two matrices?
First we have scalar product, i.e. the product of a scalar and
a matrix, and the sum of two matrices. These are straightforward
because they operate elementwise.
- scalar-matrix product: define $c\cdot A$, where $c$ is a scalar
and $A$ is an $m\times n$ matrix,
to be the $m\times n$ matrix entry $i,j$ equal to $c\cdot
a_{i,j}$. In other words:
If $c$ is a scalar
and $A$ is an $m\times n$ matrix,
the scalar-matrix product $c\cdot A$
is defined as
$$
c \cdot
\begin{bmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n}\\
a_{2,1} & a_{2,2} & \cdots & a_{2,n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{bmatrix}
=
\begin{bmatrix}
c\cdot a_{1,1} & c\cdot a_{1,2} & \cdots & c\cdot a_{1,n}\\
c\cdot a_{2,1} & c\cdot a_{2,2} & \cdots & c\cdot a_{2,n}\\
\vdots & \vdots & \ddots & \vdots\\
c\cdot a_{m,1} & c\cdot a_{m,2} & \cdots & c\cdot a_{m,n}
\end{bmatrix}.
$$
Example:
$
5\cdot
\begin{bmatrix}
3 & -1\\
2 & 4
\end{bmatrix}
=
\begin{bmatrix}
5\cdot 3 & 5\cdot -1\\
5\cdot 2 & 5\cdot 4
\end{bmatrix}
=
\begin{bmatrix}
15 & -5\\
10 & 20
\end{bmatrix}
$
- matrix-matrix sum:
define $A + B$, where $A$ and $B$ are both $m\times n$
matrices, to be the $m\times n$ matrix entry $i,j$ equal to
$a_{i,j} + b_{i,j}$. In other words:
If
$A$ and $B$ are $m\times n$ matrices,
the matrix sum $A+B$
is defined as
$$
\begin{bmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n}\\
a_{2,1} & a_{2,2} & \cdots & a_{2,n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{bmatrix}
+
\begin{bmatrix}
b_{1,1} & b_{1,2} & \cdots & b_{1,n}\\
b_{2,1} & b_{2,2} & \cdots & b_{2,n}\\
\vdots & \vdots & \ddots & \vdots\\
b_{m,1} & b_{m,2} & \cdots & b_{m,n}
\end{bmatrix}
=
\begin{bmatrix}
a_{1,1}+b_{1,1} & a_{1,2}+b_{1,2} & \cdots & a_{1,n}+b_{1,n}\\
a_{2,1}+b_{2,1} & a_{2,2}+b_{2,2} & \cdots & a_{2,n}+b_{2,n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{m,1}+b_{m,1} & a_{m,2}+b_{m,2} & \cdots & a_{m,n}+b_{m,n}
\end{bmatrix}.
$$
Example:
$
\begin{bmatrix}
3 & -1\\
2 & 4
\end{bmatrix}
+
\begin{bmatrix}
-5 & 0\\
6 & -3
\end{bmatrix}
=
\begin{bmatrix}
3+-5 & -1+0\\
2+6 & 4+-3
\end{bmatrix}
=
\begin{bmatrix}
-2 & -1\\
8 & 1
\end{bmatrix}
$
Observe that we can always view an $m\times n$ matrix as a
vector with $mn$ elements and, as long as we remember the
dimension, we can bounce back and forth between the matrix
view and the vector view. The reason this observation is
important is because addition and scalar multiplication are
the same in either view. (Hopefully this is easy to see.) That
means that the associativity rules for scalar multiplication
and sum that we have for vectors carries over to matrices, and
we get that for free without having to do new proofs!
If $A$ and $B$ are $m \times n$ matrices, and $c$ and $d$ are scalars,
then
- $c\cdot (d\cdot A) = (c\cdot d)\cdot A$
- matrix addition is commutative and associative
- $c\cdot(A + B) = c\cdot A + c\cdot B$
ACTIVITY ← we might skip for time
Prove the following theorem
If $A$ and $B$ are $m \times n$ matrices and $\boldsymbol{v}$ is
a vector, $(A + B)\cdot \boldsymbol{v} = A\cdot\boldsymbol{v} + B\cdot\boldsymbol{v}$.
- matrix-matrix product:
Matrix multiplication is where we really have to face
something new. The basic intuition is that $A \cdot B$
will be defined as doing matrix-vector product of $A$ with
each of the column vectors of $B$, and the resulting column
vectors are collected into a matrix, which is the final result
of the matrix-matrix product.
But this means the number of elements in a row of $A$
has to equal the number of elements in a column of
$B$, otherwise the dot product isn't defined.
If
$A$ is $m\times n$ matrix, and
$B$ is $n\times k$ matrix (note: the number of columns in
$A$ is the same as the number of rows in $B$),
the matrix product $A\cdot B$
is the matrix of dimension $m\times k$ defined as
$$
\begin{bmatrix}
a_{1,1} & a_{1,2} & \cdots & a_{1,n}\\
a_{2,1} & a_{2,2} & \cdots & a_{2,n}\\
\vdots & \vdots & \ddots & \vdots\\
a_{m,1} & a_{m,2} & \cdots & a_{m,n}
\end{bmatrix}
\cdot
\begin{bmatrix}
b_{1,1} & b_{1,2} & \cdots & b_{1,k}\\
b_{2,1} & b_{2,2} & \cdots & b_{2,k}\\
\vdots & \vdots & \ddots & \vdots\\
b_{n,1} & b_{n,2} & \cdots & b_{n,k}
\end{bmatrix}
=
\begin{bmatrix}
c_{1,1} & c_{1,2} & \cdots & c_{1,k}\\
c_{2,1} & c_{2,2} & \cdots & c_{2,k}\\
\vdots & \vdots & \ddots & \vdots\\
c_{m,1} & c_{m,2} & \cdots & c_{m,k}
\end{bmatrix}
\text{, where }
c_{i,j} = \begin{bmatrix}a_{i,1} & \cdots & a_{i,n}\end{bmatrix}\cdot
\begin{bmatrix}
b_{1,j}\\
\vdots\\
b_{n,j}
\end{bmatrix}
$$
Example:
$
\begin{bmatrix}
3 & -1\\
2 & 4
\end{bmatrix}
\cdot
\begin{bmatrix}
-5 & 0\\
6 & -3
\end{bmatrix}
=
\begin{bmatrix}
%% 1,1
\begin{bmatrix}3&-1\end{bmatrix} \cdot
\begin{bmatrix}-5\\
6\end{bmatrix}\hspace{12pt}
%%
&
%% 1,2
\begin{bmatrix}3&-1\end{bmatrix} \cdot
\begin{bmatrix}0\\
-3\end{bmatrix}\\
%%
&\\
%% 2,1
\begin{bmatrix}2&4\end{bmatrix} \cdot
\begin{bmatrix}-5\\
6\end{bmatrix}
%%
&
%% 2,2
\begin{bmatrix}2&4\end{bmatrix} \cdot
\begin{bmatrix}0\\
-3\end{bmatrix}
%%
\end{bmatrix}
=
\begin{bmatrix}
-21&3\\
14&-12
\end{bmatrix}
$
ACTIVITY
In-class activity on matrix multiplication.
Note: this is really important stuff!
Is matrix multiplication commutative? In class I
asked you to figure this out and prove either yes or no. The
answer is no!, and to disprove all we need to do is find a
counter example: just one pair $A$ and $B$ such that
$AB \neq BA$. In fact, almost any pair you chose works. Some
work because the dimensions do not match up properly when you
switch the order. But I suggest you write down two random 2x2
matrixes and try multiplying them in both orders. You will
almost certainly see different results!
So matrix multuplication is not commutative, not even if we
restrict ourselves to square matrices.
Is matrix multiplication associative? The answer is
yes!, but the proof is tedious and not very
informative. If you write out the symbolic expressions for the
$i,j$th element of $(AB)C$ and $A(BC)$ you'll see that they
are equal. I won't make you do it because of the tedium.
Recall our definition of the
unit vectors:
The $i$th unit vector in vector space $R^n$, denoted
$\boldsymbol{e_i}$, is the vector that is $1$ in its $i$th
component and zero everywhere else. If it is not clear by
context, we will specify whether it is a row or column vector.
and the
identity matrix:
The $n\times n$ identity matrix, denoted by $I_n$, is the matrix whose $i$th
row is $\boldsymbol{e_i}$, the $i$th unit (row) vector.
Note that this also means that the $i$th column is the
$i$th unit (column) vector.
In fact, we have the following theorem:
If $A$ is an $n\times n$ matrix, then $I_n\cdot A = A$.