5
$\begingroup$

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{equation} \begin{bmatrix} \cos (t) & \sin(t) \\ -\sin(t) & \cos(t) \end{bmatrix} \end{equation}$$

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

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

$A ? B ? C=\begin{equation} \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} \end{equation}$

How would this be done in a generalizable way in Mathematica? One could use a permutation matrix, $P=\begin{equation} \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} \end{equation}$ 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)

$\endgroup$
4
  • 2
    $\begingroup$ For a start, please include your matrices as plain text Mathematica code, so we can copy and paste them. $\endgroup$
    – MarcoB
    Jan 20, 2021 at 1:56
  • 1
    $\begingroup$ perm = {1, 2, 3, 5, 4, 6}; p = IdentityMatrix[6][[perm]];? $\endgroup$
    – kglr
    Jan 20, 2021 at 4:01
  • $\begingroup$ and p = UnitVector[6, #] & /@ perm? $\endgroup$
    – kglr
    Jan 20, 2021 at 4:01
  • $\begingroup$ $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]. $\endgroup$ Jan 20, 2021 at 4:18

1 Answer 1

10
$\begingroup$
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 SparseArray`SparseBlockMatrix:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.