Skip to main content

Unit 1.2.2 The leading dimension

Very frequently, we will work with a matrix that is a submatrix of a larger matrix. Consider Figure 1.2.2.

\begin{equation*} {\tt ldA}~~~~~\left\{\begin{array}{c | c | c | c } 1 \amp 2 \amp 3 \amp \times \\ 4 \amp 5 \amp 6 \amp \times \\ 7 \amp 8 \amp 9 \amp \times \\ 10 \amp 11 \amp 12 \amp \times \\ \times \amp \times \amp \times \amp \times \\ \vdots \amp \vdots \amp \vdots \amp \vdots \\ \times \amp \times \amp \times \amp \times \\ \end{array} \right. ~~~~~~~~~ \begin{array}{ l c c c l} {\tt A[{\color{red} 0}*ldA+{\color{blue} 0}]} \amp \longrightarrow \amp 1 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 0},{\color{red} 0} )}\\ {\tt A[{\color{red} 0}*ldA+{\color{blue} 1}]} \amp \longrightarrow \amp 4 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 1},{\color{red} 0} )}\\ {\tt A[{\color{red} 0}*ldA+{\color{blue} 2}]} \amp \longrightarrow \amp 7 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 2},{\color{red} 0} )}\\ {\tt A[{\color{red} 0}*ldA+{\color{blue} 3}]} \amp \longrightarrow \amp 10 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 3},{\color{red} 0} )}\\ \amp\amp \times \amp \\ \amp\amp \vdots \amp \vdots \amp\\ \amp\amp \times \\ \hline {\tt A[{\color{red} 1}*ldA+{\color{blue} 0}]} \amp \longrightarrow \amp 2 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 0},{\color{red} 1} )}\\ {\tt A[{\color{red} 1}*ldA+{\color{blue} 1}]} \amp \longrightarrow \amp 5 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 1},{\color{red} 1} )}\\ {\tt A[{\color{red} 1}*ldA+{\color{blue} 2}]} \amp \longrightarrow \amp 8 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 2},{\color{red} 1} )}\\ {\tt A[{\color{red} 1}*ldA+{\color{blue} 3}]} \amp \longrightarrow \amp 11 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 3},{\color{red} 1} )}\\ \amp\amp \times \\ \amp\amp \vdots \\ \amp\amp \times \\ \hline {\tt A[{\color{red} 2}*ldA+{\color{blue} 0}]} \amp \longrightarrow \amp 3 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 0},{\color{red} 2} )}\\ {\tt A[{\color{red} 2}*ldA+{\color{blue} 1}]} \amp \longrightarrow \amp 6 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 1},{\color{red} 2} )}\\ {\tt A[{\color{red} 2}*ldA+{\color{blue} 2}]} \amp \longrightarrow \amp 9 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 2},{\color{red} 2} )}\\ {\tt A[{\color{red} 2}*ldA+{\color{blue} 3}]} \amp \longrightarrow \amp 12 \amp \longleftarrow \amp {\tt alpha( {\color{blue} 3},{\color{red} 2} )}\\ \amp\amp \times \\ \amp\amp \vdots \\ \amp\amp \times \\ \hline \amp\amp \times \\ \amp\amp \times \\ \amp\amp \times \\ \amp\amp \times \\ \amp\amp \times \\ \amp\amp \vdots \\ \amp\amp \times \\ \end{array} \end{equation*}
Figure 1.2.2. Addressing a matrix embedded in an array with \({\tt ldA} \) rows. At the left we illustrate a \(4 \times 3 \) submatrix of a \({\tt ldA} \times 4 \) matrix. In the middle, we illustrate how this is mapped into an linear array \({\tt a}\text{.}\) In the right, we show how defining the C macro #define alpha(i,j) A[ (j)*ldA + (i)] allows us to address the matrix in a more natural way.

What we depict there is a matrix that is embedded in a larger matrix. The larger matrix consists of ldA (the leading dimension) rows and some number of columns. If column-major order is used to store the larger matrix, then addressing the elements in the submatrix requires knowledge of the leading dimension of the larger matrix. In the C programming language, if the top-left element of the submatrix is stored at address A, then one can address the \((i,j) \) element as A[ j*ldA + i ]. In our implementations, we define a macro that makes addressing the elements more natural:

#define alpha(i,j) A[ (j)*ldA + (i) ]

where we assume that the variable or constant ldA holds the leading dimension parameter.

Homework 1.2.2.1.

Consider the matrix

\begin{equation*} \left( \begin{array}{r r r r r} 0.0 \amp 0.1 \amp 0.2 \amp 0.3 \\ 1.0 \amp {\Large \mathbf {1.1}} \amp {\Large \mathbf {1.2}} \amp {\Large \mathbf {1.3}} \\ 2.0 \amp {\Large \mathbf {2.1}} \amp {\Large \mathbf{2.2}} \amp {\Large \mathbf {2.3}} \\ 3.0 \amp 3.1 \amp 3.2 \amp 3.3 \\ 4.0 \amp 4.1 \amp 4.2 \amp 4.3 \\ \end{array} \right) \end{equation*}

If this matrix is stored in column-major order in a linear array A,

  1. The highlighted submatrix starts at A[...].

  2. The number of rows (height) of the highlighted submatrix equals ....

  3. The number of columns (width) of the highlighted submatrix equals ....

  4. The leading dimension of the highlighted submatrix is ....

Solution
  1. The highlighted submatrix starts at A[ 6 ].

  2. The number of rows of the boxed submatrix is 2.

  3. The number of columns of the boxed submatrix is 3.

  4. The leading dimension of the boxed submatrix is 5.