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.

  • $\begingroup$ Wow, that's weird! $\endgroup$ – Eli Lansey Apr 25 '12 at 20:05
  • $\begingroup$ Seems like GridBoxes don't work well with packed arrays. Another packed array bug? $\endgroup$ – Rojo Apr 25 '12 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 '12 at 20:08
  • $\begingroup$ Please tell me you can reproduce that...? $\endgroup$ – sebhofer Apr 25 '12 at 20:08
  • 1
    $\begingroup$ foo===bar yields True... $\endgroup$ – faleichik Apr 25 '12 at 20:10

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

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

DiagonalMatrix returns a packed array

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


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
| improve this answer | |
  • $\begingroup$ Good catch, would never have thought of that... $\endgroup$ – sebhofer Apr 25 '12 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 '12 at 20:23
  • $\begingroup$ I'll report it as a bug when I find the time... $\endgroup$ – sebhofer Apr 25 '12 at 20:32
  • $\begingroup$ Although it's not a bug, you definitely found the culprit :) Thanks again! $\endgroup$ – sebhofer Apr 26 '12 at 19:23
  • $\begingroup$ @sebhofer, why is it not considered a bug? $\endgroup$ – Rojo Apr 26 '12 at 19:37
foo = ToExpression[DiagonalMatrix[{1, 1}]]
GridBox[foo] // DisplayForm

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

| improve this answer | |
  • $\begingroup$ Seems like applying ToExpression to packed arrays serves to unpack them $\endgroup$ – Rojo Apr 25 '12 at 20:15

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.