4 .. contents :: Table of Contents
6 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.
8 .. _ndarray: http://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html
9 .. _dtype: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#data-type-objects-dtype
11 ``<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
27 class ndarray : public object
34 NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2,
35 ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8,
36 CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8,
37 FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8,
38 UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8
41 ndarray view(dtype const & dt) const;
42 ndarray astype(dtype const & dt) const;
44 int const shape(int n) const;
45 int const strides(int n) const;
46 char * get_data() const;
47 dtype get_dtype() const;
48 python::object get_base() const;
49 void set_base(object const & base);
50 Py_intptr_t const * get_shape() const;
51 Py_intptr_t const * get_strides() const;
52 int const get_nd() const;
54 bitflag const get_flags() const;
56 ndarray transpose() const;
57 ndarray squeeze() const;
58 ndarray reshape(tuple const & shape) const;
59 object scalarize() const;
62 ndarray zeros(tuple const & shape, dtype const & dt);
63 ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
65 ndarray empty(tuple const & shape, dtype const & dt);
66 ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
68 ndarray array(object const & obj);
69 ndarray array(object const & obj, dtype const & dt);
71 template <typename Container>
72 ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner);
73 template <typename Container>
74 ndarray from_data(void const * data, dtype const & dt, Container shape, Container strides, object const & owner);
76 ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
77 ndarray from_object(object const & obj, dtype const & dt,int nd, ndarray::bitflag flags=ndarray::NONE);
78 ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE);
79 ndarray from_object(object const & obj, int nd_min, int nd_max,ndarray::bitflag flags=ndarray::NONE);
80 ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE);
81 ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE)
83 ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b) ;
84 ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b);
94 ndarray view(dtype const & dt) const;
96 :Returns: new ndarray with old ndarray data cast as supplied dtype
100 ndarray astype(dtype const & dt) const;
102 :Returns: new ndarray with old ndarray data converted to supplied dtype
106 ndarray copy() const;
108 :Returns: Copy of calling ndarray object
112 ndarray transpose() const;
114 :Returns: An ndarray with the rows and columns interchanged
118 ndarray squeeze() const;
120 :Returns: An ndarray with all unit-shaped dimensions removed
124 ndarray reshape(tuple const & shape) const;
126 :Requirements: The new ``shape`` of the ndarray must be supplied as a tuple
128 :Returns: An ndarray with the same data but reshaped to the ``shape`` supplied
133 object scalarize() const;
135 :Returns: A scalar if the ndarray has only one element, otherwise it returns the entire array
139 ndarray zeros(tuple const & shape, dtype const & dt);
140 ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
142 :Requirements: The following parameters must be supplied as required :
144 * the ``shape`` or the size of all dimensions, as a tuple
145 * the ``dtype`` of the data
146 * the ``nd`` size for a square shaped ndarray
147 * the ``shape`` Py_intptr_t
149 :Returns: A new ndarray with the given shape and data type, with data initialized to zero.
153 ndarray empty(tuple const & shape, dtype const & dt);
154 ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
157 :Requirements: The following parameters must be supplied :
159 * the ``shape`` or the size of all dimensions, as a tuple
160 * the ``dtype`` of the data
161 * the ``shape`` Py_intptr_t
163 :Returns: A new ndarray with the given shape and data type, with data left uninitialized.
167 ndarray array(object const & obj);
168 ndarray array(object const & obj, dtype const & dt);
170 :Returns: A new ndarray from an arbitrary Python sequence, with dtype of each element specified optionally
174 template <typename Container>
175 inline ndarray from_data(void * data,dtype const & dt,Container shape,Container strides,python::object const & owner)
177 :Requirements: The following parameters must be supplied :
179 * the ``data`` which is a generic C++ data container
180 * the dtype ``dt`` of the data
181 * the ``shape`` of the ndarray as Python object
182 * the ``strides`` of each dimension of the array as a Python object
183 * the ``owner`` of the data, in case it is not the ndarray itself
185 :Returns: ndarray with attributes and data supplied
187 :Note: The ``Container`` typename must be one that is convertible to a std::vector or python object type
191 ndarray from_object(object const & obj, dtype const & dt,int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
193 :Requirements: The following parameters must be supplied :
195 * the ``obj`` Python object to convert to ndarray
196 * the dtype ``dt`` of the data
197 * minimum number of dimensions ``nd_min`` of the ndarray as Python object
198 * maximum number of dimensions ``nd_max`` of the ndarray as Python object
199 * optional ``flags`` bitflags
201 :Returns: ndarray constructed with dimensions and data supplied as parameters
205 inline ndarray from_object(object const & obj, dtype const & dt, int nd, ndarray::bitflag flags=ndarray::NONE);
207 :Requirements: The following parameters must be supplied :
209 * the ``obj`` Python object to convert to ndarray
210 * the dtype ``dt`` of the data
211 * number of dimensions ``nd`` of the ndarray as Python object
212 * optional ``flags`` bitflags
214 :Returns: ndarray with dimensions ``nd`` x ``nd`` and suplied parameters
218 inline ndarray from_object(object const & obj, dtype const & dt, ndarray::bitflag flags=ndarray::NONE)
220 :Requirements: The following parameters must be supplied :
222 * the ``obj`` Python object to convert to ndarray
223 * the dtype ``dt`` of the data
224 * optional ``flags`` bitflags
226 :Returns: Supplied Python object as ndarray
230 ndarray from_object(object const & obj, int nd_min, int nd_max, ndarray::bitflag flags=ndarray::NONE);
232 :Requirements: The following parameters must be supplied :
234 * the ``obj`` Python object to convert to ndarray
235 * minimum number of dimensions ``nd_min`` of the ndarray as Python object
236 * maximum number of dimensions ``nd_max`` of the ndarray as Python object
237 * optional ``flags`` bitflags
239 :Returns: ndarray with supplied dimension limits and parameters
241 :Note: dtype need not be supplied here
245 inline ndarray from_object(object const & obj, int nd, ndarray::bitflag flags=ndarray::NONE);
247 :Requirements: The following parameters must be supplied :
249 * the ``obj`` Python object to convert to ndarray
250 * the dtype ``dt`` of the data
251 * number of dimensions ``nd`` of the ndarray as Python object
252 * optional ``flags`` bitflags
254 :Returns: ndarray of ``nd`` x ``nd`` dimensions constructed from the supplied object
258 inline ndarray from_object(object const & obj, ndarray::bitflag flags=ndarray::NONE)
260 :Requirements: The following parameters must be supplied :
262 * the ``obj`` Python object to convert to ndarray
263 * optional ``flags`` bitflags
265 :Returns: ndarray of same dimensions and dtype as supplied Python object
273 int const shape(int n) const;
275 :Returns: The size of the n-th dimension of the ndarray
279 int const strides(int n) const;
281 :Returns: The stride of the nth dimension.
285 char * get_data() const;
287 :Returns: Array's raw data pointer as a char
289 :Note: This returns char so stride math works properly on it.User will have to reinterpret_cast it.
293 dtype get_dtype() const;
295 :Returns: Array's data-type descriptor object (dtype)
300 object get_base() const;
302 :Returns: Object that owns the array's data, or None if the array owns its own data.
307 void set_base(object const & base);
309 :Returns: Set the object that owns the array's data. Exercise caution while using this
314 Py_intptr_t const * get_shape() const;
316 :Returns: Shape of the array as an array of integers
321 Py_intptr_t const * get_strides() const;
323 :Returns: Stride of the array as an array of integers
328 int const get_nd() const;
330 :Returns: Number of array dimensions
335 bitflag const get_flags() const;
337 :Returns: Array flags
341 inline ndarray::bitflag operator|(ndarray::bitflag a, ndarray::bitflag b)
343 :Returns: bitflag logically OR-ed as (a | b)
347 inline ndarray::bitflag operator&(ndarray::bitflag a, ndarray::bitflag b)
349 :Returns: bitflag logically AND-ed as (a & b)
357 namespace p = boost::python;
358 namespace np = boost::python::numpy;
360 p::object tu = p::make_tuple('a','b','c') ;
361 np::ndarray example_tuple = np::array (tu) ;
364 np::ndarray example_list = np::array (l) ;
366 np::dtype dt = np::dtype::get_builtin<int>();
367 np::ndarray example_list1 = np::array (l,dt);
369 int data[] = {1,2,3,4} ;
370 p::tuple shape = p::make_tuple(4) ;
371 p::tuple stride = p::make_tuple(4) ;
373 np::ndarray data_ex = np::from_data(data,dt,shape,stride,own);
375 uint8_t mul_data[][4] = {{1,2,3,4},{5,6,7,8},{1,3,5,7}};
376 shape = p::make_tuple(3,2) ;
377 stride = p::make_tuple(4,2) ;
378 np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
380 np::ndarray mul_data_ex = np::from_data(mul_data,dt1, p::make_tuple(3,4),p::make_tuple(4,1),p::object());
381 mul_data_ex = np::from_data(mul_data,dt1, shape,stride,p::object());