f2py array valued functions

我只是一个虾纸丫 提交于 2020-01-02 03:44:06

问题


Do recent versions of f2py support wrapping array-valued fortran functions? In some ancient documentation this wasn't supported. How about it now?

Let's for example save the following function as func.f95.

function func(x)
    implicit none
    double precision :: x(:),func(size(x))
    integer :: i
    do i=1,size(x)
        func(i) = i*x(i)
    end do
end function

I compile this with f2py --fcompiler=gnu95 -c -m func func.f95

Then let the following python code be test_func.py

import func
from numpy import array

x = array(xrange(1,10),dtype='float64')
print 'x=',x

y = func.func(x)
print 'func(x)=',y

The output from
python test_func.py is

x= [ 1.  2.  3.  4.  5.  6.  7.  8.  9.]
Segmentation fault

回答1:


The mechanism of f2py turns Fortran subroutines into python functions. It doesn't understand how to turn a Fortran function into a python function. I have found that I need to wrap all Fortran functions with a subroutine, or even better, rewrite them as subroutines.



来源:https://stackoverflow.com/questions/10913003/f2py-array-valued-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!