# Permute subsystems of a matrix?

Suppose I have a matrix $$M$$ acts on the space $$\mathbb C^4\otimes \mathbb C^2\otimes \mathbb C^4\otimes \mathbb C^3$$. Is there a method to permute the 2nd subsystem of $$M$$ and the 4th subsystem of $$M$$? In other words, I have a matrix $$M=A_1\otimes A_2\otimes A_3\otimes A_4$$, how can I permute the second and the fourth subsystem of $$M$$ to get $$A_1\otimes A_4\otimes A_3\otimes A_2$$ when I only have access to $$M$$ while don't have access to $$A_i$$?

For example, I have a matrix $$M=\left( \begin{array}{cccc} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \\ 13 & 14 & 15 & 16 \\ \end{array} \right)$$ how can I switch the first and second subsystems permitted the first system is of dimension $$2$$ and the second system is of dimension $$2$$. In index form, $$M$$ can be written as $$\left( \begin{matrix} m_{00,00}& m_{00,01}& m_{00,10}& m_{00,11}\\ m_{01,00}& m_{01,01}& m_{01,10}& m_{01,11}\\ m_{10,00}& m_{10,01}& m_{10,10}& m_{10,11}\\ m_{11,00}& m_{11,01}& m_{11,10}& m_{11,11}\\ \end{matrix} \right) .$$ The element $$m_{00,10}$$ after switching should be in place $$00,01$$. Anyway, the transformed $$M$$ in numerical form should be $$\left( \begin{array}{cccc} 1 & 3 & 2 & 4 \\ 9 & 11 & 10 & 12 \\ 5 & 7 & 6 & 8 \\ 13 & 15 & 14 & 16 \\ \end{array} \right).$$

• You do not have access to $A_i$. What is known then, dimensions? Jan 1 at 15:40
• Is this what you are looking for? myM[i1_,i2_,i3_,i4_]:= M[i1,i4,i3,i2] Jan 1 at 16:24
• @yarchik Thank you for your comment! I have access to $M$. Jan 2 at 0:12
• @DanielHuber Thank you for your comment, but I think it's not. I have $M$ in matrix form which can, but I don't know what $A_i$ is, be written as KroneckerProduct[A_1,A_2,A_3,A_4]. When I have access to the matrix $M$, how can I switch the two subsystems $A_2$ and $A_4$? Jan 2 at 0:28

My current solution is as follows. The core idea is to utilize the TensorTranspose function. Let's clarify things using the example in the original question. I first reshape the matrix using

testmat = ArrayReshape[mat, {2, 2, 2, 2}];


Then I switch the first and second subsystems using TensorTranspose

testmat = TensorTranspose[testmat, {2, 1, 4, 3}];


Finally, I recover the matrix from testmat using reshape function

testmat = ArrayReshape[testmat, {4, 4}];


The code above is summarized as follows

mat = ArrayReshape[Range@16, {4, 4}];
testmat = ArrayReshape[mat, {2, 2, 2, 2}];
testmat = TensorTranspose[testmat, {2, 1, 4, 3}];
testmat = ArrayReshape[testmat, {4, 4}];
testmat // MatrixForm