Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2633 > unrolled thread

Re: Part isn't recursive?

Started byBill Rowe <readnews@sbcglobal.net>
First post2011-05-23 10:25 +0000
Last post2011-05-25 09:57 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Part isn't recursive? Bill Rowe <readnews@sbcglobal.net> - 2011-05-23 10:25 +0000
    Re: Part isn't recursive? "p.ramsden" <p.ramsden@imperial.ac.uk> - 2011-05-24 09:59 +0000
      Re: Part isn't recursive? BernieTheJet <berniethejet@gmail.com> - 2011-05-25 09:57 +0000

#2633 — Re: Part isn't recursive?

FromBill Rowe <readnews@sbcglobal.net>
Date2011-05-23 10:25 +0000
SubjectRe: Part isn't recursive?
Message-ID<irdcnk$3o5$1@smc.vnet.net>
On 5/22/11 at 6:55 AM, berniethejet@gmail.com (BernieTheJet) wrote:

>I was a little surprised, after all these years, to discover that
>Part doesn't automatically recurse over lists of lists of
>indexes. Doesn't that seem like an obvious ability?

>So, for example,

>X = Table[i + j, {i, 3}, {j, 6}] {{2, 3, 4, 5, 6, 7}, {3, 4, 5, 6,
>7, 8}, {4, 5, 6, 7, 8, 9}}

>X[[All, {1, 2, 3}]] {{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}

>X[[All, {4, 5, 6}]] {{5, 6, 7}, {6, 7, 8}, {7, 8, 9}}

>X[[All, {{1, 2, 3}, {4, 5, 6}}]] Part::pspec: Part specification
>{{1,2,3},{4,5,6}} is neither an integer nor a list of integers. >>

>Clearly, what I had hoped for was: {{{2, 3, 4}, {3, 4, 5}, {4, 5,
>6}}, {{5, 6, 7}, {6, 7, 8}, {7, 8, 9}}}

Easily done as:

In[19]:= x[[All, #]] & /@ {{1, 2, 3}, {4, 5, 6}}

Out[19]= {{{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}, {{5, 6, 7}, {6, 7, 8},
      {7, 8, 9}}}

>I mean now I have to resort to an inelegant programming form that I
>thought I had left behind with Mathematica:

>Table[X[[All, i]], {i, {{1, 2, 3}, {4, 5, 6}}}]

Certainly that works too. But it clearly isn't the only solution.

As to why Part doesn't work the way you would like, all I can
say is that isn't the way Part was designed. It seems to me
there is little point in asking why Mathematica doesn't work
differently. It is far more useful to understand how Mathematica
works and can be used to achieve what you want.

[toc] | [next] | [standalone]


#2678

From"p.ramsden" <p.ramsden@imperial.ac.uk>
Date2011-05-24 09:59 +0000
Message-ID<irfvih$bv0$1@smc.vnet.net>
In reply to#2633
I suspect there's no easy way to set the attribute Listable over only
some arguments, and that this goes quite deep into Mathematica's
design. (For Part to work as you'd like, you in effect need it be
Listable over all arguments except the first.)

Could be wrong though.

I don't think it's a huge problem; this sort of thing is why we have
Map...

[toc] | [prev] | [next] | [standalone]


#2697

FromBernieTheJet <berniethejet@gmail.com>
Date2011-05-25 09:57 +0000
Message-ID<irijpf$qes$1@smc.vnet.net>
In reply to#2678
On May 24, 5:59 am, "p.ramsden" <p.rams...@imperial.ac.uk> wrote:
> I suspect there's no easy way to set the attribute Listable over only
> some arguments, and that this goes quite deep into Mathematica's
> design. (For Part to work as you'd like, you in effect need it be
> Listable over all arguments except the first.)
>
> Could be wrong though.
>
> I don't think it's a huge problem; this sort of thing is why we have
> Map...

I am not sure I understand what you mean by 'Listable over all
arguments'.  I am pretty sure this is not what I am thinking should be
the default behaviour.  Map and Table here for my example achieve the
same result, but what I am talking about is the recursiveness.  This
example I gave works for Table and Map only because it just happens to
have only one more level.  But if I create a further level in X, i.e.:

X = Table[i + j + k, {i, 3}, {j, 6}, {k,9}]

then I would also like Part to automatically handle

X[[All, { {{1, 2, 3}, {4, 5, 6}}, {{1, 2, 3}, {4, 5, 6}}}]]

Right?

For Map to work you'd have to, again, role it by hand with two levels
of Map, I believe.

What would be required, in effect, is to have something like a Mapped
Nest:

MapNest[f, {{a,b},{c,d}}] -> f[{f[a],f[b]},{f[c], f[d]}]

Is that right?

I thought Inner might work, i.e.

Inner[Part[#1, #2] &, X, {{1, 2, 3}, {4, 5, 6}}, Part[#1, #2] &]

but to no effect.

Anyway, the point is the recursiveness.

As for Bill's point: I understand how Mathematica works, and I have already
achieved what I want.  Now I am on to the next step of telling Wolfram
what I would like them to do to improve Mathematica.

B

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web