arrays - Fortran 函数返回具有非平凡边界的可分配数组
问题描述
根据对象,我正在努力理解返回可分配数组的函数背后的逻辑。我喜欢这种结构,因为与子例程相比它更清晰,而且 fortran 中的纯函数是编写干净的函数式编程代码的绝佳方式。
假设我必须编写一个简单的函数,返回一个具有任意边界的索引数组,例如在这个程序中:
program test_allocatable_functionReturn
implicit none
integer, allocatable :: fun(:),sub(:),noa(:)
integer, parameter :: low = -4
integer, parameter :: hi = 3
call testsub(sub,low,hi)
fun = testfun(low,hi)
noa = testfun_noalloc(low,hi)
print '(4(a,i3),a)', 'testsub: lbound=',lbound(sub),'(expected = ',low,'), ubound=',ubound(sub),'(expected = ',hi,')'
print '(4(a,i3),a)', 'testfun: lbound=',lbound(fun),'(expected = ',low,'), ubound=',ubound(fun),'(expected = ',hi,')'
print '(4(a,i3),a)', 'no alloc: lbound=',lbound(noa),'(expected = ',low,'), ubound=',ubound(noa),'(expected = ',hi,')'
contains
pure function testfun_noalloc(low,hi) result(array)
integer, intent(in) :: low,hi
integer :: array(low:hi)
integer :: i
forall(i=low:hi) array(i) = i
end function testfun_noalloc
pure function testfun(low,hi) result(array)
integer, allocatable :: array(:)
integer, intent(in) :: low,hi
integer :: i
allocate(array(low:hi))
forall(i=low:hi) array(i) = i
end function testfun
pure subroutine testsub(array,low,hi)
integer, intent(out), allocatable :: array(:)
integer, intent(in) :: low,hi
integer :: i
allocate(array(low:hi))
forall(i=low:hi) array(i) = i
end subroutine testsub
end program
我通过三种方式实现了它:
- 返回可分配数组的函数 (
testfun
) - 子程序 (
testsub
) - 返回静态数组的函数 (
testfun_noalloc
)
该子例程对返回数组进行操作,并正确分配它。(-4:3)
在示例中,应返回一个大小合适的数组。在任一实现中,该函数都返回一个(1:hi-low+1)
-sized 数组:
testsub: lbound= -4(expected = -4), ubound= 3(expected = 3)
testfun: lbound= 1(expected = -4), ubound= 8(expected = 3)
no alloc: lbound= 1(expected = -4), ubound= 8(expected = 3)
为什么会这样?我得到这样一个事实,即在将函数返回值分配给我的 LHS 数组时,fortran 可能会重新分配数组,但即便如此,为什么它没有分配正确的边界?我知道在将静态数组传递给具有 f2003 风格的 lhs 重新分配的可分配对象时可能会发生这种情况,但是以可分配数组作为输入,我希望保留边界信息。我在这里错过了什么吗?顺便说一句,这个例子是用 gfortran 9.2.0 编译的。
谢谢,费德里科
解决方案
推荐阅读
- python - 如何解决 anaconda 虚拟环境问题?
- linux - 允许 Apache config 使用 wordpress 登录来保护 linux 服务器上的私有目录
- c# - 如何从表的html列中获取数据并传递给C#
- reactjs - React Native - 在 maxlength 之后移动到下一个输入
- python - 如何用 allennlp 训练语义角色标签?
- python - 创建一个新数据框,其中某些行是另一个数据框中单行的扩展版本
- android - Android 快捷方式在 VIVO 10 中不起作用
- xamarin - 设置默认页面并添加标签点击事件
- python - 如何从列表中随机打印不带括号、引号和逗号的单词
- python - 如何将每个条形的值显示到 matplotlib 中的条形图中