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.
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.
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.
If the example in your question really is what you want to do, you can use the Fortran90 intrinsic `pack':
The output of the example program is: 1 2 3
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 arraytemp(size(x))
, and make the output yallocatable
. 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.Edit:
Based on M.S.B.'s answer, here's a revised version of the function that grows
tempy
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.