4 Answers
4

MatrixForm is a wrapper that pretty-prints your matrices. When you do the following:

cov = {{0.02, -0.01}, {-0.01, 0.04}} // MatrixForm

you're assigning the prettified matrix to cov (i.e., wrapped inside a MatrixForm). This is not accepted as an input by most functions (perhaps all) that take matrix arguments. What you should be doing to actually assign the raw matrix to cov, yet get a pretty print in the output, is the following:

(cov = {{0.02, -0.01}, {-0.01, 0.04}}) // MatrixForm

You can also read more about why this happens due to the different precedences here in Leonid's book.

You can also avoid having to use MatrixForm every time by setting the default display for matrix outputs to be typeset accordingly. For this, you set the $PrePrint variable in your init.m file as:

Thanks. Was always wondering why it always did that. I just turned to a calculator if it was a big complicated system. But now I wanted to figure out why so I can do it in mathematica.
–
night owlMar 24 '12 at 4:32

Of course MatrixForm (like all formatting functions) is special in that it is stripped away from the output stored in Out if it is applied top-level (but only in that case). This may cause the wrong impression that it has no effect on calculation, but only on display.
–
celtschkMar 24 '12 at 8:13

David's answer is correct and the one you need to solve your specific problem. I thought nonetheless that it is worth providing some additional information that might help explain how to diagnose similar issues.

Matrix/tensor operations like Dot and Inverse are designed to work with lists, that is, expressions with a Head of List. It also works with SparseArray objects. From the documentation:

When its arguments are not lists or sparse arrays, Dot remains unevaluated.

You can check whether your expressions have compatible Heads using FullForm. It is common for people to use postfix (//) notation to check this. As you can see, in your version of the code, a has head List while b has head MatrixForm. So they can't combine.

a//FullForm

List[List[1,0,1,0],List[2,1,1,1],List[1,2,1,0],List[0,1,1,1]]

b//FullForm

MatrixForm[List[List[0],List[0],List[0],List[1]]]

If you discover you have erroneously created a matrix wrapped in MatrixForm, you can change it back to a list using First.

FullForm[First[b]]

List[List[0],List[0],List[0],List[1]]

As an aside, I don't see any point assigning the variable inv to represent the Inverse of a. Unless your real problem uses $a'$ more than once (especially if it is expensive to calculate), you can just as easily do:

Inverse[a].(First@b)

Note that I have mixed using of @ and [] style notation for pedagogical reasons.

Thank You! This was very pleasing to read. I like to read something and it physically makes since (in terms of how the kernel internally deciphers computations) and learn different ways to implement the same task, while also making it robust and more efficient without defining extra variables unnecessarily. +1 :)
–
night owlMar 25 '12 at 23:47

While the question has been more than answered there are still some things that seem to me worth adding. The first is that, in my opinion, MatrixForm is "essentially" obsolete. If you wish your matrices always look like matrices (in the output) you can set the format type of output cells to TraditionalForm (use the Appearance tab in the Preferences menu). In fact, you can also set the format type of your input cells to TraditionalForm, although you have to be a little careful if you do that (doing that is not recommended by WRI but it has some well known supporters...).

Alternatively you can use the ConvertTo menu to convert any matrices to TraditionalForm while keeping other cells or expressions in StandardForm (if you prefer that). The keyboard shortcut for this is Ctrl-Shift-T. Finally, if you would like all your matrices always to appear in MatrixForm and avoid these evaluation problems, you can evaluate at the beginning of your Mathematica session

$Post = If[MatrixQ[#], MatrixForm[#], #] &

or you can put it into an init file and have it evaluate automatically. (Of course you can use TraditionalForm in place of MatrixForm).

Coming back to the issue of TraditionalForm vs MatrixForm for matrices: the only problem I can see with using the former is that it looks "too nice" so that if the rest of your output is in StandardForm the style of your matrices will not match the rest of your output. But other than that I can't think of any use for MatrixForm.

Thanks. This was useful information to know. So exactly, how does this $Post = If[MatrixQ[#], MatrixForm[#], #] & works internally or in general? Because it let's the kernel know of some preliminary settings before you actually start creating List to do computations on, correct. And Mathematica will know what to do with the result (in terms of how you want it to be outputted to the screen).
–
night owlMar 25 '12 at 23:52

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.