]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/src/numpy/ndarray.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / python / src / numpy / ndarray.cpp
1 // Copyright Jim Bosch 2010-2012.
2 // Copyright Stefan Seefeld 2016.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 #define BOOST_PYTHON_NUMPY_INTERNAL
8 #include <boost/python/numpy/internal.hpp>
9 #include <boost/scoped_array.hpp>
10
11 namespace boost { namespace python {
12 namespace converter
13 {
14 NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyArray_Type, numpy::ndarray)
15 } // namespace boost::python::converter
16
17 namespace numpy
18 {
19 namespace detail
20 {
21
22 ndarray::bitflag numpy_to_bitflag(int const f)
23 {
24 ndarray::bitflag r = ndarray::NONE;
25 if (f & NPY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);
26 if (f & NPY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS);
27 if (f & NPY_ALIGNED) r = (r | ndarray::ALIGNED);
28 if (f & NPY_WRITEABLE) r = (r | ndarray::WRITEABLE);
29 return r;
30 }
31
32 int bitflag_to_numpy(ndarray::bitflag f)
33 {
34 int r = 0;
35 if (f & ndarray::C_CONTIGUOUS) r |= NPY_C_CONTIGUOUS;
36 if (f & ndarray::F_CONTIGUOUS) r |= NPY_F_CONTIGUOUS;
37 if (f & ndarray::ALIGNED) r |= NPY_ALIGNED;
38 if (f & ndarray::WRITEABLE) r |= NPY_WRITEABLE;
39 return r;
40 }
41
42 bool is_c_contiguous(std::vector<Py_intptr_t> const & shape,
43 std::vector<Py_intptr_t> const & strides,
44 int itemsize)
45 {
46 std::vector<Py_intptr_t>::const_reverse_iterator j = strides.rbegin();
47 int total = itemsize;
48 for (std::vector<Py_intptr_t>::const_reverse_iterator i = shape.rbegin(); i != shape.rend(); ++i, ++j)
49 {
50 if (total != *j) return false;
51 total *= (*i);
52 }
53 return true;
54 }
55
56 bool is_f_contiguous(std::vector<Py_intptr_t> const & shape,
57 std::vector<Py_intptr_t> const & strides,
58 int itemsize)
59 {
60 std::vector<Py_intptr_t>::const_iterator j = strides.begin();
61 int total = itemsize;
62 for (std::vector<Py_intptr_t>::const_iterator i = shape.begin(); i != shape.end(); ++i, ++j)
63 {
64 if (total != *j) return false;
65 total *= (*i);
66 }
67 return true;
68 }
69
70 bool is_aligned(std::vector<Py_intptr_t> const & strides,
71 int itemsize)
72 {
73 for (std::vector<Py_intptr_t>::const_iterator i = strides.begin(); i != strides.end(); ++i)
74 {
75 if (*i % itemsize) return false;
76 }
77 return true;
78 }
79
80 inline PyArray_Descr * incref_dtype(dtype const & dt)
81 {
82 Py_INCREF(dt.ptr());
83 return reinterpret_cast<PyArray_Descr*>(dt.ptr());
84 }
85
86 ndarray from_data_impl(void * data,
87 dtype const & dt,
88 python::object const & shape,
89 python::object const & strides,
90 python::object const & owner,
91 bool writeable)
92 {
93 std::vector<Py_intptr_t> shape_(len(shape));
94 std::vector<Py_intptr_t> strides_(len(strides));
95 if (shape_.size() != strides_.size())
96 {
97 PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match.");
98 python::throw_error_already_set();
99 }
100 for (std::size_t i = 0; i < shape_.size(); ++i)
101 {
102 shape_[i] = python::extract<Py_intptr_t>(shape[i]);
103 strides_[i] = python::extract<Py_intptr_t>(strides[i]);
104 }
105 return from_data_impl(data, dt, shape_, strides_, owner, writeable);
106 }
107
108 ndarray from_data_impl(void * data,
109 dtype const & dt,
110 std::vector<Py_intptr_t> const & shape,
111 std::vector<Py_intptr_t> const & strides,
112 python::object const & owner,
113 bool writeable)
114 {
115 if (shape.size() != strides.size())
116 {
117 PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match.");
118 python::throw_error_already_set();
119 }
120 int itemsize = dt.get_itemsize();
121 int flags = 0;
122 if (writeable) flags |= NPY_WRITEABLE;
123 if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_C_CONTIGUOUS;
124 if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_F_CONTIGUOUS;
125 if (is_aligned(strides, itemsize)) flags |= NPY_ALIGNED;
126 ndarray r(python::detail::new_reference
127 (PyArray_NewFromDescr(&PyArray_Type,
128 incref_dtype(dt),
129 shape.size(),
130 const_cast<Py_intptr_t*>(&shape.front()),
131 const_cast<Py_intptr_t*>(&strides.front()),
132 data,
133 flags,
134 NULL)));
135 r.set_base(owner);
136 return r;
137 }
138
139 } // namespace detail
140
141 ndarray ndarray::view(dtype const & dt) const
142 {
143 return ndarray(python::detail::new_reference
144 (PyObject_CallMethod(this->ptr(), const_cast<char*>("view"), const_cast<char*>("O"), dt.ptr())));
145 }
146
147 ndarray ndarray::astype(dtype const & dt) const
148 {
149 return ndarray(python::detail::new_reference
150 (PyObject_CallMethod(this->ptr(), const_cast<char*>("astype"), const_cast<char*>("O"), dt.ptr())));
151 }
152
153 ndarray ndarray::copy() const
154 {
155 return ndarray(python::detail::new_reference
156 (PyObject_CallMethod(this->ptr(), const_cast<char*>("copy"), const_cast<char*>(""))));
157 }
158
159 dtype ndarray::get_dtype() const
160 {
161 return dtype(python::detail::borrowed_reference(get_struct()->descr));
162 }
163
164 python::object ndarray::get_base() const
165 {
166 if (get_struct()->base == NULL) return object();
167 return python::object(python::detail::borrowed_reference(get_struct()->base));
168 }
169
170 void ndarray::set_base(object const & base)
171 {
172 Py_XDECREF(get_struct()->base);
173 if (base != object())
174 {
175 Py_INCREF(base.ptr());
176 get_struct()->base = base.ptr();
177 }
178 else
179 {
180 get_struct()->base = NULL;
181 }
182 }
183
184 ndarray::bitflag ndarray::get_flags() const
185 {
186 return numpy::detail::numpy_to_bitflag(get_struct()->flags);
187 }
188
189 ndarray ndarray::transpose() const
190 {
191 return ndarray(python::detail::new_reference
192 (PyArray_Transpose(reinterpret_cast<PyArrayObject*>(this->ptr()), NULL)));
193 }
194
195 ndarray ndarray::squeeze() const
196 {
197 return ndarray(python::detail::new_reference
198 (PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(this->ptr()))));
199 }
200
201 ndarray ndarray::reshape(python::tuple const & shape) const
202 {
203 return ndarray(python::detail::new_reference
204 (PyArray_Reshape(reinterpret_cast<PyArrayObject*>(this->ptr()), shape.ptr())));
205 }
206
207 python::object ndarray::scalarize() const
208 {
209 Py_INCREF(ptr());
210 return python::object(python::detail::new_reference(PyArray_Return(reinterpret_cast<PyArrayObject*>(ptr()))));
211 }
212
213 ndarray zeros(python::tuple const & shape, dtype const & dt)
214 {
215 int nd = len(shape);
216 boost::scoped_array<Py_intptr_t> dims(new Py_intptr_t[nd]);
217 for (int n=0; n<nd; ++n) dims[n] = python::extract<Py_intptr_t>(shape[n]);
218 return ndarray(python::detail::new_reference
219 (PyArray_Zeros(nd, dims.get(), detail::incref_dtype(dt), 0)));
220 }
221
222 ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt)
223 {
224 return ndarray(python::detail::new_reference
225 (PyArray_Zeros(nd, const_cast<Py_intptr_t*>(shape), detail::incref_dtype(dt), 0)));
226 }
227
228 ndarray empty(python::tuple const & shape, dtype const & dt)
229 {
230 int nd = len(shape);
231 boost::scoped_array<Py_intptr_t> dims(new Py_intptr_t[nd]);
232 for (int n=0; n<nd; ++n) dims[n] = python::extract<Py_intptr_t>(shape[n]);
233 return ndarray(python::detail::new_reference
234 (PyArray_Empty(nd, dims.get(), detail::incref_dtype(dt), 0)));
235 }
236
237 ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt)
238 {
239 return ndarray(python::detail::new_reference
240 (PyArray_Empty(nd, const_cast<Py_intptr_t*>(shape), detail::incref_dtype(dt), 0)));
241 }
242
243 ndarray array(python::object const & obj)
244 {
245 return ndarray(python::detail::new_reference
246 (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ENSUREARRAY, NULL)));
247 }
248
249 ndarray array(python::object const & obj, dtype const & dt)
250 {
251 return ndarray(python::detail::new_reference
252 (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ENSUREARRAY, NULL)));
253 }
254
255 ndarray from_object(python::object const & obj, dtype const & dt, int nd_min, int nd_max, ndarray::bitflag flags)
256 {
257 int requirements = detail::bitflag_to_numpy(flags);
258 return ndarray(python::detail::new_reference
259 (PyArray_FromAny(obj.ptr(),
260 detail::incref_dtype(dt),
261 nd_min, nd_max,
262 requirements,
263 NULL)));
264 }
265
266 ndarray from_object(python::object const & obj, int nd_min, int nd_max, ndarray::bitflag flags)
267 {
268 int requirements = detail::bitflag_to_numpy(flags);
269 return ndarray(python::detail::new_reference
270 (PyArray_FromAny(obj.ptr(),
271 NULL,
272 nd_min, nd_max,
273 requirements,
274 NULL)));
275 }
276
277 }}} // namespace boost::python::numpy