如何获取先验未知数组作为Fortran中函数的输出 [英] How to get priorly-unknown array as the output of a function in 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中:
The questions are in Fortran:
- 如何声明y(?)?
- 如何为x声明预定义值
- 如何避免显示尺寸信息n
- how to declare y(?)?
- how to declare predefined values for x
- how to avoid dimension info n
如果定义为 y(n),则为1,输出将为:
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's.
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.
更新: 可以在此处找到链接的问题.
Update: A linked question could be found at here.
推荐答案
我希望有一个真正的Fortran程序员,但是在没有更好建议的情况下,我只会指定形状而不是x(:)
的大小,请使用临时数组temp(size(x))
,并使输出y allocatable
.然后在第一遍之后,allocate(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.的回答,这是该函数的修订版,其增加了 temp y
的过度分配. 像之前一样,将结果最后复制到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屋!