## Unit1.2.1Mapping matrices to memory

Matrices are usually visualized as two-dimensional arrays of numbers while computer memory is inherently one-dimensional in the way it is addressed. So, we need to agree on how we are going to store matrices in memory.

Consider the matrix

\begin{equation*} \left(\begin{array}{rrr} 1 \amp -2 \amp 2\\ -1 \amp 1 \amp 3\\ -2 \amp 2 \amp -1 \end{array}\right) \end{equation*}

from the opener of this week. In memory, this may be stored in a one-dimentional array A by columns:

\begin{equation*} \begin{array}{ l r r} {\tt A[0]} \amp\longrightarrow\amp 1 \\ {\tt A[1]}\amp \longrightarrow\amp -1 \\ {\tt A[2]} \amp\longrightarrow\amp -2 \\ \hline {\tt A[3]}\amp \longrightarrow\amp -2 \\ {\tt A[4]} \amp \longrightarrow\amp 1 \\ {\tt A[5]} \amp \longrightarrow \amp 2 \\ \hline {\tt A[6]} \amp \longrightarrow \amp 2 \\ {\tt A[7]} \amp \longrightarrow \amp 3 \\ {\tt A[8]} \amp \longrightarrow \amp -1 \\ \end{array} \end{equation*}

which is known as column-major ordering.

More generally, consider the matrix

\begin{equation*} \left(\begin{array}{c c c c} \alpha_{0,0} \amp \alpha_{0,1} \amp \cdots \amp \alpha_{0,n-1} \\ \alpha_{1,0} \amp \alpha_{1,1} \amp \cdots \amp \alpha_{1,n-1} \\ \vdots \amp \vdots \amp \amp \vdots \\ \alpha_{m-1,0} \amp \alpha_{m-1,1} \amp \cdots \amp \alpha_{m-1,n-1} \end{array} \right). \end{equation*}

Column-major ordering would store this in array A as illustrated by Figure 1.2.1.

Obviously, one could use the alternative known as row-major ordering.

###### Homework1.2.1.1.

Let the following picture represent data stored in memory starting at address A:

\begin{equation*} \begin{array}{ l r r} \amp \amp 3 \\ {\tt A[0]} \amp\longrightarrow\amp 1 \\ \amp\amp -1 \\ \amp\amp -2 \\ \amp\amp -2 \\ \amp\amp 1 \\ \amp\amp 2 \\ \amp\amp 2 \\ \end{array} \end{equation*}

and let $A$ be the $2 \times 3$ matrix stored there in column-major order. Then

$A =$

and let $A$ be the $2 \times 3$ matrix stored there in row-major order. Then