# Direct sum of matrices [duplicate]

Is there a simple way (perhaps even a built-in function) that allows one to compute the direct sum of two square matrices?

-

## marked as duplicate by ssch, m_goldberg, Pinguin Dirk, R. M.♦Sep 25 '13 at 5:56

Yes, it's partially built-in. Here is an example:

M1 = RandomInteger[{-5, 5}, {5, 5}];
M2 = RandomInteger[{-5, 5}, {6, 6}];
M3 = RandomInteger[{-5, 5}, {3, 3}];
MatrixForm[ArrayFlatten[( {
{M1, 0, 0},
{0, M2, 0},
{0, 0, M3}} )]]


Note that ArrayFlatten automatically inserts zero matrices of the proper sizes wherever you type a "0". How many matrices are you planning on using to construct your block diagonal matrix? If there are less than 10 or 20, you can probably enter them by hand, as in the above example. If you are using more than 20, I'm sure there's probably a way to automate what I did above, but in that case you would be dealing with quite large matrices, and may want to consider using a system better suited to handling large block arrays, like MATLAB.

Also, what kind of physics problem are you doing that requires the use of matrix sums? Is this representation theory, or are you constructing multidimensional Hamiltonians for systems? If it's the latter case, you will want to use Kronecker sums, not matrix sums.

-
Thanks that works. For my problem at hand, it's a direct sum that I need. Although it is indirectly related to representation theory, I am not taking the direct sum of two representations; it's just that writing a particular expression is facilitated by using direct sums. – joshphysics Sep 25 '13 at 0:12
Automating: MatrixForm @ ArrayFlatten @ ReleaseHold @ DiagonalMatrix[Hold /@ {M1, M2, M3}] – panda-34 Sep 25 '13 at 3:27