# The NumPy Fortran-ordering quiz

Stefan van der Walt stefan at sun.ac.za
Tue Oct 17 21:22:29 CDT 2006

```On Tue, Oct 17, 2006 at 07:53:11PM -0600, Travis Oliphant wrote:
> Stefan van der Walt wrote:
> > Hi all,
> >
> > Some of you may have seen the interesting thread on Fortran-ordering
> > earlier.  I thought it might be fun to set up a short quiz which tests
> > your knowledge on the topic.
> >
> > If you're up for the challenge, take a look at
> >
> > http://mentat.za.net/numpy/quiz
> >
> > I won't be held liable for any emotional trauma caused, self-inflicted
> > wounds or brain-core meltdowns.
> >
>
> Cute (especially the comment if you get them all right).   I'm not sure
> if this quiz is a veiled complaint about the rules for Fortran ordering
> or not ;-)

If it is, it is very tactfully disguised ;) Actually, I was just
trying to figure out how Fortran ordering works and thought others
might be interested.

I reckoned you would be the only one to get 100%, although now you've
explained the rules so clearly a couple of other might grasp for the
holy grail too.

> In my mind the Fortran ordering rules are consistent (if not completely
> bug-free).   You just have to get the right idea of what is meant by the
> order argument when you use it.   If you think you are having trouble
> figuring out the rules, think of the trouble it was to figure out what
> they should be and then to code them up.

My sympathies (no, really).

> 2) On reshaping, the order argument specifies how you think the array is
> organized.  Whenever you make a significant reshape you are telling the
> computer to re-interpret the chunk of data in a different way, it makes
> a big difference as to how you think about that chunk of data.  Do you
> think of it as organized rows-first (C-order) or columns-first
> (Fortran-order).   The order argument allows you to specify how you
> think about it and indicates the 1-d indexing order of the array.  It
> also fills in the newly-shaped array in exactly that same order.
> Semantically, one could technically separate those two concepts and have
> one order argument that specifies how you think about the input and
> another that specifies how you think about the output.   But, I really
> didn't want to go there and couldn't see a real advantage to that.  So,
> the single order argument specifies how you think about both.

Thanks for the detailed overview (we should put it on the wiki).
Another thing I'm wondering about: when exactly does reshape need to
make copies?

One last case, which confuses me still (probably because it is
04:16am):

In [41]: x = N.array([[0,1,2],[3,4,5]],order='F')

In [42]: x
Out[42]:
array([[0, 1, 2],
[3, 4, 5]])

I assume the data is now stored in memory as

[0 3 1 4 2 5] (column-wise)

If I now do

x.reshape((3,2),order='C')

i.e. take that block of memory, assume it is in 'C' order, and make
its shape (3,2), I expect

[[0 3]
[1 4]
[2 5]]

but get

[[1 2]
[3 4]
[5 6]]

I'm obviously missing something trivial -- I'll try again tomorrow.

Cheers
Stéfan

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier