Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6197 > unrolled thread
| Started by | "Scot T. Martin" <smartin@seas.harvard.edu> |
|---|---|
| First post | 2011-11-05 09:51 +0000 |
| Last post | 2011-11-06 11:03 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Part // interesting undocumented feature "Scot T. Martin" <smartin@seas.harvard.edu> - 2011-11-05 09:51 +0000
Re: Part // interesting undocumented feature David Bailey <dave@removedbailey.co.uk> - 2011-11-06 11:03 +0000
| From | "Scot T. Martin" <smartin@seas.harvard.edu> |
|---|---|
| Date | 2011-11-05 09:51 +0000 |
| Subject | Part // interesting undocumented feature |
| Message-ID | <j930uu$a9g$1@smc.vnet.net> |
A colleague new to Mathematica showed me something that I thought should cause an error: Part[data[[All,1]]] Of course, I thought the form should just be "data[[All,1]". It turns out that there is form of Part that works like an identity function: In[1]:= Part[5] Out[1]= 5 Just out of a Mathematica trivia challenge question, can anyone think of an example when this undocumented syntax of Part[ ] could possibly be of use or why the programmers have it as an acceptable syntax?
[toc] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2011-11-06 11:03 +0000 |
| Message-ID | <j95phv$m8d$1@smc.vnet.net> |
| In reply to | #6197 |
On 05/11/2011 09:51, Scot T. Martin wrote:
> A colleague new to Mathematica showed me something that I thought should cause an error:
>
> Part[data[[All,1]]]
>
> Of course, I thought the form should just be "data[[All,1]".
>
> It turns out that there is form of Part that works like an identity function:
>
> In[1]:= Part[5]
> Out[1]= 5
>
> Just out of a Mathematica trivia challenge question, can anyone think of an example when this undocumented syntax of Part[ ] could possibly be of use or why the programmers have it as an acceptable syntax?
>
>
>
I was not aware of this, but it makes sense.
It is the limiting case of the construction Part[expr,i,j,k....] - the
case where no part specifiers are supplied.
Usually this sort of feature is included to avoid having to perform
special tests for end conditions. For example:
In[673]:= pp={1,2};
expr={{a,b},{c,d}};
Part[expr,Sequence@@pp]
Out[675]= b
In[677]:= pp={1};
In[678]:= Part[expr,Sequence@@pp]
Out[678]= {a, b}
In[680]:= pp={};
In[681]:= Part[expr,Sequence@@pp]
Out[681]= {{a, b}, {c, d}}
Thus the case pp={} does not need to be handled as a special case.
David Bailey
http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web