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


Groups > comp.soft-sys.math.mathematica > #2596

Part isn't recursive?

From BernieTheJet <berniethejet@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Part isn't recursive?
Date 2011-05-22 10:56 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iraq57$lpp$1@smc.vnet.net> (permalink)

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Part isn't recursive? BernieTheJet <berniethejet@gmail.com> - 2011-05-22 10:56 +0000

csiph-web