# Head of a MatrixForm expression?

An example in http://www-zeuthen.desy.de/theory/capp2005/Course/hahn/mathematica.pdf amounts to the following

    mat = {{1, 2}, {3,4}};
mat // MatrixForm
(* I've suppressed output *)
(* List *)


Why is that head List rather than MatrixForm, in view of the following?

    Head[MatrixForm[mat]]
(* MatrixForm *)

• Very good question. Somehow the assignment to Out is special. Here's another special case: 1; clearly evaluates to Null yet the value assigned to the corresponding Out expression will be 1. A related question would be: how can we create our own function or wrapper that influences that value assigned to Out? – Szabolcs Mar 17 '14 at 18:35
• another interesting case, if you do mat//MatrixForm; now Out gets assigned the MatrixForm. – george2079 Mar 17 '14 at 18:46
• @Szabolcs: the example with 1; I find especially troubling! Compare the behavior of that with that of a; b which has output b and then % also gives b. So why doesn't 1;  then % give either nothing or else Null? – murray Mar 17 '14 at 21:59
• @murray To make it even more troubling, we can actually write 1; as 1;Null. Well, I guess this is a useful feature but a bit weird. I learned about it on MathGroup. I was also very surprised. – Szabolcs Mar 17 '14 at 22:04

As already explained, this happens because MatrixForm act as a wrapper.

The answer to the question about how this behavior is implemented and how can eventually be reproduced is contained in the Informmation of the system symbol $OutputForms. Indeed ??$OutputForms


returns:

$OutputForms is a list of the formatting functions that get stripped off when wrapped around the output. and $OutputForms={InputForm,OutputForm,TextForm,CForm,Short,Shallow,MatrixForm,TableForm,TreeForm,FullForm,NumberForm,EngineeringForm,ScientificForm,QuantityForm,PaddedForm,AccountingForm,BaseForm,DisplayForm,StyleForm,FortranForm,ScriptForm,MathMLForm,TeXForm,StandardForm,TraditionalForm}


As noted earlier, this affects what the kernel stores in the output history when it encounter a result with any of the previous Head.

For example:

Unprotect[$OutputForms]; AppendTo[$OutputForms, foo];

foo[1]


foo[1]

Integer


I discovered this last summer while working on a way to better show rational matrices: if interested please see this question. I ended up writing my own PrettyMatrixForm with the same output behavior as one of the previous XyzForm.

• Thanks for posting this. I added an example to make the significance to this answer more apparent. – Mr.Wizard Feb 9 '15 at 12:43

"MatrixForm acts as a "wrapper", which affects printing, but not evaluation. "

The output returned by % or Out[] is the result obtained prior to applying the wrapper.

It is important to note this behavior is pecular to the assignment to the output history. For example:

 m = mat // MatrixForm


actually assigns the MatrixForm to m (Note this is a rarely desirable thing to do, and leads to frequent beginners confusion since the resulting m is not a matrix to be used in further calculations)

 Head[m]


MatrixForm

note even here the history variable holds the non-wrapped expression:

 Head[Out[-2]]


List

See here HowInputAndOutputWork

• I'm afraid it's not as simple as that. This answer could mislead many people. Consider now a = MatrixForm[mat] and Head[a]. Now the head is MatrixForm and the expression can't be used as a matrix any more (a common mistake is to try to anyway). – Szabolcs Mar 17 '14 at 18:33
• @Szabolcs that issue is specifically addressed in the linked documentation. – george2079 Mar 17 '14 at 18:38
• Well, the documentation states that this is what happens, but it doesn't explain the mechanism behind it. For example it doesn't give enough information to understand how to create our own wrappers. But the main reason I commented is because I think this answer, in its current short form, is misleading to beginners and thus a bit dangerous. After reading the first highlighted line, people are likely to assume that MatrixForm[...] is a proper matrix that can be used for calculations, and we already get enough questions on this site where precisely that was the problem ... – Szabolcs Mar 17 '14 at 18:43
• It will be useful if you extend the answer a but and show this pitfall. – Szabolcs Mar 17 '14 at 18:44
• Agreed, I edited. Actually I was half expecting this to get flagged as readily found in the docs.. – george2079 Mar 17 '14 at 19:07

"That's just how it is." The documentation page Wolfram System Sessions explains this:

Any formatting wrapper will be stripped before the expression is assigned to Out. This includes MatrixForm which is such a wrapper.

As Mr. Wizard notes in a comment and as it is written in other answers, the list of formatting wrappers treated in this way is given by \$OutputForms.