[Date Index]
[Thread Index]
[Author Index]
Re: Why does Transpose[m, {1, 1}] give the diagonal of m?
*To*: mathgroup at christensen.cybernetics.net
*Subject*: [mg1888] Re: [mg1859] Why does Transpose[m, {1, 1}] give the diagonal of m?
*From*: Richard Mercer <richard at seuss.math.wright.edu>
*Date*: Sat, 12 Aug 1995 22:51:22 -0400
David Cabana <drc at gate.net> in [mg1859] Why does Transpose[m, {1,
1}] give the diagonal of m?
writes
> In a recent article in the thread Re: [mg1741] RealDigits, Roman
> Maeder explains one way to test whether a matrix is diagonal:
>>even faster and certainly simpler:
>>dQ[m_?MatrixQ] /; SameQ @@ Dimensions[m] :=
>> m == DiagonalMatrix[Transpose[m, {1, 1}]]
>>(note: Transpose[m, {1, 1}] returns the list of diagonal elements.)
>
>An elegant test. My question: why does Transpose[m, {1, 1}] return
>the list of diagonal elements?
David,
Below is a slightly edited version of my previous posting,
[mg1131], in which I tried to understand Transpose.
Allan Hayes
De Montfort University Leicester
hay at haystack.demon.co.uk
********
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 transposed 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.
Here is my simulation of Transpose, Trans.
(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
Prev by Date:
**Win95 and MMA**
Next by Date:
**Comparison of MMA on Various Machines**
Previous by thread:
**Why does Transpose[m, {1, 1}] give the diagonal of m?**
Next by thread:
**Re: Why does Transpose[m, {1, 1}] give the diagonal of m?**
| |