在Fortran ord中使用Python Numpy创建列向量

2024-09-26 22:53:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个3行1列的numpy数组(用于创建区域时的pycgn)。你知道吗

当问它是否是NPY.isfortran(X)时,这一定是真的。你知道吗

我试过好几种方法,但都不管用。你知道吗

例如

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

Tags: 方法numpyfalse区域a1zerosorder数组
3条回答

引用^{} documentation(我的重点):

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.

这里有几点需要注意:

  1. 向量是Fortran和C连续的,因此函数返回false。你知道吗
  2. 功能已过时

有一种检查数组的替代方法,应该适合您的情况:

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

不能编辑标志。不过,有一些技巧。例如,可以使用转置将2D C数组转换为F数组(尽管使用交换的维度):

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
# ....

pyCGNS无法创建列向量。你知道吗

我用C语言编程CGNS报头(base和zone)解决了这个问题

然后在pyCGNS中从C程序加载创建的文件并在其上构建。你知道吗

函数是obsolete。它返回:

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

您的数组是fortran连续的,C连续的。你知道吗

相关问题 更多 >

    热门问题