RE: Automatic Display in MatrixForm

*To*: mathgroup at smc.vnet.net*Subject*: [mg19265] RE: [mg19206] Automatic Display in MatrixForm*From*: "Ersek, Ted R" <ErsekTR at navair.navy.mil>*Date*: Thu, 12 Aug 1999 01:24:21 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Earlier I improved a method Bruno Daniel gave that would ensure matrices are displayed in MatrixForm. My solution was: $Post = (#/. x_?MatrixQ :> MatrixForm[x])&; A response from Allan Hayes (paraphrased) said: ------------------- There are still problems: In[1]:= $Post=#/.(x_?MatrixQ:>MatrixForm[x])&; In[2]:= mtrx={{2,0,0},{0,-1,0},{0,0,3}}; mtrx.a Out[3]= MatrixForm[{{2, 0, 0}, {0, -1, 0}, {0, 0, 3}}] . a In[4]:= %/.a->mtrx Out[4]= MatrixForm[MatrixForm[{{2, 0, 0}, {0, -1, 0}, {0, 0, 3}}]] . MatrixForm[{{2, 0, 0}, {0, -1, 0}, {0, 0, 3}}] -------------------------- ---[Alan Hayes] again ---- The MatrixForm wrappers above prevent the Dot product. Perhaps a more general solution is as follows (where MatrixForm is first replaces by Identity to allow evaluation, and is then replaced appropriately). Allan awaits a list of problems with this also! In[5]:= $Post=(#/.MatrixForm->Identity/.x_?MatrixQ:>MatrixForm[x])&; In[6]:= mtrx.a Out[6]= MatrixForm[{{2, 0, 0}, {0, -1, 0}, {0, 0, 3}}] . a In[7]:= %/.a->mtrx Out[7]//MatrixForm= MatrixForm[{{4, 0, 0}, {0, 1, 0}, {0, 0, 9}}] ------------------ ---[Ted Ersek]---- It seems Alan's version works better than mine. When this solution does cause trouble one can easily change $Post back and forth. Example: DisplayMatrices=(#/.MatrixForm->Identity/.x_?MatrixQ:>MatrixForm[x])&; $Post=DisplayMatrices; (** Use MatrixForm **) $Post=. (** Don't use MatrixForm **) ------------------------- ---[Ted Ersek] again ---- Also TraditionalForm uses MatrixForm to display matrices, so you could make the menu selection: Cell > Default Output Format Type > Traditio0nalForm This of course forces the other conventions that come with TraditionalForm, and you may not want that. ------------------------- ---[Ted Ersek] again ---- For a completely different approach one can use MakeBoxes to force StandardForm formats matrices the same way MatrixForm does. About two years ago I came up with the following to do this: MakeBoxes[m_?MatrixQ, StandardForm]:= RowBox[{"(",GridBox[Map[MakeBoxes[#, StandardForm]&, m, {2}]],")"}] But then if you evaluate HoldForm[3*{{1,2},{3,4}}] you get the MatrixForm equivalent of {{3,6},{9,12}}. -------------------- Via technical support Dave Withoff solved the problem above. Dave replied: Typesetting rules often require meticulous control of evaluation. MakeBoxes[m_?MatrixQ, StandardForm]:= RowBox[{"(",GridBox[Map[MakeBoxes[#, StandardForm]&, m, {2}]],")"}] will be applied to ( 3*{{1,2},{3,4}} ) for the same reason that MatrixQ[3*{{1,2},{3,4}}] returns True. The original input isn't a matrix, but it evaluates to something that is a matrix, so the rule is applied. Since the expression inside of Hold or HoldForm must be displayed, these rules are applied regardless of whether or not the enclosing expression would otherwise hold the expression unevaluated. Dave's recommended solution is given below, and I know of no problems with it. (********************) MakeBoxes[m_? (Function[t, MatrixQ[Unevaluated[t]]&&(t=!={{}}),HoldAll]), StandardForm ]:= RowBox[{"(", GridBox[ Map[Function[t, MakeBoxes[t, StandardForm], HoldAll], Unevaluated[m],{2} ] ], ")"}] (********************) Regards, Ted Ersek