3
$\begingroup$

I'm trying to display Matrices with square brackets in Mathematica.

I've found this post from a mailing list in 2009 http://forums.wolfram.com/mathgroup/archive/2009/Aug/msg00458.html and it seems to work fine for whole numbers and symbols.

NotebookWrite[InputNotebook[], 
 TemplateBox[{GridBox[{{a, b}, {c, d}}]}, "Identity", 
  DisplayFunction -> (RowBox[{StyleBox["[", 
        SpanMaxSize -> \[Infinity]], #1, 
       StyleBox["]", SpanMaxSize -> \[Infinity]]}] &)]]

(* Outputs: *)
Identity[{
  {a, b},
  {c, d}
 }]

(* Which displays correctly as a square matrix. *)

But as soon as I try to input floating point numbers, I get floating point precision markers displayed. For example:

NotebookWrite[InputNotebook[], 
 TemplateBox[{GridBox[{{2.1, 1}, {1, 1.2}}]}, "Identity", 
  DisplayFunction -> (RowBox[{StyleBox["[", 
        SpanMaxSize -> \[Infinity]], #1, 
       StyleBox["]", SpanMaxSize -> \[Infinity]]}] &)]]

(* Outputs: *)
Identity[{
  {2.1000000000000001`, 1},
  {1, 1.2`}
 }]

(* Which displays correctly as a square matrix. *)

I don't see any function in there that would force the numbers to be evaluated into machine precision form. I think using HoldForm or others could solve this issue but I'm not too sure where that can be placed since GridBox needs a list and RowBox needs a box - evaluating them individually displays decimal numbers just fine.

For clarification, I'm looking for something like this:

(* In the square bracketed matrix display form of course *)
Identity[{
  {2.1, 1},
  {1, 1.2}
 }]

Any help for a beginner? Thank you!

$\endgroup$
  • $\begingroup$ Obviously I think this has to do with floating point representation inaccuracies in binary forms but I want Mathematica to display those numbers symbolically without evaluating them, if that makes sense. $\endgroup$ – Michael Yoo Apr 28 '16 at 10:03
  • $\begingroup$ And if I use Defer[] in one of the matrix elements, I get an error message saying An unknown box name (Defer) was sent as the BoxForm for the expression. Check the format rules for the expression. $\endgroup$ – Michael Yoo Apr 28 '16 at 10:18
  • $\begingroup$ Okay, so using ToBoxes[Defer[2.1]] instead of Defer[2.1] seems to have solved the problem. Now just have to figure out how to eliminate the backtick precision marker... $\endgroup$ – Michael Yoo Apr 28 '16 at 10:20
  • $\begingroup$ What about makeBrackMat[mat_?MatrixQ] := DisplayForm[RowBox[{"[", GridBox[mat], "]"}]]? $\endgroup$ – user31159 Apr 28 '16 at 10:21
  • $\begingroup$ Probably better: makeBrackMat[mat_?MatrixQ] := DisplayForm[ RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]], GridBox[mat], StyleBox["]", SpanMaxSize -> \[Infinity]]}]]. $\endgroup$ – user31159 Apr 28 '16 at 10:24
3
$\begingroup$
makeBrackMat[mat_?MatrixQ] := 
     DisplayForm[
         RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]], 
                 GridBox[mat], 
                 StyleBox["]", SpanMaxSize -> \[Infinity]]}
         ]
     ];

Exact numbers:

mat1 = Partition[Range[12], 3];
makeBrackMat[mat1]

enter image description here

Machine-precision numbers:

mat2 = {{1.3, 2.9}, {9.5, 8.4}, {7.6, 0.2}};
makeBrackMat[mat2]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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