Transpose, Re: The Case of the Mystery Option

• To: mathgroup at christensen.cybernetics.net
• Subject: [mg1131] Transpose, Re: [mg1056] The Case of the Mystery Option
• From: Allan Hayes <hay at haystack.demon.co.uk>
• Date: Wed, 17 May 1995 02:39:13 -0400

```>From: Jack Goldberg <jackgold at math.lsa.umich.edu> in [mg1056] The
Robert Villegas responded in mg[1063].

than for getting the diagonal of a square matrix by
Transpose[M,{1,1}]. I did find a use for calculating the determinant
(mathgroup April 1992 I think). The result is given after the
following notes.

********
First, let's look at the way we extract the elements of a tensor T.
Suppose that its tensor rank is 4 and that its dimensions  are
{d1,d2,d3,d4} then we need 4 positive integers  a1,a2,a3,a4 to get
an element :
T[[a1,a2,a3,a4]]
and, of course ,we must have  ai <= di for all i.

Transpose[ T, {2,1,3,2}]] is a tensor that given  three  positive
integers a1,a2,a3  will return the value

Transpose[ T, {2,1,3,2}] [[a1,a2,a3]]  =  T[[a2,a1,a3,a2]]

We only need three numbers because the repeated 2 uses the second
one twice. So the tensor rank is 3  and we must have   a1 <= d2, a2
<= d1, a2 <= d4, a3 <= d3.  In fact  Transpose  requires that  d1 =
d4. The dimensions of the transposed tensor are  {d2,d1,d4}

Similarly
Transpose[ T, {2,1,1,2}] [[a1,a2]]  =  T[[a2,a1,a1,a2]]

and the trnsposed tensor has rank 2 and dimensions  {d2,d1}

Check
T = Array[SequenceForm,{2,4,4,2}];
Dimensions[T]
{2, 4, 4, 2}

Transpose[T,{2,1,3,2}][[1,2,3]]
2132
Dimensions[Transpose[T,{2,1,3,2}]]
{4, 2, 4}

Transpose[T,{2,1,1,2}][[1,2]]
2112
Dimensions[Transpose[T,{2,1,1,2}]]
{4, 2}

The GENERAL PATTERN (for elements) is

(**

Transpose[T_, p_List][[a__]] = T[[Sequence@@({a}[[p]])]]

**)

In the examples so far the length of  p  has been equal to the
tensor rank T (this is needed t o provide the right number of inputs
to T. But it would be inconvenient to always have to provide  the
full sequence so for example, for a tensor T of tensor rank 5
Mathematica interprets
Transpose[ T, {2,1,1}] as Transpose[ T, {2,1,1,3,4}]
(appending successive integers from the first positive integer not
appearing in {2,1,1}).

So much for the elements.
But  how do we construct the transposed tensor itself as an array?
Well that's the job of the Array function.
Notice that we don't need to allow for extending  p  -- this is
automatically taken care of

Trans[T_, p_List] :=
Block[{dims, trdims},
dims = Dimensions[T];
trdims =
dims[[Position[p,#,{1},1][[1,1]]]]&/@
Range[Length[Union[p]]];
Array[ T[[Sequence@@({##}[[p]])]]&, trdims ]
]

The conditions imposed by Transpose are (where the rank of T is  r
and its dimensions are  {d1,d2,...,dr }).

1. Union[ p]  is Range[t] for some t <= r;
2. whenever   pj = pk then  dj = dk. (where p = {p1, p2, . . , pr})

The transposed  tensor  is of rank r minus the number of repeats in
p and, as for dimensions, if s occurs  in at position  t  in  p
then sth dimension is  dt , otherwise the sth dimension can be
worked out by extending  p  as  discusssed above.

Check
Trans[T,{2,1,1,2}] === Transpose[T,{2,1,1,2}]
Trans[T,{2,1,1}] === Transpose[T,{2,1,1}]
Trans[T,{1,2,2}] === Transpose[T,{1,2,2}]
True
True
True

Finding the diagonal

For a square matrix  M, Transpose[M, {1,1}] is the diagonal of M
because.
Transpose[M, {1,1}][[a]] = M[[a,a]]

Thus

Transpose[{{a,A},{b,B}}, {1,1}]
{a, B}

But for non-square matrices

Transpose[{{a,A},{b,B},{c,C}}, {1,1}]
Transpose::perm: {1, 1} is not a permutation. (*message*)
Transpose[{{a, A}, {b, B}, {c, C}}, {1, 1}]
The message really means  that  the condition 2. is not met.

This can be allowed for by changing the definition of  trdims   in
the code for Trans

Trans2[T_, p_] :=
Block[{dims},
dims = Dimensions[T];
trdims =
Min[dims[[Flatten[Position[p,#,{1}]]]]]&/@
Range[Length[Union[p]]];
Array[ T[[Sequence@@({##}[[p]])]]&, trdims ]
]

Now we have

Trans2[{{a,A},{b,B},{c,C}}, {1,1}]

{a, B}
Trans2[Transpose[{{a,A},{b,B},{c,C}}], {1,1}]
{a, B}
It seems resaonable to call this the diagonal of these matrices

APPLICATION OF TRANSPOSE
Transpose[T,{1,1}]  seems to be the quickest way to get the
diagonal of a square matrix.

The code for the determinant that I arrived at was

NewDet4[M_] :=
( Signature[M]
Plus@@Times@@
Append[ Transpose[#,{2,1,1}],(Signature/@#) ]&@
Permutations[ M ]
)

Timings
m = Table[ StringJoin["a",ToString[i],ToString[j]], {i,1,7},{j,1,7}];
NewDet4[m];//Timing
{6.58333 Second, Null}
Det[m];//Timing
{10.3667 Second, Null}

But be warned: I think that it was Dave Withoff who pointed out
that the result for Det was because of internal use of Expand and,
maybe,Together.

Block[{Expand},Det[m];//Timing]
{0.166667 Second, Null}

(Block[{Expand},Det[m]] - Det[m])//Expand
0

Allan Hayes
hay at haystack.demon.co.uk

```

• Prev by Date: Re: Maximum Likelihood Estimation
• Next by Date: Convert Products to Lists
• Previous by thread: Books on Mma graphics
• Next by thread: Pretty Graphics of Astroid by envolope