File indexing completed on 2025-10-31 08:55:06
0001 
0002 
0003 
0004 
0005 
0006 
0007 #ifndef boost_python_numpy_ndarray_hpp_
0008 #define boost_python_numpy_ndarray_hpp_
0009 
0010 
0011 
0012 
0013 
0014 #include <boost/python.hpp>
0015 #include <boost/utility/enable_if.hpp>
0016 #include <boost/python/detail/type_traits.hpp>
0017 #include <boost/python/numpy/numpy_object_mgr_traits.hpp>
0018 #include <boost/python/numpy/dtype.hpp>
0019 #include <boost/python/numpy/config.hpp>
0020 
0021 #include <vector>
0022 
0023 namespace boost { namespace python { namespace numpy {
0024 
0025 
0026 
0027 
0028 
0029 
0030 
0031  
0032 class BOOST_NUMPY_DECL ndarray : public object
0033 {
0034 
0035   
0036 
0037 
0038 
0039 
0040 
0041   struct array_struct 
0042   {
0043     PyObject_HEAD
0044     char * data;
0045     int nd;
0046     Py_intptr_t * shape;
0047     Py_intptr_t * strides;
0048     PyObject * base;
0049     PyObject * descr;
0050     int flags;
0051     PyObject * weakreflist;
0052   };
0053   
0054   
0055   array_struct * get_struct() const { return reinterpret_cast<array_struct*>(this->ptr()); }
0056 
0057 public:
0058   
0059   
0060 
0061 
0062 
0063 
0064 
0065 
0066 
0067 
0068 
0069   enum bitflag 
0070   {
0071     NONE=0x0, C_CONTIGUOUS=0x1, F_CONTIGUOUS=0x2, V_CONTIGUOUS=0x1|0x2, 
0072     ALIGNED=0x4, WRITEABLE=0x8, BEHAVED=0x4|0x8,
0073     CARRAY_RO=0x1|0x4, CARRAY=0x1|0x4|0x8, CARRAY_MIS=0x1|0x8,
0074     FARRAY_RO=0x2|0x4, FARRAY=0x2|0x4|0x8, FARRAY_MIS=0x2|0x8,
0075     UPDATE_ALL=0x1|0x2|0x4, VARRAY=0x1|0x2|0x8, ALL=0x1|0x2|0x4|0x8
0076   };
0077 
0078   BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(ndarray, object);
0079 
0080   
0081   ndarray view(dtype const & dt) const;
0082     
0083   
0084   ndarray astype(dtype const & dt) const;
0085 
0086   
0087   ndarray copy() const;
0088 
0089   
0090   Py_intptr_t shape(int n) const;
0091 
0092   
0093   Py_intptr_t strides(int n) const;
0094     
0095   
0096 
0097 
0098 
0099 
0100 
0101   char * get_data() const { return get_struct()->data; }
0102 
0103   
0104   dtype get_dtype() const;
0105   
0106   
0107   object get_base() const;
0108   
0109   
0110   void set_base(object const & base);
0111   
0112   
0113   Py_intptr_t const * get_shape() const { return get_struct()->shape; }
0114   
0115   
0116   Py_intptr_t const * get_strides() const { return get_struct()->strides; }
0117   
0118   
0119   int get_nd() const { return get_struct()->nd; }
0120   
0121   
0122   bitflag get_flags() const;
0123   
0124   
0125   ndarray transpose() const;
0126   
0127   
0128   ndarray squeeze() const;
0129   
0130   
0131   ndarray reshape(python::tuple const & shape) const;
0132   
0133   
0134 
0135 
0136 
0137 
0138 
0139   object scalarize() const;
0140 };
0141 
0142 
0143 
0144 
0145 BOOST_NUMPY_DECL ndarray zeros(python::tuple const & shape, dtype const & dt);
0146 BOOST_NUMPY_DECL ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt);
0147 
0148 
0149 
0150 
0151 BOOST_NUMPY_DECL ndarray empty(python::tuple const & shape, dtype const & dt);
0152 BOOST_NUMPY_DECL ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt);
0153 
0154 
0155 
0156 
0157 
0158 
0159 BOOST_NUMPY_DECL ndarray array(object const & obj);
0160 BOOST_NUMPY_DECL ndarray array(object const & obj, dtype const & dt);
0161 
0162 namespace detail 
0163 {
0164 
0165 BOOST_NUMPY_DECL ndarray from_data_impl(void * data,
0166                     dtype const & dt,
0167                     std::vector<Py_intptr_t> const & shape,
0168                     std::vector<Py_intptr_t> const & strides,
0169                     object const & owner,
0170                     bool writeable);
0171 
0172 template <typename Container>
0173 ndarray from_data_impl(void * data,
0174                dtype const & dt,
0175                Container shape,
0176                Container strides,
0177                object const & owner,
0178                bool writeable,
0179                typename boost::enable_if< boost::python::detail::is_integral<typename Container::value_type> >::type * enabled = NULL)
0180 {
0181   std::vector<Py_intptr_t> shape_(shape.begin(),shape.end());
0182   std::vector<Py_intptr_t> strides_(strides.begin(), strides.end());
0183   return from_data_impl(data, dt, shape_, strides_, owner, writeable);    
0184 }
0185 
0186 BOOST_NUMPY_DECL ndarray from_data_impl(void * data,
0187                     dtype const & dt,
0188                     object const & shape,
0189                     object const & strides,
0190                     object const & owner,
0191                     bool writeable);
0192 
0193 } 
0194 
0195 
0196 
0197 
0198 
0199 
0200 
0201 
0202 
0203 
0204 
0205 
0206 
0207 
0208 template <typename Container>
0209 inline ndarray from_data(void * data,
0210              dtype const & dt,
0211              Container shape,
0212              Container strides,
0213              python::object const & owner)
0214 {
0215   return numpy::detail::from_data_impl(data, dt, shape, strides, owner, true);
0216 }    
0217 
0218 
0219 
0220 
0221 
0222 
0223 
0224 
0225 
0226 
0227 
0228 
0229 
0230 
0231 
0232 
0233 template <typename Container>
0234 inline ndarray from_data(void const * data,
0235              dtype const & dt,
0236              Container shape,
0237              Container strides,
0238              python::object const & owner)
0239 {
0240   return numpy::detail::from_data_impl(const_cast<void*>(data), dt, shape, strides, owner, false);
0241 }    
0242 
0243 
0244 
0245 
0246 
0247 
0248 
0249 
0250 
0251 
0252 
0253 BOOST_NUMPY_DECL ndarray from_object(object const & obj,
0254                      dtype const & dt,
0255                      int nd_min,
0256                      int nd_max,
0257                      ndarray::bitflag flags=ndarray::NONE);
0258 
0259 BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
0260                         dtype const & dt,
0261                         int nd,
0262                         ndarray::bitflag flags=ndarray::NONE)
0263 {
0264   return from_object(obj, dt, nd, nd, flags);
0265 }
0266 
0267 BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
0268                         dtype const & dt,
0269                         ndarray::bitflag flags=ndarray::NONE)
0270 {
0271   return from_object(obj, dt, 0, 0, flags);
0272 }
0273 
0274 BOOST_NUMPY_DECL ndarray from_object(object const & obj,
0275                      int nd_min,
0276                      int nd_max,
0277                      ndarray::bitflag flags=ndarray::NONE);
0278 
0279 BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
0280                         int nd,
0281                         ndarray::bitflag flags=ndarray::NONE)
0282 {
0283   return from_object(obj, nd, nd, flags);
0284 }
0285 
0286 BOOST_NUMPY_DECL inline ndarray from_object(object const & obj,
0287                         ndarray::bitflag flags=ndarray::NONE)
0288 {
0289   return from_object(obj, 0, 0, flags);
0290 }
0291 
0292 BOOST_NUMPY_DECL inline ndarray::bitflag operator|(ndarray::bitflag a,
0293                            ndarray::bitflag b)
0294 {
0295   return ndarray::bitflag(int(a) | int(b));
0296 }
0297 
0298 BOOST_NUMPY_DECL inline ndarray::bitflag operator&(ndarray::bitflag a,
0299                            ndarray::bitflag b)
0300 {
0301   return ndarray::bitflag(int(a) & int(b));
0302 }
0303 
0304 } 
0305 
0306 namespace converter 
0307 {
0308 
0309 NUMPY_OBJECT_MANAGER_TRAITS(numpy::ndarray);
0310 
0311 }}} 
0312 
0313 #endif