# Write Sum of Matrices Explicitly

I have sum of lots of matrices A+B+C+D+E+... and I want that Mathematica shows me this as explicit matrix sum, i.e.

Explicit Matrix A + Explicit Matrix B + Explicit Matrix C + ....

and not just the result of this sum. Is there any way to tell Mathematica that I want the explicit sum instead of the result?

(My sum yields a wrong result and I want to check which term is troublesome)

Maybe

m1 = {{1, 2}, {3, 4}};
m2 = m1*10;
m3 = m1/10;

(plus = Inactivate[m1 + m2 + m3, Plus]) /. a_List :> MatrixForm@a


Activate@plus


• +1 ... BTW, ain't that Activate/Inactivate a fresh 10.0 addition? How would one do it in older version? AFAIR, Hold was used for it?
– user11734
Commented Dec 28, 2015 at 4:56
• @vaxquis, take a look below, it might help. Commented Dec 28, 2015 at 15:13

Here's a pre-V10 version, which also works in V10 of course:

m1 = {{1, 2}, {3, 4}};
m2 = m1*10;
m3 = m1/10;

Block[{Plus}, HoldForm @@ {m1 + m2 + m3}]
(*  {{1, 2}, {3, 4}} + {{10, 20}, {30, 40}} + {{1/10, 1/5}, {3/10, 2/5}}  *)


I don't usually use MatrixForm, except perhaps when printing out a notebook for someone else to read, which I never do. But for fans, this will prettyprint it:

Map[MatrixForm, %, {2}]


I might add a suggestion in response to this piece of contextual information:

(My sum yields a wrong result and I want to check which term is troublesome)

I don't know where or how in the OP's code the list of matrices is generated, but if there is a line of code that adds them up with Plus, then duplicating the line and modifying one of them, with just a little typing as follows, can store the list of matrices:

sum = A + B + ...
Block[{Plus = List}, foo = A + B +...]


Then foo will contain the list of your matrices, which you can examine one-by-one foo[[1]], foo[[2]], etc. (or in a loop or whatever). I'm assuming it's not a very large list, because inspecting the Plus expression would be unmanageable if it were.

If the code is very complicated and you're worried about leaving such debugging lines lying around, you could add

If[TrueQ@ debugQ, Block[{Plus = List}, foo = A + B +...]]


where debugQ is a global variable you would set debugQ = True only when debugging. If it is unset or set to False, there is little overhead in having the extra line in the code.

If you don't like having the extra variable debugQ in the Global context, you can put it in an unlikely-to-cause-trouble context by calling it tmpdebugQ or perhaps PrivatedebugQ` and so forth.