# Row-major to Column-major

Is there a straightforward way to switch matrix operations from row-major to column-major? From wikipedia,
Treating a row-major array as a column-major array is the same as transposing it. Because performing a transpose requires data movement, and is quite difficult to do in-place for non-square matrices, such transpositions are rarely performed explicitly. For example, software libraries for linear algebra, such as the BLAS, typically provide options to specify that certain matrices are to be interpreted in transposed order to avoid the necessity of data movement.
But applying this to the nested do-loop I am working with isn't so straightforward.

Code:
! ncol = 666
! nmu = 18
! ncomp = 4

do 120 j=1,ncol
do 130 i=1,nmu
temppp = 0.
temppm = 0.
do 140 k=1,ncomp
temppp = temppp + (bcomp(k)/btotal)*betatP(i,j,k)
temppm = temppm + (bcomp(k)/btotal)*betatM(i,j,k)
140        continue
totlpp(i,j) = temppp
totlpm(i,j) = temppm
130     continue
120  continue

betaP and betaM should have dimensions (k,i,j) for this to be column-major, but I'm not sure how to go about changing the computations to do so.

Any ideas?

Hhhmmm, I have never concerned myself with these kind of things, my data is not that large and computers are fast enough, by now.

Is your data that large? Are you really concerned about speed? performance? Because making sure that the fastest varying index takes you from one memory address to the adjacent one, it's all that will be achieved if you manage to line up your memory and your calculations.

How did betaP and betaM come about? Can you make the switch back there in the first place? Then you can come back to these nested loops and do the right thing.

Last edited:
Is this the kind of thing you are looking for?
Code:
totlpp=0.0  ! array operation, I presume you are using Fortran 90
totlpm=0.0  !
do k = 1, ncomp
coeff = bcomp(k)/btotal
do j = 1, ncol
do i = 1, nmu
totlpp(i,j) = totlpp(i,j) + coeff*betatP(i,j,k)
totlpm(i,j) = totlpm(i,j) + coeff*betatM(i,j,k)
end do
end do
end do
I don't have a fortran compiler at the moment and have not tested whether this produces the same results or not or whether it is what you are looking for or not...I still am not quite sure what you want to achieve...