dimanche 5 juin 2011

Row- or Column major arrays and loops order.

After a too long period of silence, I'm coming back to learn Python. I just want to point out a little difference between IDL and Python in the order arrays are stored in the computer memory.
There is two ways arrays can be stored: row- or column major. It has a direct impact on the way one has to loop on the arrays. IDL is like Fortran (column major) and Python is like C (row major). It means that in Python, as you move linearly through the memory of an array, the second dimension (rigthmost) changes the fastest, while in IDL the first (leftmost) dimension changes the fastest.
Consequence on the loop order:

for i in range(0,5):
 for j in range(0,4):
              ... a[i,j] ...

1 commentaire:

  1. Hello, very interesting blog, thanks for writing it!
    However, as it is explained here: http://www.idlcoyote.com/misc_tips/colrow_major.html, the terms row/column-major can be umbiguous.

    It depends if you decide that the first index indicates columns or rows. In IDL the first index run faster (array A is stored in memory as A[0,0],A[1,0],... Then you can chose to represent it in either orientation and yet be right.