This is a really newbie question, but it has me confused. Why does this code work without // MatrixForm
and doesn't work with // MatrixForm
?
cov = {{0.02, -0.01}, {-0.01, 0.04}} // MatrixForm
W = {w1, w2}; FindMinimum[ W.cov.W, W]
The error is:
The function value ... is not a real number
Is MatrixForm
supposed to be typesetting feature or it has other impact?
A similar problem appears here:
a = {{1, 0, 1, 0}, {2, 1, 1, 1}, {1, 2, 1, 0}, {0, 1, 1, 1}};
inv = Inverse[a];
b = MatrixForm@{{0}, {0}, {0}, {1}};
soln = inv.b
Answer
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:
$PrePrint = Replace[# , mat_?MatrixQ :> MatrixForm[mat]] &
You can also find this in Szabolcs's mathematica tricks. To reset the value of $PrePrint
, simply unset it with $PrePrint=.
Comments
Post a Comment