# Operations with matrices

To define the addition of two matrices $\mathbf{A}=[a_{ij}]$ and $\mathbf{B}=[b_{ij}]$, they must be of equal dimensions. The result of addition of these two matrices is matrix $\mathbf{C}=[c_{ij}]$  which is of the same dimensions and

$$a_{ij} + b_{ij} =c_{ij}, \forall i= 1, \ldots , m , \forall j = 1, \ldots , n$$

is valid.

$$[a_{ij}] + [b_{ij}] = \left[ \begin{array} {ccccc} a_{11} & a_{12} & a_{13} & \ldots & a_{1n} \\ a_{21} & a_{22} & a_{23} & \ldots & a_{2n} \\ a_{31} & a_{32} & a_{33} & \ldots & a_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots\\ a_{m1} & a_{m2} & a_{m3} & \ldots & a_{mn} \\ \end{array} \right] + \left[ \begin{array} {ccccc} b_{11} & b_{12} & b_{13} & \ldots & b_{1n} \\ b_{21} & b_{22} & b_{23} & \ldots & b_{2n} \\ b_{31} & b_{32} & b_{33} & \ldots & b_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots\\ b_{m1} & b_{m2} & b_{m3} & \ldots & b_{mn} \\ \end{array} \right] =$$

$$=\left[ \begin{array} {ccccc} a_{11}+b_{11} & a_{12}+b_{12} & a_{13}+ b_{13} & \ldots & a_{12n}+ b_{1n} \\ a_{21} + b_{21} & a_{22} + b_{22} & a_{23} + b_{23} & \ldots & a_{2n}+ b_{2n} \\ a_{31}+b_{31} & a_{32}+ b_{32} & a_{33}+ b_{33} & \ldots & a_{3n}+ b_{3n} \\ \vdots & \vdots & \vdots & \ddots & \vdots\\ a_{m1}+b_{m1} & a_{m2}+b_{m2} & a_{m3}+b_{m3} & \ldots & a_{mn}+b_{mn} \\ \end{array} \right] =[c_{ij}].$$

Example 1.

$$\mathbf{A} = \left [\begin{array} {cccc} 1 & 2 & -4 & 3 \\ -9 & 11 & 36 & -6 \\ 41 & -3 & 17 & 76 \\ \end{array} \right] , \mathbf{B} = \left [\begin{array} {cccc} 0 & -3 & 16 & -98 \\ 12 & -12 & -16 & 10 \\ -31 & 9 & 4 & -57 \\ \end{array} \right].$$

Solution:

$$\mathbf{A} + \mathbf{B} = \left [\begin{array} {cccc} 1 & 2 & -4 & 3 \\ -9 & 11 & 36 & -6 \\ 41 & -3 & 17 & 76 \\ \end{array} \right] + \left [\begin{array} {cccc} 0 & -3 & 16 & -98 \\ 12 & -12 & -16 & 10 \\ -31 & 9 & 4 & -57 \\ \end{array} \right] =$$

$$= \left [\begin{array} {cccc} 1 + 0 & 2 +(-3) & -4 +16 & 3 + (-98) \\ -9 +12 & 11 + (-12) & 36 + (-16) & -6 +10 \\ 41 + (-31) & -3 +9 & 17 +4 & 76 + (-57) \\ \end{array} \right] =$$

$$= \left [\begin{array} {cccc} 1 & -1 & 12 & -95 \\ 3 & -1 & 20 & 4 \\ 10 & 6 & 21 & 19 \\ \end{array} \right].$$

For every two matrices $\mathbf{A}=[a_{ij}], \mathbf{B} = [b_{ij}] \in \mathbb{R}^{m \times n}$  is valid:

$$\mathbf{A} + \mathbf{B} = \mathbf{B} + \mathbf{A},$$

that is, the addition of matrices is commutative.

For every three matrices $\mathbf{A}=[a_{ij}], \mathbf{B} = [b_{ij}], \mathbf{C} = [c_{ij}] \in \mathbb{R}^{m \times n}$  is valid:

$$(\mathbf{A} + \mathbf{B}) +\mathbf{C} = \mathbf{A} + (\mathbf{B} + \mathbf{C}),$$

that is, the addition of matrices is associative.

A zero matrix is a neutral element for addition of matrices.

## Multiplication a matrix by a scalar

If $\alpha \in \mathbb{R}$ is a scalar and $\mathbf{A} \in \mathbb{R}^{m \times n}$ a matrix, then the multiplication a matrix $\mathbf{A}$ by a scalar $\alpha$ is defined as $\alpha \cdot \mathbf{A}$. This means that every element of a matrix $\mathbf{A}$ is multiplied by $\alpha$.

Example 2.

For the matrix $\mathbf{A} =\left [\begin{array} {cccc} 3 & -2 & 7 \\ -4 & 1 & 0 \\ 11 & -6 & 22 \\ \end{array} \right]$ calculate $3\cdot \mathbf{A}$ and $-2\cdot \mathbf{A}$.

Solution:

$$3 \cdot \mathbf{A} = 3 \cdot \left [\begin{array} {cccc} 3 & -2 & 7 \\ -4 & 1 & 0 \\ 11 & -6 & 22 \\ \end{array} \right] = \left [\begin{array} {cccc} 3 \cdot 3 & 3 \cdot (-2) & 3 \cdot 7 \\ 3 \cdot (-4) & 3 \cdot 1 & 3 \cdot 0 \\ 3 \cdot 11 & 3 \cdot (-6) & 3 \cdot 22 \\ \end{array} \right] = \left [\begin{array} {cccc} 9 & -6 & 21 \\ -12 & 3 & 0 \\ 33 & -18 & 66 \\ \end{array} \right].$$

$$-2 \cdot \mathbf{A} = -2\cdot \left [\begin{array} {cccc} 3 & -2 & 7 \\ -4 & 1 & 0 \\ 11 & -6 & 22 \\ \end{array} \right] = \left [\begin{array} {cccc} -6 & 4 & -14 \\ 8 & -2 & 0 \\ -22 & 12 & -44 \\ \end{array} \right].$$

If $\alpha = 0$ , then matrix $\mathbf{A}$ is equal to zero matrix.

If $\alpha = -1$ ,then $\mathbf{A} = \mathbf {-A}$.

$\mathbf {-A}$ is called  the opposite matrix of matrix $\mathbf{A}$.

## Multiplication of matrices

The product of two matrices is not defined for any two matrices, is not even defined for two matrices of the same dimensions.

Firstly, consider the multiplication of row vector by column vector. Let the dimensions of vector $\mathbf{a}$ be $1 \times n$, and $n \times 1$ of vector $\mathbf{b}$. Then their product is:

$$\mathbf{a} \cdot \mathbf{b} = \left[ \begin{array} {ccccc} a_{1} & a_{2} & a_{3} & \ldots & a_{n} \\ \end{array} \right] \cdot \left[\begin{array} {c} b_{1} \\ b_{2} \\ b_{3} \\ \vdots \\ b_{n} \\ \end{array} \right] = a_{1}b_{1} +a_{2}b_{2} + a_{3}b_{3} + \ldots + a_{n}b_{n},$$

Their product is a scalar.

We use previously on the example:

$$\left[ \begin{array} {cccc} 2 & 4 & -1 & 3 \\ \end{array} \right] \cdot \left[ \begin{array} {c} -1 \\ 6 \\ -2 \\ 0\\ \end{array} \right] = 2 \cdot (-1) + 4\cdot 6 + (-1) \cdot (-2) + 3 \cdot 0 = -2 + 24 +2 +0 = 24.$$

Matrix $\mathbf{A}$  can be multiplied by matrix $\mathbf{B}$ if matrix $\mathbf{B}$ has the same number of rows as matrix $\mathbf{A}$ columns. The product $\mathbf{A} \mathbf{B}$ then has the same number of rows as matrix $\mathbf{A}$ and columns the same as matrix $\mathbf{B}$.

The proper definition follows.

If $\mathbf{A} = [a_{ij}] \in \mathbb{R}^{m\times n}$ and $\mathbf{B} = [b_{ij}] \in \mathbb{R}^{n\times r}$ , then their product is defined as matrix $\mathbf{A} \mathbf{B} = [c_{ij}] \in \mathbb{R}^{m\times r}$, whereby $[c_{ij}] = a_{i1} b_{1j} +a_{i2} b_{2j} + a_{i3} b_{3j} + \ldots + a_{in} b_{nj}$.

This means that element $c_{ij}$ is the scalar product of elements that are located in $i$-th row of matrix $\mathbf{A}$ and in $j$-th column of matrix $\mathbf{B}$.

For example,

$c_{11} = a_{11}b_{11} + a_{12}b_{21} + a_{13}b_{31} + \ldots + a_{1n}b_{n1}$.

Example 3.

Multiply the following matrices:

$\mathbf{A}= \left [\begin{array} {cccc} 1 & -3 & -2 & -1 \\ 6 & 0 & 5 & 2 \\ -1 & 7 & 4 & 0\\ \end{array} \right]$ and $\mathbf{B}= \left [\begin{array} {cc} 2 & -1 \\ 0 & 3 \\ 7 & 1 \\ -1 & -8 \\ \end{array} \right]$.

Solution:

The dimensions of matrix $\mathbf{A}$ is $3 \times 4$ and of matrix $\mathbf{B}$ is $4 \times 2$ , therefore,  it is possible to multiply these two matrices.

$$\mathbf{A} \cdot \mathbf{B}=\left [\begin{array} {cccc} 1 & -3 & -2 & -1 \\ 6 & 0 & 5 & 2 \\ -1 & 7 & 4 & 0\\ \end{array} \right] \cdot \left [\begin{array} {cc} 2 & -1 \\ 0 & 3 \\ 7 & 1 \\ -1 & -8 \\ \end{array} \right] =$$

$$=\left[ \begin {array} {cc} 1\cdot 2 + (-3)\cdot 0 + (-2) \cdot 7 +(-1) \cdot (-1) & 1\cdot (-1) + (-3)\cdot 3 + (-2) \cdot 1 +(-1) \cdot (-8) \\ 6 \cdot 2 + 0 \cdot 0 + 5 \cdot 7 + 2 \cdot (-1) & 6 \cdot (-1) + 0 \cdot 3 + 5 \cdot 1 + 2 \cdot (-8) \\ -1 \cdot 2 + 7 \cdot 0 + 4 \cdot 7 + 0 \cdot (-1) & -1 \cdot (-1) + 7 \cdot 3 + 4 \cdot 1 + 0 \cdot (-8) \\ \end{array} \right] =$$

$$=\left[ \begin {array} {cc} 2 + 0 -14 +1 & -1 -9 -2 +8 \\ 12 + 0 +35 -2 & -6 + 0 +5 -16 \\ -2 + 0 +28 + 0 & 1 +21 +4 + 0 \\ \end{array} \right] =\left[ \begin {array} {cc} -11 & -4 \\ 45 & -17 \\ 26 & 26 \\ \end{array} \right].$$

Multiplication of matrices is not commutative, as we can see from the previous example. The product $\mathbf{B} \mathbf{A}$ is not even defined, because matrix $\mathbf{B}$ has two columns and matrix $\mathbf{A}$ three rows.

The following properties are valid for the multiplication of matrices (if the specified products are well defined):

•  $\mathbf{A} (\mathbf{B} + \mathbf{C}) = \mathbf{A} \mathbf{B} + \mathbf{A} \mathbf{C}$  ( right distributive)
• $(\mathbf{A} + \mathbf{B}) \mathbf{C} = \mathbf{A} \mathbf{C} + \mathbf{B} \mathbf{C}$  ( left distributive)
• $(\alpha \mathbf{A}) \mathbf{B} = \mathbf{A} ( \alpha \mathbf{B}) = \alpha (\mathbf{A} \mathbf{B}) , \forall \alpha \in \mathbb{R}$  (quasi associative)
• $(\mathbf{A} \mathbf{B}) \mathbf{C} = \mathbf{A} (\mathbf{B} \mathbf{C})$ (associative)
• $\mathbf{A} \mathbf{I} = \mathbf{A} , \mathbf{I} \mathbf{A} = \mathbf{A}$ (an identity matrix is a neutral element for multiplication)