Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

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

2 Answers 2

up vote 11 down vote accepted

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
share|improve this answer
    
Good catch, would never have thought of that... –  sebhofer Apr 25 '12 at 20:17
    
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 –  Rojo Apr 25 '12 at 20:23
    
I'll report it as a bug when I find the time... –  sebhofer Apr 25 '12 at 20:32
    
Although it's not a bug, you definitely found the culprit :) Thanks again! –  sebhofer Apr 26 '12 at 19:23
    
@sebhofer, why is it not considered a bug? –  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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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