Lecture 3 : Matrix representation and arithmetic

May, 2022 - François HU

Master of Science - EPITA

This lecture is available here: https://curiousml.github.io/

image.png

Table of contents

  1. Linear algebra refresher
  1. Exercices: Matrix arithmetic using python lists
  1. Matrix arithmetic using numpy arrays

The ATLAS (Automatically Tuned Linear Algebra Software) project is an ongoing research effort focusing on applying empirical techniques in order to provide portable performance.

GEMM (for generic matrix multiplication)

1. Linear algebra: refresher on the basics

More advanced notions will be seen in the next lecture.

1.1. Vector arithmetic

1.1.1. vector-vector addition

$a,b\in\mathbb{R}^k$ two vectors of dimension $k$

$$a + b = \begin{bmatrix} a_0\\ a_1\\ \vdots\\ a_{k-2}\\ a_{k-1} \end{bmatrix} + \begin{bmatrix} b_0\\ b_1\\ \vdots\\ b_{k-2}\\ b_{k-1} \end{bmatrix} = \begin{bmatrix} a_0 +b_0 \\ a_1 +b_1 \\ \vdots\\ a_{k-2}+b_{k-2}\\ a_{k-1}+b_{k-1} \end{bmatrix}$$

1.1.2. scalar-vector multiplication

$\lambda\in\mathbb{R}$ a scalar and $a\in\mathbb{R}^k$ a vector $$\lambda \times a = \lambda \times \begin{bmatrix} a_0\ a1\ \vdots\ a{k-2}\ a_{k-1}

\end{bmatrix}

\begin{bmatrix} \lambda\times a_0\\ \lambda\times a_1\\ \vdots \\ \lambda\times a_{k-2}\\ \lambda\times a_{k-1} \end{bmatrix}

$$

1.1.3. Inner product (or dot product)

$a,b\in\mathbb{R}^k$ two vectors of dimension $k$

$$<a, b> = a^T b = \begin{bmatrix} a_0& a_1& \cdots& a_{k-2}& a_{k-1} \end{bmatrix} \times \begin{bmatrix} b_0\\ b_1\\ \vdots\\ b_{k-2}\\ b_{k-1} \end{bmatrix} = a_0 b_0 +a_1 b_1 +\cdots +a_{k-2} b_{k-2} +a_{k-1}b_{k-1} $$

1.2. Matrix arithmetic

$$A = \begin{bmatrix} a_{0, 0}& a_{0, 1}& \cdots & a_{0, m-1}\\ a_{1, 0}& a_{1, 1}& \cdots & a_{1, m-1}\\ \vdots& \vdots& \cdots & \vdots\\ a_{n-1, 0}& a_{n-1, 1}& \cdots & a_{n-1, m-1} \end{bmatrix}\in\mathbb{R}^{n\times m}$$

1.2.1. matrix-matrix addition and matrix-scalar multiplication

Like vectors we can add (and substract) matrices of the same size

Matrix-matrix addition: $A, B\in\mathbb{R}^{n\times m}$ two matrices of dimension $(n, m)$

$$A + B = \begin{bmatrix} a_{0, 0}& \cdots & a_{0, m-1}\\ \vdots& \cdots & \vdots\\ a_{n-1, 0}& \cdots & a_{n-1, m-1} \end{bmatrix} + \begin{bmatrix} b_{0, 0}& \cdots & b_{0, m-1}\\ \vdots& \cdots & \vdots\\ b_{n-1, 0}& \cdots & b_{n-1, m-1} \end{bmatrix} = \begin{bmatrix} a_{0, 0}+b_{0, 0}& \cdots & a_{0, m-1}+b_{0, m-1}\\ \vdots& \cdots & \vdots\\ a_{n-1, 0}+b_{n-1, 0}& \cdots & a_{n-1, m-1}+b_{n-1, m-1} \end{bmatrix} $$

Matrix-scalar multiplication: $A\in\mathbb{R}^{n\times m}$, $\lambda\in\mathbb{R}$

$$\lambda\times A = \begin{bmatrix} \lambda \times a_{0, 0}& \cdots & \lambda \times a_{0, m-1}\\ \vdots& \cdots & \vdots\\ \lambda \times a_{n-1, 0}& \cdots & \lambda \times a_{n-1, m-1} \end{bmatrix} $$

1.2.2. matrix-vector multiplication

$A\in\mathbb{R}^{n\times m}, x\in\mathbb{R}^m$ $$A x = \begin{bmatrix} a_{0, 0}& \cdots & a_{0, m-1}\\ \vdots& \cdots & \vdots\\ a_{n-1, 0}& \cdots & a_{n-1, m-1} \end{bmatrix} \times \begin{bmatrix} x_{0}\\ \vdots\\ x_{m-1} \end{bmatrix} = \begin{bmatrix} a_{0, 0} x_0 + \dots + a_{0, m-1} x_{m-1}\\ \vdots\\ a_{n-1, 0} x_0 + \dots + a_{n-1, m-1} x_{m-1} \end{bmatrix} $$

Row interpretation: $y = Ax$ is a "batch" inner product of all rows of $A$ with $x$

Column interpretation: $y = Ax = a_1x_1 + \dots a_{m-1}x_{m-1}$ is linear combination of columns $a_1, \dots a_{m-1}$ of $A$ with coefficients $x_1, \dots, x_{m-1}$

1.2.3. matrix-matrix multiplication

$A\in\mathbb{R}^{n\times p}$ and $B\in\mathbb{R}^{p\times m}$

$$ C = A\times B \in\mathbb{R}^{n\times m} $$

with $$C_{i, j} = \sum\limits_{k=1}^{p}A_{i, k}B_{k, j} $$

for $i, j\in\{0, \dots, n-1\}\times\{0, \dots, m-1\}$

1.2.4. Other operations

$$A^T_{i, j} = A_{j, i}$$ $$ A^{-1}A = AA^{-1} = I $$ $$ \text{tr}(A) = \sum\limits_{i=1}^{n}A_{i, i} $$

2. Exercices

These exercices are supposed to enhance your low-level programming skills. Therefore let us set some restrictions:

Exercice 1: Inner product

Write a function that returns a scalar resulting from the inner product of two vectors $a, b\in\mathbb{R}^k$

Exercice 2: Matrix addition

Write a function that returns a matrix resulting from the addition of two matrices $A, B\in\mathbb{R}^{n\times m}$

Exercice 3: Matrix-vector multiplication

Write a function that returns a vector in $\mathbb{R}^{n}$ resulting from the multiplication $Ax$ with $A\in\mathbb{R}^{n\times m}$ and $x\in\mathbb{R}^{m}$

Exercice 4: Naive Matrix multiplication

Write a function that returns a matrix in $\mathbb{R}^{n, m}$ resulting from the multiplication of two matrices $A\in\mathbb{R}^{n\times p}$ and $B\in\mathbb{R}^{p\times m}$

Exercice 5: Matrix multiplication, improved version (Strassen 1969)

The Strassen method is an algorithm for matrix multiplication. This methdod is faster than the standard matrix multiplication algorithm for large matrices, with a better asymptotic complexity through a divide-and-conquer approach.

The key observation is that multiplying two $2 × 2$ matrices can be done with only $7$ multiplications, instead of the usual $8$.

If $n=2^t$ then we have the following recursive approach:

$$ \begin{bmatrix} B_{0, 0} & B_{0, 1}\\ B_{1, 0} & B_{1, 1} \end{bmatrix} \times \begin{bmatrix} A_{0, 0} & A_{0, 1}\\ A_{1, 0} & A_{1, 1} \end{bmatrix} = \begin{bmatrix} P_{0, 0} & P_{0, 1}\\ P_{1, 0} & P_{1, 1} \end{bmatrix} $$

with $$ \begin{cases} T_1 = (B_{0, 0}+B_{1, 1})\times(A_{0, 0}+A_{1, 1})\\ T_2 = (B_{1, 0}+B_{1, 1})\times A_{0, 0}\\ T_3 = B_{0, 0}\times (A_{0, 1}-A_{1, 1})\\ T_4 = B_{1, 1}\times (A_{1, 0}-A_{0, 0})\\ T_5 = (B_{0, 0}+B_{0, 1})\times A_{1, 1}\\ T_6 = (B_{1, 0}-B_{0, 0})\times (A_{0, 0}+A_{0, 1})\\ T_7 = (B_{0, 1}-B_{1, 1})\times (A_{1, 0}+A_{1, 1})\\ \end{cases}\quad\text{,}\quad \begin{cases} P_{0, 0} = T_1+T_4-T_5+T_7\\ P_{0, 1} = T_3+T_5 \\ P_{1, 0} = T_2+T_4 \\ P_{1, 1} = T_1-T_2+T_3+T_6 \\ \end{cases} $$

and $P_{i, j}, A_{i, j}, B_{i,j}$ are matrices of dimension $\frac{n}{2}\times\frac{n}{2}$.

Write a function that returns the multiplication of two matrices $A\in\mathbb{R}^{n\times n}$ and $B\in\mathbb{R}^{n\times n}$ using the Strassen method. We assume that $n=2^t$ for $t\in\mathbb{N}$.

3. Matrix arithmetic using numpy arrays