2

Using F2Py to compile Fortran routines being suitable to be used within Python, the following piece of code is successfully compiled configured gfortran as the compiler while using F2Py, however, at the time of invoking in Python it raises a runtime error!
Any comments and solutions?

function select(x) result(y)
   implicit none
   integer,intent(in):: x(:) 
   integer:: i,j,temp(size(x))
   integer,allocatable:: y(:)
   j = 0
   do i=1,size(x)
      if (x(i)/=0) then
         j = j+1
         temp(j) = x(i)
      endif
   enddo
   allocate(y(j))
   y = temp(:j)
end function select

A similar StackOverflow post can be found here.

4

2 回答 2

0

看看这篇文章http://www.shocksolution.com/2009/09/f2py-binding-fortran-python/,尤其是例子和含义

!f2py depend(len_a) a, bar

但是,作者并没有涉及到生成不同大小的数组的问题。

于 2011-12-15T09:42:32.883 回答
-2

你的函数应该被声明:

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
   ...

事实上,Python 是用 C 编写的,您的 Fortran 例程必须遵循 Iso_C_binding 的规则。特别是,禁止使用假定的形状数组。

无论如何,我更喜欢一个子程序:

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

ny 是实际用于 y 的大小 (ny <= nx)

于 2014-03-13T01:02:42.433 回答