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


1 Answer 1


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.

  • 1
    $\begingroup$ 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. $\endgroup$ Commented Sep 25, 2013 at 0:12
  • 10
    $\begingroup$ Automating: MatrixForm @ ArrayFlatten @ ReleaseHold @ DiagonalMatrix[Hold /@ {M1, M2, M3}] $\endgroup$
    – panda-34
    Commented Sep 25, 2013 at 3:27

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