Re: Transpose matrix does not work when MatrixForm is used, why?
- To: mathgroup at smc.vnet.net
 - Subject: [mg45398] Re: Transpose matrix does not work when MatrixForm is used, why?
 - From: Dr Bob <drbob at bigfoot.com>
 - Date: Tue, 6 Jan 2004 04:16:56 -0500 (EST)
 - References: <0BB4C7A6482ABB4EB233E1EF37A399CDE19C10@NAEAPAXREX10VA.nadsusea.nads.navy.mil>
 - Reply-to: drbob at bigfoot.com
 - Sender: owner-wri-mathgroup at wolfram.com
 
Great work!
But.... MatrixForm@m (when m is already MatrixForm) is an infinite loop.
After some laborious sleuthing, I found that adding MatrixQ to the 
HeadTest list fixes that:
Unprotect[MatrixForm];
HeadTest[func_] := Intersection[{func}, {
     If, List, Set, SetDelayed, UpSet, UpSetDelayed,
        TagSet, TagSetDelayed,
         Format, MakeBoxes, MakeExpression, ToBoxes, Shallow, Short,
         Hold, HoldForm, HoldComplete, HoldPattern,
         Verbatim, Blank, BlankSequence,
         BlankNullSequence, Alternatives, Pattern, Optional, Repeated, \
RepeatedNull, Condition, PatternTest, Rule, RuleDelayed,
         FullForm, InputForm, MatrixQ}] === {}
MatrixForm /: (f_?HeadTest)[a1___, a2_MatrixForm, a3___] := Module[{
         result}, result = Replace[{a1, a2, a3}, MatrixForm -> Identity, 2, 
\
Heads -> True];
     result = f @@ result;
     If[MatrixQ@result, MatrixForm[result], result]]
Protect[MatrixForm];
BUT... now MatrixQ@m isn't True, and that's not good, is it?
Bobby
On Mon, 5 Jan 2004 15:29:45 -0500, Ersek, Ted  CIV NASPATUXENTRIVERMD 
4.5.1.2 <ted.ersek at navy.mil> wrote:
> Recall the recent thread on how do ensure we can do math on matrices 
> wrapped in MatrixForm.
> Apparently other solutions didn't quite do the trick.  I think the code 
> below is better.
> (*------------------------------------------*)
>
> Unprotect[MatrixForm];
>
> HeadTest[func_]:= Intersection[{func},
>   {If,List,Set,SetDelayed,UpSet,UpSetDelayed,TagSet,TagSetDelayed,Format,
>      MakeBoxes,MakeExpression,ToBoxes,Shallow,Short,Hold,HoldForm,HoldComplete,
>      HoldPattern,Verbatim,Blank,BlankSequence,BlankNullSequence,Alternatives,
>      Pattern,Optional,Repeated,RepeatedNull,Condition,PatternTest,Rule,
>      RuleDelayed,FullForm,InputForm
>    }]==={}
>
>
> MatrixForm/:(f_?HeadTest)[a1___, a2_MatrixForm, a3___]:=
>   Module[{result},
>     result = Replace[ {a1,a2,a3}, MatrixForm->Identity, 2, Heads->True ];
>       result = f@@result;
>      If[ MatrixQ@result, MatrixForm[result], result ]
>     ]
>
>
> (*---------------------------------------------------------*)
> Now we can do the following and get the output in MatrixForm.
>
>
>   m1=MatrixForm[{{2,3,4},{1,5,2},{6,3,1}}]
>
>   Inverse[m1]
>
>   MatrixPower[m1,4]
>
>   m2={{2,0,4},{0,5,2},{0,3,1}};
>   m1.m1.m1.m2
>
>   Det[m1]
>
>   Part[m1,1,3]
>
>
> ----------------
> Regards,
>    Ted Ersek
>
> Download Mathematica Tips From
> http://www.verbeia.com/mathematica/tips/Tricks.html
>
>
>