next up previous contents index
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:
$a_{Fortran}(d_1,...,d_N) \leftrightarrow a_C[d_N,...,d_1]$
where $N$ is the number of dimensions and $d_i$ the $i^{th}$ dimension.
Remember also that first element through one dimension has index 1 in Fortran, and index 0 in C. Finally, we can write:
$a_{Fortran}(i,j,...,k) \equiv a_C[k-1,...,j-1,i-1]$
This element is the same but accessed from Fortran or from C respectively.



Gildas manager 2014-07-01