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?
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.
On Tue, 28 Aug 2001, Eric Nodwell wrote:
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.
It is different from MATLAB but that hardly makes it less useful as I suspect you'd agree after trying it out for awhile.
What is your application domain that so requires 1based indexing.
Travis E. Oliphant, Ph.D. Assistant Professor Brigham Young University Provo, UT 84602 (801) 3783108 oliphant.travis@ieee.org
Hi all,
I wrote last week looking for hints for writing a new clip function, that would be faster than the existing one. I got some suggestions, and have written the function. It works, but I'd love to get a little code review from the more experienced folks here. I have a few specific questions:
A) Are there any tricks to making a function work with multiple types? I currently have it working with only double arrays, but it would be great for it ot be universal (and it could then be added to the main NumPy distribution, I suppose) I seem tohave to typecast the data to do the comparison I want, so I can't figure out how to make it general. For example, I have this line:
if (*(double *)(Array>data + i*elementsize) > *(double *)(Max>data + i*elementsize)) { *(double *)(Array>data + i*elementsize) = *(double *)(Max>data + i*elementsize) ; }
How could I do that without the explicit typecasts? I'm pretty sure I could make the rest of the routine general.
B) I was trying to figure out how to loop through all the elements of a noncontiguous array. I got two hints: "Rob W. W. Hooft" wrote:
Never done this in C for numpy yet, but I normally program this along the following lines:
n=[2,3,2,3] x=[0,0,0,0] while 1: print x i=len(n)1 while i>=0 and x[i]+1==n[i]: x[i]=0 i=i1 if i<0: break x[i]=x[i]+1
It is always going to be slower than the straight loop for contiguous arrays.
This is what I did, and it is a LOT slower (about a factor of ten). See the enclosed C code. There has got to be a way to optimize this!
So if you want to do it properly, you'd have to check whether the array is contiguous, and if it is, use the fast way.
Since I had already written code for the contiguous case, it was easy to special case it.
Pete Shinners wrote:
it's easiest to use recursion than nested loops. then you can support any number of dimensions. that should point you in the right direction :]
When I read this, I thought: "well, duh!", but then I couldn't figure out how to do it. Would there be any advantage over the above loops anyway?
Is there either a more elgant or faster way to loop through all the elements of these three arrays?
C) Where is the DL_EXPORT macro defined? I havn't been able to find it with grep yet. Does it work on all NumPy supported platforms?
D) Is there an exising function or Macro for checking if two arrays are the same shape? I wrote one, but I imagine it exists.
F) I needed to work with input that could be any Python number, or an Array of the right size. What I did was call: PyArray_FromObject on the min and max inputs, so that I would get a PyArray, then I could check if it was a ranko array, or the right size. I havn't been able to decide if this is a nifty trcik, or a kludge. Any other suggestions?
E) Have I got reference counting right? I tried to Py_DECREF all my temp arrays, but I'm not sure I did it right.
Sorry to enclose the code if you have no interest in it, but it's not really that big.
Chris
On Wed, 29 Aug 2001, Chris Barker wrote:
A) Are there any tricks to making a function work with multiple types? I currently have it working with only double arrays, but it would be great for it ot be universal (and it could then be added to the main NumPy distribution, I suppose) I seem tohave to typecast the data to do the comparison I want, so I can't figure out how to make it general. For example, I have this line:
if (*(double *)(Array>data + i*elementsize) > *(double *)(Max>data + i*elementsize)) { *(double *)(Array>data + i*elementsize) = *(double *)(Max>data + i*elementsize) ; }
How could I do that without the explicit typecasts? I'm pretty sure I could make the rest of the routine general.
Some time ago, I used a union like: typedef union { short *sdata; int *idata; long *ldata; float *fdata; double *ddata; /* And so on */ } MyNumPyUnion;
Then, you can assign (and CAST) the data field of the NumPy array according to the typecode of the array, like in:
MyNumPyUnion theunion;
theunion.fdata=(float*) thenumpyarray>data; /* If it is a Float32 */
Finally, you use sdata, idata, ldata, fdsata and so on to iterate dereferencing accordingly, depending on the typecode of the array.
It is a nightmare to write this code, but I can not imagine of any other approach. May be other developers can help you with a better approach.
Hope this helps.
Jon Saenz.  Tfno: +34 946012470 Depto. Fisica Aplicada II  Fax: +34 944648500 Facultad de Ciencias. \ Universidad del Pais Vasco \ Apdo. 644 \ 48080  Bilbao \ SPAIN
participants (4)

Chris Barker

Eric Nodwell

Jon Saenz

Travis Oliphant