# How Leading Dimension Is Used for Matrices

- The location of the matrix within the array
- The leading dimension
- 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)`A(3,0)`

for
a 4 by 4 matrix **, where**

*A*`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` 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 a_{11}, a

_{12}, a

_{13}, and a

_{14}, 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 a_{11}, a_{21}, a_{31},
and a_{41}, 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.