       Re: Re-indexing Mathematica

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: Re-indexing Mathematica
• From: David Withoff <withoff>
• Date: Mon, 20 Jul 1992 10:14:03 -0500

```> Mathgroup,
>
> I've just begun trying to convert several weeks' worth of ideas into
> Mathematica code, and I found a small problem.  I'm working with points in R^k
> for several different values of k, and (don't ask me why) I did all my indexing
> from 0.  That is, a point in R^k is (x_0, ..., x_{k-1}).  And I just realized
> that Mathematica doesn't quite like this.  You see, I'm representing points as
> lists.  But x[] is "List", not the x_0 that I want.  I would REALLY like to
> index from 0 in Mathematica, because all these subscripts are buried in all
> kinds of summations and sequences and such, and I don't want to have to go
> through and re-index everything.  I was thinking I could Unprotect[Part] and
> muck about with it, but I'm not sure how to do that without getting into
> infinite recursion stuff (Part[i_] := Part[i+1] just doesn't make MMA very
> happy).
>
> I'm using Mathematica v. 2.0 for SPARC.  Thanks for any input you can offer.
>
>         David McMath
>         mcdave at rice.edu

Redefining something as basic as Part is likely to break lots of
internal algorithms.  It's probably safer to somehow tag the Part
expressions that you want to change, either with an editor or with
\$PreRead, then attach rules to the tag.  For example:

In:= \$PreRead = StringReplace[#, "[[" -> "[[TAG,"] &

In:= TAG/: Part[e_, TAG, n_] := Part[e, n+1]

In:= f = {a, b, c, d}

In:= f[]

Out= a

In:= f[]

Out= d

In:= Part[f, 1]

Out= a

It works, but it looks like a kludge tower....

Dave Withoff
withoff at wri.com

```