如果我打算通过f2py将它们传递给布尔NumPy数组,应该如何键入Fortran变量?我已经尝试了integer*1logical*1,但是它们都表明该数组已复制。

例如,如果我编译文件foo.f95,其中包含:

subroutine foo(x, n)
    logical*1 x(n)
    !f2py intent(in) x
    !f2py intent(hide), depend(x) :: n=shape(x,0)
    ...
end subroutine


使用f2py -c -m foo foo.f90 -DF2PY_REPORT_ON_ARRAY_COPY=1并运行类似的内容:

import numpy as np
import foo
x = np.random.randn(100) < 0
foo.foo(x)


它打印

copied an array: size=100, elsize=1


如果将logical*1更改为integer*1,则会得到相同的结果。 Fortran文件中布尔数组的正确类型是什么,以便不复制该数组?

请注意,这不是内存连续性的问题,因为数组是一维的-foo.foo(np.asfortranarray(x))打印相同的复制消息。

最佳答案

从一些实验(*)来看,似乎Python / f2py将np.int8视为与logical*1兼容,而np.boolnp.bool8出于某种原因。将print *, "(fort) x = ", x插入foo.f90后,我们得到:

>>> foo.foo( np.array( [ True, False, False ], dtype=np.bool ) )
copied an array: size=3, elsize=1
 (fort) x =  T F F
>>> foo.foo( np.array( [ False, True, False ], dtype=np.bool8 ) )
copied an array: size=3, elsize=1
 (fort) x =  F T F
>>> foo.foo( np.array( [ False, False, True ], dtype=np.int8 ) ) # no copy
 (fort) x =  F F T


由于TrueFalse只是映射为1和0,因此在Python端使用int8数组可能会很方便。



(*)一些实验

在这里,我将f2py意向注释更改为inout,以查看是否可以从Fortran端修改数组。

foo.f90:

subroutine foo(x, n)
    use iso_c_binding
    implicit none
    integer n
    logical*1 x( n )
    ! logical x( n )
    ! logical(c_bool) x( n )

    !f2py intent(inout) x
    !f2py intent(hide), depend(x) :: n=shape(x,0)

    print *, "(fort) x = ", x
    print *, "(fort) sizeof(x(1)) = ", sizeof(x(1))
    print *, "(fort) resetting x(:) to true"
    x(:) = .true.
end subroutine


test.py:

import numpy as np
import foo

for T in [ np.bool, np.bool8,
           np.int,  np.int8,  np.int32,  np.int64,
           np.uint, np.uint8, np.uint32, np.uint64,
           np.dtype('b'), np.dtype('int8'), np.dtype('int32') ]:

    print( "-------------------------" )
    print( "dtype =", T )

    x = np.array( [ True, False, True ], dtype=T )
    print( "input x =", x )

    try:
        foo.foo( x )
        print( "output x =", x )
    except:
        print( "failed" )


logical*1结果:

-------------------------
dtype = <class 'bool'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'numpy.bool_'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'int'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int8'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.int32'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint8'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.uint32'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = int8
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = int32
input x = [1 0 1]
failed


logical的结果(默认种类):

-------------------------
dtype = <class 'bool'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'numpy.bool_'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'int'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int32'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     4
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.int64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint32'>
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     4
 (fort) resetting x(:) to true
output x = [1 1 1]
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int32
input x = [1 0 1]
 (fort) x =  T F T
 (fort) sizeof(x(1)) =                     4
 (fort) resetting x(:) to true
output x = [1 1 1]


logical(c_bool)的结果(通过iso_c_binding):

-------------------------
dtype = <class 'bool'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'numpy.bool_'>
input x = [ True False  True]
failed
-------------------------
dtype = <class 'int'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.int32'>
input x = [1 0 1]
 (fort) x =  T F F
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [65793     0     1]
-------------------------
dtype = <class 'numpy.int64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint8'>
input x = [1 0 1]
failed
-------------------------
dtype = <class 'numpy.uint32'>
input x = [1 0 1]
 (fort) x =  T F F
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [65793     0     1]
-------------------------
dtype = <class 'numpy.uint64'>
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int8
input x = [1 0 1]
failed
-------------------------
dtype = int32
input x = [1 0 1]
 (fort) x =  T F F
 (fort) sizeof(x(1)) =                     1
 (fort) resetting x(:) to true
output x = [65793     0     1]


由于某种原因,最后一个logical(c_bool)不能与上述用法一起使用...(f2py似乎将logical(c_bool)视为4个字节,而gfortran则将其视为1个字节,因此有些不一致...)

10-01 00:08