ndarray.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. ndarray
  2. =======
  3. .. contents :: Table of Contents
  4. A `ndarray`_ is an N-dimensional array which contains items of the same type and size, where N is the number of dimensions and is specified in the form of a ``shape`` tuple. Optionally, the numpy ``dtype`` for the objects contained may also be specified.
  5. .. _ndarray: http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
  6. .. _dtype: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#data-type-objects-dtype
  7. ``<boost/python/numpy/ndarray.hpp>`` contains the structures and methods necessary to move raw data between C++ and Python and create ndarrays from the data
  8. synopsis
  9. --------
  10. ::
  11. namespace boost
  12. {
  13. namespace python
  14. {
  15. namespace numpy
  16. {
  17. class ndarray : public object
  18. {
  19. public:
  20. enum bitflag
  21. {
  22. NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2,
  23. ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8,
  24. CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8,
  25. FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8,
  26. UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8
  27. };
  28. ndarray view(dtype const & dt) const;
  29. ndarray astype(dtype const & dt) const;
  30. ndarray copy() const;
  31. int const shape(int n) const;
  32. int const strides(int n) const;
  33. char * get_data() const;
  34. dtype get_dtype() const;
  35. python::object get_base() const;
  36. void set_base(object const & base);
  37. Py_intptr_t const * get_shape() const;
  38. Py_intptr_t const * get_strides() const;
  39. int const get_nd() const;
  40. bitflag const get_flags() const;
  41. ndarray transpose() const;
  42. ndarray squeeze() const;
  43. ndarray reshape(tuple const & shape) const;
  44. object scalarize() const;
  45. };
  46. ndarray zeros(tuple const & shape, dtype const & dt);
  47. ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
  48. ndarray empty(tuple const & shape, dtype const & dt);
  49. ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
  50. ndarray array(object const & obj);
  51. ndarray array(object const & obj, dtype const & dt);
  52. template <typename Container>
  53. ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner);
  54. template <typename Container>
  55. ndarray from_data(void const * data, dtype const & dt, Container shape, Container strides, object const & owner);
  56. ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
  57. ndarray from_object(object const & obj, dtype const & dt,int nd, ndarray::bitflag flags=ndarray::NONE);
  58. ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE);
  59. ndarray from_object(object const & obj, int nd_min, int nd_max,ndarray::bitflag flags=ndarray::NONE);
  60. ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE);
  61. ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE)
  62. ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) ;
  63. ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b);
  64. }
  65. constructors
  66. ------------
  67. ::
  68. ndarray view(dtype const & dt) const;
  69. :Returns: new ndarray with old ndarray data cast as supplied dtype
  70. ::
  71. ndarray astype(dtype const & dt) const;
  72. :Returns: new ndarray with old ndarray data converted to supplied dtype
  73. ::
  74. ndarray copy() const;
  75. :Returns: Copy of calling ndarray object
  76. ::
  77. ndarray transpose() const;
  78. :Returns: An ndarray with the rows and columns interchanged
  79. ::
  80. ndarray squeeze() const;
  81. :Returns: An ndarray with all unit-shaped dimensions removed
  82. ::
  83. ndarray reshape(tuple const & shape) const;
  84. :Requirements: The new ``shape`` of the ndarray must be supplied as a tuple
  85. :Returns: An ndarray with the same data but reshaped to the ``shape`` supplied
  86. ::
  87. object scalarize() const;
  88. :Returns: A scalar if the ndarray has only one element, otherwise it returns the entire array
  89. ::
  90. ndarray zeros(tuple const & shape, dtype const & dt);
  91. ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
  92. :Requirements: The following parameters must be supplied as required :
  93. * the ``shape`` or the size of all dimensions, as a tuple
  94. * the ``dtype`` of the data
  95. * the ``nd`` size for a square shaped ndarray
  96. * the ``shape`` Py_intptr_t
  97. :Returns: A new ndarray with the given shape and data type, with data initialized to zero.
  98. ::
  99. ndarray empty(tuple const & shape, dtype const & dt);
  100. ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
  101. :Requirements: The following parameters must be supplied :
  102. * the ``shape`` or the size of all dimensions, as a tuple
  103. * the ``dtype`` of the data
  104. * the ``shape`` Py_intptr_t
  105. :Returns: A new ndarray with the given shape and data type, with data left uninitialized.
  106. ::
  107. ndarray array(object const & obj);
  108. ndarray array(object const & obj, dtype const & dt);
  109. :Returns: A new ndarray from an arbitrary Python sequence, with dtype of each element specified optionally
  110. ::
  111. template <typename Container>
  112. inline ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner)
  113. :Requirements: The following parameters must be supplied :
  114. * the ``data`` which is a generic C++ data container
  115. * the dtype ``dt`` of the data
  116. * the ``shape`` of the ndarray as Python object
  117. * the ``strides`` of each dimension of the array as a Python object
  118. * the ``owner`` of the data, in case it is not the ndarray itself
  119. :Returns: ndarray with attributes and data supplied
  120. :Note: The ``Container`` typename must be one that is convertible to a std::vector or python object type
  121. ::
  122. ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
  123. :Requirements: The following parameters must be supplied :
  124. * the ``obj`` Python object to convert to ndarray
  125. * the dtype ``dt`` of the data
  126. * minimum number of dimensions ``nd_min`` of the ndarray as Python object
  127. * maximum number of dimensions ``nd_max`` of the ndarray as Python object
  128. * optional ``flags`` bitflags
  129. :Returns: ndarray constructed with dimensions and data supplied as parameters
  130. ::
  131. inline ndarray from_object(object const & obj, dtype const & dt, int nd, ndarray::bitflag flags=ndarray::NONE);
  132. :Requirements: The following parameters must be supplied :
  133. * the ``obj`` Python object to convert to ndarray
  134. * the dtype ``dt`` of the data
  135. * number of dimensions ``nd`` of the ndarray as Python object
  136. * optional ``flags`` bitflags
  137. :Returns: ndarray with dimensions ``nd`` x ``nd`` and suplied parameters
  138. ::
  139. inline ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE)
  140. :Requirements: The following parameters must be supplied :
  141. * the ``obj`` Python object to convert to ndarray
  142. * the dtype ``dt`` of the data
  143. * optional ``flags`` bitflags
  144. :Returns: Supplied Python object as ndarray
  145. ::
  146. ndarray from_object(object const & obj, int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
  147. :Requirements: The following parameters must be supplied :
  148. * the ``obj`` Python object to convert to ndarray
  149. * minimum number of dimensions ``nd_min`` of the ndarray as Python object
  150. * maximum number of dimensions ``nd_max`` of the ndarray as Python object
  151. * optional ``flags`` bitflags
  152. :Returns: ndarray with supplied dimension limits and parameters
  153. :Note: dtype need not be supplied here
  154. ::
  155. inline ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE);
  156. :Requirements: The following parameters must be supplied :
  157. * the ``obj`` Python object to convert to ndarray
  158. * the dtype ``dt`` of the data
  159. * number of dimensions ``nd`` of the ndarray as Python object
  160. * optional ``flags`` bitflags
  161. :Returns: ndarray of ``nd`` x ``nd`` dimensions constructed from the supplied object
  162. ::
  163. inline ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE)
  164. :Requirements: The following parameters must be supplied :
  165. * the ``obj`` Python object to convert to ndarray
  166. * optional ``flags`` bitflags
  167. :Returns: ndarray of same dimensions and dtype as supplied Python object
  168. accessors
  169. ---------
  170. ::
  171. int const shape(int n) const;
  172. :Returns: The size of the n-th dimension of the ndarray
  173. ::
  174. int const strides(int n) const;
  175. :Returns: The stride of the nth dimension.
  176. ::
  177. char * get_data() const;
  178. :Returns: Array's raw data pointer as a char
  179. :Note: This returns char so stride math works properly on it.User will have to reinterpret_cast it.
  180. ::
  181. dtype get_dtype() const;
  182. :Returns: Array's data-type descriptor object (dtype)
  183. ::
  184. object get_base() const;
  185. :Returns: Object that owns the array's data, or None if the array owns its own data.
  186. ::
  187. void set_base(object const & base);
  188. :Returns: Set the object that owns the array's data. Exercise caution while using this
  189. ::
  190. Py_intptr_t const * get_shape() const;
  191. :Returns: Shape of the array as an array of integers
  192. ::
  193. Py_intptr_t const * get_strides() const;
  194. :Returns: Stride of the array as an array of integers
  195. ::
  196. int const get_nd() const;
  197. :Returns: Number of array dimensions
  198. ::
  199. bitflag const get_flags() const;
  200. :Returns: Array flags
  201. ::
  202. inline ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b)
  203. :Returns: bitflag logically OR-ed as (a | b)
  204. ::
  205. inline ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b)
  206. :Returns: bitflag logically AND-ed as (a & b)
  207. Example(s)
  208. ----------
  209. ::
  210. namespace p = boost::python;
  211. namespace np = boost::python::numpy;
  212. p::object tu = p::make_tuple('a','b','c') ;
  213. np::ndarray example_tuple = np::array (tu) ;
  214. p::list l ;
  215. np::ndarray example_list = np::array (l) ;
  216. np::dtype dt = np::dtype::get_builtin<int>();
  217. np::ndarray example_list1 = np::array (l,dt);
  218. int data[] = {1,2,3,4} ;
  219. p::tuple shape = p::make_tuple(4) ;
  220. p::tuple stride = p::make_tuple(4) ;
  221. p::object own ;
  222. np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
  223. uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
  224. shape = p::make_tuple(3,2) ;
  225. stride = p::make_tuple(4,2) ;
  226. np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
  227. np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
  228. mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());