Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INDEX statement or INDEX intrinsic function?

Status
Not open for further replies.

lesaadmi

Technical User
Aug 29, 2012
11
MX
Hello everyone, I have the following subroutine code (it is a textual copy of a document):

1 subroutine contin(grid,nx,ny,dx,dy,dz,store)
2 dimension grid(nx*ny),store(2*nx*ny),nn(2)
3 real kx,ky,k
4 complex cgrid,cmplx
5 data pi/3.14159265/
6 index(i,j,ncol)=(j-l)*ncol+i
7 nn(1)=ny
8 nn(2)=nx
9 ndim=2
10 dkx=2.*pi/(nx*dx)
11 dky=2.*pi/(ny*dy)
12 do 10 j=l,nx
13 do 10 i=l,ny
14 ij=index(i,j,ny)
15 store(2*ij-l)=grid(ij)
16 10 store(2*ij)=0.

My doubt is in line 5, with the INDEX function in the the [highlight #EF2929]index(i,j,ncol)=(j-l)*ncol+i[/highlight]. I have been checking and the INDEX is a fortran intrinsic function but in this case they are using it as a variable. I am using GFORTRAN to compile the subroutine.

Does anyone understand what they are trying to do whit the INDEX statement in line 5?

I appreciate any coments. Best regards.
 
Hhhmmm...you say is a textual copy, but it does not look correct at all...did you copy/paste or re-typed it? There are typos and a bunch of variables not being declared, etc. For example, there seem to be several letters 'l' (lower case 'L') instead of the number 1.

In any case, the function "index(i,j,ncol) = (j-1)*ncol + i " is basically turning a 2D index set (i,j) into a single 1D index.

In other words, when you have a 2D matrix, you typically refer to the entries in the various locations via both row and column indices (i,j)...but in reality, all this memory is actually stored in a way that behaves as a one-dimensional array, i.e., you use a single memory address to find something...a single array index, if you will.

Fortran stores matrices in column-major order, this means that if you go to the computer memory, you will find entry (1,1) followed by entry (2,1), all the way to the bottom entry of the first column and then the entire first column followed by the second column.

The index function listed above does the conversion from (i,j) to a single column-major index.

And yes, INDEX is already a Fortran intrinsic to find out where a substring starts within a string...I would stay away from re-using names...best to rename your index function to something else: indx or something.

 
Hello,

Yes, I copy/paste.
The typos like 'l' instead of '1' are my mistake, I did not notice them.
Yes, there are a bunch of variables that are not declare, like the variable 'ncol'. This 'ncol' is the one that made me think that I had to use INDEX as an intrinsic function.

Thank you for your answer, I will now check how to work with the 'single column-major index' that you mentioned.

Best regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top