Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65789 - in sandbox/numpy: boost/python/numpy libs/python/numpy/test
From: talljimbo_at_[hidden]
Date: 2010-10-06 15:05:32


Author: jbosch
Date: 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
New Revision: 65789
URL: http://svn.boost.org/trac/boost/changeset/65789

Log:
boost.python.numpy - moved dtype::invoke_matching_template into separate header, added similar code for invocation based on dimensionality
Added:
   sandbox/numpy/boost/python/numpy/invoke_matching.hpp
      - copied, changed from r65778, /sandbox/numpy/boost/python/numpy/ndarray.hpp
Text files modified:
   sandbox/numpy/boost/python/numpy/dtype.hpp | 68 ------
   sandbox/numpy/boost/python/numpy/invoke_matching.hpp | 395 ++++++++++++++-------------------------
   sandbox/numpy/boost/python/numpy/ndarray.hpp | 1
   sandbox/numpy/boost/python/numpy/numpy.hpp | 1
   sandbox/numpy/libs/python/numpy/test/templates.py | 12
   sandbox/numpy/libs/python/numpy/test/templates_mod.cpp | 45 +++
   6 files changed, 186 insertions(+), 336 deletions(-)

Modified: sandbox/numpy/boost/python/numpy/dtype.hpp
==============================================================================
--- sandbox/numpy/boost/python/numpy/dtype.hpp (original)
+++ sandbox/numpy/boost/python/numpy/dtype.hpp 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
@@ -46,76 +46,8 @@
 
     BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(dtype, object);
 
- template <typename Sequence, typename Function>
- void invoke_matching_template(Function f) const;
-
 };
 
-namespace detail {
-
-struct add_pointer_meta {
-
- template <typename T>
- struct apply {
- typedef typename boost::add_pointer<T>::type type;
- };
-
-};
-
-struct dtype_template_match_found {};
-
-template <typename Function>
-struct dtype_template_invoker {
-
- template <typename T>
- void operator()(T * x) const {
- if (dtype::get_builtin<T>() == m_dtype) {
- m_func.template apply<T>();
- throw dtype_template_match_found();
- }
- }
-
- dtype_template_invoker(dtype const & dtype_, Function func) :
- m_dtype(dtype_), m_func(func) {}
-
-private:
- dtype const & m_dtype;
- Function m_func;
-};
-
-template <typename Function>
-struct dtype_template_invoker< boost::reference_wrapper<Function> > {
-
- template <typename T>
- void operator()(T * x) const {
- if (dtype::get_builtin<T>() == m_dtype) {
- m_func.template apply<T>();
- throw dtype_template_match_found();
- }
- }
-
- dtype_template_invoker(dtype const & dtype_, Function & func) :
- m_dtype(dtype_), m_func(func) {}
-
-private:
- dtype const & m_dtype;
- Function & m_func;
-};
-
-} // namespace boost::python::numpy::detail
-
-template <typename Sequence, typename Function>
-void dtype::invoke_matching_template(Function f) const {
- detail::dtype_template_invoker<Function> invoker(*this, f);
- try {
- boost::mpl::for_each< Sequence, detail::add_pointer_meta >(invoker);
- } catch (detail::dtype_template_match_found &) {
- return;
- }
- PyErr_SetString(PyExc_TypeError, "numpy.dtype not found in template list.");
- throw_error_already_set();
-}
-
 } // namespace boost::python::numpy
 
 namespace converter {

Copied: sandbox/numpy/boost/python/numpy/invoke_matching.hpp (from r65778, /sandbox/numpy/boost/python/numpy/ndarray.hpp)
==============================================================================
--- /sandbox/numpy/boost/python/numpy/ndarray.hpp (original)
+++ sandbox/numpy/boost/python/numpy/invoke_matching.hpp 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
@@ -1,288 +1,175 @@
-#ifndef BOOST_PYTHON_NUMPY_NDARRAY_HPP_INCLUDED
-#define BOOST_PYTHON_NUMPY_NDARRAY_HPP_INCLUDED
+#ifndef BOOST_PYTHON_NUMPY_INVOKE_MATCHING_HPP_INCLUDED
+#define BOOST_PYTHON_NUMPY_INVOKE_MATCHING_HPP_INCLUDED
 
 /**
  * @file boost/python/numpy/ndarray.hpp
  * @brief Object manager and various utilities for numpy.ndarray.
  */
 
-#include <boost/python.hpp>
-#include <boost/utility/enable_if.hpp>
-#include <boost/type_traits/is_integral.hpp>
-#include <boost/python/numpy/numpy_object_mgr_traits.hpp>
 #include <boost/python/numpy/dtype.hpp>
+#include <boost/python/numpy/ndarray.hpp>
 
-#include <vector>
+namespace boost { namespace python { namespace numpy {
 
-namespace boost { namespace python {
-namespace numpy {
-
-/**
- * @brief A boost.python "object manager" (subclass of object) for numpy.ndarray.
- *
- * @todo This could have a lot more functionality (like boost::python::numeric::array).
- * Right now all that exists is what was needed to move raw data between C++ and Python.
- */
-class ndarray : public object {
-
- /**
- * @brief An internal struct that's byte-compatible with PyArrayObject.
- *
- * This is just a hack to allow inline access to this stuff while hiding numpy/arrayobject.h
- * from the user.
- */
- struct array_struct {
- PyObject_HEAD
- char * data;
- int nd;
- Py_intptr_t * shape;
- Py_intptr_t * strides;
- PyObject * base;
- PyObject * descr;
- int flags;
- PyObject * weakreflist;
- };
-
- /// @brief Return the held Python object as an array_struct.
- array_struct * get_struct() const { return reinterpret_cast<array_struct*>(this->ptr()); }
+namespace detail {
 
-public:
+struct add_pointer_meta {
 
- /**
- * @brief Enum to represent (some) of Numpy's internal flags.
- *
- * These don't match the actual Numpy flag values; we can't get those without including
- * numpy/arrayobject.h or copying them directly. That's very unfortunate.
- *
- * @todo I'm torn about whether this should be an enum. It's very convenient to not
- * make these simple integer values for overloading purposes, but the need to
- * define every possible combination and custom bitwise operators is ugly.
- */
- enum bitflag {
- NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2,
- ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8,
- CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8,
- FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8,
- UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8
+ template <typename T>
+ struct apply {
+ typedef typename boost::add_pointer<T>::type type;
     };
 
- BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(ndarray, object);
-
- /// @brief Return a view of the scalar with the given dtype.
- ndarray view(dtype const & dt) const;
-
- /// @brief Copy the scalar (deep for all non-object fields).
- ndarray copy() const;
+};
 
- /// @brief Return the size of the nth dimension.
- int const shape(int n) const { return get_shape()[n]; }
+struct dtype_template_match_found {};
+struct nd_template_match_found {};
 
- /// @brief Return the stride of the nth dimension.
- int const strides(int n) const { return get_strides()[n]; }
+template <typename Function>
+struct dtype_template_invoker {
     
- /**
- * @brief Return the array's raw data pointer.
- *
- * This returns char so stride math works properly on it. It's pretty much
- * expected that the user will have to reinterpret_cast it.
- */
- char * get_data() const { return get_struct()->data; }
-
- /// @brief Return the array's data-type descriptor object.
- dtype get_dtype() const;
-
- /// @brief Return the object that owns the array's data, or None if the array owns its own data.
- object get_base() const;
-
- /// @brief Set the object that owns the array's data. Use with care.
- void set_base(object const & base);
-
- /// @brief Return the shape of the array as an array of integers (length == get_nd()).
- Py_intptr_t const * get_shape() const { return get_struct()->shape; }
-
- /// @brief Return the stride of the array as an array of integers (length == get_nd()).
- Py_intptr_t const * get_strides() const { return get_struct()->strides; }
-
- /// @brief Return the number of array dimensions.
- int const get_nd() const { return get_struct()->nd; }
-
- /// @brief Return the array flags.
- bitflag const get_flags() const;
-
- /// @brief Reverse the dimensions of the array.
- ndarray transpose() const;
-
- /// @brief Eliminate any unit-sized dimensions.
- ndarray squeeze() const;
-
- /// @brief Equivalent to self.reshape(*shape) in Python.
- ndarray reshape(tuple const & shape) const;
-
- /**
- * @brief If the array contains only a single element, return it as an array scalar; otherwise return
- * the array.
- *
- * @internal This is simply a call to PyArray_Return();
- */
- object scalarize() const;
+ template <typename T>
+ void operator()(T * x) const {
+ if (dtype::get_builtin<T>() == m_dtype) {
+ m_func.apply(x);
+ throw dtype_template_match_found();
+ }
+ }
+
+ dtype_template_invoker(dtype const & dtype_, Function func) :
+ m_dtype(dtype_), m_func(func) {}
+
+private:
+ dtype const & m_dtype;
+ Function m_func;
 };
 
-/**
- * @brief Construct a new array with the given shape and data type, with data initialized to zero.
- */
-ndarray zeros(tuple const & shape, dtype const & dt);
-ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
-
-/**
- * @brief Construct a new array with the given shape and data type, with data left uninitialized.
- */
-ndarray empty(tuple const & shape, dtype const & dt);
-ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
-
-/**
- * @brief Construct a new array from an arbitrary Python sequence.
- *
- * @todo This does't seem to handle ndarray subtypes the same way that "numpy.array" does in Python.
- */
-ndarray array(object const & obj);
-ndarray array(object const & obj, dtype const & dt);
-
-namespace detail {
+template <typename Function>
+struct dtype_template_invoker< boost::reference_wrapper<Function> > {
+
+ template <typename T>
+ void operator()(T * x) const {
+ if (dtype::get_builtin<T>() == m_dtype) {
+ m_func.apply(x);
+ throw dtype_template_match_found();
+ }
+ }
+
+ dtype_template_invoker(dtype const & dtype_, Function & func) :
+ m_dtype(dtype_), m_func(func) {}
+
+private:
+ dtype const & m_dtype;
+ Function & m_func;
+};
 
-ndarray from_data_impl(
- void * data,
- dtype const & dt,
- std::vector<Py_intptr_t> const & shape,
- std::vector<Py_intptr_t> const & strides,
- object const & owner,
- bool writeable
-);
-
-template <typename Container>
-ndarray from_data_impl(
- void * data,
- dtype const & dt,
- Container shape,
- Container strides,
- object const & owner,
- bool writeable,
- typename boost::enable_if< boost::is_integral<typename Container::value_type> >::type * enabled = NULL
-) {
- std::vector<Py_intptr_t> shape_(shape.begin(),shape.end());
- std::vector<Py_intptr_t> strides_(strides.begin(), strides.end());
- return from_data_impl(data, dt, shape_, strides_, owner, writeable);
-}
+template <typename Function>
+struct nd_template_invoker {
+
+ template <typename T>
+ void operator()(T * x) const {
+ if (m_nd == T::value) {
+ m_func.apply(x);
+ throw nd_template_match_found();
+ }
+ }
+
+ nd_template_invoker(int nd, Function func) :
+ m_nd(nd), m_func(func) {}
+
+private:
+ int m_nd;
+ Function m_func;
+};
 
-ndarray from_data_impl(
- void * data,
- dtype const & dt,
- object const & shape,
- object const & strides,
- object const & owner,
- bool writeable
-);
+template <typename Function>
+struct nd_template_invoker< boost::reference_wrapper<Function> > {
+
+ template <typename T>
+ void operator()(T * x) const {
+ if (m_nd == T::value) {
+ m_func.apply(x);
+ throw nd_template_match_found();
+ }
+ }
+
+ nd_template_invoker(int nd, Function & func) :
+ m_nd(nd), m_func(func) {}
+
+private:
+ int m_nd;
+ Function & m_func;
+};
 
 } // namespace boost::python::numpy::detail
 
-/**
- * @brief Construct a new ndarray object from a raw pointer.
- *
- * @param[in] data Raw pointer to the first element of the array.
- * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin().
- * @param[in] shape Shape of the array as STL container of integers; must have begin() and end().
- * @param[in] strides Shape of the array as STL container of integers; must have begin() and end().
- * @param[in] owner An arbitray Python object that owns that data pointer. The array object will
- * keep a reference to the object, and decrement it's reference count when the
- * array goes out of scope. Pass None at your own peril.
- *
- * @todo Should probably take ranges of iterators rather than actual container objects.
- */
-template <typename Container>
-inline ndarray from_data(
- void * data,
- dtype const & dt,
- Container shape,
- Container strides,
- object const & owner
-) {
- return numpy::detail::from_data_impl(data, dt, shape, strides, owner, true);
-}
-
-/**
- * @brief Construct a new ndarray object from a raw pointer.
- *
- * @param[in] data Raw pointer to the first element of the array.
- * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin().
- * @param[in] shape Shape of the array as STL container of integers; must have begin() and end().
- * @param[in] strides Shape of the array as STL container of integers; must have begin() and end().
- * @param[in] owner An arbitray Python object that owns that data pointer. The array object will
- * keep a reference to the object, and decrement it's reference count when the
- * array goes out of scope. Pass None at your own peril.
- *
- * This overload takes a const void pointer and sets the "writeable" flag of the array to false.
- *
- * @todo Should probably take ranges of iterators rather than actual container objects.
- */
-template <typename Container>
-inline ndarray from_data(
- void const * data,
- dtype const & dt,
- Container shape,
- Container strides,
- object const & owner
-) {
- return numpy::detail::from_data_impl(const_cast<void*>(data), dt, shape, strides, owner, false);
-}
-
-/**
- * @brief Transform an arbitrary object into a numpy array with the given requirements.
- *
- * @param[in] obj An arbitrary python object to convert. Arrays that meet the requirements
- * will be passed through directly.
- * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin().
- * @param[in] nd_min Minimum number of dimensions.
- * @param[in] nd_max Maximum number of dimensions.
- * @param[in] flags Bitwise OR of flags specifying additional requirements.
- */
-ndarray from_object(object const & obj, dtype const & dt,
- int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
-
-inline ndarray from_object(object const & obj, dtype const & dt,
- int nd, ndarray::bitflag flags=ndarray::NONE) {
- return from_object(obj, dt, nd, nd, flags);
+template <typename Sequence, typename Function>
+void invoke_matching_nd(int nd, Function f) {
+ detail::nd_template_invoker<Function> invoker(nd, f);
+ try {
+ boost::mpl::for_each< Sequence, detail::add_pointer_meta >(invoker);
+ } catch (detail::nd_template_match_found &) {
+ return;
+ }
+ PyErr_SetString(PyExc_TypeError, "number of dimensions not found in template list.");
+ throw_error_already_set();
+}
+
+template <typename Sequence, typename Function>
+void invoke_matching_dtype(dtype const & dtype_, Function f) {
+ detail::dtype_template_invoker<Function> invoker(dtype_, f);
+ try {
+ boost::mpl::for_each< Sequence, detail::add_pointer_meta >(invoker);
+ } catch (detail::dtype_template_match_found &) {
+ return;
+ }
+ PyErr_SetString(PyExc_TypeError, "dtype not found in template list.");
+ throw_error_already_set();
 }
 
-inline ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE) {
- return from_object(obj, dt, 0, 0, flags);
-}
-
-ndarray from_object(object const & obj, int nd_min, int nd_max,
- ndarray::bitflag flags=ndarray::NONE);
-
-inline ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE) {
- return from_object(obj, nd, nd, flags);
-}
+namespace detail {
 
-inline ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE) {
- return from_object(obj, 0, 0, flags);
-}
+template <typename DimSequence, typename Function>
+struct array_template_invoker_wrapper {
 
-inline ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) {
- return ndarray::bitflag(int(a) | int(b));
-}
+ template <typename T>
+ void apply(T * x) const {
+ invoke_matching_nd<DimSequence>(m_nd, m_func.nest(x));
+ }
+
+ array_template_invoker_wrapper(int nd, Function func) :
+ m_nd(nd), m_func(func) {}
+
+private:
+ int m_nd;
+ Function m_func;
+};
 
-inline ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b) {
- return ndarray::bitflag(int(a) & int(b));
-}
+template <typename DimSequence, typename Function>
+struct array_template_invoker_wrapper< DimSequence, boost::reference_wrapper<Function> > {
 
+ template <typename T>
+ void apply(T * x) const {
+ invoke_matching_nd<DimSequence>(m_nd, m_func.nest(x));
+ }
+
+ array_template_invoker_wrapper(int nd, Function & func) :
+ m_nd(nd), m_func(func) {}
+
+private:
+ int m_nd;
+ Function & m_func;
+};
 
-} // namespace boost::python::numpy
+} // namespace boost::python::numpy::detail
 
-namespace converter {
+template <typename TypeSequence, typename DimSequence, typename Function>
+void invoke_matching_array(ndarray const & array_, Function f) {
+ detail::array_template_invoker_wrapper<DimSequence,Function> wrapper(array_.get_nd(), f);
+ invoke_matching_dtype<TypeSequence>(array_.get_dtype(), wrapper);
+}
 
-NUMPY_OBJECT_MANAGER_TRAITS(python::numpy::ndarray);
 
-} // namespace boost::python::converter
-}} // namespace boost::python
+}}} // namespace boost::python::numpy
 
-#endif // !BOOST_PYTHON_NUMPY_NDARRAY_HPP_INCLUDED
+#endif // !BOOST_PYTHON_NUMPY_INVOKE_MATCHING_HPP_INCLUDED

Modified: sandbox/numpy/boost/python/numpy/ndarray.hpp
==============================================================================
--- sandbox/numpy/boost/python/numpy/ndarray.hpp (original)
+++ sandbox/numpy/boost/python/numpy/ndarray.hpp 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
@@ -275,7 +275,6 @@
     return ndarray::bitflag(int(a) & int(b));
 }
 
-
 } // namespace boost::python::numpy
 
 namespace converter {

Modified: sandbox/numpy/boost/python/numpy/numpy.hpp
==============================================================================
--- sandbox/numpy/boost/python/numpy/numpy.hpp (original)
+++ sandbox/numpy/boost/python/numpy/numpy.hpp 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
@@ -11,6 +11,7 @@
 #include <boost/python/numpy/scalars.hpp>
 #include <boost/python/numpy/matrix.hpp>
 #include <boost/python/numpy/ufunc.hpp>
+#include <boost/python/numpy/invoke_matching.hpp>
 
 namespace boost { namespace python {
 namespace numpy {

Modified: sandbox/numpy/libs/python/numpy/test/templates.py
==============================================================================
--- sandbox/numpy/libs/python/numpy/test/templates.py (original)
+++ sandbox/numpy/libs/python/numpy/test/templates.py 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
@@ -6,12 +6,16 @@
 
     def testTemplates(self):
         for dtype in (numpy.int16, numpy.int32, numpy.float32, numpy.complex128):
- a1 = numpy.zeros((12,), dtype=dtype)
- a2 = numpy.arange(12, dtype=dtype)
- templates_mod.fill(a1)
- self.assert_((a1 == a2).all())
+ v = numpy.arange(12, dtype=dtype)
+ for shape in ((12,), (4, 3), (2, 6)):
+ a1 = numpy.zeros(shape, dtype=dtype)
+ a2 = v.reshape(a1.shape)
+ templates_mod.fill(a1)
+ self.assert_((a1 == a2).all())
         a1 = numpy.zeros((12,), dtype=numpy.float64)
         self.assertRaises(TypeError, templates_mod.fill, a1)
+ a1 = numpy.zeros((12,2,3), dtype=numpy.float32)
+ self.assertRaises(TypeError, templates_mod.fill, a1)
 
 if __name__=="__main__":
     unittest.main()

Modified: sandbox/numpy/libs/python/numpy/test/templates_mod.cpp
==============================================================================
--- sandbox/numpy/libs/python/numpy/test/templates_mod.cpp (original)
+++ sandbox/numpy/libs/python/numpy/test/templates_mod.cpp 2010-10-06 15:05:20 EDT (Wed, 06 Oct 2010)
@@ -1,21 +1,48 @@
 #include <boost/python/numpy/numpy.hpp>
 #include <boost/mpl/vector.hpp>
+#include <boost/mpl/vector_c.hpp>
 
 namespace bp = boost::python;
 
 struct ArrayFiller {
 
- typedef boost::mpl::vector< short, int, float, std::complex<double> > Sequence;
+ typedef boost::mpl::vector< short, int, float, std::complex<double> > TypeSequence;
+ typedef boost::mpl::vector_c< int, 1, 2 > DimSequence;
 
     template <typename T>
- void apply() const {
- char * p = argument.get_data();
- int stride = argument.strides(0);
- int size = argument.shape(0);
- for (int n = 0; n != size; ++n, p += stride) {
- *reinterpret_cast<T*>(p) = static_cast<T>(n);
+ struct nested {
+
+ void apply(boost::mpl::integral_c<int,1> * ) const {
+ char * p = argument.get_data();
+ int stride = argument.strides(0);
+ int size = argument.shape(0);
+ for (int n = 0; n != size; ++n, p += stride) {
+ *reinterpret_cast<T*>(p) = static_cast<T>(n);
+ }
+ }
+
+ void apply(boost::mpl::integral_c<int,2> * ) const {
+ char * row_p = argument.get_data();
+ int row_stride = argument.strides(0);
+ int col_stride = argument.strides(1);
+ int rows = argument.shape(0);
+ int cols = argument.shape(1);
+ int i = 0;
+ for (int n = 0; n != rows; ++n, row_p += row_stride) {
+ char * col_p = row_p;
+ for (int m = 0; m != cols; ++i, ++m, col_p += col_stride) {
+ *reinterpret_cast<T*>(col_p) = static_cast<T>(i);
+ }
+ }
         }
- }
+
+ explicit nested(bp::numpy::ndarray const & arg) : argument(arg) {}
+
+ bp::numpy::ndarray argument;
+ };
+
+ template <typename T>
+ nested<T> nest(T *) const { return nested<T>(argument); }
 
     bp::numpy::ndarray argument;
 
@@ -25,7 +52,7 @@
 
 void fill(bp::numpy::ndarray const & arg) {
     ArrayFiller filler(arg);
- arg.get_dtype().invoke_matching_template< ArrayFiller::Sequence >(filler);
+ bp::numpy::invoke_matching_array< ArrayFiller::TypeSequence, ArrayFiller::DimSequence >(arg, filler);
 }
 
 BOOST_PYTHON_MODULE(templates_mod) {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk