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!
Answer
makeBrackMat[mat_?MatrixQ] :=
DisplayForm[
RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]],
GridBox[mat],
StyleBox["]", SpanMaxSize -> \[Infinity]]}
]
];
Exact numbers:
mat1 = Partition[Range[12], 3];
makeBrackMat[mat1]
Machine-precision numbers:
mat2 = {{1.3, 2.9}, {9.5, 8.4}, {7.6, 0.2}};
makeBrackMat[mat2]
Comments
Post a Comment