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[[0]] 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[1]:= $PreRead = StringReplace[#, "[[" -> "[[TAG,"] & In[2]:= TAG/: Part[e_, TAG, n_] := Part[e, n+1] In[3]:= f = {a, b, c, d} In[4]:= f[[0]] Out[4]= a In[5]:= f[[3]] Out[5]= d In[6]:= Part[f, 1] Out[6]= a It works, but it looks like a kludge tower.... Dave Withoff withoff at wri.com