Re: Transpose

*To*: mathgroup at smc.vnet.net*Subject*: [mg5703] Re: [mg5679] Transpose*From*: Allan Hayes <hay at haystack.demon.co.uk>*Date*: Sat, 11 Jan 1997 14:29:03 -0500*Sender*: owner-wri-mathgroup at wolfram.com

marliesb at sci.kun.nl (Marlies Brinksma) in [mg5679] Transpose writes >Now suppos matrix is a square matrix. Why gives >Transpose[matrix,{1,1}] the diagonal? Marlies: 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, ..., d4} then we need 4 positive integers a1,a2, ..., a4 to get an element : T[[a1, a2, ... , a4]] and we must have ai L di for all i. Now, Transpose[ T, {2,1,3,2}]] is a new tensor that given three positive integers a1, a2, a3 will return the value Transpose[ T, {2,1,2,3}] [[ a1,a2,a3]] = T[[a2, a1, a2, a3]] We only need three numbers because the repeated 2 uses the second one twice. So the new tensor rank is 3 and we must have a1 Ld2, a2 L d1, a2 L d3, a3 L d4 (in fact we also impose the condition d1 = d3); its dimensions are {d2, d1,d4} Similarly Transpose[ T, {2,1,1,2}] [[ a1,a2]] = T[[a2, a1, a1, a2]] the transposed vector has rank 2 and dimensions {d2, d1} (and we impose the conditiions d1 = d4 and d2 = d3 ) Check T = Array[SequenceForm,{2,4,4,2}]; Dimensions[T] {2, 4, 4, 2} Transpose[T,{2,1,1,2}] {{1111, 2112}, {1221, 2222}, {1331, 2332}, {1441, 2442}} Dimensions[%] {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 always to 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 version, 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 L 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. 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 Diagonalization 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} Transpose[{{a,A},{b,B},{c,C}}, {1,1}] Transpose::perm: {1, 1} is not a permutation. Transpose[{{a, A}, {b, B}, {c, C}}, {1, 1}] The message really means that the condition 2. is not met. Allan Hayes hay at haystack.demom.co.uk http://www.haystack.demon.co.uk