18

I am using quite a lot of fortran libraries to do some mathematical computation. So all the arrays in numpy need to be Fortran-contiguous.
Currently I accomplish this with numpy.asfortranarray().

My questions are:

  1. Is this a fast way of telling numpy that the array should be stored in fortran style or is there a faster one?
  2. Is there the possibility to set some numpy flag, so that every array that is created is in fortran style?
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Woltan
  • 13,723
  • 15
  • 78
  • 104

2 Answers2

14

Use optional argument order='F' (default 'C'), when generating numpy.array objects. This is the way I do it, probably does the same thing that you are doing. About number 2, I am not aware of setting default order, but it's easy enough to just include order optional argument when generating arrays.

milancurcic
  • 6,202
  • 2
  • 34
  • 47
7

Regarding question 2: you may be concerned about retaining Fortran ordering after performing array transformations and operations. I had a similar issue with endianness. I loaded a big-endian raw array from file, but when I applied a log transformation, the resultant array would be little-endian. I got around the problem by first allocating a second big-endian array, then performing an in-place log:

b=np.zeros(a.shape,dtype=a.dtype)
np.log10(1+100*a,b)

In your case you would allocate b with Fortran ordering.

bdforbes
  • 1,486
  • 1
  • 13
  • 31