ndarray.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright Jim Bosch 2010-2012.
  2. // Copyright Stefan Seefeld 2016.
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef boost_python_numpy_ndarray_hpp_
  7. #define boost_python_numpy_ndarray_hpp_
  8. /**
  9. * @brief Object manager and various utilities for numpy.ndarray.
  10. */
  11. #include <boost/python.hpp>
  12. #include <boost/utility/enable_if.hpp>
  13. #include <boost/python/detail/type_traits.hpp>
  14. #include <boost/python/numpy/numpy_object_mgr_traits.hpp>
  15. #include <boost/python/numpy/dtype.hpp>
  16. #include <boost/python/numpy/config.hpp>
  17. #include <vector>
  18. namespace boost { namespace python { namespace numpy {
  19. /**
  20. * @brief A boost.python "object manager" (subclass of object) for numpy.ndarray.
  21. *
  22. * @todo This could have a lot more functionality (like boost::python::numeric::array).
  23. * Right now all that exists is what was needed to move raw data between C++ and Python.
  24. */
  25. class BOOST_NUMPY_DECL ndarray : public object
  26. {
  27. /**
  28. * @brief An internal struct that's byte-compatible with PyArrayObject.
  29. *
  30. * This is just a hack to allow inline access to this stuff while hiding numpy/arrayobject.h
  31. * from the user.
  32. */
  33. struct array_struct
  34. {
  35. PyObject_HEAD
  36. char * data;
  37. int nd;
  38. Py_intptr_t * shape;
  39. Py_intptr_t * strides;
  40. PyObject * base;
  41. PyObject * descr;
  42. int flags;
  43. PyObject * weakreflist;
  44. };
  45. /// @brief Return the held Python object as an array_struct.
  46. array_struct * get_struct() const { return reinterpret_cast<array_struct*>(this->ptr()); }
  47. public:
  48. /**
  49. * @brief Enum to represent (some) of Numpy's internal flags.
  50. *
  51. * These don't match the actual Numpy flag values; we can't get those without including
  52. * numpy/arrayobject.h or copying them directly. That's very unfortunate.
  53. *
  54. * @todo I'm torn about whether this should be an enum. It's very convenient to not
  55. * make these simple integer values for overloading purposes, but the need to
  56. * define every possible combination and custom bitwise operators is ugly.
  57. */
  58. enum bitflag
  59. {
  60. NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2,
  61. ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8,
  62. CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8,
  63. FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8,
  64. UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8
  65. };
  66. BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(ndarray, object);
  67. /// @brief Return a view of the scalar with the given dtype.
  68. ndarray view(dtype const & dt) const;
  69. /// @brief Copy the array, cast to a specified type.
  70. ndarray astype(dtype const & dt) const;
  71. /// @brief Copy the scalar (deep for all non-object fields).
  72. ndarray copy() const;
  73. /// @brief Return the size of the nth dimension. raises IndexError if k not in [-get_nd() : get_nd()-1 ]
  74. Py_intptr_t shape(int n) const;
  75. /// @brief Return the stride of the nth dimension. raises IndexError if k not in [-get_nd() : get_nd()-1]
  76. Py_intptr_t strides(int n) const;
  77. /**
  78. * @brief Return the array's raw data pointer.
  79. *
  80. * This returns char so stride math works properly on it. It's pretty much
  81. * expected that the user will have to reinterpret_cast it.
  82. */
  83. char * get_data() const { return get_struct()->data; }
  84. /// @brief Return the array's data-type descriptor object.
  85. dtype get_dtype() const;
  86. /// @brief Return the object that owns the array's data, or None if the array owns its own data.
  87. object get_base() const;
  88. /// @brief Set the object that owns the array's data. Use with care.
  89. void set_base(object const & base);
  90. /// @brief Return the shape of the array as an array of integers (length == get_nd()).
  91. Py_intptr_t const * get_shape() const { return get_struct()->shape; }
  92. /// @brief Return the stride of the array as an array of integers (length == get_nd()).
  93. Py_intptr_t const * get_strides() const { return get_struct()->strides; }
  94. /// @brief Return the number of array dimensions.
  95. int get_nd() const { return get_struct()->nd; }
  96. /// @brief Return the array flags.
  97. bitflag get_flags() const;
  98. /// @brief Reverse the dimensions of the array.
  99. ndarray transpose() const;
  100. /// @brief Eliminate any unit-sized dimensions.
  101. ndarray squeeze() const;
  102. /// @brief Equivalent to self.reshape(*shape) in Python.
  103. ndarray reshape(python::tuple const & shape) const;
  104. /**
  105. * @brief If the array contains only a single element, return it as an array scalar; otherwise return
  106. * the array.
  107. *
  108. * @internal This is simply a call to PyArray_Return();
  109. */
  110. object scalarize() const;
  111. };
  112. /**
  113. * @brief Construct a new array with the given shape and data type, with data initialized to zero.
  114. */
  115. BOOST_NUMPY_DECL ndarray zeros(python::tuple const & shape, dtype const & dt);
  116. BOOST_NUMPY_DECL ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
  117. /**
  118. * @brief Construct a new array with the given shape and data type, with data left uninitialized.
  119. */
  120. BOOST_NUMPY_DECL ndarray empty(python::tuple const & shape, dtype const & dt);
  121. BOOST_NUMPY_DECL ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
  122. /**
  123. * @brief Construct a new array from an arbitrary Python sequence.
  124. *
  125. * @todo This does't seem to handle ndarray subtypes the same way that "numpy.array" does in Python.
  126. */
  127. BOOST_NUMPY_DECL ndarray array(object const & obj);
  128. BOOST_NUMPY_DECL ndarray array(object const & obj, dtype const & dt);
  129. namespace detail
  130. {
  131. BOOST_NUMPY_DECL ndarray from_data_impl(void * data,
  132. dtype const & dt,
  133. std::vector<Py_intptr_t> const & shape,
  134. std::vector<Py_intptr_t> const & strides,
  135. object const & owner,
  136. bool writeable);
  137. template <typename Container>
  138. ndarray from_data_impl(void * data,
  139. dtype const & dt,
  140. Container shape,
  141. Container strides,
  142. object const & owner,
  143. bool writeable,
  144. typename boost::enable_if< boost::python::detail::is_integral<typename Container::value_type> >::type * enabled = NULL)
  145. {
  146. std::vector<Py_intptr_t> shape_(shape.begin(),shape.end());
  147. std::vector<Py_intptr_t> strides_(strides.begin(), strides.end());
  148. return from_data_impl(data, dt, shape_, strides_, owner, writeable);
  149. }
  150. BOOST_NUMPY_DECL ndarray from_data_impl(void * data,
  151. dtype const & dt,
  152. object const & shape,
  153. object const & strides,
  154. object const & owner,
  155. bool writeable);
  156. } // namespace boost::python::numpy::detail
  157. /**
  158. * @brief Construct a new ndarray object from a raw pointer.
  159. *
  160. * @param[in] data Raw pointer to the first element of the array.
  161. * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin().
  162. * @param[in] shape Shape of the array as STL container of integers; must have begin() and end().
  163. * @param[in] strides Shape of the array as STL container of integers; must have begin() and end().
  164. * @param[in] owner An arbitray Python object that owns that data pointer. The array object will
  165. * keep a reference to the object, and decrement it's reference count when the
  166. * array goes out of scope. Pass None at your own peril.
  167. *
  168. * @todo Should probably take ranges of iterators rather than actual container objects.
  169. */
  170. template <typename Container>
  171. inline ndarray from_data(void * data,
  172. dtype const & dt,
  173. Container shape,
  174. Container strides,
  175. python::object const & owner)
  176. {
  177. return numpy::detail::from_data_impl(data, dt, shape, strides, owner, true);
  178. }
  179. /**
  180. * @brief Construct a new ndarray object from a raw pointer.
  181. *
  182. * @param[in] data Raw pointer to the first element of the array.
  183. * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin().
  184. * @param[in] shape Shape of the array as STL container of integers; must have begin() and end().
  185. * @param[in] strides Shape of the array as STL container of integers; must have begin() and end().
  186. * @param[in] owner An arbitray Python object that owns that data pointer. The array object will
  187. * keep a reference to the object, and decrement it's reference count when the
  188. * array goes out of scope. Pass None at your own peril.
  189. *
  190. * This overload takes a const void pointer and sets the "writeable" flag of the array to false.
  191. *
  192. * @todo Should probably take ranges of iterators rather than actual container objects.
  193. */
  194. template <typename Container>
  195. inline ndarray from_data(void const * data,
  196. dtype const & dt,
  197. Container shape,
  198. Container strides,
  199. python::object const & owner)
  200. {
  201. return numpy::detail::from_data_impl(const_cast<void*>(data), dt, shape, strides, owner, false);
  202. }
  203. /**
  204. * @brief Transform an arbitrary object into a numpy array with the given requirements.
  205. *
  206. * @param[in] obj An arbitrary python object to convert. Arrays that meet the requirements
  207. * will be passed through directly.
  208. * @param[in] dt Data type descriptor. Often retrieved with dtype::get_builtin().
  209. * @param[in] nd_min Minimum number of dimensions.
  210. * @param[in] nd_max Maximum number of dimensions.
  211. * @param[in] flags Bitwise OR of flags specifying additional requirements.
  212. */
  213. BOOST_NUMPY_DECL ndarray from_object(object const & obj,
  214. dtype const & dt,
  215. int nd_min,
  216. int nd_max,
  217. ndarray::bitflag flags=ndarray::NONE);
  218. BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
  219. dtype const & dt,
  220. int nd,
  221. ndarray::bitflag flags=ndarray::NONE)
  222. {
  223. return from_object(obj, dt, nd, nd, flags);
  224. }
  225. BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
  226. dtype const & dt,
  227. ndarray::bitflag flags=ndarray::NONE)
  228. {
  229. return from_object(obj, dt, 0, 0, flags);
  230. }
  231. BOOST_NUMPY_DECL ndarray from_object(object const & obj,
  232. int nd_min,
  233. int nd_max,
  234. ndarray::bitflag flags=ndarray::NONE);
  235. BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
  236. int nd,
  237. ndarray::bitflag flags=ndarray::NONE)
  238. {
  239. return from_object(obj, nd, nd, flags);
  240. }
  241. BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
  242. ndarray::bitflag flags=ndarray::NONE)
  243. {
  244. return from_object(obj, 0, 0, flags);
  245. }
  246. BOOST_NUMPY_DECL inline ndarray::bitflag operator|(ndarray::bitflag a,
  247. ndarray::bitflag b)
  248. {
  249. return ndarray::bitflag(int(a) | int(b));
  250. }
  251. BOOST_NUMPY_DECL inline ndarray::bitflag operator&(ndarray::bitflag a,
  252. ndarray::bitflag b)
  253. {
  254. return ndarray::bitflag(int(a) & int(b));
  255. }
  256. } // namespace boost::python::numpy
  257. namespace converter
  258. {
  259. NUMPY_OBJECT_MANAGER_TRAITS(numpy::ndarray);
  260. }}} // namespace boost::python::converter
  261. #endif