8
$\begingroup$

This question already has an answer here:

I think the following can only be tested in a notebook. (Please correct me if not.)

Clear[a, b, c, d]
mA = {{a, b}, {c, d}};
mfA = MatrixForm[mA]
Head[Out[]]   (* List *)
Head[mfA]     (* MatrixForm *)

So Out[] is storing something other than the value of the previous expression. I probably should not have been surprised, since this is evident in the notebook by examining the output line. (In version 11 anyway; I do not recall whether this was true in previous versions.) Similarly, TableForm stores in Out[] only its first argument However, this wrapper stripping is not true of all forms. For example, after evaluation of a StringForm we find that Out[] stores a StringForm, not a String.

My question: what rule governs the value of Out[]?

$\endgroup$

marked as duplicate by Mr.Wizard Jun 26 '18 at 14:22

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.

  • $\begingroup$ Same behavior in v8.0.4 and v9.0.1. $\endgroup$ – xzczd Dec 4 '16 at 14:47
  • $\begingroup$ Related question: mathematica.stackexchange.com/q/11370/3066 $\endgroup$ – m_goldberg Dec 5 '16 at 0:27
  • 1
    $\begingroup$ @xzczd. I think this behavior has been in the main loop evaluation cycle all the way back to V1.0. $\endgroup$ – m_goldberg Dec 5 '16 at 0:40
5
$\begingroup$

The value stored in Out is, as a rule, what is displayed in the output. The only exception from that are the various *Forms, MatrixForm being an example of that. When one of such "wrappers" (as the help calls it) is detected in the top level of the output, it is removed from the expression prior to assigning to Out. The front end makes it clear that what's displayed is not the value of Out:

In[1]:= {{1,2},{0,1}} // MatrixForm

Out[1]//MatrixForm= 1   2
         ^^^^^^^^^^    
                     0   1

In[2]:= Out[1]

Out[2]= {{1, 2}, {0, 1}}

The reason for this is that it's convenient for further processing of the output. The MatrixForm itself would be inoperable:

In[3]:= ({{1,2},{0,1}} // MatrixForm) * 2

Out[3]= 2 1   2

           0   1

One would need to manually remove the outer layer every time of using Out[] as the price for wanting to have the output nicely displayed. So Mathematica does that for the user as a convenience.

In your example you assigned MatrixForm[mA] to mfA so that's what mfA is. (This is often exactly the undesirable behaviour.) As a result of the assignment, MatrixForm[mA] is printed. But now the rule gets applied, the top-level MatrixForm is shaved, and only mA stored in Out for that input line.


Update: the list of functions which behave as wrappers is $OutputForms, as found here:

? $OutputForms

$OutputForms is a list of the formatting functions that get stripped off when wrapped around the output.

You can unprotect it, add or remove some (or all of them) and cause MatrixForm to be stored in the Out or StringForm not to:

In[1]:= Unprotect[$OutputForms];                                                

In[2]:= $OutputForms = {StringForm};                                            

In[3]:= {{1,2},{0,1}} // MatrixForm                                             

Out[3]= 1   2

         0   1

In[4]:= % + 2                                                                   

Out[4]= 2 + 1   2

             0   1

In[5]:= "abc" // StringForm                                                     

Out[5]//StringForm= abc
         ^^^^^^^^^^

In[6]:= Head[%]                                                                 

Out[6]= String
$\endgroup$
  • $\begingroup$ Yes, as I said in the original question, "this is evident in the notebook by examining the output line". And I agree with your description, which is also discussed in my original question. I also agree that wrapper stripping offers a certain convenience, although mfA[[1]] is explicit and not tiresome. In fact, when the wrapper is stripped, it looks to me like the rule is to use the first part of the form. My question: is there a rule that allows me to predict that TableForm will be stripped but not SringForm? Or just "usefulness" as determined by Wolfram? $\endgroup$ – Alan Dec 4 '16 at 22:56
  • $\begingroup$ @Alan Seems I misunderstood. I've always thought it was a property of all the *Forms but it's not. Good catch. I'll do more research. $\endgroup$ – The Vee Dec 4 '16 at 23:01
  • 2
    $\begingroup$ @Alan How about this? mathematica.stackexchange.com/a/51968/6041 $\endgroup$ – The Vee Dec 4 '16 at 23:02
  • $\begingroup$ Yes! While that does not provide a "rule", it does pin it down concretely. Thanks! $\endgroup$ – Alan Dec 4 '16 at 23:12
  • $\begingroup$ @TheVee : I believe another exception to your rule is when $PrePrint is used since, quoting from the help documentation, "$PrePrint is applied after Out[n] is assigned, but before the output result is printed." $\endgroup$ – theorist Dec 31 '16 at 0:06
7
$\begingroup$

You should look at Out's down-values. Here is what I get.

out

You can see the Out is essentially an indexed variable with assignments.

Update

Out[ ] is equivalent to

Out[Length[DownValues[Out]]]

Update 2

OK, lets look at the code exactly as you wrote it.

out2

In this case, Out[3] has its MatrixForm wrapper stripped off, so it is the same as Out[2]. This is explained in this Documentation Center article in the section The Main Loop. So what you see is entirely as expected. My example where the assignment to mfA was followed by ; is more interesting. In this case the special rule for ; is invoked and that rule apparently does not strip off wrappers.

$\endgroup$
  • $\begingroup$ Yes, but that just pushes the question back a level: what is the assignment rule? (Btw, you can a different result because of your extra semicolon.) $\endgroup$ – Alan Dec 4 '16 at 16:03
  • $\begingroup$ @Alan. The assignment rules are shown in the down-values. $\endgroup$ – m_goldberg Dec 4 '16 at 16:22
  • $\begingroup$ @Alan. For the effect of ;, see this answer $\endgroup$ – m_goldberg Dec 4 '16 at 16:39
  • $\begingroup$ No, what is show in the downvalues is only what is assigned as down values. My question is exactly about this: what is the rule for creating down values for Out? Either I am completely missing your point, or you have not yet responded to that question. Btw, I pointed out the effect of the semicolon because your example does not display the problem that is at the core of my question; see my example in the original question. $\endgroup$ – Alan Dec 4 '16 at 22:44

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