# [SciPy-User] F_CONTIGUOUS and C_CONTIGUOUS

Pauli Virtanen pav+sp@iki...
Fri Jan 29 07:32:24 CST 2010

```Fri, 29 Jan 2010 13:00:55 +0100, Ramon Crehuet wrote:
> I have some doubts about the meaning of F_CONTIGUOUS and C_CONTIGUOUS. I
> thought thay refered to storing matrices "in rows" or "in columns",
> but...  Imagine 2 arrays:
> y=np.zeros((10000, 10))
> y2=np.zeros((10000, 10), order='F')
>
> I can understand the y.flags and y2.flags, however I would expect
> y[0,:].flags to be F_CONTIGUOUS False, because it is the last index
> which is changing. And y[:,0].flags to be C_CONTIGUOUS False, because
> this is a column of that matrix. I am wrong in boths.

Both y[0,:] and y[:,0] are 1-d arrays. For 1-d arrays, there is no
distinction between Fortran-contiguous and C-contiguous: an 1-d array is
either contiguous or not.

> Similarly, I don'y understand why:
> In [114]: y2[:,0].flags
> Out[114]:
>   C_CONTIGUOUS : True
>   F_CONTIGUOUS : True

Here, the elements span a contiguous block of memory.
The memory layout of y2 is (r1c2 = element (row=1, column=2))

[ r0c0 r1c0 ... r0c1 r1c1 ... ]

Note that with order='C' it would be instead

[ r0c0 r0c1 ... r1c0 r1c1 ... ]

Consequently, y2[:,0] has here the layout

[ r0c0 r1c0 ... rnc0 ]

where all elements occur immediately after each other. Hence, it's
contiguous.

> and:
> In [113]: y2[0,:].flags
> Out[113]:
>   C_CONTIGUOUS : False
>   F_CONTIGUOUS : False

Now y2[0,:] has the layout

[ r0c0 ### ... r0c1 ### ... ]

ie., all elements except those belonging to the first row are skipped.
The memory layout is discontiguous.

> So I guess I have some deep minunderstanding about the meaning of this
> flags and I would appreciate some enlightening. Thanks!

The main point is (very tersely) explained here:

http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html#internal-
memory-layout-of-an-ndarray

```