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
8 # include <boost/python/detail/prefix.hpp>
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>
18 namespace boost { namespace python { namespace numeric {
24 struct BOOST_PYTHON_DECL array_base : object
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()
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());
37 object diagonal(long offset = 0, long axis1 = 0, long axis2 = 1) const;
39 bool is_c_array() const;
40 bool isbyteswapped() const;
41 array new_(object type) const;
43 object trace(long offset = 0, long axis1 = 0, long axis2 = 1) const;
45 char typecode() const;
48 object const& sequence = object()
49 , object const& typecode = object()
51 , bool savespace = false
52 , object type = object()
53 , object shape = object());
55 object getflat() 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;
64 void put(object const& indices, object const& values);
68 object repeat(object const& repeats, long axis=0);
70 void resize(object const& shape);
72 void setflat(object const& flat);
73 void setshape(object const& shape);
75 void swapaxes(long axis1, long axis2);
77 object take(object const& sequence, long axis = 0) const;
79 void tofile(object const& file) const;
83 void transpose(object const& axes = object());
87 public: // implementation detail - do not touch.
88 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array_base, object);
91 struct BOOST_PYTHON_DECL array_object_manager_traits
93 static bool check(PyObject* obj);
94 static detail::new_non_null_reference adopt(PyObject* obj);
95 static PyTypeObject const* get_pytype() ;
99 class array : public aux::array_base
101 typedef aux::array_base base;
104 object astype() { return base::astype(); }
106 template <class Type>
107 object astype(Type const& type_)
109 return base::astype(object(type_));
112 template <class Type>
113 array new_(Type const& type_) const
115 return base::new_(object(type_));
118 template <class Sequence>
119 void resize(Sequence const& x)
121 base::resize(object(x));
124 # define BOOST_PP_LOCAL_MACRO(n) \
125 void resize(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \
127 resize(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \
129 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
130 # include BOOST_PP_LOCAL_ITERATE()
132 template <class Sequence>
133 void setshape(Sequence const& x)
135 base::setshape(object(x));
138 # define BOOST_PP_LOCAL_MACRO(n) \
139 void setshape(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \
141 setshape(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \
143 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
144 # include BOOST_PP_LOCAL_ITERATE()
146 template <class Indices, class Values>
147 void put(Indices const& indices, Values const& values)
149 base::put(object(indices), object(values));
152 template <class Sequence>
153 object take(Sequence const& sequence, long axis = 0)
155 return base::take(object(sequence), axis);
158 template <class File>
159 void tofile(File const& f) const
161 base::tofile(object(f));
166 return base::factory();
169 template <class Sequence>
170 object factory(Sequence const& sequence)
172 return base::factory(object(sequence));
175 template <class Sequence, class Typecode>
177 Sequence const& sequence
178 , Typecode const& typecode_
180 , bool savespace = false
183 return base::factory(object(sequence), object(typecode_), copy, savespace);
186 template <class Sequence, class Typecode, class Type>
188 Sequence const& sequence
189 , Typecode const& typecode_
195 return base::factory(object(sequence), object(typecode_), copy, savespace, object(type));
198 template <class Sequence, class Typecode, class Type, class Shape>
200 Sequence const& sequence
201 , Typecode const& typecode_
208 return base::factory(object(sequence), object(typecode_), copy, savespace, object(type), object(shape));
211 # define BOOST_PYTHON_ENUM_AS_OBJECT(z, n, x) object(BOOST_PP_CAT(x,n))
212 # define BOOST_PP_LOCAL_MACRO(n) \
213 template <BOOST_PP_ENUM_PARAMS_Z(1, n, class T)> \
214 explicit array(BOOST_PP_ENUM_BINARY_PARAMS_Z(1, n, T, const& x)) \
215 : base(BOOST_PP_ENUM_1(n, BOOST_PYTHON_ENUM_AS_OBJECT, x)) \
217 # define BOOST_PP_LOCAL_LIMITS (1, 7)
218 # include BOOST_PP_LOCAL_ITERATE()
219 # undef BOOST_PYTHON_AS_OBJECT
221 static BOOST_PYTHON_DECL void set_module_and_type(char const* package_name = 0, char const* type_attribute_name = 0);
222 static BOOST_PYTHON_DECL std::string get_module_name();
224 public: // implementation detail -- for internal use only
225 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, base);
228 } // namespace boost::python::numeric
233 struct object_manager_traits< numeric::array >
234 : numeric::aux::array_object_manager_traits
236 BOOST_STATIC_CONSTANT(bool, is_specialized = true);
240 }} // namespace boost::python
242 #endif // NUMARRAY_DWA2002922_HPP