Tomas Sedlacek
Tomas Sedlacek

Reputation: 365

How can be numpy array C_CONTIGUOUS as well as F_CONTIGUOUS

I thought I understand the concept of row-major (C_CONTIGUOUS) and column-major (F_CONTIGUOUS) memory alignment of numpy arrays. I thought that those two flags are mutually exclusive. But then I saw an array where both these flags were set to True.

In particular I tried the following commands:

b = np.arange(8,dtype='int8')
b.reshape(2,4,order='F')
b.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False

I would expect that after command b.reshape(2,4,order='F'), the array will have F_CONTIGUOUS set to True and C_CONTIGUOUS set to False.

Can someone please explain me what is going on?

Thanks.

Upvotes: 3

Views: 4075

Answers (1)

tel
tel

Reputation: 13999

What's going on? Less than you think. ndarry.reshape is not an in-place operation. Thus this:

b = np.arange(8,dtype='int8')
b.reshape(2,4,order='F')
print(b.shape)

gives this as output:

(8,)

In other words, b is still 1D, and so can have both orders. Saving the result of reshape to a new array gives the result that you expected:

b = np.arange(8,dtype='int8')
c= b.reshape(2,4,order='F')
print(c.flags)

Output:

C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False

Upvotes: 1

Related Questions