# What does Fortran order mean?

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 17 12:01:08 CDT 2006

```Stefan van der Walt wrote:

>On Tue, Oct 17, 2006 at 10:01:51AM -0600, Travis Oliphant wrote:
>
>
>>Charles R Harris wrote:
>>
>>
>>
>>>Travis,
>>>
>>>I note that
>>>
>>>
>>>
>>>>>>a = arange(6).reshape(2,3,order='F')
>>>>>>a
>>>>>>
>>>>>>
>>>array([[0, 1, 2],
>>>       [3, 4, 5]])
>>>
>>>Shouldn't that be 3x2? Or maybe [[0,2,4],[1,3,5]]? Reshape is making a
>>>copy, but flat, flatten, and tostring all show the elements in 'C'
>>>order. I ask because I wonder if changing the order can be used to
>>>prepare arrays for input into the LaPack routines.
>>>
>>>
>>The order argument to reshape means (how should the big-chunk of data be
>>interpreted when you reshape).  So, yes, this should be
>>[[0,2,4],[1,3,5]].  It is a bug that it does not do the right thing in
>>this case.
>>
>>
>
>A bit counter-ituitive (I somehow expect reshape to return an array
>that satisfies all the constraints specified as parameters -- i.e.
>shape and order in memory), but consistent with Numeric.
>
>
Perhaps, but that is not what reshape does.  When you are reshaping an
array, unless you are just adding ones to the shape somewhere or aren't
actually changing the shape, then you are implicitly thinking about the
array as a 1-d chunk of memory in some order.   The default is C-order.
The order argument let's you think about it differently.

>What confuses me is that, if you call the array constructor, you get
>
>In [2]: N.array(N.array([[1,2,3],[4,5,6]]),order='F')
>Out[2]:
>array([[1, 2, 3],
>       [4, 5, 6]])
>
>so here 'order' means something else.
>
>

Not really.  I can't imagine what else you would expect here.  You have
specified the array explicitly.  The order argument only specifies how
the array will be arranged in memory.  It is an implementation detail.
The user is allowed some control in order to interface to compiled code
more effectively.

>So then you do
>
>x = N.array([[0,1,2],[3,4,5]],order='F')
>x.reshape((2,3),order='C')
>
>and you get
>
>array([[0, 1, 2],
>       [3, 4, 5]])
>
>
>

I don't see the problem.

The first line gives you an array:

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

which in memory is laid out as [0,3,1,4,2,5]

The second line says reshape into a (2,3) array but this is exactly the
same shape as the input array so in fact only a new view of the same
data is given to you.   The fact that you specify order is
inconsequential since you aren't really changing the shape, it doesn't
matter.

The reshape command is not intended to alter the underlying ordering of
an input array.   The ordering of an array is an implementation detail
that shouldn't matter to the user unless you are trying to interface to
compiled code.   The require command can help you get the right kind of
array for that purpose.

In general, arrays can have arbitrary strides.  C-order and
Fortran-order are just two special cases of that generality.   So, you
cannot think of any array as just being either in C-order or
Fortran-order.

-Travis

-------------------------------------------------------------------------
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