[C++-sig] Retrieving lvalues using custom converters (numpy 1-D converters attached)
Ravi
lists_ravi at lavabit.com
Thu Oct 2 15:34:29 CEST 2008
On Thursday 02 October 2008 07:41:04 Neal Becker wrote:
> This is very interesting! But how do I use it?
>
> What I tried:
> your numpy.h (but comment out)
> #ifndef IN_FILE_IMPORTING_ARRAY
> #define NO_IMPORT_ARRAY
> #endif
> #define PY_ARRAY_UNIQUE_SYMBOL Wicket_Numpy
Unless you removed the above for simplicity in you trivial module, you should
generally have something like the above if you plan to include numpy.h from
multiple source files for a single extension module; please see the numpy book
for details.
> My code:
> ,----[ /home/nbecker/numpy.new/num.cc ]
>
> | #include "numpy.h"
> | #include <boost/python/module.hpp>
> | #include <boost/python/def.hpp>
> | #include <numeric>
> |
> | namespace ublas=boost::numeric::ublas;
> | using namespace boost::python;
> |
> | double do_sum (ublas::vector<double> const& u) {
> | return std::accumulate (u.begin(), u.end(), 0.0);
> | }
Replace the above with:
double do_sum( numpy::array_from_py<double>::type u ) { ... }
The reason is that arrays coming *from* numpy are converted to this type which
is actually ublas::vector< double, numpy_storage_array<double> >. The
allocator (numpy_storage_array<double>) is what converts storage in a
PyArrayObject to something that ublas can use. I expect that the argument type
above will be used only for functions dealing with values from python. I have
attached an example of using numpy.h. Note that, in dump_vec(), a "regular"
ublas vector is subtracted from the numpy vector with the results visible in
python.
Also, FYI, since you pass by const reference, you cannot modify the contents
of 'u', but if you pass by value, you can. Passing by value costs you the two
pointers instead of one but nothing more since the underlying storage is
reference counted.
> |
> | BOOST_PYTHON_MODULE (num) {
> | import_array();
> | numpy::register_ublas_from_python_converters();
> | numpy::register_default_ublas_to_python();
> | def ("do_sum", &do_sum);
> | }
>
> `----
> ,----[ /home/nbecker/numpy.new/test_num.py ]
>
> | from num import *
> | from numpy import *
> |
> | u = array (xrange(10), dtype=float64)
> | v = do_sum (u)
>
> `----
Regards,
Ravi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: decco.cc
Type: text/x-c++src
Size: 1821 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081002/98d86561/attachment.cc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: decco_example.py
Type: text/x-python
Size: 331 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081002/98d86561/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: reference_existing_object_1.hpp
Type: text/x-c++hdr
Size: 1104 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20081002/98d86561/attachment.hpp>
More information about the Cplusplus-sig
mailing list