# How Leading Dimension Is Used for Matrices

The leading dimension for a two-dimensional array is an increment that is used to find the starting point for the matrix elements in each successive column of the array. To define exactly which elements become the conceptual matrix in the array, the following items are used together:
• The location of the matrix within the array
• The number of rows, m, to be processed in the array
• The number of columns, n, to be processed in the array

The leading dimension must always be positive. It must always be greater than or equal to m, the number of rows in the matrix to be processed. For an array, `A`, declared as `A(E1:E2,F1:F2)`, the leading dimension is equal to:

(`E2`-`E1`+1)

The starting point for selecting the matrix elements from the array is at the location specified by the argument for the matrix in the ESSL calling sequence. For example, if you specify `A(3,0)` for a 4 by 4 matrix A, where `A` is declared as `A(1:7,0:4)`:
``````
| 1.0   8.0  15.0  22.0  29.0 |
| 2.0   9.0  16.0  23.0  30.0 |
| 3.0  10.0  17.0  24.0  31.0 |
| 4.0  11.0  18.0  25.0  32.0 |
| 5.0  12.0  19.0  26.0  33.0 |
| 6.0  13.0  20.0  27.0  34.0 |
| 7.0  14.0  21.0  28.0  35.0 |
``````

then processing begins at the element at row 3 and column 0 in array `A`, which is 3.0.

The leading dimension is used to find the starting point for the matrix elements in each of the n successive columns in the array. ESSL subroutines assume that the arrays are stored in column-major order, as described under How Do You Set Up Your Arrays?, and they add the leading dimension (times the size of the element in bytes) to the starting point. They do this n-1 times. This finds the starting point in each of the n columns of the array.

In the above example, the leading dimension is:

`E2`-`E1`+1 = 7-1+1 = 7
If the number of columns, n, to be processed is 4, the starting points are: ` A(3,0), A(3,1), A(3,2),` and `A(3,3)`. These are elements 3.0, 10.0, 17.0, and 24.0 for a11, a12, a13, and a14, respectively.

In general terms, this results in the following starting positions of each column in the matrix being calculated as follows:

`A`(`BEGINI`, `BEGINJ`)
`A`(`BEGINI`, `BEGINJ`+1)
`A`(`BEGINI`, `BEGINJ`+2)
.
.
.
`A`(`BEGINI`, `BEGINJ`+n-1)

To find the elements in each column of the array, 1 is added successively to the starting point in the column until m elements are selected. This is why the leading dimension must be greater than or equal to m; otherwise, you go past the end of each dimension of the array. In the above example, if the number of elements, m, to be processed in each column is 4, the following elements are selected from array `A` for the first column of the matrix: `A(3,0), A(4,0), A(5,0)`, and `A(6,0)`. These are elements 3.0, 4.0, 5.0, and 6.0, corresponding to the matrix elements a11, a21, a31, and a41, respectively.

Column element selection can also be expressed in general terms. Using `A(BEGINI,BEGINJ)` as the starting point in the array, this results in the following elements being selected from each column in the array:

`A`(`BEGINI`, `BEGINJ`)
`A`(`BEGINI`+1, `BEGINJ`)
`A`(`BEGINI`+2, `BEGINJ`)
.
.
.
`A`(`BEGINI`+m-1, `BEGINJ`)

Combining this with the technique already described for finding the starting point in each column of the array, the resulting matrix in the example is:

As shown in this example, a matrix does not have to include all columns and rows of an array. The elements of matrix A are selected from rows 3 through 6 and columns 0 through 3 of the array. Rows 1, 2, and 7 and column 4 of the array are not used.