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
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);
98 class array : public aux::array_base
100 typedef aux::array_base base;
103 object astype() { return base::astype(); }
105 template <class Type>
106 object astype(Type const& type_)
108 return base::astype(object(type_));
111 template <class Type>
112 array new_(Type const& type_) const
114 return base::new_(object(type_));
117 template <class Sequence>
118 void resize(Sequence const& x)
120 base::resize(object(x));
123 # define BOOST_PP_LOCAL_MACRO(n) \
124 void resize(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \
126 resize(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \
128 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
129 # include BOOST_PP_LOCAL_ITERATE()
131 template <class Sequence>
132 void setshape(Sequence const& x)
134 base::setshape(object(x));
137 # define BOOST_PP_LOCAL_MACRO(n) \
138 void setshape(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \
140 setshape(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \
142 # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
143 # include BOOST_PP_LOCAL_ITERATE()
145 template <class Indices, class Values>
146 void put(Indices const& indices, Values const& values)
148 base::put(object(indices), object(values));
151 template <class Sequence>
152 object take(Sequence const& sequence, long axis = 0)
154 return base::take(object(sequence), axis);
157 template <class File>
158 void tofile(File const& f) const
160 base::tofile(object(f));
165 return base::factory();
168 template <class Sequence>
169 object factory(Sequence const& sequence)
171 return base::factory(object(sequence));
174 template <class Sequence, class Typecode>
176 Sequence const& sequence
177 , Typecode const& typecode_
179 , bool savespace = false
182 return base::factory(object(sequence), object(typecode_), copy, savespace);
185 template <class Sequence, class Typecode, class Type>
187 Sequence const& sequence
188 , Typecode const& typecode_
194 return base::factory(object(sequence), object(typecode_), copy, savespace, object(type));
197 template <class Sequence, class Typecode, class Type, class Shape>
199 Sequence const& sequence
200 , Typecode const& typecode_
207 return base::factory(object(sequence), object(typecode_), copy, savespace, object(type), object(shape));
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)) \
216 # define BOOST_PP_LOCAL_LIMITS (1, 7)
217 # include BOOST_PP_LOCAL_ITERATE()
218 # undef BOOST_PYTHON_AS_OBJECT
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();
223 public: // implementation detail -- for internal use only
224 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, base);
227 } // namespace boost::python::numeric
232 struct object_manager_traits< numeric::array >
233 : numeric::aux::array_object_manager_traits
235 BOOST_STATIC_CONSTANT(bool, is_specialized = true);
239 }} // namespace boost::python
241 #endif // NUMARRAY_DWA2002922_HPP