# Transpose matrix in Fortran?

• Fortran
• d4n1el
In summary: 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.And O

#### d4n1el

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

M Naeem Tahir
d4n1el said:
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

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?

## 1. What is a transpose matrix in Fortran?

A transpose matrix in Fortran is a matrix in which the rows and columns are interchanged. This means that the elements of the original matrix become the elements of the new matrix, but in a different order.

## 2. How is a matrix transposed in Fortran?

In Fortran, a matrix can be transposed using the TRANSPOSE function. This function takes a matrix as its argument and returns a new matrix with the rows and columns interchanged.

## 3. What are the benefits of transposing a matrix in Fortran?

Transposing a matrix in Fortran can be beneficial in many applications, such as data analysis and linear algebra. It can simplify calculations and make the data easier to interpret.

## 4. Can a matrix be transposed in-place in Fortran?

Yes, a matrix can be transposed in-place in Fortran. This means that the original matrix is modified rather than creating a new transposed matrix. This can be achieved by using the TRANSPOSE intrinsic function with the INTENT(INOUT) attribute.

## 5. Are there any special considerations when transposing a matrix in Fortran?

Yes, there are a few things to keep in mind when transposing a matrix in Fortran. The dimensions of the transposed matrix will be the opposite of the original matrix, and the original matrix must be square for the transpose to be valid. Additionally, the elements of the original matrix must be of the same data type as the elements of the transposed matrix.