Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question
add comment

marked as duplicate by ssch, m_goldberg, Pinguin Dirk, rm -rf Sep 25 '13 at 5:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

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.

share|improve this answer
    
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
2  
Automating: MatrixForm @ ArrayFlatten @ ReleaseHold @ DiagonalMatrix[Hold /@ {M1, M2, M3}] –  panda-34 Sep 25 '13 at 3:27
add comment

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