From: Bob H. <bh...@co...> - 2011-07-29 04:23:43
|
On 7/28/2011 6:50 PM, Bob Hood wrote: > On 7/27/2011 7:03 PM, Bob Hood wrote: >> On 7/26/2011 2:33 PM, Stefan Zager wrote: >>> The problem is that you haven't defined indexing operators for type double[]. >> >> Hi, Stefan. Thanks for the reply. >> >> Hmm, this isn't very clear to me. I thought SWIG generated that sort of thing >> automatically when it found a <type>[<size>] declaration. Is there some >> example to which you can point me in the SWIG distribution (or documentation) >> that illustrates this? Or, if not, could you perhaps give me a brief example? >> >> I really appreciate your time. > > I have addressed this (I guess) with an "out" typemap, for example: > > %typemap(out) double [ANY] { > $result = PyList_New($1_dim0); > for(int i = 0; i < $1_dim0; i++) > { > PyObject *o = PyFloat_FromDouble((double)$1[i]); > PyList_SetItem($result, i, o); > } > } > > But this seems like a really inefficient approach. It means that Python code > like this: > > for i in range(3): > ms.a1[i] = ms.a1[i] * (i * i) > > is going to be copying and releasing whole lists at a time (assuming it even > works to begin with)... Confirmed: It doesn't. The array has to be "pulled out" first, and then "put back in" afterward to actually work: values = ms.a1 for i in range(3): values[i] = values[i] * (i * i) ms.a1 = values This is awkward. I need some help here, please. |