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.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by Mr.Wizard Jul 25 at 7:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

2 Answers 2

up vote 3 down vote accepted

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

IdentityMatrix[2] // matrixform

enter image description here

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

enter image description here

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}}  // Developer`PackedArrayQ
IdentityMatrix[2] // Developer`PackedArrayQ
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] // Developer`FromPackedArray // matrixform

enter image description here

You can therefore correct your definition by adding this:

matrixform[mat_?Developer`PackedArrayQ] :=
  Developer`FromPackedArray[mat] // matrixform
share|improve this answer
    
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 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 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_] := 
 TraditionalForm[
  DisplayForm[
   RowBox[{StyleBox["[", SpanMaxSize -> \[Infinity]], Grid[mat], 
     StyleBox["]", SpanMaxSize -> \[Infinity]]}]]];

To align the numbers properly use Grid[mat, Alignment -> Right]

share|improve this answer
    
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 at 17:25

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