|
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
IanLambley
Joined: 17 Dec 2006 Posts: 490 Location: Sunderland
|
Posted: Tue Nov 25, 2014 1:58 pm Post subject: |
|
|
You asked what the function actually does.
It seems to me that if you were to pass a two dimensional array to the subroutine, in which it is dimensioned using the old Fortran 66 method of dimensioning it to (1). Array bounds checking did not happen in those days so just declaring it with one element was enough to locate the start of the array in memory. In the subroutine, it becomes a one dimensional array. Outside the subroutine, one of the dimensions was equal to NUGL. This function determines the position in the one dimensional array that corresponds to element (IZ,IS).
For example consider the two dimensional array outside the subroutine:
a1 a2 a3 a4
b1 b2 b3 b4
c1 c2 c3 c4
Within the subroutine in one dimensional arrangement, it would look like:
a1 a2 a3 a4 b1 b2 b3 b4 c1 c2 c3 c4
and NUGL would be 4. Therefore to access element b3 for example, the two dimensional subscripts would be column=3, row=2. Evaluating the function would give:
(2 - 1) * 4 + 3 = 7
and looking at the seventh element of the one dimensional array, we see that it is b3, so we have located it correctly.
Thus we can assume that NUGL is one of the dimensions of the array and is passed to the subroutine. No doubt the other dimension can be located by examining the range of IS in some part of the subroutine. The dimension statements could then use these dimensions to re-create the two dimensional arrays in the subroutine and all occurances of INDU(?,?) in an array subscript could be replaced with just the ?,?.
Ian |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1885
|
Posted: Tue Nov 25, 2014 4:57 pm Post subject: |
|
|
Ian Lambley wrote: Quote: | Within the subroutine in one dimensional arrangement, it would look like:
a1 a2 a3 a4 b1 b2 b3 b4 c1 c2 c3 c4 |
In Fortran, the equivalent one-dimensional array would actually be
a1 b1 c1 a2 b2 c2 a3 b3 c3 a4 b4 c4
We are used to reading matrices row by row. Fortran stores matrices by columns, i.e., all elements in col-1 are stored in sequence, then col-2, etc.
C and other languages use the convention of storing by rows. |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Wed Nov 26, 2014 4:54 am Post subject: |
|
|
One man's row is another man's column!
Using Excel notation, I would agree with Ian,
but if the array is defined as (a:c,1:4) then I would agree with mecej4.
Unfortunately the picture Ian painted was not of excel layout.
I've always thought this inconsistency is over-rated. It is just a matter of observing the correct subscript order, which is left to right in Fortran and I presume right to left in C.
After all, there is no concept of "row" and "column" in Fortran, which makes reference to these attributes somewhat difficult. When I first learnt matrix mathematics, I did learn about rows and columns but was not aware of Fortran.
It's a bit like considering if memory addresses count up, down, left or right. Then there are the 4 bytes of an INTEGER*4, which count to the left !!
John |
|
Back to top |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Nov 26, 2014 8:08 am Post subject: |
|
|
Actually C doesn't have 2-dimensional arrays at all. You can only mimic 2D arrays using arrays of arrays. It is then a matter of choice whether you represent these as "arrays of rows" or "arrays of columns".
Conventionally the elements of a matrix A (a mathematical object) with row index r = 1 ... M and column index c = 1 ... N are references in C as A[r][c] meaning it is convenient to think of arrays of rows (read indices right-to-left).
However, if you reference elements as A[c][r] it is convenient to think of the representation of arrays of columns. The latter notation has the same layout in memory as 2D arrays in Fortran.
So you could just as well say, C uses the same memory layout for 2D arrays as Fortran but requires the indices to be given in reverse order! _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
|
mecej4
Joined: 31 Oct 2006 Posts: 1885
|
Posted: Wed Nov 26, 2014 8:16 pm Post subject: Re: |
|
|
Although the following comments are about C, and therefore a little bit off-topic for this forum, since C is often used with Fortran it is worth noting them.
davidb wrote: | Actually C doesn't have 2-dimensional arrays at all. You can only mimic 2D arrays using arrays of arrays. It is then a matter of choice whether you represent these as "arrays of rows" or "arrays of columns". |
That is not quite correct. C does have 2-dimensional arrays, although there are some limitations to their usefulness. Here is a program to illustrate the differences between a native 2D array and a 2D array implemented through an array of row pointers. Note that the 2D array, c1, has 3 X 5 = 15 elements, and occupies exactly 60 bytes -- there is no space there to hold any pointers. The array of pointers version, on the other hand, occupies a total of 72 bytes (84 on a 64-bit target).
There are other differences. (i) For example, c1[2] is a const pointer, and cannot be changed; c2[2], on the other hand, is not defined until being assigned to the pointer returned by calloc(), and could be redefined later. (ii) The array c1 must be rectangular (or square, as a special case). An array of pointers variable, such as c2, can even be a "ragged" array, with each row containing a different number of elements than the other rows, or with a first index different from 0 (even negative if desired), and this feature is useful in working with banded matrices.
The program outputs:
(32-bit): 60 12 72
(64-bit): 60 24 84
Code: | #include <stdio.h>
#include <stdlib.h>
int main(){
int c1[3][5], *c2[3];
int i,s,*p;
for(i=0, s=0; i<3; i++){
c2[i]=(int *)calloc(5,sizeof(int));
s+=sizeof(c2[i])+sizeof(int)*5;
}
printf("%d %d %d\n",sizeof(c1),sizeof(c2),s);
}
|
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|