如何获取Fortran中函数的未知数组作为输出

15 投票
3 回答
12356 浏览
提问于 2025-04-17 07:01

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. 如何为x声明预定义值
  3. 如何避免维度信息n

如果第一个被定义为y(n),输出将会是:

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的一个bug,或者是我对它的使用还不够了解。我会在另一个帖子中尝试解决这个问题。

更新: 可以在这里找到一个相关的问题。

3 个回答

5

如果你问题中的例子真的是你想要做的事情,你可以使用Fortran90自带的`pack`功能:

program pack_example

implicit none

integer, dimension(6) :: x

x = (/ 1,0,2,0,0,3 /)

! you can also use other masks than 'x/=0'
write(*,*) pack(x, x/=0)

end program pack_example

这个例子程序的输出结果是: 1 2 3

13

这里有一个Fortran函数的例子,它可以返回一个可变长度的数组。这是Fortran 2003的一个新特性。在测试驱动程序中,还使用了自动分配的功能,这也是Fortran 2003的一个特点。

module my_subs

contains

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

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

    allocate ( y (1:j) )

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

    return

end function select

end module my_subs

program test

use my_subs

implicit none
integer, dimension (6) :: array = [ 5, 0, 3, 0, 6, 1 ]
integer, dimension (:), allocatable :: answer

answer = select (array)

write (*, *) size (array), size (answer)
write (*, *) array
write (*, *) answer

stop


end program test

这里有一个替代方案,它使用一个临时数组来根据需要“扩展”输出数组(函数返回值)。虽然避免了对输入数组进行两次遍历,但需要进行数组复制。Fortran 2003的另一个特性move_alloc可以减少所需的复制次数。move_alloc还负责输出数组(这里是“y”)的(重新)分配和输入数组(这里是“temp”)的释放。也许这个方法更优雅,但由于使用了多个复制,效率可能较低。这个版本可能更适合学习,而不是实际使用。@eryksun的版本只需要一次遍历和一次复制,但代价是临时数组的大小要达到最大。

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

    j = 0
    do i=1, size (x)
        if (x(i)/=0) then
            j = j+1
            allocate (temp (1:j))
            if ( allocated (y) ) temp (1:j-1) = y
            call move_alloc (temp, y)
            y(j) = x(i)
        endif
    enddo

    return

end function select
13

我希望能有真正的Fortran程序员来解答这个问题,不过在没有更好建议的情况下,我建议只指定x(:)的形状,而不指定大小。可以使用一个临时数组temp(size(x)),然后让输出的y变成allocatable。在第一次处理后,使用allocate(y(j))来分配空间,并把临时数组中的值复制到y里。不过我必须强调,我并不是Fortran程序员,所以不确定这个语言是否有可以动态增长的数组,或者是否有相关的库。

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.的回答,这里有一个修订版的函数,它通过过度分配来扩展temp y和之前一样,它在最后把结果复制到y中。 结果发现,不需要在最后显式地分配一个新的数组,而是可以通过赋值自动完成。

    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

撰写回答