Keywords: gildas-python

Next: The vars() built-in function Up: Python basics Previous: Identifiers Contents Index

#### Multidimensional-array arrangement in memory

Multidimensional arrays in C (and thus Python) are stored in row-major order; in Fortran they are in column-major order. For a 2-dimensional array (matrix), this means that C stores each row contiguously in memory, while Fortran stores each column contiguously. More generally, for an N-dimensional array, in C the last dimension is contiguous in memory, while in Fortran the first dimension is contiguous. This means that for the same area allocated in memory, Fortran and C indices are transposed:
where is the number of dimensions and the dimension.
Remember also that first element through one dimension has index 1 in Fortran, and index 0 in C. Finally, we can write:
This element is the same but accessed from Fortran or from C respectively.