[SciPy-user] numpy array in ctype struct
Andrew Straw
strawman@astraw....
Mon Jan 21 02:10:41 CST 2008
Hi Rob,
I am far from being a weave expert, having used it once in my life, but
I think you may find some macros of interest if you examine the output C
file. Examining these files was how I derived the following function:
def increment_fast(final_array_shape, idxa,idxb,idxc):
counts = numpy.zeros(final_array_shape, dtype=numpy.uint64)
assert len(idxa.shape)==1
assert len(idxa)==len(idxb)
assert len(idxa)==len(idxc)
code = r"""
for (int i=0; i<Nidxa[0]; i++) {
COUNTS3( IDXA1(i), IDXB1(i), IDXC1(i) )++;
}
"""
weave.inline( code, ['counts', 'idxa','idxb','idxc'])
return counts
Note that "COUNTS3" is automatically defined from the 'counts' array and
allows indexing more-or-less as if it's a rank-3 ndarray.
Rob Hetland wrote:
> On Jan 20, 2008, at 10:01 AM, Matthieu Brucher wrote:
>
>
>> This function signature suppose that you will access an element by
>> data[i][j] which is not the way Numpy works.
>>
>
> This seems to be the way the example attached by Paul Kienzle works,
> albeit for fixed size arrays. Would there be a way to dynamically
> set the size for the fixed array, based on other input integers?
>
>
>
>> You can create a wrapper function that will allocate a double** 1D
>> array pointing to the adequate *double (start of a line), pass it
>> to your function and then deallocate the array when returning from
>> the function.
>>
>
>
> Can you show an example? I tried to do something similar, but failed
> (due to poor coding skills..). Also, is it better to create the
> needed list of pointers on the python side, or the C side?
>
> I've had this sort of problem a few times, and I can't seem to find a
> general solution anywhere. Any advice would help,
>
> -Rob
>
> ----
> Rob Hetland, Associate Professor
> Dept. of Oceanography, Texas A&M University
> http://pong.tamu.edu/~rob
> phone: 979-458-0096, fax: 979-845-6331
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-user
mailing list