F2Py: Working with allocatable arrays in Fortran being invoked through Python

余生长醉 提交于 2019-12-04 15:06:47

Take a look at this article http://www.shocksolution.com/2009/09/f2py-binding-fortran-python/ , especially at the example and the meaning of

!f2py depend(len_a) a, bar

However, the author does not touch of the problem of generating a an array of different size.

Your function should be declared :

function select(n,x) result(y)
   implicit none
   integer,intent(in) :: n
   integer,intent(in) :: x(n) 
   integer :: y(n) ! in maximizing the size of y
   ...

Indeed, Python is written in C and your Fortran routine must follow the rules of the Iso_C_binding. In particular, assumed shape arrays are forbidden.

In any case, I would prefer a subroutine :

  subroutine select(nx,y,ny,y)
     implicit none
     integer,intent(in) :: nx,x(nx)
     integer,intent(out) :: ny,y(nx)

ny being the size really used for y (ny <= nx)

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