RE: [Numpydiscussion] oneoffset arrays
Indices in Fortran can be defined at the time an array is declared, so Fortran is not necessarily "oneindexed".
Kevin Rodgers Northrop Grumman Ryan Aeronautical
Original Message From: Eric Nodwell [mailto:nodwell@physics.ubc.ca] Sent: Tuesday, August 28, 2001 10:01 PM To: Numpydiscussion@lists.sourceforge.net Subject: Re: [Numpydiscussion] oneoffset arrays
Does anyone have a simple method of using oneoffset arrays? Specifically, can I define an array "a" so that a[1] refers to the first element?
Inherit from the UserArray and redefine slicing to your hearts content.
Without oneoffset indexing, it seems to me that Python is minimally useful for numerical computations. Many, perhaps the majority, of numerical algorithms are oneindexed. Matlab for example is onebased for this reason. In fact it seems strange to me that a "highlevel" language like Python should use zerooffset lists.
Wow, that is a pretty condescendingsounding statement, though I'm sure
you
didn't mean it that way. Python is indeed being used for quite serious numerical computations. I have been using Python for quite some time for Numerical work and find it's zerobased indexing combined with the leavelastoneout slicing notation to be much more convenient.
Oops, what I really need is a wetware (i.e. brain) macro which enforces the correct order of the pair (think,write,send)! The above unconsidered comments arose from the sequence (frustration,write,send,think,regret). ;)
Obviously there is a lot of numerical work being done with python and people are very happy with it. But for me I still think it would be "minimally useful" without 1indexed arrays. Here's why:
In my view, the most important reason to prefer 1based indexing versus 0based indexing is compatibility. For numerical work, some of the languages which I use or have used are Matlab, Mathematica, Maple and Fortran. These are all 1indexed. (C is by nature 0indexed because it is so close to machine architecture, but with a little bit of notentirelyclean pointer manipulation, you can easily make 1indexed arrays and matrices.) Obviously Python can't be compatible with these languages in a strict sense, but like most people who do some programming work, I've built up a library of my own commonly used routines specific to my work; in general it's a trivial matter to translate numerical routines from one language to the another if translation is just a matter of substituting of one set of syntactical symbols and function names for anther. However it can be damn tricky to convert 1indexed code to 0indexed code or visa versa without introducing any errors believe me! (Yes it's possible to call nearly any language from nearly any other language these days so in theory you don't have to recode, but there are lots of reasons why often recoding is the preferable route.)
The other reason for choosing 1based indexing is to keep the code as near to the standard notation as possible. This is one of the advantages of using a high level language  it approximates the way you think about things instead of the way the computer organizes them. Of course, this can go either way depending on the quantity in question: as a simple example a spatial vector (x,y,z) is conventionally labelled 1,2,3 (1indexed), but a relativistic fourvector with time included (t,x,y,z) is conventionally labelled 0,1,2,3 (0indexed). So ideally one would be able to choose the indexingtype casebycase. I'm sure that computer programmers will argue vehemently that code which mixes both 0indexed and 1indexed arrays is hard to understand and maintain, but for experts in a particular field who are accustomed to certain ingrained notations, it is the code which breaks the conventional notation which is most errorprone. In my case, I'm dealing at the moment with crystal structures with which are associated certain conventional sets of vectors and tensors  all 1indexed by convention. I find it a delicate enough task to always get the correct vector or tensor without having to remember that d[2] is actually d3. Defining d1,d2,d3 is not convenient because often the index itself needs to be calculated.
I guess if I understood the reason for 0indexed lists and tuples in Python I would be happier. In normal, everyday usage, sets, collections and lists are 1indexed (the first item, the second item, the third item, and so on). Python is otherwise such an elegant and natural language. Why the ugly exception of making the user conform to the underlying mechanism of an array being an address plus an offset?
All this is really neither here nor there, since this debate, at least as far as Python is concerned, was probably settled 10 years ago and I'm sure nobody wants to hear anything more about it at this point. As you point out, I can define my own array type with inheritance. I will also need my own range command and several other functions which haven't occured to me yet. I was hoping that there would be a standard module to implement this.
By the way, what is leavelastoneout slicing? Is it a[:1] or is it a[0,...] or is it something else?
Eric
_______________________________________________ Numpydiscussion mailing list Numpydiscussion@lists.sourceforge.net http://lists.sourceforge.net/lists/listinfo/numpydiscussion
participants (1)

Rodgers, Kevin