os/ossrv/ossrv_pub/boost_apis/boost/python/numeric.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright David Abrahams 2002.
     2 // Distributed under the Boost Software License, Version 1.0. (See
     3 // accompanying file LICENSE_1_0.txt or copy at
     4 // http://www.boost.org/LICENSE_1_0.txt)
     5 #ifndef NUMARRAY_DWA2002922_HPP
     6 # define NUMARRAY_DWA2002922_HPP
     7 
     8 # include <boost/python/detail/prefix.hpp>
     9 
    10 # include <boost/python/tuple.hpp>
    11 # include <boost/python/str.hpp>
    12 # include <boost/preprocessor/iteration/local.hpp>
    13 # include <boost/preprocessor/cat.hpp>
    14 # include <boost/preprocessor/repetition/enum.hpp>
    15 # include <boost/preprocessor/repetition/enum_params.hpp>
    16 # include <boost/preprocessor/repetition/enum_binary_params.hpp>
    17 
    18 namespace boost { namespace python { namespace numeric {
    19 
    20 class array;
    21 
    22 namespace aux
    23 {
    24   struct BOOST_PYTHON_DECL array_base : object
    25   {
    26 # define BOOST_PP_LOCAL_MACRO(n)                                \
    27       array_base(BOOST_PP_ENUM_PARAMS_Z(1, n, object const& x));
    28 # define BOOST_PP_LOCAL_LIMITS (1, 7)
    29 # include BOOST_PP_LOCAL_ITERATE()
    30 
    31       object argmax(long axis=-1);
    32       object argmin(long axis=-1);
    33       object argsort(long axis=-1);
    34       object astype(object const& type = object());
    35       void byteswap();
    36       object copy() const;
    37       object diagonal(long offset = 0, long axis1 = 0, long axis2 = 1) const;
    38       void info() const;
    39       bool is_c_array() const;
    40       bool isbyteswapped() const;
    41       array new_(object type) const;
    42       void sort();
    43       object trace(long offset = 0, long axis1 = 0, long axis2 = 1) const;
    44       object type() const;
    45       char typecode() const;
    46 
    47       object factory(
    48           object const& sequence = object()
    49         , object const& typecode = object()
    50         , bool copy = true
    51         , bool savespace = false
    52         , object type = object()
    53         , object shape = object());
    54 
    55       object getflat() const;
    56       long getrank() const;
    57       object getshape() const;
    58       bool isaligned() const;
    59       bool iscontiguous() const;
    60       long itemsize() const;
    61       long nelements() const;
    62       object nonzero() const;
    63    
    64       void put(object const& indices, object const& values);
    65    
    66       void ravel();
    67    
    68       object repeat(object const& repeats, long axis=0);
    69    
    70       void resize(object const& shape);
    71       
    72       void setflat(object const& flat);
    73       void setshape(object const& shape);
    74    
    75       void swapaxes(long axis1, long axis2);
    76    
    77       object take(object const& sequence, long axis = 0) const;
    78    
    79       void tofile(object const& file) const;
    80    
    81       str tostring() const;
    82    
    83       void transpose(object const& axes = object());
    84    
    85       object view() const;
    86 
    87    public: // implementation detail - do not touch.
    88       BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array_base, object);
    89   };
    90 
    91   struct BOOST_PYTHON_DECL array_object_manager_traits
    92   {
    93       static bool check(PyObject* obj);
    94       static detail::new_non_null_reference adopt(PyObject* obj);
    95   };
    96 } // namespace aux
    97 
    98 class array : public aux::array_base
    99 {
   100     typedef aux::array_base base;
   101  public:
   102 
   103     object astype() { return base::astype(); }
   104     
   105     template <class Type>
   106     object astype(Type const& type_)
   107     {
   108         return base::astype(object(type_));
   109     }
   110 
   111     template <class Type>
   112     array new_(Type const& type_) const
   113     {
   114         return base::new_(object(type_));
   115     }
   116 
   117     template <class Sequence>
   118     void resize(Sequence const& x)
   119     {
   120         base::resize(object(x));
   121     }
   122     
   123 # define BOOST_PP_LOCAL_MACRO(n)                                \
   124       void resize(BOOST_PP_ENUM_PARAMS_Z(1, n, long x))              \
   125       {                                                         \
   126           resize(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x)));       \
   127       }
   128 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
   129 # include BOOST_PP_LOCAL_ITERATE()
   130 
   131     template <class Sequence>
   132     void setshape(Sequence const& x)
   133     {
   134         base::setshape(object(x));
   135     }
   136     
   137 # define BOOST_PP_LOCAL_MACRO(n)                                \
   138     void setshape(BOOST_PP_ENUM_PARAMS_Z(1, n, long x))              \
   139     {                                                           \
   140         setshape(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x)));       \
   141     }
   142 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
   143 # include BOOST_PP_LOCAL_ITERATE()
   144 
   145     template <class Indices, class Values>
   146     void put(Indices const& indices, Values const& values)
   147     {
   148         base::put(object(indices), object(values));
   149     }
   150     
   151     template <class Sequence>
   152     object take(Sequence const& sequence, long axis = 0)
   153     {
   154         return base::take(object(sequence), axis);
   155     }
   156 
   157     template <class File>
   158     void tofile(File const& f) const
   159     {
   160         base::tofile(object(f));
   161     }
   162 
   163     object factory()
   164     {
   165         return base::factory();
   166     }
   167     
   168     template <class Sequence>
   169     object factory(Sequence const& sequence)
   170     {
   171         return base::factory(object(sequence));
   172     }
   173     
   174     template <class Sequence, class Typecode>
   175     object factory(
   176         Sequence const& sequence
   177       , Typecode const& typecode_
   178       , bool copy = true
   179       , bool savespace = false
   180     )
   181     {
   182         return base::factory(object(sequence), object(typecode_), copy, savespace);
   183     }
   184 
   185     template <class Sequence, class Typecode, class Type>
   186     object factory(
   187         Sequence const& sequence
   188       , Typecode const& typecode_
   189       , bool copy
   190       , bool savespace
   191       , Type const& type
   192     )
   193     {
   194         return base::factory(object(sequence), object(typecode_), copy, savespace, object(type));
   195     }
   196     
   197     template <class Sequence, class Typecode, class Type, class Shape>
   198     object factory(
   199         Sequence const& sequence
   200       , Typecode const& typecode_
   201       , bool copy
   202       , bool savespace
   203       , Type const& type
   204       , Shape const& shape
   205     )
   206     {
   207         return base::factory(object(sequence), object(typecode_), copy, savespace, object(type), object(shape));
   208     }
   209     
   210 # define BOOST_PYTHON_ENUM_AS_OBJECT(z, n, x) object(BOOST_PP_CAT(x,n))
   211 # define BOOST_PP_LOCAL_MACRO(n)                                        \
   212     template <BOOST_PP_ENUM_PARAMS_Z(1, n, class T)>                    \
   213     explicit array(BOOST_PP_ENUM_BINARY_PARAMS_Z(1, n, T, const& x))    \
   214     : base(BOOST_PP_ENUM_1(n, BOOST_PYTHON_ENUM_AS_OBJECT, x))          \
   215     {}
   216 # define BOOST_PP_LOCAL_LIMITS (1, 7)
   217 # include BOOST_PP_LOCAL_ITERATE()
   218 # undef BOOST_PYTHON_AS_OBJECT
   219 
   220     static BOOST_PYTHON_DECL void set_module_and_type(char const* package_name = 0, char const* type_attribute_name = 0);
   221     static BOOST_PYTHON_DECL std::string get_module_name();
   222 
   223  public: // implementation detail -- for internal use only
   224     BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, base);
   225 };
   226 
   227 } // namespace boost::python::numeric
   228 
   229 namespace converter
   230 {
   231   template <>
   232   struct object_manager_traits< numeric::array >
   233       : numeric::aux::array_object_manager_traits
   234   {
   235       BOOST_STATIC_CONSTANT(bool, is_specialized = true);
   236   };
   237 }
   238 
   239 }} // namespace boost::python
   240 
   241 #endif // NUMARRAY_DWA2002922_HPP