Transpose matrix in Fortran?

  • Fortran
  • Thread starter d4n1el
  • Start date
  • #1
3
1

Main Question or Discussion Point

Hi!
I'm working on a programming project(fortran 77).
and I need to transpose a big matrix, and for the moment i'm doing it by to do-loops:

DO 20 J = 2,NP
DO 10 I = 1,J-1
T = P(I,J)
P(I,J) = P(J,I)
P(J,I) = T
10 CONTINUE
20 CONTINUE

But is this the most optimized way? is it any way you could do it by using for example BLAS?
I've been googleing, but could't find out how to do it..

/Daniel
 

Answers and Replies

  • #2
Astronuc
Staff Emeritus
Science Advisor
18,704
1,718
Hi!
I'm working on a programming project(fortran 77).
and I need to transpose a big matrix, and for the moment i'm doing it by to do-loops:

DO 20 J = 2,NP
DO 10 I = 1,J-1
T = P(I,J)
P(I,J) = P(J,I)
P(J,I) = T
10 CONTINUE
20 CONTINUE

But is this the most optimized way? is it any way you could do it by using for example BLAS?
I've been googleing, but could't find out how to do it..

/Daniel
It looks like one is marching by column then rows on the top triangular array. Then the above code exchanges top triangle element with the diagonally symmetric member and leaves the diagonal alone, since the diagonal does not change. That is the optimal way if memory (size) is constrained. On has to calculate the upper limit of I as J-1 which is correct.

If memory is not a problem, then one could simply copy and transpose the entire matrix A to a new one AT (A transpose), which preserves A, i.e. AT(J,I) = A(I,J), and still use two DO loops. There is just the one = operation.

Software optimization for high-performance computing
 
  • #3
492
0
Just a suggestion... but you could probably use logic to indicate whether the matrix is transposed, and then branch when you use the matrix. For instance...

M : large matrix, N x M array of integers
T : true if M is to be interpreted as being transposed, false otherwise.

Then (provided you don't need to multiply M by transpose M) you could do this...

MatrixMultiply(A, false, B, false, C, false); // multiplies A by B and put it into C
MatrixMultiply(A, true, D, false, E, false); // take the transpose of A, multiply by D, and put it into E.

You see what I'm saying? It may complicate the functions themselves (but not by too much) and you won't have to actually take the transpose of anything.

And O(1) is a lot faster than O(n^2). Will this work for you?
 

Related Threads for: Transpose matrix in Fortran?

  • Last Post
Replies
4
Views
6K
Replies
4
Views
3K
Replies
1
Views
2K
  • Last Post
Replies
5
Views
5K
Top