Part isn't recursive?
- To: mathgroup at smc.vnet.net
- Subject: [mg119118] Part isn't recursive?
- From: BernieTheJet <berniethejet at gmail.com>
- Date: Sun, 22 May 2011 06:55:30 -0400 (EDT)
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