123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #ifndef boost_python_numpy_ndarray_hpp_
- #define boost_python_numpy_ndarray_hpp_
- #include <boost/python.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/python/detail/type_traits.hpp>
- #include <boost/python/numpy/numpy_object_mgr_traits.hpp>
- #include <boost/python/numpy/dtype.hpp>
- #include <boost/python/numpy/config.hpp>
- #include <vector>
- namespace boost { namespace python { namespace numpy {
-
- class BOOST_NUMPY_DECL ndarray : public object
- {
-
- 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;
- };
-
-
- array_struct * get_struct() const { return reinterpret_cast<array_struct*>(this->ptr()); }
- public:
-
-
- 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
- };
- BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(ndarray, object);
-
- ndarray view(dtype const & dt) const;
-
-
- ndarray astype(dtype const & dt) const;
-
- ndarray copy() const;
-
- Py_intptr_t shape(int n) const;
-
- Py_intptr_t strides(int n) const;
-
-
- char * get_data() const { return get_struct()->data; }
-
- dtype get_dtype() const;
-
-
- object get_base() const;
-
-
- void set_base(object const & base);
-
-
- Py_intptr_t const * get_shape() const { return get_struct()->shape; }
-
-
- Py_intptr_t const * get_strides() const { return get_struct()->strides; }
-
-
- int get_nd() const { return get_struct()->nd; }
-
-
- bitflag get_flags() const;
-
-
- ndarray transpose() const;
-
-
- ndarray squeeze() const;
-
-
- ndarray reshape(python::tuple const & shape) const;
-
-
- object scalarize() const;
- };
- BOOST_NUMPY_DECL ndarray zeros(python::tuple const & shape, dtype const & dt);
- BOOST_NUMPY_DECL ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
- BOOST_NUMPY_DECL ndarray empty(python::tuple const & shape, dtype const & dt);
- BOOST_NUMPY_DECL ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
- BOOST_NUMPY_DECL ndarray array(object const & obj);
- BOOST_NUMPY_DECL ndarray array(object const & obj, dtype const & dt);
- namespace detail
- {
- BOOST_NUMPY_DECL 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::python::detail::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);
- }
- BOOST_NUMPY_DECL ndarray from_data_impl(void * data,
- dtype const & dt,
- object const & shape,
- object const & strides,
- object const & owner,
- bool writeable);
- }
- template <typename Container>
- inline ndarray from_data(void * data,
- dtype const & dt,
- Container shape,
- Container strides,
- python::object const & owner)
- {
- return numpy::detail::from_data_impl(data, dt, shape, strides, owner, true);
- }
- template <typename Container>
- inline ndarray from_data(void const * data,
- dtype const & dt,
- Container shape,
- Container strides,
- python::object const & owner)
- {
- return numpy::detail::from_data_impl(const_cast<void*>(data), dt, shape, strides, owner, false);
- }
- BOOST_NUMPY_DECL ndarray from_object(object const & obj,
- dtype const & dt,
- int nd_min,
- int nd_max,
- ndarray::bitflag flags=ndarray::NONE);
- BOOST_NUMPY_DECL 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);
- }
- BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
- dtype const & dt,
- ndarray::bitflag flags=ndarray::NONE)
- {
- return from_object(obj, dt, 0, 0, flags);
- }
- BOOST_NUMPY_DECL ndarray from_object(object const & obj,
- int nd_min,
- int nd_max,
- ndarray::bitflag flags=ndarray::NONE);
- BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
- int nd,
- ndarray::bitflag flags=ndarray::NONE)
- {
- return from_object(obj, nd, nd, flags);
- }
- BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
- ndarray::bitflag flags=ndarray::NONE)
- {
- return from_object(obj, 0, 0, flags);
- }
- BOOST_NUMPY_DECL inline ndarray::bitflag operator|(ndarray::bitflag a,
- ndarray::bitflag b)
- {
- return ndarray::bitflag(int(a) | int(b));
- }
- BOOST_NUMPY_DECL inline ndarray::bitflag operator&(ndarray::bitflag a,
- ndarray::bitflag b)
- {
- return ndarray::bitflag(int(a) & int(b));
- }
- }
- namespace converter
- {
- NUMPY_OBJECT_MANAGER_TRAITS(numpy::ndarray);
- }}}
- #endif
|