Inplace matrix transposition
Inplace matrix transposition, also called insitu matrix transposition, is the problem of transposing an N×M matrix inplace in computer memory, ideally with O(1) (bounded) additional storage, or at most with additional storage much less than NM. Typically, the matrix is assumed to be stored in rowmajor order or columnmajor order (i.e., contiguous rows or columns, respectively, arranged consecutively).
Performing an inplace transpose (insitu transpose) is most difficult when N ≠ M, i.e. for a nonsquare (rectangular) matrix, where it involves a complicated permutation of the data elements, with many cycles of length greater than 2. In contrast, for a square matrix (N = M), all of the cycles are of length 1 or 2, and the transpose can be achieved by a simple loop to swap the upper triangle of the matrix with the lower triangle. Further complications arise if one wishes to maximize memory locality in order to improve cache line utilization or to operate outofcore (where the matrix does not fit into main memory), since transposes inherently involve nonconsecutive memory accesses.
The problem of nonsquare inplace transposition has been studied since at least the late 1950s, and several algorithms are known, including several which attempt to optimize locality for cache, outofcore, or similar memoryrelated contexts.
Background
On a computer, one can often avoid explicitly transposing a matrix in memory by simply accessing the same data in a different order. For example, software libraries for linear algebra, such as BLAS, typically provide options to specify that certain matrices are to be interpreted in transposed order to avoid data movement.
However, there remain a number of circumstances in which it is necessary or desirable to physically reorder a matrix in memory to its transposed ordering. For example, with a matrix stored in rowmajor order, the rows of the matrix are contiguous in memory and the columns are discontiguous. If repeated operations need to be performed on the columns, for example in a fast Fourier transform algorithm (e.g. Frigo & Johnson, 2005), transposing the matrix in memory (to make the columns contiguous) may improve performance by increasing memory locality. Since these situations normally coincide with the case of very large matrices (which exceed the cache size), performing the transposition inplace with minimal additional storage becomes desirable.
Also, as a purely mathematical problem, inplace transposition involves a number of interesting number theory puzzles that have been worked out over the course of several decades.
Example
For example, consider the 2×4 matrix:
In rowmajor format, this would be stored in computer memory as the sequence (11, 12, 13, 14, 21, 22, 23, 24), i.e. the two rows stored consecutively. If we transpose this, we obtain the 4×2 matrix:
which is stored in computer memory as the sequence (11, 21, 12, 22, 13, 23, 14, 24).
Position  0  1  2  3  4  5  6  7 

Original storage  11  12  13  14  21  22  23  24 
Transposed storage  11  21  12  22  13  23  14  24 
If we number the storage locations 0 to 7, from left to right, then this permutation consists of four cycles:
 (0), (1 2 4), (3 6 5), (7)
That is, the value in position 0 goes to position 0 (a cycle of length 1, no data motion). Next, the value in position 1 (in the original storage: 11, 12, 13, 14, 21, 22, 23, 24) goes to position 2 (in the transposed storage 11, 21, 12, 22, 13, 23, 14, 24), while the value in position 2 (11, 12, 13, 14, 21, 22, 23, 24) goes to position 4 (11, 21, 12, 22, 13, 23, 14, 24), and position 4 (11, 12, 13, 14, 21, 22, 23, 24) goes back to position 1 (11, 21, 12, 22, 13, 23, 14, 24). Similarly for the values in position 7 and positions (3 6 5).
Properties of the permutation
In the following, we assume that the N×M matrix is stored in rowmajor order with zerobased indices. This means that the (n,m) element, for n = 0,...,N−1 and m = 0,...,M−1, is stored at an address a = Mn + m (plus some offset in memory, which we ignore). In the transposed M×N matrix, the corresponding (m,n) element is stored at the address a' = Nm + n, again in rowmajor order. We define the transposition permutation to be the function a' = P(a) such that:
 for all
This defines a permutation on the numbers .
It turns out that one can define simple formulas for P and its inverse (Cate & Twigg, 1977). First:
where "mod" is the modulo operation.
Proof 

If 0 ≤ a = Mn + m < MN − 1, then Na mod (MN−1) = MN n + Nm mod (MN − 1) = n + Nm. [ProofNote 1][ProofNote 2] 
Second, the inverse permutation is given by:
(This is just a consequence of the fact that the inverse of an N×M transpose is an M×N transpose, although it is also easy to show explicitly that P^{−1} composed with P gives the identity.)
As proved by Cate & Twigg (1977), the number of fixed points (cycles of length 1) of the permutation is precisely 1 + gcd(N−1,M−1), where gcd is the greatest common divisor. For example, with N = M the number of fixed points is simply N (the diagonal of the matrix). If N − 1 and M − 1 are coprime, on the other hand, the only two fixed points are the upperleft and lowerright corners of the matrix.
The number of cycles of any length k>1 is given by (Cate & Twigg, 1977):
where μ is the Möbius function and the sum is over the divisors d of k.
Furthermore, the cycle containing a=1 (i.e. the second element of the first row of the matrix) is always a cycle of maximum length L, and the lengths k of all other cycles must be divisors of L (Cate & Twigg, 1977).
For a given cycle C, every element has the same greatest common divisor .
Proof (Brenner, 1973) 

Let s be the smallest element of the cycle, and . From the definition of the permutation P above, every other element x of the cycle is obtained by repeatedly multiplying s by N modulo MN−1, and therefore every other element is divisible by d. But, since N and MN − 1 are coprime, x cannot be divisible by any factor of MN − 1 larger than d, and hence . 
This theorem is useful in searching for cycles of the permutation, since an efficient search can look only at multiples of divisors of MN−1 (Brenner, 1973).
Laflin & Brebner (1970) pointed out that the cycles often come in pairs, which is exploited by several algorithms that permute pairs of cycles at a time. In particular, let s be the smallest element of some cycle C of length k. It follows that MN−1−s is also an element of a cycle of length k (possibly the same cycle).
Proof by the definition of P above 

The length k of the cycle containing s is the smallest k > 0 such that . Clearly, this is the same as the smallest k>0 such that , since we are just multiplying both sides by −1, and . 
Note of proofs 


Algorithms
The following briefly summarizes the published algorithms to perform inplace matrix transposition. Source code implementing some of these algorithms can be found in the references, below.
Accessor Transpose
Because physically transposing a matrix is computationally expensive, instead of moving values in memory, the access path may be transposed instead. It is trivial to perform this operation for CPU access, as the access paths of iterators must simply be exchanged,[1] however hardware acceleration may require that still be physically realigned.[2]
Square matrices
For a square N×N matrix A_{n,m} = A(n,m), inplace transposition is easy because all of the cycles have length 1 (the diagonals A_{n,n}) or length 2 (the upper triangle is swapped with the lower triangle). Pseudocode to accomplish this (assuming zerobased array indices) is:
for n = 0 to N  2 for m = n + 1 to N  1 swap A(n,m) with A(m,n)
This type of implementation, while simple, can exhibit poor performance due to poor cacheline utilization, especially when N is a power of two (due to cacheline conflicts in a CPU cache with limited associativity). The reason for this is that, as m is incremented in the inner loop, the memory address corresponding to A(n,m) or A(m,n) jumps discontiguously by N in memory (depending on whether the array is in columnmajor or rowmajor format, respectively). That is, the algorithm does not exploit locality of reference.
One solution to improve the cache utilization is to "block" the algorithm to operate on several numbers at once, in blocks given by the cacheline size; unfortunately, this means that the algorithm depends on the size of the cache line (it is "cacheaware"), and on a modern computer with multiple levels of cache it requires multiple levels of machinedependent blocking. Instead, it has been suggested (Frigo et al., 1999) that better performance can be obtained by a recursive algorithm: divide the matrix into four submatrices of roughly equal size, transposing the two submatrices along the diagonal recursively and transposing and swapping the two submatrices above and below the diagonal. (When N is sufficiently small, the simple algorithm above is used as a base case, as naively recurring all the way down to N=1 would have excessive functioncall overhead.) This is a cacheoblivious algorithm, in the sense that it can exploit the cache line without the cacheline size being an explicit parameter.
Nonsquare matrices: Following the cycles
For nonsquare matrices, the algorithms are more complicated. Many of the algorithms prior to 1980 could be described as "followthecycles" algorithms. That is, they loop over the cycles, moving the data from one location to the next in the cycle. In pseudocode form:
for each length>1 cycle C of the permutation
pick a starting address s in C
let D = data at s
let x = predecessor of s in the cycle
while x ≠ s
move data from x to successor of x
let x = predecessor of x
move data from D to successor of s
The differences between the algorithms lie mainly in how they locate the cycles, how they find the starting addresses in each cycle, and how they ensure that each cycle is moved exactly once. Typically, as discussed above, the cycles are moved in pairs, since s and MN−1−s are in cycles of the same length (possibly the same cycle). Sometimes, a small scratch array, typically of length M+N (e.g. Brenner, 1973; Cate & Twigg, 1977) is used to keep track of a subset of locations in the array that have been visited, to accelerate the algorithm.
In order to determine whether a given cycle has been moved already, the simplest scheme would be to use O(MN) auxiliary storage, one bit per element, to indicate whether a given element has been moved. To use only O(M+N) or even O(log MN) auxiliary storage, more complicated algorithms are required, and the known algorithms have a worstcase linearithmic computational cost of O(MN log MN) at best, as first proved by Knuth (Fich et al., 1995; Gustavson & Swirszcz, 2007).
Such algorithms are designed to move each data element exactly once. However, they also involve a considerable amount of arithmetic to compute the cycles, and require heavily nonconsecutive memory accesses since the adjacent elements of the cycles differ by multiplicative factors of N, as discussed above.
Improving memory locality at the cost of greater total data movement
Several algorithms have been designed to achieve greater memory locality at the cost of greater data movement, as well as slightly greater storage requirements. That is, they may move each data element more than once, but they involve more consecutive memory access (greater spatial locality), which can improve performance on modern CPUs that rely on caches, as well as on SIMD architectures optimized for processing consecutive data blocks. The oldest context in which the spatial locality of transposition seems to have been studied is for outofcore operation (by Alltop, 1975), where the matrix is too large to fit into main memory ("core").
For example, if d = gcd(N,M) is not small, one can perform the transposition using a small amount (NM/d) of additional storage, with at most three passes over the array (Alltop, 1975; Dow, 1995). Two of the passes involve a sequence of separate, small transpositions (which can be performed efficiently out of place using a small buffer) and one involves an inplace d×d square transposition of blocks (which is efficient since the blocks being moved are large and consecutive, and the cycles are of length at most 2). This is further simplified if N is a multiple of M (or vice versa), since only one of the two outofplace passes is required.
Another algorithm for noncoprime dimensions, involving multiple subsidiary transpositions, was described by Catanzaro et al. (2014). For the case where N − M is small, Dow (1995) describes another algorithm requiring N − M ⋅ min(N,M) additional storage, involving a min(N, M) ⋅ min(N, M) square transpose preceded or followed by a small outofplace transpose. Frigo & Johnson (2005) describe the adaptation of these algorithms to use cacheoblivious techniques for generalpurpose CPUs relying on cache lines to exploit spatial locality.
Work on outofcore matrix transposition, where the matrix does not fit in main memory and must be stored largely on a hard disk, has focused largely on the N = M squarematrix case, with some exceptions (e.g. Alltop, 1975). Recent reviews of outofcore algorithms, especially as applied to parallel computing, can be found in e.g. Suh & Prasanna (2002) and Krishnamoorth et al. (2004).
References
 "numpy.swapaxes — NumPy v1.15 Manual". docs.scipy.org. Retrieved 22 January 2019.
 Harris, Mark (18 February 2013). "An Efficient Matrix Transpose in CUDA C/C++". NVIDIA Developer Blog.
 P. F. Windley, "Transposing matrices in a digital computer," Computer Journal 2, p. 4748 (1959).
 G. Pall, and E. Seiden, "A problem in Abelian Groups, with application to the transposition of a matrix on an electronic computer," Math. Comp. 14, p. 189192 (1960).
 J. Boothroyd, "Algorithm 302: Transpose vector stored array," ACM Transactions on Mathematical Software 10 (5), p. 292293 (1967).
 Susan Laflin and M. A. Brebner, "Algorithm 380: insitu transposition of a rectangular matrix," ACM Transactions on Mathematical Software 13 (5), p. 324326 (1970). Source code.
 Norman Brenner, "Algorithm 467: matrix transposition in place," ACM Transactions on Mathematical Software 16 (11), p. 692694 (1973). Source code.
 W. O. Alltop, "A computer algorithm for transposing nonsquare matrices," IEEE Trans. Comput. 24 (10), p. 10381040 (1975).
 Esko G. Cate and David W. Twigg, "Algorithm 513: Analysis of InSitu Transposition," ACM Transactions on Mathematical Software 3 (1), p. 104110 (1977). Source code.
 Bryan Catanzaro, Alexander Keller, and Michael Garland, [A decomposition for inplace matrix transposition http://dl.acm.org/citation.cfm?id=2555253], Proceedings of the 19th ACM SIGPLAN symposium on Principles and practice of parallel programming (PPoPP '14), pp. 193–206 (2014).
 Murray Dow, "Transposing a matrix on a vector computer," Parallel Computing 21 (12), p. 19972005 (1995).
 Donald E. Knuth, The Art of Computer Programming Volume 1: Fundamental Algorithms, third edition, section 1.3.3 exercise 12 (AddisonWesley: New York, 1997).
 M. Frigo, C. E. Leiserson, H. Prokop, and S. Ramachandran, "Cacheoblivious algorithms," in Proceedings of the 40th IEEE Symposium on Foundations of Computer Science (FOCS 99), p. 285297 (1999). Extended abstract at IEEE, at Citeseer.
 J. Suh and V. K. Prasanna, "An efficient algorithm for outofcore matrix transposition," IEEE Trans. Computers 51 (4), p. 420438 (2002).
 S. Krishnamoorthy, G. Baumgartner, D. Cociorva, C.C. Lam, and P. Sadayappan, "Efficient parallel outofcore matrix transposition," International Journal of High Performance Computing and Networking 2 (24), p. 110119 (2004).
 M. Frigo and S. G. Johnson, "The Design and Implementation of FFTW3," Proceedings of the IEEE 93 (2), 216–231 (2005). Source code of the FFTW library, which includes optimized serial and parallel square and nonsquare transposes, in addition to FFTs.
 Faith E. Fich, J. Ian Munro, and Patricio V. Poblete, "Permuting in place," SIAM Journal on Computing 24 (2), p. 266278 (1995).
 Fred G. Gustavson and Tadeusz Swirszcz, "Inplace transposition of rectangular matrices," Lecture Notes in Computer Science 4699, p. 560569 (2007), from the Proceedings of the 2006 Workshop on StateoftheArt [sic] in Scientific and Parallel Computing (PARA 2006) (Umeå, Sweden, June 2006).
 Sloane, N. J. A. (ed.). "Sequence A093055 (Number of nonsingleton cycles in the insitu transposition of a rectangular j X k matrix)". The OnLine Encyclopedia of Integer Sequences. OEIS Foundation.
 Sloane, N. J. A. (ed.). "Sequence A093056 (Length of the longest cycle in the insitu transposition of a rectangular j X k matrix)". The OnLine Encyclopedia of Integer Sequences. OEIS Foundation.
 Sloane, N. J. A. (ed.). "Sequence A093057 (Number of matrix elements remaining at fixed position in the insitu transposition of a rectangular j X k matrix)". The OnLine Encyclopedia of Integer Sequences. OEIS Foundation.
External links
Source code
 OFFT  recursive block inplace transpose of square matrices, in Fortran
 Jason Stratos Papadopoulos, blocked inplace transpose of square matrices, in C, sci.math.numanalysis newsgroup (April 7, 1998).
 See "Source code" links in the references section above, for additional code to perform inplace transposes of both square and nonsquare matrices.
 libmarshal Blocked inplace transpose of rectangular matrices for the GPUs.