相关疑难解决方法(0)

f2py:在与python接口时指定fortran中的实际精度?

我正在玩f2py.我对numpy内在类型与fortran 90类型有点混淆.在与python进行交互时,似乎我只能在fortran 90中使用单精度实数.让我用一个例子来说明:

假设我有这个fortran 90模块test.f90,用f2py编译并在python中导入:

module test
implicit none

integer, parameter :: sp = selected_real_kind(6,37) ! single precision
integer, parameter :: dp = selected_real_kind(15,307) ! double precision

real(sp) :: r_sp = 1.0
real(dp) :: r_dp = 1.0_dp
end module
Run Code Online (Sandbox Code Playgroud)

我这样编译:

f2py -c -m test test.f90

然后,在python中:

>>> import test
>>> test.test.r_sp
array(1.0, dtype=float32)
>>> test.test.r_dp
array(1.0)
Run Code Online (Sandbox Code Playgroud)

IOW,似乎f2py不接受双精度.当从python传递输入到fortran 90子例程时,这变得更加成问题.假设我将模块扩展到:

module test

implicit none
integer, parameter :: sp = selected_real_kind(6,37) ! single precision
integer, parameter :: dp = selected_real_kind(15,307) ! double precision …
Run Code Online (Sandbox Code Playgroud)

python fortran90 f2py

8
推荐指数
1
解决办法
3613
查看次数

标签 统计

f2py ×1

fortran90 ×1

python ×1