Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.

Note: be aware of the automatic formatting that is applied even to held expressions; see:

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.

Note: be aware of the automatic formatting that is applied even to held expressions; see:

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.

Note: be aware of the automatic formatting that is applied even to held expressions; see:

added 227 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.

Note: be aware of the automatic formatting that is applied even to held expressions; see:

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.

Note: be aware of the automatic formatting that is applied even to held expressions; see:

Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

The previous answers all use Inner to perform the specific operation of Dot, but these do not provide a general way to visualize results. I cannot provide a truly general way either but I feel that this has wider application:

SetAttributes[show, HoldFirst] form[expr_] := expr /. m_ /; MatrixQ@Unevaluated@m :> MatrixForm[m] show[expr_] := Row[{Defer[expr], Unevaluated[expr] /. n_?NumericQ :> Defer[n], expr}, "="] // form 

Test:

{{1, 2}, {3, 4}}.{{5, 6}, {7, 8}} // show 

enter image description here

In the simpler example a possible problem appears:

{1, 1} + {1, -1} // show 

enter image description here

This is because of the behavior:

"foo" + "foo" + "foo" 
3 "foo" 

If this is unacceptable I shall have to include special handling.