As others have pointed out, using MatrixForm
when assigning values will result in something that's no longer treated as a matrix for many functions. You can see the difference directly with the following example.
a = IdentityMatrix[2];
b = MatrixForm[a];
FullForm[a]
(* List[List[1, 0], List[0, 1]] *)
FullForm[b]
(* MatrixForm[List[List[1, 0], List[0, 1]]] *)
FullForm[a.b]
(* Dot[List[List[1, 0], List[0, 1]], MatrixForm[List[List[1, 0], List[0, 1]]]] *)
In this case, the function Dot
simply doesn't have a definition for handling inputs with the pattern MatrixForm[_]
. However, it's often the case that you still want to view the results of intermediate calculations in a readable format. Here are a couple of ways to get around this:
Change the way you make assignments
You could make your assignments like so:
MatrixForm[wd = -Log[1 - od/(25 + id)]]

That way, the correct value is assigned, but the output will still display the way you want to, since MatrixForm
is only being placed on the output of the assignment, not the assigned value itself.
wd

Change the default behavior of Dot
Another option is to directly add the rule to Dot
to accept MatrixForm
inputs like so:
Unprotect[Dot]
Dot[MatrixForm[m1_], m2_] := Dot[m1, m2]
Dot[m1_, MatrixForm[m2_]] := Dot[m1, m2]
Protect[Dot]
This way, when you attempt to multiply matrices that have a MatrixForm
wrapper, Dot
will automatically use the contents of the wrapper instead. This behavior will remain for the current session.
However, you need to be careful with using Unprotect
to change the behavior of built-in functions. It's often the case that you'll just keep running into similar issues down the road and you'll need to keep modifying built-in functions to the point where you'll have a giant unpredictable mess on your hands.
Other functions
As a relevant example, note that the Total
function also has different behaviors for the following:
u = {x, y, z};
v = MatrixForm[u];
Total[u]
(* x + y + z *)
Total[v]
(* {x, y, z} *)
This is because MatrixForm
will assume a row vector when its input is a 1-dimensional list. So Total[v]
is like
Total[{{x, y, z}}]
(notice the extra braces)
This behavior for Total
is why your last line isn't working properly, because you're trying to compute
Total[MatrixForm[M.wd], {1}]
To fix this, you could also use Unprotect
for Total
and add a rule in, but I wouldn't recommend that route. Instead, the first option is probably safest, even if it means you have to type things a bit differently.
MatrixForm[x = M.wd]
M=AdjacencyMatrix[s]
and (2) ` wd=-Log[1 - od/(25+id)` . $\endgroup$