[Numpy-discussion] C API questions and tricks.
Rob W. W. Hooft
rob at hooft.net
Fri Aug 24 01:49:40 CDT 2001
>>>>> "CB" == Chris Barker <chrishbarker at home.net> writes:
I wrote my own specialized clip function too, only for 'f' and 'i'
types, and only for contiguous arrays....
CB> A) How do I loop through all the elements of a discontiguous
CB> array of arbitraty dimensions? If I knew the rank ahead of time,
CB> I could just nest some for loops and use the strides[]
CB> values. Not knowing before hand, it seems that I should be able
CB> to do some nesting of loops using nd and dimensions[], but I
CB> can't seem to work it out. Someone must have come up with a nifty
CB> way to do this. Is there an existing macro or function to do it?
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=i-1
if i<0:
break
x[i]=x[i]+1
It is always going to be slower than the straight loop for contiguous
arrays. 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.
Regards,
Rob Hooft
--
===== rob at hooft.net http://www.hooft.net/people/rob/ =====
===== R&D, Bruker Nonius BV, Delft http://www.nonius.nl/ =====
===== PGPid 0xFA19277D ================================= Use Linux! =========
More information about the Numpy-discussion
mailing list