Alternative zero-based subscripting code

• To: MathGroup at yoda.physics.unc.edu
• Subject: Alternative zero-based subscripting code
• From: villegas
• Date: Thu, 30 Jul 92 19:43:37 CDT

```Here is a slightly shorter version of the zero-based subscripting function
which takes advantage of the ability to add a number to all elements of a
list, and the fact that pure functions can be used with Apply as well as with
Map:

ZPart[obj_,partlist___Integer?NonNegative] :=
Apply[Part[obj, ##]&, 1 + {partlist} ]

The construct "##" in the pure function stands for the whole sequence of
arguments fed to the function.  If {partlist} is {0, 3, 0, 2}, then
1 + {partlist} --> {1, 4, 1, 3}.  So the Apply of the pure function ends up
constructing the command Part[obj, 1, 4, 1, 3].

The restriction in the pattern to nonnegative integers is to prevent this from
being used on part-specifications involving negative coordinates, which
indicate positions measured from the end instead of the beginning.  If you
wanted to enable negative coordinates, you could make it:

ZPart[obj_,partlist___Integer] :=
Apply[Part[obj, ##]&, Map[If[NonNegative[#], # + 1, #]&, {partlist}] ]

The If will avoid adding 1 to negative coordinates, so you can mix the desired
zero-based indices with the existing convention for negative indices.

Robby Villegas

P.S.  An interesting, purely mathematical way to do what the Map of the If
does, without any programming needed, is:

{partlist} + Sign[1 + Sign[{partlist}] ]

```

• Prev by Date: ListPlot3D type plotting
• Next by Date: Re: FontForm
• Previous by thread: ListPlot3D type plotting
• Next by thread: Re: Derivative