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 | |
---|
6 | #include <boost/python/numeric.hpp> |
---|
7 | #include <boost/python/handle.hpp> |
---|
8 | #include <boost/python/cast.hpp> |
---|
9 | #include <boost/python/tuple.hpp> |
---|
10 | #include <boost/python/detail/raw_pyobject.hpp> |
---|
11 | #include <boost/python/extract.hpp> |
---|
12 | |
---|
13 | namespace boost { namespace python { namespace numeric { |
---|
14 | |
---|
15 | namespace |
---|
16 | { |
---|
17 | enum state_t { failed = -1, unknown, succeeded }; |
---|
18 | state_t state = unknown; |
---|
19 | std::string module_name; |
---|
20 | std::string type_name; |
---|
21 | |
---|
22 | handle<> array_module; |
---|
23 | handle<> array_type; |
---|
24 | handle<> array_function; |
---|
25 | |
---|
26 | void throw_load_failure() |
---|
27 | { |
---|
28 | PyErr_Format( |
---|
29 | PyExc_ImportError |
---|
30 | , "No module named '%s' or its type '%s' did not follow the NumPy protocol" |
---|
31 | , module_name.c_str(), type_name.c_str()); |
---|
32 | throw_error_already_set(); |
---|
33 | |
---|
34 | } |
---|
35 | |
---|
36 | bool load(bool throw_on_error) |
---|
37 | { |
---|
38 | if (!state) |
---|
39 | { |
---|
40 | if (module_name.size() == 0) |
---|
41 | { |
---|
42 | module_name = "numarray"; |
---|
43 | type_name = "NDArray"; |
---|
44 | if (load(false)) |
---|
45 | return true; |
---|
46 | module_name = "Numeric"; |
---|
47 | type_name = "ArrayType"; |
---|
48 | } |
---|
49 | |
---|
50 | state = failed; |
---|
51 | PyObject* module = ::PyImport_Import(object(module_name).ptr()); |
---|
52 | if (module) |
---|
53 | { |
---|
54 | PyObject* type = ::PyObject_GetAttrString(module, const_cast<char*>(type_name.c_str())); |
---|
55 | |
---|
56 | if (type && PyType_Check(type)) |
---|
57 | { |
---|
58 | array_type = handle<>(type); |
---|
59 | PyObject* function = ::PyObject_GetAttrString(module, const_cast<char*>("array")); |
---|
60 | |
---|
61 | if (function && PyCallable_Check(function)) |
---|
62 | { |
---|
63 | array_function = handle<>(function); |
---|
64 | state = succeeded; |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | if (state == succeeded) |
---|
71 | return true; |
---|
72 | |
---|
73 | if (throw_on_error) |
---|
74 | throw_load_failure(); |
---|
75 | |
---|
76 | PyErr_Clear(); |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | object demand_array_function() |
---|
81 | { |
---|
82 | load(true); |
---|
83 | return object(array_function); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void array::set_module_and_type(char const* package_name, char const* type_attribute_name) |
---|
88 | { |
---|
89 | state = unknown; |
---|
90 | module_name = package_name ? package_name : "" ; |
---|
91 | type_name = type_attribute_name ? type_attribute_name : "" ; |
---|
92 | } |
---|
93 | |
---|
94 | std::string array::get_module_name() |
---|
95 | { |
---|
96 | load(false); |
---|
97 | return module_name; |
---|
98 | } |
---|
99 | |
---|
100 | namespace aux |
---|
101 | { |
---|
102 | bool array_object_manager_traits::check(PyObject* obj) |
---|
103 | { |
---|
104 | if (!load(false)) |
---|
105 | return false; |
---|
106 | return ::PyObject_IsInstance(obj, array_type.get()); |
---|
107 | } |
---|
108 | |
---|
109 | python::detail::new_non_null_reference |
---|
110 | array_object_manager_traits::adopt(PyObject* obj) |
---|
111 | { |
---|
112 | load(true); |
---|
113 | return detail::new_non_null_reference( |
---|
114 | pytype_check(downcast<PyTypeObject>(array_type.get()), obj)); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | # define BOOST_PYTHON_AS_OBJECT(z, n, _) object(x##n) |
---|
119 | # define BOOST_PP_LOCAL_MACRO(n) \ |
---|
120 | array_base::array_base(BOOST_PP_ENUM_PARAMS(n, object const& x)) \ |
---|
121 | : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(n, x))) \ |
---|
122 | {} |
---|
123 | # define BOOST_PP_LOCAL_LIMITS (1, 6) |
---|
124 | # include BOOST_PP_LOCAL_ITERATE() |
---|
125 | # undef BOOST_PYTHON_AS_OBJECT |
---|
126 | |
---|
127 | array_base::array_base(BOOST_PP_ENUM_PARAMS(7, object const& x)) |
---|
128 | : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(7, x))) |
---|
129 | {} |
---|
130 | |
---|
131 | object array_base::argmax(long axis) |
---|
132 | { |
---|
133 | return attr("argmax")(axis); |
---|
134 | } |
---|
135 | |
---|
136 | object array_base::argmin(long axis) |
---|
137 | { |
---|
138 | return attr("argmin")(axis); |
---|
139 | } |
---|
140 | |
---|
141 | object array_base::argsort(long axis) |
---|
142 | { |
---|
143 | return attr("argsort")(axis); |
---|
144 | } |
---|
145 | |
---|
146 | object array_base::astype(object const& type) |
---|
147 | { |
---|
148 | return attr("astype")(type); |
---|
149 | } |
---|
150 | |
---|
151 | void array_base::byteswap() |
---|
152 | { |
---|
153 | attr("byteswap")(); |
---|
154 | } |
---|
155 | |
---|
156 | object array_base::copy() const |
---|
157 | { |
---|
158 | return attr("copy")(); |
---|
159 | } |
---|
160 | |
---|
161 | object array_base::diagonal(long offset, long axis1, long axis2) const |
---|
162 | { |
---|
163 | return attr("diagonal")(offset, axis1, axis2); |
---|
164 | } |
---|
165 | |
---|
166 | void array_base::info() const |
---|
167 | { |
---|
168 | attr("info")(); |
---|
169 | } |
---|
170 | |
---|
171 | bool array_base::is_c_array() const |
---|
172 | { |
---|
173 | return extract<bool>(attr("is_c_array")()); |
---|
174 | } |
---|
175 | |
---|
176 | bool array_base::isbyteswapped() const |
---|
177 | { |
---|
178 | return extract<bool>(attr("isbyteswapped")()); |
---|
179 | } |
---|
180 | |
---|
181 | array array_base::new_(object type) const |
---|
182 | { |
---|
183 | return extract<array>(attr("new")(type))(); |
---|
184 | } |
---|
185 | |
---|
186 | void array_base::sort() |
---|
187 | { |
---|
188 | attr("sort")(); |
---|
189 | } |
---|
190 | |
---|
191 | object array_base::trace(long offset, long axis1, long axis2) const |
---|
192 | { |
---|
193 | return attr("trace")(offset, axis1, axis2); |
---|
194 | } |
---|
195 | |
---|
196 | object array_base::type() const |
---|
197 | { |
---|
198 | return attr("type")(); |
---|
199 | } |
---|
200 | |
---|
201 | char array_base::typecode() const |
---|
202 | { |
---|
203 | return extract<char>(attr("typecode")()); |
---|
204 | } |
---|
205 | |
---|
206 | object array_base::factory( |
---|
207 | object const& sequence |
---|
208 | , object const& typecode |
---|
209 | , bool copy |
---|
210 | , bool savespace |
---|
211 | , object type |
---|
212 | , object shape |
---|
213 | ) |
---|
214 | { |
---|
215 | return attr("factory")(sequence, typecode, copy, savespace, type, shape); |
---|
216 | } |
---|
217 | |
---|
218 | object array_base::getflat() const |
---|
219 | { |
---|
220 | return attr("getflat")(); |
---|
221 | } |
---|
222 | |
---|
223 | long array_base::getrank() const |
---|
224 | { |
---|
225 | return extract<long>(attr("getrank")()); |
---|
226 | } |
---|
227 | |
---|
228 | object array_base::getshape() const |
---|
229 | { |
---|
230 | return attr("getshape")(); |
---|
231 | } |
---|
232 | |
---|
233 | bool array_base::isaligned() const |
---|
234 | { |
---|
235 | return extract<bool>(attr("isaligned")()); |
---|
236 | } |
---|
237 | |
---|
238 | bool array_base::iscontiguous() const |
---|
239 | { |
---|
240 | return extract<bool>(attr("iscontiguous")()); |
---|
241 | } |
---|
242 | |
---|
243 | long array_base::itemsize() const |
---|
244 | { |
---|
245 | return extract<long>(attr("itemsize")()); |
---|
246 | } |
---|
247 | |
---|
248 | long array_base::nelements() const |
---|
249 | { |
---|
250 | return extract<long>(attr("nelements")()); |
---|
251 | } |
---|
252 | |
---|
253 | object array_base::nonzero() const |
---|
254 | { |
---|
255 | return attr("nonzero")(); |
---|
256 | } |
---|
257 | |
---|
258 | void array_base::put(object const& indices, object const& values) |
---|
259 | { |
---|
260 | attr("put")(indices, values); |
---|
261 | } |
---|
262 | |
---|
263 | void array_base::ravel() |
---|
264 | { |
---|
265 | attr("ravel")(); |
---|
266 | } |
---|
267 | |
---|
268 | object array_base::repeat(object const& repeats, long axis) |
---|
269 | { |
---|
270 | return attr("repeat")(repeats, axis); |
---|
271 | } |
---|
272 | |
---|
273 | void array_base::resize(object const& shape) |
---|
274 | { |
---|
275 | attr("resize")(shape); |
---|
276 | } |
---|
277 | |
---|
278 | void array_base::setflat(object const& flat) |
---|
279 | { |
---|
280 | attr("setflat")(flat); |
---|
281 | } |
---|
282 | |
---|
283 | void array_base::setshape(object const& shape) |
---|
284 | { |
---|
285 | attr("setshape")(shape); |
---|
286 | } |
---|
287 | |
---|
288 | void array_base::swapaxes(long axis1, long axis2) |
---|
289 | { |
---|
290 | attr("swapaxes")(axis1, axis2); |
---|
291 | } |
---|
292 | |
---|
293 | object array_base::take(object const& sequence, long axis) const |
---|
294 | { |
---|
295 | return attr("take")(sequence, axis); |
---|
296 | } |
---|
297 | |
---|
298 | void array_base::tofile(object const& file) const |
---|
299 | { |
---|
300 | attr("tofile")(file); |
---|
301 | } |
---|
302 | |
---|
303 | str array_base::tostring() const |
---|
304 | { |
---|
305 | return str(attr("tostring")()); |
---|
306 | } |
---|
307 | |
---|
308 | void array_base::transpose(object const& axes) |
---|
309 | { |
---|
310 | attr("transpose")(axes); |
---|
311 | } |
---|
312 | |
---|
313 | object array_base::view() const |
---|
314 | { |
---|
315 | return attr("view")(); |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | }}} // namespace boost::python::numeric |
---|