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:
- how to declare y(?)?
- how to declare predefined values for x
- 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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…