Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Transpose matrix in Fortran?

  1. Apr 14, 2009 #1
    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
     
  2. jcsd
  3. Apr 14, 2009 #2

    Astronuc

    User Avatar
    Staff Emeritus
    Science Advisor

    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
     
  4. Apr 15, 2009 #3
    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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Transpose matrix in Fortran?
  1. Fortran transpose (Replies: 4)

  2. Textbook wrong? (Replies: 35)

Loading...