]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/src/numeric.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / python / src / numeric.cpp
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 PyTypeObject const* array_object_manager_traits::get_pytype()
118 {
119 load(false);
120 if(!array_type) return 0;
121 return downcast<PyTypeObject>(array_type.get());
122 }
123
124 # define BOOST_PYTHON_AS_OBJECT(z, n, _) object(x##n)
125 # define BOOST_PP_LOCAL_MACRO(n) \
126 array_base::array_base(BOOST_PP_ENUM_PARAMS(n, object const& x)) \
127 : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(n, x))) \
128 {}
129 # define BOOST_PP_LOCAL_LIMITS (1, 6)
130 # include BOOST_PP_LOCAL_ITERATE()
131 # undef BOOST_PYTHON_AS_OBJECT
132
133 array_base::array_base(BOOST_PP_ENUM_PARAMS(7, object const& x))
134 : object(demand_array_function()(BOOST_PP_ENUM_PARAMS(7, x)))
135 {}
136
137 object array_base::argmax(long axis)
138 {
139 return attr("argmax")(axis);
140 }
141
142 object array_base::argmin(long axis)
143 {
144 return attr("argmin")(axis);
145 }
146
147 object array_base::argsort(long axis)
148 {
149 return attr("argsort")(axis);
150 }
151
152 object array_base::astype(object const& type)
153 {
154 return attr("astype")(type);
155 }
156
157 void array_base::byteswap()
158 {
159 attr("byteswap")();
160 }
161
162 object array_base::copy() const
163 {
164 return attr("copy")();
165 }
166
167 object array_base::diagonal(long offset, long axis1, long axis2) const
168 {
169 return attr("diagonal")(offset, axis1, axis2);
170 }
171
172 void array_base::info() const
173 {
174 attr("info")();
175 }
176
177 bool array_base::is_c_array() const
178 {
179 return extract<bool>(attr("is_c_array")());
180 }
181
182 bool array_base::isbyteswapped() const
183 {
184 return extract<bool>(attr("isbyteswapped")());
185 }
186
187 array array_base::new_(object type) const
188 {
189 return extract<array>(attr("new")(type))();
190 }
191
192 void array_base::sort()
193 {
194 attr("sort")();
195 }
196
197 object array_base::trace(long offset, long axis1, long axis2) const
198 {
199 return attr("trace")(offset, axis1, axis2);
200 }
201
202 object array_base::type() const
203 {
204 return attr("type")();
205 }
206
207 char array_base::typecode() const
208 {
209 return extract<char>(attr("typecode")());
210 }
211
212 object array_base::factory(
213 object const& sequence
214 , object const& typecode
215 , bool copy
216 , bool savespace
217 , object type
218 , object shape
219 )
220 {
221 return attr("factory")(sequence, typecode, copy, savespace, type, shape);
222 }
223
224 object array_base::getflat() const
225 {
226 return attr("getflat")();
227 }
228
229 long array_base::getrank() const
230 {
231 return extract<long>(attr("getrank")());
232 }
233
234 object array_base::getshape() const
235 {
236 return attr("getshape")();
237 }
238
239 bool array_base::isaligned() const
240 {
241 return extract<bool>(attr("isaligned")());
242 }
243
244 bool array_base::iscontiguous() const
245 {
246 return extract<bool>(attr("iscontiguous")());
247 }
248
249 long array_base::itemsize() const
250 {
251 return extract<long>(attr("itemsize")());
252 }
253
254 long array_base::nelements() const
255 {
256 return extract<long>(attr("nelements")());
257 }
258
259 object array_base::nonzero() const
260 {
261 return attr("nonzero")();
262 }
263
264 void array_base::put(object const& indices, object const& values)
265 {
266 attr("put")(indices, values);
267 }
268
269 void array_base::ravel()
270 {
271 attr("ravel")();
272 }
273
274 object array_base::repeat(object const& repeats, long axis)
275 {
276 return attr("repeat")(repeats, axis);
277 }
278
279 void array_base::resize(object const& shape)
280 {
281 attr("resize")(shape);
282 }
283
284 void array_base::setflat(object const& flat)
285 {
286 attr("setflat")(flat);
287 }
288
289 void array_base::setshape(object const& shape)
290 {
291 attr("setshape")(shape);
292 }
293
294 void array_base::swapaxes(long axis1, long axis2)
295 {
296 attr("swapaxes")(axis1, axis2);
297 }
298
299 object array_base::take(object const& sequence, long axis) const
300 {
301 return attr("take")(sequence, axis);
302 }
303
304 void array_base::tofile(object const& file) const
305 {
306 attr("tofile")(file);
307 }
308
309 str array_base::tostring() const
310 {
311 return str(attr("tostring")());
312 }
313
314 void array_base::transpose(object const& axes)
315 {
316 attr("transpose")(axes);
317 }
318
319 object array_base::view() const
320 {
321 return attr("view")();
322 }
323 }
324
325 }}} // namespace boost::python::numeric