I have attempted to convert a subroutine into a function (since I have now reduced the outputs to 1).
The original subroutine was this…
subroutine girl(a, n, m)
implicit none
double precision, intent(inout) :: a(*)
integer, intent(in) :: m, n
double precision :: amax, temporaryStore
integer :: i, i1, i2, ik, j, jk, jm, k
save
do i = 1, n
amax = abs(a(i * n - n + i))
jm = i
if ((i + 1) <= n) then
do j = (i + 1), n
if (abs(a(i * n - n + j)) > amax) then
amax = abs(a(i * n - n + j))
jm = j
end if
end do
if (jm /= i) then
i1 = jm + i * n - n
i2 = i * n - n + i
do j = i, (n + m)
temporaryStore = a(i1)
a(i1) = a(i2)
a(i2) = temporaryStore
i1 = i1 + n
i2 = i2 + n
end do
end if
end if
if (a(i * n - n + i) == 0d0) then
write (6, '(a)') "error in subroutine girl"
stop 'girl'
end if
do j = 1, n
if (j /= i) then
ik = i * n - n + j
jk = i * n - n + i
do k = (i + 1), (n + m)
jk = jk + n
ik = ik + n
a(ik) = a(ik) + (-a(i * n - n + j) / a(i * n - n + i)) * a(jk)
end do
end if
end do
jk = i * n - n + i
do j = (i + 1), (n + m)
jk = jk + n
a(jk) = a(jk) * (1d0 / a(i * n - n + i))
end do
end do
return
end subroutine girl
And the new function became this…
function girl(a, n, m)
implicit none
double precision, intent(inout) :: a(:)
integer, intent(in) :: m, n
double precision :: girl(size(a))
double precision :: amax, temporaryStore
integer :: i, i1, i2, ik, j, jk, jm, k
save
do i = 1, n
amax = abs(a(i * n - n + i))
jm = i
if ((i + 1) <= n) then
do j = (i + 1), n
if (abs(a(i * n - n + j)) > amax) then
amax = abs(a(i * n - n + j))
jm = j
end if
end do
if (jm /= i) then
i1 = jm + i * n - n
i2 = i * n - n + i
do j = i, (n + m)
girl(i1) = a(i2)
girl(i2) = a(i1)
i1 = i1 + n
i2 = i2 + n
end do
end if
end if
if (a(i * n - n + i) == 0d0) then
write (6, '(a)') "error in subroutine girl"
stop 'girl'
end if
do j = 1, n
if (j /= i) then
ik = i * n - n + j
jk = i * n - n + i
do k = (i + 1), (n + m)
jk = jk + n
ik = ik + n
girl(ik) = a(ik) + (-a(i * n - n + j) / a(i * n - n + i)) * a(jk)
end do
end if
end do
jk = i * n - n + i
do j = (i + 1), (n + m)
jk = jk + n
girl(jk) = a(jk) * (1d0 / a(i * n - n + i))
end do
end do
return
end function girl
I get the following error…
stelcor.f90:3028:14:
3028 | ha = girl (ha, mh, mh + 1)
| 1
Error: Rank mismatch in argument 'a' at (1) (rank-1 and rank-2)
I’m assuming that this is due to the fact that the input is a 2 dimensional array yet appears in the original subroutine as a 1 dimensional array (which I have maintained in the new function).
Two things come to mind…
- How has the 2-dimensional array become a 1-dimensional array; how has the system dealt with this without raising an error before?
- And how do I resolve this error now that it is a function?