如何获得而先行 - 未知的数组作为函数的Fortran语言输出 [英] How to get priorly-unkown array as the output of a function in Fortran

查看:271
本文介绍了如何获得而先行 - 未知的数组作为函数的Fortran语言输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Python


def select(x):
    y = []
    for e in x:
        if e!=0:
            y.append(e)
    return y

这是工作方式:


x = [1,0,2,0,0,3]
select(x)
[1,2,3]

被翻译成的Fortran


function select(x,n) result(y)
    implicit none
    integer:: x(n),n,i,j,y(?)
    j = 0
    do i=1,n
        if (x(i)/=0) then
            j = j+1
            y(j) = x(i)
        endif
    enddo
end function

的问题是用Fortran:结果
1 如何申报Y(?)?结果
2 - 如何为申报predefined值x 结果
3 如何避免维信息ñ

1,如果它被定义为 Y(N)输出将是:

for 1 if it is defined as y(n) the output will be:


x = (/1,0,2,0,0,3/)
print *,select(x,6)
1,2,3,0,0,0

这是不希望!结果
!-------------------------------结果
评论:结果
1 - 所有给出的答案都在这个职位很有用的。特别M.S.B和eryksun的。结果
2 - 我试图去适应的思想我的问题,并与 F2Py 编译但它并不成功。我一直在使用GFortran已经调试他们和所有的成功。这可能是在 F2Py 或东西,我不知道正确使用它的错误。我会尽量覆盖在另一篇文章这个问题。

which is not desired!
!-------------------------------
Comments:
1- All given answers are useful in this post. Specially M.S.B and eryksun's.
2- I tried to adapt the ideas for my problem and compile with F2Py however it was not successful. I had already debugged them using GFortran and all were successful. It might be a bug in F2Py or something that I don't know about using it properly. I will try to cover this issue in another post.

更新:
链接的问题,可以在<一个找到href=\"http://stackoverflow.com/questions/8487043/f2py-working-with-allocatable-arrays-in-fortran-being-invoked-through-python\">here.

推荐答案

我希望一个真正的Fortran编程到来,但在没有更好的建议,我只会指定的形状和体积不大 X(:),使用临时数组温度(尺寸(X)),使输出y 可分配。然后第一遍后,分配(Y(J)),并从临时数组复制值​​。但我不能强调不够,我不是一个Fortran程序员,所以如果语言有一个可增长的数组或我不能说,如果不存在对后者的库。

I hope a real Fortran programmer comes along, but in the absence of better advice, I would only specify the shape and not the size of x(:), use a temporary array temp(size(x)), and make the output y allocatable. Then after the first pass, allocate(y(j)) and copy the values from the temporary array. But I can't stress enough that I'm not a Fortran programmer, so I can't say if the language has a growable array or if a library exists for the latter.

program test
    implicit none
    integer:: x(10) = (/1,0,2,0,3,0,4,0,5,0/)
    print "(10I2.1)", select(x)

contains

    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

end program test


编辑:

根据M.S.B.的回答,这里的<击>温度 ,生长与过度分配功能的修订版。 <击>由于之前副本的结果为y结尾。原来我没有必要在最后的大小来显式地分配一个新的数组。相反,它可以自动赋值来完成。

Based on M.S.B.'s answer, here's a revised version of the function that grows temp y with over-allocation. As before it copies the result to y at the end. It turns out i's not necessary to explicitly allocate a new array at the final size. Instead it can be done automatically with assignment.

    function select(x) result(y)
        implicit none
        integer, intent(in):: x(:) 
        integer:: i, j, dsize
        integer, allocatable:: temp(:), y(:)

        dsize = 0; allocate(y(0))

        j = 0
        do i = 1, size(x)
            if (x(i) /= 0) then
                j = j + 1

                if (j >= dsize) then         !grow y using temp
                    dsize = j + j / 8 + 8 
                    allocate(temp(dsize))
                    temp(:size(y)) = y
                    call move_alloc(temp, y) !temp gets deallocated
                endif

                y(j) = x(i)
            endif
        enddo
        y = y(:j)
    end function select

这篇关于如何获得而先行 - 未知的数组作为函数的Fortran语言输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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