Reputation: 364
I am trying to write a subroutine that can take as input a one-dimensional array OR a two-dimensional array. How can I declare that the input of the subroutine can be either be a vector or a matrix?
If I do this:
SUBROUTINE TEST1(x)
REAL, INTENT(IN) :: x(:)
<do something>
END SUBROUTINE TEST1
I clearly cannot pass a matrix as an input in the subroutine. A non-elegant solution could be to pass the matrix in vectorized form and then re-arrange it in matrix form inside the subroutine (I would need a couple of extra inputs, of course). Is there a better way of doing this?
Upvotes: 1
Views: 337
Reputation: 724
With Intel you can also use MAP/UNION, so you can pass the 1d as a one 1d, and also pass the 2D as it's MAP/UNION 1D version.
You may want to use RESHAPE, but usually it is not needed, depending on what you are doing.
Your comment of "clearly cannot pass in a matrix" seems counter to what I know. If it is 2D, and always 2D, then there is no issue whatsoever. You can pass any rank in.
Upvotes: 0
Reputation: 6989
creating a generic interface seems a pretty clean way to do it. (per comment, but I though worth writing up )
module gen
interface test1
module procedure t1,t2
end interface
contains
subroutine t1(y)
real y(:)
write(*,*)'shape is',shape(y)
y=2*y
end subroutine
subroutine t2(y)
real y(:,:)
write(*,*)'shape is',shape(y)
y=2*y
end subroutine
end module
use gen
real m(4),n(3,3)
m=4
n=3
call test1(m)
call test1(n)
end
Upvotes: 1