3
$\begingroup$
Unprotect[Power];
Power[0, 0] = 1;
Protect[Power];
Unprotect[Dot];
Dot[x_, y_?NumberQ] := x y;
Dot[x_?NumberQ, y_] := x y;
Protect[Dot];
$Post = # /. f_[{x__}] :> MatrixFunction[f, {x}] &;
Sqrt[( {
    {-1, 0},
    {0, -1}
   } )] // MatrixForm

The output comes in list form, not as a matrix.

$\endgroup$

2 Answers 2

1
$\begingroup$

You only need the last 2 lines to reproduce the issue, and the last 2 lines can be further simplified to

$Post = # /. f_[{x__}] :> aaa[f, {x}] &;
{{-1, 0}, {0, -1}} // MatrixForm
(* aaa[MatrixForm, {{-1, 0}, {0, -1}}] *)

As we can see, the rule in $Post is executed later than MatrixForm i.e. the code above is amount to

$Post =.;
{{-1, 0}, {0, -1}} // MatrixForm // (# /. f_[{x__}] :> aaa[f, {x}] &)

This is consistent with the description in the document of $Post:

$Post is a global variable whose value, if set, is applied to every output expression.

See also the introduction in the tutorial The Main Loop.

$\endgroup$
6
  • $\begingroup$ Is there a way to format the output as a matrix? $\endgroup$
    – Anixx
    Nov 2, 2022 at 16:05
  • $\begingroup$ @Anixx Of course, just move the MatrixForm to $Post, too. (Alternatively, $PrePrint. ) See this post for more info: mathematica.stackexchange.com/q/3098/1871 $\endgroup$
    – xzczd
    Nov 2, 2022 at 16:09
  • $\begingroup$ Moving it to $Post does not work for me. $\endgroup$
    – Anixx
    Nov 2, 2022 at 16:12
  • $\begingroup$ @Anixx How do you move it? $\endgroup$
    – xzczd
    Nov 2, 2022 at 16:35
  • $\begingroup$ $Post = ReleaseHold[Hold[#] /. f_[{x__}] :> MatrixFunction[f, {x}]]//MatrixForm &; $\endgroup$
    – Anixx
    Nov 2, 2022 at 16:40
0
$\begingroup$

Consider the full form your your evaluated expression:

Sqrt[({{-1, 0}, {0, -1}})] // MatrixForm //FullForm

enter image description here

This matches your pattern:

 f_[{x__}]

With f == MatrixForm and {x_} == {{-1, 0}, {0, -1}}

Therefore, the matrix representation is changed into:

MatrixFunction[Sqrt, {{-1, 0}, {0, -1}}]

and this evaluates into:

{{I, 0}, {0, I}}
$\endgroup$
2
  • $\begingroup$ "With f == Sqrt and {x_} == {{-1, 0}, {0, -1}}" Clearly not. The Sqrt is no longer there. $\endgroup$
    – xzczd
    Nov 2, 2022 at 15:56
  • $\begingroup$ (-1) The answer is still incorrect after edit. $\endgroup$
    – xzczd
    Nov 3, 2022 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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