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

发布于 2024-12-17 16:54:32 字数 1280 浏览 5 评论 0原文

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. 如何避免

1 的维度信息 n(如果定义为 y(n)),输出将是:

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

这是不希望的!
!--------------------------------
评论:
1- 所有给出的答案在这篇文章中都很有用。特别是 MSB 和 eryksun 的。
2- 我尝试根据我的问题调整想法并使用 F2Py 进行编译,但没有成功。我已经使用 GFortran 调试了它们,并且全部成功。这可能是 F2Py 中的一个错误,或者是我不知道如何正确使用它的问题。我将尝试在另一篇文章中讨论这个问题。

更新: 可以在此处找到链接的问题。

In Python:

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

that works as:

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

to be translated into 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

The questions are in Fortran:

  1. how to declare y(?)?
  2. how to declare predefined values for x
  3. how to avoid dimension info 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

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.

Update:
A linked question could be found at here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情域 2024-12-24 16:54:32

我希望有一个真正的 Fortran 程序员出现,但在没有更好的建议的情况下,我只会指定 x(:) 的形状而不是大小,使用临时数组 temp(size (x)),并使输出 y 可分配。然后,在第一遍之后,allocate(y(j)) 并从临时数组中复制值。但我不能强调我不是 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

编辑:

根据 MSB 的回答,这是该函数的修订版本,该函数通过过度分配来增加 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

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

Edit:

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
深爱成瘾 2024-12-24 16:54:32

下面是返回可变长度数组的 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

Here is an example of a Fortran function returning a variable length array. This is a feature of Fortran 2003. Also used in the test driver is automatic allocation on assignment, another Fortran 2003 feature.

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

Here is an alternative solution that uses a temporary array to "grow" the output array (function return) as needed. While two passes through the input array are avoided, array copies are required. Another Fortran 2003 feature, move_alloc, reduces the number of copies needed. move_alloc also takes care of the (re)allocation of the output array (here "y") and deallocation of the input array (here "temp"). Perhaps this is more elegant, but it is probably less efficient since multiple copies are used. This version is probably more educational then useful. @eryksun's version uses one pass and one copy, at the expense of making the temporary array full size.

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
听风吹 2024-12-24 16:54:32

如果您问题中的示例确实是您想要做的,您可以使用 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

If the example in your question really is what you want to do, you can use the Fortran90 intrinsic `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

The output of the example program is: 1 2 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文