f2py: some of returned arrays are unchanged/empty

好久不见. 提交于 2019-12-04 21:28:10

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)

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

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