The NumPy Fortran-ordering quiz

Travis Oliphant oliphant.travis at ieee.org
Wed Oct 18 00:35:07 EDT 2006


Stefan van der Walt wrote:
> 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.
>   

I think I see what is going on and where people are getting tripped up.  
  You have to remember, that to NumPy it doesn't semantically matter 
what the "ordering" of the array is.  There is no guarantee that C- 
order or Fortran-order is *ever* preserved through an operation.    
Because, in fact the general memory model of the array has no defined 
"order".  It's defined by the strides array.  It just so happens that 
two special-cases are tracked so that we can call out to compiled 
routines that expect contiguous arrays more easily.  

So, your mistake is trying to think that the "block" of memory is [0, 3, 
1, 4, 2, 5]  when you pass the Fortran-order array to the reshape 
method.    While this is true, and it means that you will save a copy if 
you passed this off to a Fortran routine, the reshape command does not 
use this information in determining how to "think-about" the input 
array.  In fact,  the reshape method does not allow any way to specify 
the order of the "input" array (self) separately from the order of the 
output array.   The order argument indicates the defined order of both 
input and output.  You might think that the order of self should be used 
as the order of  the input array.  The problem with this is, again, that 
a general array does not have a defined "order".  What should be used as 
the assumed "order" for an un-strided array?  You're left with an 
unresolved question.

To avoid two input order arguments, I just let order indicate the order 
for both the input and the output arrays.  We could provide both, but 
this seems a bit over-done as the same could be accomplished by 
separately raveling the input to 1-d and then specifying the order 
argument on reshape.

Please continue to question.  All the code needs as much review as it 
can get.

Best regards,


-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
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list