f2py数组值函数 [英] f2py array valued functions

查看:155
本文介绍了f2py数组值函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近版本的f2py支持包装数组值的fortran函数吗?
在一些古代文献中,这不被支持。那么现在呢?



举个例子,将下面的函数保存为func.f95。

 函数func(x)
隐式无
双精度:: x(:),func(size(x))
integer :: i
i = 1,size(x)
func(i)= i * x(i)
end do
end function

我用 f2py --fcompiler = gnu95 -c -m func func.f95

编译b
$ b

然后让下面的python代码为test_func.py

  import func 
从numpy导入数组

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

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



python test_func.py

  x = [1. 2. 3. 4. 5. 6. 7. 8. 9.] 
分段错误


解决方案

f2py的机制使Fortran 子程序插入到python函数中。它不知道如何将Fortran 函数转换为python函数。我发现我需要用子程序包装所有Fortran函数,或者甚至更好地将它们重写为子程序。

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

解决方案

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.

这篇关于f2py数组值函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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