Re: Why does Transpose[m, {1, 1}] give the diagonal of m?
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1886] 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:00 -0400
> An elegant test. My question: why does Transpose[m, {1, > 1}] return the list of diagonal elements? Looking under > Transpose in Wolfram's Mathematica Reference Guide (an > appendix to Mathematica, 2nd ed.), I find: > Transpose[list, {n1,n2, ...}] transposes list so that the kth level > in list is the nkth level in the result. > I expected Depth[Transpose[list, {n1,n2, ...}]] to be > the same as Depth[list]. I expected Transpose to swap > levels; I did not expect it to throw any away. Evidently > I am laboring under some misconception. Can someone > explain what is going on? I think so, at the price of a certain amount of abstraction and background discussion. Consider the matrix A = {{16,27},{38,49}}, or A = [ 16 27 ] [ 38 49 ] Then C = Transpose[A,{1,1}] gives {16,49} Why? *Short Version* Since {1,1} is not a permutation, Transpose[list,{1,1}] is not defined by the Reference Guide. The effect of using {1,1} is to say the two entries must be the same, so C[[1]] = A[[1,1]] = 16, and C[[2]] = A[[2,2]] = 49. The effect is to compress a two-dimensional list into a one-dimensional list. If you believe that, I have some Netscape shares I'd like to sell you! So you'd better read the... *Long Version* (0) The discussion below works just as well for non-square matrices and their higher-dimensional analogues, but it's simplest to consider a 2x2 matrix. (1) The structured list objects on which Transpose operates can be viewed as functions on certain integer lattices. Sounds fancy, but an example shows it's really a simple idea. The matrix A can be thought of as a function of two indices with the following values: A(1,1) = 16 A(1,2) = 27 A(2,1) = 38 A(2,2) = 49 (In Mathematica you use double brackets instead of parentheses.) The domain of the function A is therefore the set of ordered pairs D = {(1,1), (1,2), (2,1), (2,2)}. (set notation, not Mathematica notation!) D is the same as the set (cartesian) product {1,2} X {1,2}. (2) The ordinary transpose operation can be viewed as the composition of the function A and the map t defined from D to D as follows: t(1,1) = (1,1) t(1,2) = (2,1) t(2,1) = (1,2) t(2,2) = (2,2) So if B = Transpose[A], then B(1,1) = A(t(1,1)) = A(1,1) = 16 B(1,2) = A(t(1,2)) = A(2,1) = 38 B(2,1) = B(t(2,1)) = A(1,2) = 27 B(2,2) = B(t(2,2)) = A(2,2) = 49 Therefore B = {{16,38},{27,49}} in Mathematica notation, or B = [ 16 38 ] [ 27 49 ] as expected. (3) The key is to understand the map t and how it can be generalized to other situations. The map t is defined by a "permutation" or reordering applied to the elements of the domain D. In this case the reordering is described by {2,1} because -- the first entry of the result is the second(2) entry of the input, -- the second entry of the result is the first(1) entry of the input, i.e. t(a,b) = (b,a). Therefore Transpose[A,{2,1}] means the same as Transpose[A], and Transpose[A,{1,2}] = A, because the permutation {1,2} means no reordering at all. (4) If we now extend this discussion to the non-permutation {1,1}, it should define a map s by -- the first entry of the result is the first(1) entry of the input, -- the second entry of the result is the first(1) entry of the input, i.e. s(a) = (a,a). Why not s(a,b) = (a,a)? Since only the first entry of the input is used, we assume the input has only one entry. s maps from {1,2} into {1,2} X {1,2} = D. (5) By analogy, C = Transpose[A,{1,1}] should now represent the composition of the function A with the map s. The result is a function on {1,2}, with values C(1) = A(s(1)) = A(1,1) = 16 C(2) = A(s(2)) = A(2,2) = 49 This discussion could be continued to cases of higher dimensional lists, but I'm amazed if you've read this far! Exercise for the reader: If you have a three dimension list, e.g. A = {{{16,27},{38,49}},{{91,82},{73,64}}} what should be the result of Transpose[A,{1,1,2}] ? Explain why Mathematica accepts the non-permutation {1,1,2} but not {1,1,3} in its place. Richard Mercer