f2py:一些返回数组不变/空 [英] f2py: some of returned arrays are unchanged/empty

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

问题描述

你好我使用f2py包裹LAPACK例行dgesvd,通过编译dgesvd.f文件和链接它反对llapack,作为解释<一个href=\"http://stackoverflow.com/questions/10766969/f2py-wrapping-fortran-module-which-makes-use-of-subrouines-distributed-in-diffe\">here

Hi I'm using f2py to wrap the lapack routine dgesvd, by compiling the dgesvd.f file and linking it against llapack, as explained here

根据文档字符串,所述dgesvd模块具有签名:

according to the docstring, the dgesvd module has the signature:

dgesvd - Function signature:
  dgesvd(jobu,jobvt,m,n,a,s,u,vt,work,lwork,info,[lda,ldu,ldvt])
Required arguments:
  jobu : input string(len=1)
  jobvt : input string(len=1)
  m : input int
  n : input int
  a : input rank-2 array('d') with bounds (lda,*)
  s : input rank-1 array('d') with bounds (*)
  u : input rank-2 array('d') with bounds (ldu,*)
  vt : input rank-2 array('d') with bounds (ldvt,*)
  work : input rank-1 array('d') with bounds (*)
  lwork : input int
  info : input int
Optional arguments:
  lda := shape(a,0) input int
  ldu := shape(u,0) input int
  ldvt := shape(vt,0) input int

然后我用下面的OCDE调用模块:

Then I use the following ocde to call the module:

mat = rand(20,30)
out_u,out_s,out_vh = zeros((20,20)), zeros((20,)), zeros((30,30))
rows, cols = shape(mat)
workspace = zeros((rows*cols))
out_info = 0

dgesvd(jobu='S', 
    jobvt='S',
    m=rows,
    n=cols,
    a=mat,
    s=out_s,
    u=out_u,
    vt=out_vh,
    work=workspace,
    lwork=rows*cols,
    info=out_info)

这使我保存在 out_s 正确的奇异值,但矩阵 out_u out_vh 仍然只能用零填充,做我必须做一些不同的东西,以获得左/右奇异向量呢?

Which gives me the correct singular values stored in out_s, but the matrices out_u and out_vh are still only filled with zeros, do I have to do something differently to get the left/right singular vectors as well ?

在code贯穿而过,没有任何错误,这意味着 out_info 0

The code runs through, without any error, which means that out_info is 0.

(对于jobu和jobvt参数'S'告诉程序来计算只有第一分钟(M,N)的奇异向量。将其更改为'A',没有任何区别)

(The argument 'S' for jobu and jobvt tells the routine to compute only the first min(m,n) singular vectors. Changing it to 'A', does not make any difference)

任何想法是非常AP preciated!
谢谢
米莎

Any Ideas are highly appreciated! Thanks Mischa

推荐答案

f2py 创建Python包装与Fortran code,但所创建的Python函数不拟被称为酷似的Fortran code。在Fortran中,通常的做法是通过输出变量作为参数传递给子程序。这不是Python化的;此外,蟒蛇真的不支持以同样的方式为Fortran的子程序。出于这个原因, f2py 将您的Fortran子程序成一个Python函数,因此所有的输出变量的返回的由函数,不包括通话签名。所以,你必须调用该函数是这样的:

f2py creates python wrappers to Fortran code, but the python functions that are created are not intended to be called exactly like Fortran code. In Fortran, it is common practice to pass the output variables as an argument to the subroutine. This is not "pythonic"; besides, python doesn't really support subroutines in the same way as Fortran. For this reason, f2py turns your Fortran subroutine into a python function, and thus all output variables are returned by the function, not included in the call signature. So, you would have to call the function this way:

out_s, out_u, out_vh, info = dgesvd(jobu='S', 
                                    jobvt='S',
                                    m=rows,
                                    n=cols,
                                    a=mat,
                                    work=workspace,
                                    lwork=rows*cols)

然而,LAPACK例程写入FORTRAN77,所以它不具有任何意图声明为输入/输出变量。 f2py 使用意图声明来找出哪些变量作为输入,并要返回作为输出。根据您发布的函数签名, f2py 已假定所有变量输入,这是不是你想要的。出于这个原因,我建议编写自己的Fortran 90的包装程序调用 dgesvd ,这样您就可以添加意图声明自己给 f2py 一些提示。我个人也将使用该包装来分配工作数组传递给 dgesvd ,这样你就不必从蟒蛇的传递。 f2py 究竟如何确定输入/输出签名解释的这里(有三种方法可以做到这一点,我preFER第三)。

However, the LAPACK routine is written in FORTRAN77, so it does not have any INTENT declarations for the input/output variables. f2py uses the INTENT declarations to figure out which variables are used as input, and which are to be returned as output. Based on the function signature that you posted, f2py has assumed that all variables are input, which is not what you want. For this reason, I recommend writing your own Fortran 90 wrapper routine that calls dgesvd, so that you can add INTENT declarations yourself to give f2py some hints. I personally would also use the wrapper to allocate the work array to pass to dgesvd so that you don't have to pass it in from python. Exactly how f2py determines the input/output signature is explained here (there are three ways to do it, I prefer the third).

这篇关于f2py:一些返回数组不变/空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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