MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: give the diagonal of m?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128580] Re: give the diagonal of m?
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Wed, 7 Nov 2012 00:56:35 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <DD6GKI.3sG@wri.com>

There are more straightforward ways to get a diagonal

A = Array[a, {4, 4}];

Diagonal[A]

{a[1, 1], a[2, 2], a[3, 3], a[4, 4]}

List @@ Tr[A] (* works only with symbolic matrix *)

{a[1, 1], a[2, 2], a[3, 3], a[4, 4]}

Tr[A, List]

{a[1, 1], a[2, 2], a[3, 3], a[4, 4]}

Transpose[A, {1, 1}]

{a[1, 1], a[2, 2], a[3, 3], a[4, 4]}

% === %% === %%% === %%%%

True

The diagonal does not have to be the main diagonal

Diagonal[A, #] & /@ Range[-3, 3]

{{a[4, 1]}, {a[3, 1], a[4, 2]}, {a[2, 1], a[3, 2], a[4, 3]}, {a[1, 1],
  a[2, 2], a[3, 3], a[4, 4]}, {a[1, 2], a[2, 3], a[3, 4]}, {a[1, 3],
  a[2, 4]}, {a[1, 4]}}

B = Partition[Range[16], 4];

Diagonal[B]

{1, 6, 11, 16}

List @@ Tr[B] (* does not work  with numeric matrix *)

34

Tr[B, List]

{1, 6, 11, 16}

Transpose[B, {1, 1}]

{1, 6, 11, 16}

% === %% === %%% === %%%%

False

%% === %%% === %%%%%

True

Diagonal[B, #] & /@ Range[-3, 3]

{{13}, {9, 14}, {5, 10, 15}, {1, 6, 11, 16}, {2, 7, 12}, {3, 8}, {4}}


Bob Hanlon


On Mon, Nov 5, 2012 at 6:39 PM,  <marty.musatov at gmail.com> wrote:
> On Saturday, August 12, 1995 12:00:00 AM UTC-7, Richard Mercer wrote:
>> >  An elegant test.  My question: why does Transpose[m, {1,
>> >  1}] return the list of diagonal elements?
> Yes.  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?
> Yes.
>>
>> 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?
> Yes.
>>
>> *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)?
> Yes. 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}] ?
> Yes.
>> Explain why Mathematica accepts the non-permutation {1,1,2} but not {1,1,3} in
>> its place.
>>
>> Richard Mercer
> AN ... integers for all j = 1, ... , n, xj = 0, 1; and the rank of the matrix (aj) equals 2.Musatov
>



  • Prev by Date: Help with map /@
  • Next by Date: Re: Conformal Mapping
  • Previous by thread: Re: give the diagonal of m?
  • Next by thread: Help with map /@