Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Part isn't recursive?

13 views
Skip to first unread message

BernieTheJet

unread,
May 22, 2011, 6:56:39 AM5/22/11
to
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}}}

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}}}]


So why wouldn't this be the built-in behavior? I am thinking that
there is some ambiguity if one allows this, but I can't think of any.

Doesn't this seem like an obvious extension to Part? It seems to me
that most other List operations automatically recurse, but now that I
think about it I realize that there aren't really any examples I can
find of recursive operations over an input list, but there are
examples of functions that provide arbitrarily nested outputs based on
a list of arbitrary length, e.g. Outer.

Anyway, I would like it if it worked this way!

B

Bill Rowe

unread,
May 23, 2011, 6:25:56 AM5/23/11
to
On 5/22/11 at 6:55 AM, bernie...@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.


p.ramsden

unread,
May 24, 2011, 5:59:45 AM5/24/11
to
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...

BernieTheJet

unread,
May 25, 2011, 5:57:03 AM5/25/11
to

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


0 new messages