Recently I noticed a peculiar behavior of MatrixForm
that I failed to find a way to explain. Consider the following example:
a = {{1, 4}, {6, 9}} // MatrixForm;
a
My question is, after executing the code above, what is displayed in the notebook?
"That's simple! It's MatrixForm[{{1, 4}, {6, 9}}]
, notice you can't use this directly in evaluation!" Yeah, I thought so, until I tried:
"…OK, I know how to explain this. There's a number of functions whose output in the notebook is not the same as the one in the kernel, for example, Format
:
Format[int[f_, x_]] := HoldForm@Integrate[f, x]
int[Sin[x], x]
"and MatrixForm
is just another of them. " Good point, unless MatrixForm
doesn't behave as follows:
So the output of MatrixForm
in the notebook seems to exist as a combination of multiple states, it
- becomes a
List
if you edit and execute it; - becomes a
MatrixForm
expression if you edit and press Ctrl+Shift+i; - remains in a superposition of
List
andMatrixForm
if you don't edit and just press Ctrl+Shift+i and Ctrl+Shift+n alternately.
Another function that behaves like this is NumberForm
:
NumberForm[Pi // N, 10]
There may be more.
Further check shows this behavior has been existing at least since v3.
How to understand this behavior? A feature, or a long standing bug?
If it's a feature, is it possible to reproduce this behavior with self-made function?
Mr. Wizard thinks this post is a duplicate of this post, but that post is really about another issue. Let's look at the example in that post:
mat = {{1, 2}, {3,4}};
mat // MatrixForm
Head[%]
I've been aware of this behavior of MatrixForm
long before I discovered the one mentioned in this question, but I don't find it unexpected at all, because as we can see, in this case the output displays as
Out[5] // MatrixForm
rather than a single
Out[5]
which suggests that, the expression stored in Out[5]
is exactly a List
, rather than a MatrixForm
expression. So when we use %
(which is short for Out[]
), there's no doubt we'll obtain a List
.
In a word, that question is about how expression wrapped by MatrixForm
is
stored in the Out
variable, while mine is about how expression wrapped by MatrixForm
exists in the front end, they're different, though the answer to my question turns out to be related to $OutputForms
, too.