# Why can't I pass in a product of matrices in my modified matrixform? [duplicate]

In attempt to build a function that would work like MatrixForm but would wrap a matrix with brackets, I tried the following function:

matrixform[mat_] := TraditionalForm[DisplayForm[RowBox[{
StyleBox["[", SpanMaxSize -> \[Infinity]],
GridBox[mat],
StyleBox["]", SpanMaxSize -> \[Infinity]]}]]]


This does what I want on an ordinary matrix, say a={{1,2},{3,4}}, as well as on 2a and a+a. But if I pass in a product such as a.a it does not work properly. I have tried wrapping the product in Evaluate, and tried executing a.a separately and then passing in % and this has made no difference. Note that a better function Composition[bracketF,Grid] suggested here lacks these issues. So the question is why can we not pass in a product of matrices into matrixform? This is my first time working with functions such as StyleBox, GridBox, RowBox, etc.

-

## marked as duplicate by Mr.Wizard♦Jul 25 '14 at 7:27

This seems to be another strange behavior, probably a bug. I'll take a closer look tomorrow. –  Mr.Wizard Jul 24 '14 at 20:42
Not a bug after all. Please see my answer below. –  Mr.Wizard Jul 25 '14 at 6:57

This behavior is present in both version 7 and version 10 (Windows). Illustrated:

IdentityMatrix[2] // matrixform


{{1, 0}, {0, 1}} // matrixform


There is a difference between {{1, 0}, {0, 1}} and (the evaluated form of) IdentityMatrix[2]: the latter is a packed array.

{{1, 0}, {0, 1}}  // DeveloperPackedArrayQ
IdentityMatrix[2] // DeveloperPackedArrayQ

False

True


It seems that GridBox (or the Front End interpreting it) is not equipped to handle packed arrays.
Your original definition works if we unpack:

IdentityMatrix[2] // DeveloperFromPackedArray // matrixform


matrixform[mat_?DeveloperPackedArrayQ] :=
DeveloperFromPackedArray[mat] // matrixform

-
Thanks for figuring all of this out. Since it is a Duplicate, do I accept an answer still? I am guessing the reason bracketF from here "works" is because it uses ToBoxes which gives box expressions and GridBox works on box expressions. –  Ben Allgeier Jul 25 '14 at 14:44
@Ben It's still polite to Accept an answer but as always users are under no obligation to do so. –  Mr.Wizard Jul 25 '14 at 14:51

I can't explain this strange behaviour of GridBox. But replacing it with Grid I get the desired output (also with a.a // matrixform)

matrixform[mat_] :=

To align the numbers properly use Grid[mat, Alignment -> Right]
Thanks. I thought I tried Grid at one point but was somehow discouraged. I was under the impression that when working with RowBox and StyleBox that I had to use GridBox`. I guess that was wrong. –  Ben Allgeier Jul 24 '14 at 17:25