[C++-sig] numeric.hpp: copying raw data

David Abrahams dave at boost-consulting.com
Sat Oct 5 06:11:09 CEST 2002


----- Original Message -----
From: "Philip Austin" <paustin at eos.ubc.ca>


>
> Hi, we've been playing around with numeric.hpp and it's working well.
> I'm uncertain, however, about the best way to move data from C to
> python using numeric::array -- it looks like users with large arrays
coming
> from C need to bite the bullet and call import_libnumarray()?  I
> wanted to check to make sure we aren't missing something before we
> start implementing our own functions, like:
>
> numeric::array ptrToNum(int* data, int n=0){
>   NDInfo info;
>   object obj(detail::new_reference(NA_Empty(1, &n, tInt32)));
>   get_info(&obj, &info);
>   memcpy(info.data, data, 4*n);
>   return numeric::array(obj, "Int", n, false);
> }
>
> Regards, Phil Austin

The truth about the numeric support is this:

I discussed what to do with Paul Dubois, and he said, roughly, "what we
want is something that can be used as an argument to a wrapped function,
and which will match anything that you can construct a numarray with". I
countered with, "what about something that only matches numarrays -- you
can always use python::object if you want to match anything, and then you
can construct the numarray with that? It would be more consistent with the
other type wrappers". He said "great!" and we had a deal. We never
discussed the things we'd need to do to actually make it useful ;-)

Anyway, about the above code: is there a way to do the equivalent of

    NA_Empty(1, &n, tInt32)

from Python?

If so, you should be able to construct a numeric::array that way, right?
In that case, if you needed to touch the underlying data you could always
use its ptr() function instead of relying on implementation detail::s.

Lastly, though I could be wrong since I'm no Numeric expert, is there any
point to constructing numeric::array in the last line? I mean, don't you
already have one? It seems you could just return obj.

-Dave

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Cplusplus-sig mailing list