<div dir="ltr">On Mon, Feb 10, 2014 at 10:43 PM, Christopher Jordan-Squire <span dir="ltr"><<a href="mailto:cjordan1@uw.edu" target="_blank">cjordan1@uw.edu</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">I'm trying to wrap some C code using cython. The C code can take<br>


inputs in two modes: dense inputs and sparse inputs. For dense inputs<br>
the array indexing is naive. I have wrappers for that. In the sparse<br>
case the matrix entries are typically indexed via names. So, for<br>
example, the library documentation includes this as input you could<br>
give:<br>
<br>
struct<br>
{<br>
 char* ind;<br>
 double val, wght;<br>
} data[] = { {"camera", 15, 2}, {"necklace", 100, 20}, {"vase", 90, 20},<br>
                 {"pictures", 60, 30}, {"tv", 40, 40}, {"video", 15, 30}};<br>
<br>
At the C level, data is passed to the function by directly giving its<br>
address. (i.e. the C function takes as an argument (unsigned long)<br>
data, casting the data pointer to an int)<br></blockquote><div><br></div><div style>wow -- that's prone to error! but I"m still not sure which pointer you're talking about -- a pointer to this struct?</div><div>

<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
I'd like to create something similar using record arrays, such as<br>
<br>
np.array([("camera", 15, 2), ("necklace", 100, 20), ... ],<br>
dtype='object,f8,f8').<br></blockquote><div><br></div><div style> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


Unfortunately this fails because<br>
(1) In cython I need to determine the address of the first element and<br>
I can't take the address of a an input whose type I don't know (the<br>
exact type will vary on the application, so more or fewer fields may<br>
be in the C struct)<br>
</blockquote><div> </div><div style>still a bit confused, but if this is types as an array in Cython, you should be abel to do somethign like:</div><div style><br></div><div style>&the_array[i]</div><div style><br></div>

<div style>to get the address of the ith element.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

(2) I don't think a python object type is what I want--I need a char*<br>
representation of the string. (Unfortunately I can't test this because<br>
I haven't solved (1) -- how do you pass a record array around in<br>
cython and/or take its address?)<br></blockquote><div><br></div><div style>well, and object type will give you a pointer to a pyobject. If you know for sure that that pyobject is a string object (probably want a bytes object -- you son't want unicode here), then you should be abel to get the address of the underlying char array. But that would require passing something different off to the C code that the address of that element.</div>

<div style><br></div><div style>You could use an unsigned long for that first field, as you are assuming that in the C code anyway  but I don't hink there is a way in numpy to set that to a pointer to a char allocated elsewhere -- where would it be allocated?</div>

<div style><br></div><div style>So I would give up on expecting to store the struct directly in numpy array, and rather, put something reasonable (maybe what you have above) in the numpy array, and build the C struct you need from that rather than passing a pointer in directly.</div>

<div style><br></div><div style>-Chris</div><div style><br></div><div style><br></div></div><div><br></div>-- <br><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>

7600 Sand Point Way NE   (206) 526-6329   fax<br>Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a>
</div></div>