14
$\begingroup$

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 *)
    Head[%]
(* List *)

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

    Head[MatrixForm[mat]]
(* MatrixForm *)
$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$
    – Szabolcs
    Commented Mar 17, 2014 at 18:35
  • 1
    $\begingroup$ another interesting case, if you do mat//MatrixForm; now Out gets assigned the MatrixForm. $\endgroup$
    – george2079
    Commented Mar 17, 2014 at 18:46
  • $\begingroup$ @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? $\endgroup$
    – murray
    Commented Mar 17, 2014 at 21:59
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Commented Mar 17, 2014 at 22:04

3 Answers 3

12
$\begingroup$

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]

Head[%]
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.

$\endgroup$
1
  • $\begingroup$ Thanks for posting this. I added an example to make the significance to this answer more apparent. $\endgroup$
    – Mr.Wizard
    Commented Feb 9, 2015 at 12:43
8
$\begingroup$

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

Main loop

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.

$\endgroup$
0
7
$\begingroup$

"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

$\endgroup$
6
  • 2
    $\begingroup$ 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). $\endgroup$
    – Szabolcs
    Commented Mar 17, 2014 at 18:33
  • $\begingroup$ @Szabolcs that issue is specifically addressed in the linked documentation. $\endgroup$
    – george2079
    Commented Mar 17, 2014 at 18:38
  • $\begingroup$ 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 ... $\endgroup$
    – Szabolcs
    Commented Mar 17, 2014 at 18:43
  • $\begingroup$ It will be useful if you extend the answer a but and show this pitfall. $\endgroup$
    – Szabolcs
    Commented Mar 17, 2014 at 18:44
  • $\begingroup$ Agreed, I edited. Actually I was half expecting this to get flagged as readily found in the docs.. $\endgroup$
    – george2079
    Commented Mar 17, 2014 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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