Matrix Arithmetic

There are a few useful arithmetic operations that we need to know to effectively use matrices.Below, we look at Addition, Subtraction, Multiplication and Division.

Matrix Addition

Suppose metrics $A$ and $B$ such that

$$ A = \begin{pmatrix} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \end{pmatrix}, B = \begin{pmatrix} b_{1,1} & b_{1,2} \\ b_{2,1} & b_{2,2} \end{pmatrix} $$

Then matrix addition between matrix A and B is given as follows:

$$ C = A + B = \begin{pmatrix} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \end{pmatrix} + \begin{pmatrix} b_{1,1} & b_{1,2} \\ b_{2,1} & b_{2,2} \end{pmatrix} $$

$$ C = \begin{pmatrix} c_{1,1} & c_{1,2} \\ c_{2,1} & c_{2,2} \end{pmatrix} = \begin{pmatrix} a_{1,1}+b_{1,1} & a_{1,2}+b_{1,2} \\ a_{2,1}+b_{2,1} & a_{2,2}+b_{2,2} \end{pmatrix} $$

Example:

$$ A = \begin{pmatrix} 2 & 3 \\ 1 & 5 \end{pmatrix}, B = \begin{pmatrix} 1 & 0 \\ 2 & 5 \end{pmatrix}$$

$$ C = \begin {pmatrix} 2+1 & 3+0 \\ 1+2 & 5+5 \end{pmatrix} = \begin{pmatrix} 3 & 3 \\ 3 & 10 \end{pmatrix}$$

Matrix Addition in Numpy

import numpy as np
A = np.array([2,3,1,5]).reshape(2,2)
B = np.array([1,0,2,5]).reshape(2,2)    

A + B
array([[ 3,  3],
    [ 3, 10]])

Matrix Subtraction

$$ C = A - B $$

$$ A = \begin{pmatrix} a_{1,1} & a_{1,2} \\ a_{2,1} & a_{2,2} \end{pmatrix}, B = \begin{pmatrix} b_{1,1} & b_{1,2} \\ b_{2,1} & b_{2,2} \end{pmatrix} $$

$$ C = A - B = \begin{pmatrix} c_{1,1} & c_{1,2} \\ c_{2,1} & c_{2,2} \end{pmatrix} = \begin{pmatrix} a_{1,1}-b_{1,1} & a_{1,2}-b_{1,2} \\ a_{2,1}-b_{2,1} & a_{2,2}-b_{2,2} \end{pmatrix} $$

From the previous example

$$ A = \begin{pmatrix} 2 & 3 \\ 1 & 5 \end{pmatrix}, B = \begin{pmatrix} 1 & 0 \\ 2 & 5 \end{pmatrix}$$

$$ C = \begin {pmatrix} 2-1 & 3-0 \\ 1-2 & 5-5 \end{pmatrix} = \begin{pmatrix} 1 & 3 \\ -1 & 0 \end{pmatrix}$$

Implementation in Numpy

A - B
array([[ 1, 3], [-1, 0]])

Matrix Division

Given:

$$ A = \begin{pmatrix} 2 & 3 \\ 1 & 5 \end{pmatrix}, B = \begin{pmatrix} 1 & 1 \\ 2 & 5 \end{pmatrix}$$

Then: $$ C = \frac {A}{B} = $$

$$ C = \begin {pmatrix} 2/1 & 3/1 \\ 1/2 & 5/5 \end{pmatrix} = \begin{pmatrix} 2 & 3 \\ .5 & 1 \end{pmatrix}$$

A = np.array([2,3,1,5]).reshape(2,2)
B = np.array([1,1,2,5]).reshape(2,2)

A/B
array([[2. , 3. ], [0.5, 1. ]])