# How can I direct sum matrices into the middle of one another another?

I would like to execute the mathematical operation of the direct sum of matrices in the case where the matrices are not appended one after the other along the diagonal, but instead mixed among one another. This must be generalizable to any permutation of mixing indices along the diagonal.

For example, say we have matrix A, B and C $$A=$$\begin{bmatrix} \cos (t) & \sin(t) \\ -\sin(t) & \cos(t) \end{bmatrix}$$$$

$$B=$$\begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}$$$$

$$C=$$\begin{bmatrix} \cos (u) & \sin(u) \\ -\sin(u) & \cos(u) \end{bmatrix}$$$$

$$A ? B ? C=$$\begin{bmatrix} \cos (t) & \sin(t) & 0 & 0 & 0 & 0\\ -\sin(t) & \cos(t) & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & \cos(u) & 0 & \sin(u)\\ 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -\sin(u) & 0 & \cos(u) \end{bmatrix}$$$$

How would this be done in a generalizable way in Mathematica? One could use a permutation matrix, $$P=$$\begin{bmatrix} 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix}$$$$ where, $$A?B?C=P[A⊕B⊕C]P^T$$, but how could I create such a matrix P in Mathematica knowing only what the mapping is? In this case, the index mapping being: (1,2,3,4,5,6)->(1,2,3,5,4,6)

• For a start, please include your matrices as plain text Mathematica code, so we can copy and paste them. Jan 20, 2021 at 1:56
• perm = {1, 2, 3, 5, 4, 6}; p = IdentityMatrix[6][[perm]];?
– kglr
Jan 20, 2021 at 4:01
• and p = UnitVector[6, #] & /@ perm?
– kglr
Jan 20, 2021 at 4:01
• $P$ can be obtained exactly as @kglr introduced. The direct sum can be computed with {a, b, c} // Map[Inactive] // DiagonalMatrix // Activate // ArrayFlatten, and you get the result with p . directsum . Transpose[p]. Jan 20, 2021 at 4:18

ClearAll[r1, t, u, a, b, c, abc]

r1 = {Cos[t], Sin[t]};

a = {r1, Cross[r1]};
b = IdentityMatrix[2];
c = a /. t -> u;

abc = SparseArray[Band[{1, 1}] -> {a, b, c}];

abc // MatrixForm // TeXForm

$$\left( \begin{array}{cccccc} \cos (t) & \sin (t) & 0 & 0 & 0 & 0 \\ -\sin (t) & \cos (t) & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & \cos (u) & \sin (u) \\ 0 & 0 & 0 & 0 & -\sin (u) & \cos (u) \\ \end{array} \right)$$

perm = {1, 2, 3, 5, 4, 6};

abc[[perm, perm]] // MatrixForm // TeXForm

$$\left( \begin{array}{cccccc} \cos (t) & \sin (t) & 0 & 0 & 0 & 0 \\ -\sin (t) & \cos (t) & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & \cos (u) & 0 & \sin (u) \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -\sin (u) & 0 & \cos (u) \\ \end{array} \right)$$

Alternatively,

p = IdentityMatrix[6][[perm]];

p // MatrixForm // TeXForm

$$\left( \begin{array}{cccccc} 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ \end{array} \right)$$

p.abc.Transpose[p] // MatrixForm // TeXForm

$$\left( \begin{array}{cccccc} \cos (t) & \sin (t) & 0 & 0 & 0 & 0 \\ -\sin (t) & \cos (t) & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & \cos (u) & 0 & \sin (u) \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -\sin (u) & 0 & \cos (u) \\ \end{array} \right)$$

Note: An alternative way to get abc is to use SparseArraySparseBlockMatrix:

abc2 = SparseArraySparseBlockMatrix[MapIndexed[#2[[{1, 1}]] -> # &, {a, b, c}]];

abc2 == abc
True
• So SparseArray[Band[{1, 1}] -> {a, b, c}][[perm, perm]] // Normal is all it takes! — I would say the use of Band is brilliant :-) Jan 20, 2021 at 4:44