I have a simple question about arrays of 2 dimensions in Fortran 95 (i.e., matrices). By what I know, mathematics define an element inside of a matrix as Aij, where i represents its line and j its column. Well, if I simply code write(*,*) Matrix
, the result has lines and columns inverted! Take the following example code:
program TEST
implicit none
integer :: P(3,3), i
P(1,1)=1
P(1,2)=2
P(1,3)=3
P(2,1)=4
P(2,2)=5
P(2,3)=6
P(3,1)=7
P(3,2)=8
P(3,3)=9
do i=1,3
write(*,"(3(I1,1X))") P(i,1:3)
enddo
write(*,*)
write(*,"(3(I1,1X))") P
end program TEST
By using the loop above (which fixes a line and then print each column inside of it), I get the result I expected:
1 2 3
4 5 6
7 8 9
Now by using the last statement write(*,"(3(I1,1X))") P
, I get:
1 4 7
2 5 8
3 6 9
Am I doing something wrong here?
When you output an entire array as an entity, Fortran outputs the array in its internal memory layout. What you are seeing is that Fortran is a column-major language. See http://en.wikipedia.org/wiki/Row-major_order#Column-major_order