Re: MatrixForm
- To: mathgroup at smc.vnet.net
- Subject: [mg8856] Re: [mg8804] MatrixForm
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Tue, 30 Sep 1997 20:16:16 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Kent Day <kentd at ces.clemson.edu>
[mg8804] MatrixForm
writes
>I am new to mathematica and am having a problem understanding the
> way ituses MatrixForm. The manual says it only affects the look of
> the equation and not the way it is processed however if I use it I
> get "strange" results.
>
> a={{a11,a12},{a21,a22}}
> b={{b11,b12},{b21,b22}}
>
> MatrixForm[a+b]
> MatrixForm[a]+MatrixForm[b]
> MatrixForm[a]+b
>
> all yield different results. Only the first one being what I
> would've expected i.e. a new matrix {{a11+b11,{a12+b12} etc.. the
> last one gives the strangest result in which is each element of b
> is added to the entrire a matrix.
> any suggestions? also, once a function is in matrixForm as in
> f=MatrixForm[a] how can I get f back into list form (is there a
> ListForm kind of command?)
I'll try and deal with these points below (I have deleted 2D
outputs - please evaluate the inputs to see these)
First enter the example matrices.
In[1]:=
a={{a11,a12},{a21,a22}};
b={{b11,b12},{b21,b22}};
Now for the effects of MatrixForm:
(1) With
In[2]:= MatrixForm[a+b]
Out[2]//MatrixForm= .....
The evaluation process is
first, a+b is evaluated;
then its value is made the output from this entry;
then MatrixForm instructs how to format the display.
So MatrixForm is not part of the output.
The output cell label Out[1]//MatrixForm= is meant to show this
separation.
You can check this
In[3]:= %
Out[3]= {{a11+b11,a12+b12},{a21+b21,a22+b22}}
In[4]:= %1
Out[4]= {{b11,b12},{b21,b22}}
(2) With
In[5]:= 3 +MatrixForm[b]
Out[5]= ...
The input has FullForm Plus[3,MatrixForm[b]])
and the evaluation is
3 + MatrixForm[b]
--> 3+ MatrixForm[{{b11,b12},{b21,b22}}] (this is the output;
MatrixForm *is * part of it and affects the display; the
output cell label is simply Out[ ]=.)
Check
In[6]:= %
Out[6]= .....
This kind of thing also happens with MatrixForm[a]+b (MatrixForm[a]
is added to each entry of b )
(3) With
In[7]:= f = MatrixForm[a]
Out[7]//MatrixForm= ....
the evaluation is
f = MatrixForm[a]
--> f = MatrixForm[{{a11,a12},{a21,a22}}]
(store this assignment to f )
---> MatrixForm[{{a11,a12},{a21,a22}}]
( the situation is now as for the first example)
output is{{a11,a12},{a21,a22}} and is displayed in form
instructed bv MatrixForm.
You can see that MatrixForm is part of the value f
In[8]:= ?f
"Global`f"
f = MatrixForm[{{a11, a12}, {a21, a22}}]
but please distinguish this assignment from the output got on entering f:
In[9]:= f
Out[9]//MatrixForm= .....
In[10]:= %
Out[10]= {{a11,a12},{a21,a22}}
If you want to assign the plain list structure a to f but display in
MatrixForm you can use
In[11]:= MatrixForm[f= a]
Out[11]//MatrixForm= ....
In[12]:=f
Out[12]= {{a11,a12},{a21,a22}}
(4) OTHER APPROACHES
4.1. In TraditionalForm matrices are shown as 2D with parentheses
4.2. $Preprint can be used to automatically display in matrix form.
($Preprint: is applied after evaluation and assignment of output
but before display, so there is no evidence of its use in the outpu)
In[13]:= $PrePrint = If[MatrixQ[#], MatrixForm[#],#]&;
In[14]:= 3+a
Out[14]= ....
But this form does not go inside expressions:
In[15]:= A[a,b]
Out[15]=A[{{a11,a12},{a21,a22}},{{b11,b12},{b21,b22}}]
The following does go inside expressions.
In[16]:= $PrePrint = (#/.x_?MatrixQ :> MatrixForm[x])&;
In[17]:= A[a+b]
Out[17]= ....
In[18]:= $PrePrint=. (*clear the assignment*)
4.3 . MakeBoxes can be used similarly: it is more complicated to
use but is more controllable.
In[19]:= MakeBoxes[m_?MatrixQ, form_]:=
RowBox[{"(", GridBox[Map[MakeBoxes[#,form]&,m,{2}]],")"}]
In[20]:= 3+a
Out[20]= ...
In[21]:= MakeBoxes[m_?MatrixQ, form_]=. (*clear assignment*)
Allan Hayes
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk/training.html
voice:+44 (0)116 2714198
fax: +44 (0)116 2718642
Leicester, UK