1

I need to print DFGRD1, a matrix, in a file. I have written these lines

open(105,file='C:\temp17\FM.mtx')
WRITE(105,*) DFGRD1
close(105)

c  To check in .dat file
DO i=1,3
WRITE (7,*) (DFGRD1(i,j),j=1,3)
END DO 

However, the answers are not the same. They are the transpose of each other. Since I cannot access the real value of DFGRD1, I do not know which one is correct. I would really appreciate if someone could help me with it.

RKM
  • 61
  • 2

2 Answers2

1

Fortran is a Column major so change the DO loop to be over the J not the I. It should be something like this:

DO j=1,3
WRITE (7,*) (DFGRD1(i,j),i=1,3)
END DO 

Take care that optimum Fortran codes should have outer loop that iterates over the columns.

It is not a good idea to print some matrix to a file to check its values. You need to learn how to use a debugger (I recommend GDB) with basic tutorials you can set breakpoints and print the values.

Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
1

All arrays are stored in 1d -- that's just the way memory is laid out.

The question is how its stored. Fortran stores it as 'column-major', that is:

A(1, 1) -> A(2, 1) -> A(3, 1) -> A(1, 2) -> ... -> A(3, 3)

Most other languages store the arrays row-major, that is

A(1, 1) -> A(1, 2) -> A(1, 3) -> A(2, 1) -> ... -> A(3, 3)

(In fact, most languages start indexing at 0 by default.)

So it's no surprise that you get different outputs when you write it in one go (where Fortran would store it the way it's laid out in memory) compared to when you specifically write it in row-major by looping over the j index.

As for what is 'correct' -- well, that's up to your code. You just have to be consistent.

chw21
  • 7,970
  • 1
  • 16
  • 31