11
$\begingroup$

I have never used low-level formating functions, so this problem might have a simple solution. I'm currently pulling my hair out trying to solve it, however. Consider the following example:

foo = DiagonalMatrix[{1, 1}]
(*{{1, 0}, {0, 1}}*)

bar = {{1, 0}, {0, 1}}
(*{{1, 0}, {0, 1}}*)

GridBox[foo] // DisplayForm
(*GridBox[{{1, 0}, {0, 1}}]*)

GridBox[bar] // DisplayForm
(*gives correctly formated output*)

Why does the DisplayForm depend on the way the list was created?

Edit: I contacted WRI Technical support and got the following answer:

GridBox is doing what it should do. Box forms interpret strings and boxes and not Mathematica expressions. Thus there is no reason to expect GridBox to interpret a packed array which is a compiled expression. Grid uses GridBox for the formatting but unpacks first.

I guess the bottomline is, use Grid instead of GridBox if possible (just as I should have done), or unpack arrays in advance.

$\endgroup$
7
  • $\begingroup$ Wow, that's weird! $\endgroup$
    – Eli Lansey
    Apr 25, 2012 at 20:05
  • $\begingroup$ Seems like GridBoxes don't work well with packed arrays. Another packed array bug? $\endgroup$
    – Rojo
    Apr 25, 2012 at 20:07
  • $\begingroup$ It's also contagious. If you set foo1=foo the weirdness happens with foo1 as well. $\endgroup$
    – Eli Lansey
    Apr 25, 2012 at 20:08
  • $\begingroup$ Please tell me you can reproduce that...? $\endgroup$
    – sebhofer
    Apr 25, 2012 at 20:08
  • 1
    $\begingroup$ foo===bar yields True... $\endgroup$
    – faleichik
    Apr 25, 2012 at 20:10

2 Answers 2

11
$\begingroup$

Seems like GridBox doesn't work well with packed arrays. Or at least not with DisplayForm

r = RandomInteger[10, {4, 4}];
Developer`PackedArrayQ[r]
rnp = Developer`FromPackedArray[r];
DisplayForm[GridBox[r]]
DisplayForm[GridBox[rnp]]

DiagonalMatrix returns a packed array

Developer`PackedArrayQ[DiagonalMatrix[{1, 2, 3, 4, 5}]]

True

So, I'd say it's a bug, but a workaround to ensure it all works fine is to wrap your variable in FromPackedArray before putting it in the GridBox

foo = DiagonalMatrix[{1, 1}];
GridBox[Developer`FromPackedArray@foo] // DisplayForm
$\endgroup$
6
  • $\begingroup$ Good catch, would never have thought of that... $\endgroup$
    – sebhofer
    Apr 25, 2012 at 20:17
  • $\begingroup$ Thanks @sebhofer. I had context of a similar bug that I found in a Mr.Wizard's answer, that ended up in him asking this question. Seems like there are some corner cases when packed arrays are a little buggy, and one can only be safe by unpacking them manually. Hope they get fixed $\endgroup$
    – Rojo
    Apr 25, 2012 at 20:23
  • $\begingroup$ I'll report it as a bug when I find the time... $\endgroup$
    – sebhofer
    Apr 25, 2012 at 20:32
  • $\begingroup$ Although it's not a bug, you definitely found the culprit :) Thanks again! $\endgroup$
    – sebhofer
    Apr 26, 2012 at 19:23
  • 1
    $\begingroup$ Rojo, Wolfram claims it's not a bug (see edit above). I don't know much about the logic of these low-level formatting functions, but it seems reasonable to me to have this hierarchic structure of GridBox, Grid,... $\endgroup$
    – sebhofer
    Apr 26, 2012 at 21:36
5
$\begingroup$
foo = ToExpression[DiagonalMatrix[{1, 1}]]
GridBox[foo] // DisplayForm

works in the docs it states In StandardForm, explicit GridBox objects are output literally.

$\endgroup$
1
  • $\begingroup$ Seems like applying ToExpression to packed arrays serves to unpack them $\endgroup$
    – Rojo
    Apr 25, 2012 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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