F2py function functions with array

Do the latest versions have f2py support functions for the fortran array? Some ancient documents did not support this. How about this now?

Let, 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 will 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

Exit from
python test_func.pythere

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

The f2py mechanism turns Fortran routines into python functions. He does not understand how to turn a Fortran function into a python function. I found that I needed to wrap all Fortran functions with a subroutine or, better yet, rewrite them as subroutines.

+2
source

All Articles