Shaker Abdolrahman
Shaker Abdolrahman

Reputation: 1

Creating a Column-Vector in Python Numpy in Fortran order

I need a numpy array with 3-rows and 1 Column (for pyCGNS when creating a Zone).

And This Must be true when asking if it is NPY.isfortran(X).

I tried several ways, but none worked.

e.g.

a1 = NPY.zeros((3,1),order='F')
print NPY.isfortran(a1) 
->False

Upvotes: 0

Views: 266

Answers (3)

Shaker Abdolrahman
Shaker Abdolrahman

Reputation: 1

pyCGNS can not create a column vector.

I solved the problem by programming the CGNS header (base and zone) in C.

And then loaded the created file from the C program in pyCGNS and built upon it.

Upvotes: 0

MB-F
MB-F

Reputation: 23637

Quoting the isfortran documentation (emphasis mine):

Returns True if the array is Fortran contiguous but not C contiguous.

This function is obsolete and, because of changes due to relaxed stride checking, its return value for the same array may differ for versions of NumPy >= 1.10.0 and previous versions. If you only want to check if an array is Fortran contiguous use a.flags.f_contiguous instead.

There are a few things to note here:

  1. Your vector is both Fortran and C contiguous, so the function returns false.
  2. The function is obsolete

There is an alternative way to check the array, that should work for your case:

a = np.zeros((3, 1), order='F')
print(a.flags)
# C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# UPDATEIFCOPY : False

print(a.flags.f_contiguous)
# True

You cannot edit flags. However, there are a few tricks. For example, you can use transpose to turn a 2D C array into an F array (albeit with swapped dimensions):

print(np.ones((3, 2), order='C').flags)
#  C_CONTIGUOUS : True
#  F_CONTIGUOUS : False
# ....

print(np.ones((3, 2), order='C').T.flags)
#  C_CONTIGUOUS : False
#  F_CONTIGUOUS : True
# ....

Upvotes: 1

B. M.
B. M.

Reputation: 18638

The function is obsolete. it returns:

True if the array is Fortran contiguous BUT not C contiguous.

In [421]: np.isfortran(a1)
Out[421]: False

In [422]: a1.flags
Out[422]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

Your array is fortran contiguous AND C contiguous.

Upvotes: 1

Related Questions