Consider that you have a square matrix M(N,N) and you want to sum all pairs such that:
M(i,j)*M(i+1,j)+M(i,j)*M(i,j+1)
to do this, the easiest way is to compute:
INTEGER i,j,N, SUMT
INTEGER M(100,100), c(101)
N=100
SUMT = 0
do j=1,N
c(j) = j
end do
c(N+1)=1
do j=1,N
do i=1,N
SUMT = SUMT + M(i,j)*M(c(i+1),j)+M(i,j)*M(i,c(j*1))
end do
end do
NOTE: c is a fast way to apply periodic boundary condition.
In my problem, for a 3D system { M(N,N,N) } I should do the following:
M(i,j,k)*M(i+1,j,k)+M(i,j,k)*M(i,j+1,k)+M(i,j,k)*M(i,j,k+1)
So the code is:
INTEGER i,j,k,N, SUMT
INTEGER M(100,100), c(101)
SUMT = 0
do j=1,N
c(j) = j
end do
c(N+1)=1
N=100
do j=1,N
do i=1,N
SUMT = SUMT +M(i,j,k)*M(c(i+1),j,k)+M(i,j,k)*M(i,c(j+1),k)+M(i,j,k)*M(i,j,c(k+1))
end do
end do
At this point, my question is:
Is there any way to compute this problem with nested loops such dimension of M matrix is a parameter? I mean, I could do:
INTEGER i,j,k,l,m,n,....
INTEGER N, SUMT, D
PARAMETER (N=100)
PARAMETER (D=3) !DIMENSION
INTEGER M(N**D), c(N+1)
if (dim=1) then
do i=1,N
else if (dim=2) then
do j=1,N
do i=1,N
else if (dim=3) then
do k=1,N
do j=1,N
do i=1,N
....
but do you think about a more elegant solution in fortran 77?
I was thinking in accessing the M matrix with dim D as if it would have only one dimension with N**D spaces but I think that if apply if-instructions inside the to control limit of N it will work very slow. Any good idea or I should consider the nasty if-do loops?