F2Py:使用Python调用Fortran中的可分配数组 [英] F2Py: Working with allocatable arrays in Fortran being invoked through Python

查看:764
本文介绍了F2Py:使用Python调用Fortran中的可分配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 F2Py 来编译 Fortran 例程适合在 Python中使用

code>,下面这段代码在使用 F2Py 时成功编译配置gfortran作为编译器,但是在调用时, Python $ <$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'函数select(x)result(y)
implicit none
整数,意图(in):: x(:)
整数:: 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

可以找到类似的StackOverflow文章这里

> http://www.shocksolution.com/2009/09/f2py-binding-fortran-python/ ,特别是在示例中,以及
<$的含义p $ p> !f2py depends(len_a)a,bar

然而,作者没有触及生成不同大小数组的问题。


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.

解决方案

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.

这篇关于F2Py:使用Python调用Fortran中的可分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆