]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | static PyTypeObject const* get_pytype() ; | |
96 | }; | |
97 | } // namespace aux | |
98 | ||
99 | class array : public aux::array_base | |
100 | { | |
101 | typedef aux::array_base base; | |
102 | public: | |
103 | ||
104 | object astype() { return base::astype(); } | |
105 | ||
106 | template <class Type> | |
107 | object astype(Type const& type_) | |
108 | { | |
109 | return base::astype(object(type_)); | |
110 | } | |
111 | ||
112 | template <class Type> | |
113 | array new_(Type const& type_) const | |
114 | { | |
115 | return base::new_(object(type_)); | |
116 | } | |
117 | ||
118 | template <class Sequence> | |
119 | void resize(Sequence const& x) | |
120 | { | |
121 | base::resize(object(x)); | |
122 | } | |
123 | ||
124 | # define BOOST_PP_LOCAL_MACRO(n) \ | |
125 | void resize(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \ | |
126 | { \ | |
127 | resize(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \ | |
128 | } | |
129 | # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY) | |
130 | # include BOOST_PP_LOCAL_ITERATE() | |
131 | ||
132 | template <class Sequence> | |
133 | void setshape(Sequence const& x) | |
134 | { | |
135 | base::setshape(object(x)); | |
136 | } | |
137 | ||
138 | # define BOOST_PP_LOCAL_MACRO(n) \ | |
139 | void setshape(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \ | |
140 | { \ | |
141 | setshape(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \ | |
142 | } | |
143 | # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY) | |
144 | # include BOOST_PP_LOCAL_ITERATE() | |
145 | ||
146 | template <class Indices, class Values> | |
147 | void put(Indices const& indices, Values const& values) | |
148 | { | |
149 | base::put(object(indices), object(values)); | |
150 | } | |
151 | ||
152 | template <class Sequence> | |
153 | object take(Sequence const& sequence, long axis = 0) | |
154 | { | |
155 | return base::take(object(sequence), axis); | |
156 | } | |
157 | ||
158 | template <class File> | |
159 | void tofile(File const& f) const | |
160 | { | |
161 | base::tofile(object(f)); | |
162 | } | |
163 | ||
164 | object factory() | |
165 | { | |
166 | return base::factory(); | |
167 | } | |
168 | ||
169 | template <class Sequence> | |
170 | object factory(Sequence const& sequence) | |
171 | { | |
172 | return base::factory(object(sequence)); | |
173 | } | |
174 | ||
175 | template <class Sequence, class Typecode> | |
176 | object factory( | |
177 | Sequence const& sequence | |
178 | , Typecode const& typecode_ | |
179 | , bool copy = true | |
180 | , bool savespace = false | |
181 | ) | |
182 | { | |
183 | return base::factory(object(sequence), object(typecode_), copy, savespace); | |
184 | } | |
185 | ||
186 | template <class Sequence, class Typecode, class Type> | |
187 | object factory( | |
188 | Sequence const& sequence | |
189 | , Typecode const& typecode_ | |
190 | , bool copy | |
191 | , bool savespace | |
192 | , Type const& type | |
193 | ) | |
194 | { | |
195 | return base::factory(object(sequence), object(typecode_), copy, savespace, object(type)); | |
196 | } | |
197 | ||
198 | template <class Sequence, class Typecode, class Type, class Shape> | |
199 | object factory( | |
200 | Sequence const& sequence | |
201 | , Typecode const& typecode_ | |
202 | , bool copy | |
203 | , bool savespace | |
204 | , Type const& type | |
205 | , Shape const& shape | |
206 | ) | |
207 | { | |
208 | return base::factory(object(sequence), object(typecode_), copy, savespace, object(type), object(shape)); | |
209 | } | |
210 | ||
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)) \ | |
216 | {} | |
217 | # define BOOST_PP_LOCAL_LIMITS (1, 7) | |
218 | # include BOOST_PP_LOCAL_ITERATE() | |
219 | # undef BOOST_PYTHON_AS_OBJECT | |
220 | ||
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(); | |
223 | ||
224 | public: // implementation detail -- for internal use only | |
225 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, base); | |
226 | }; | |
227 | ||
228 | } // namespace boost::python::numeric | |
229 | ||
230 | namespace converter | |
231 | { | |
232 | template <> | |
233 | struct object_manager_traits< numeric::array > | |
234 | : numeric::aux::array_object_manager_traits | |
235 | { | |
236 | BOOST_STATIC_CONSTANT(bool, is_specialized = true); | |
237 | }; | |
238 | } | |
239 | ||
240 | }} // namespace boost::python | |
241 | ||
242 | #endif // NUMARRAY_DWA2002922_HPP |