aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/python/src/numpy/ndarray.cpp
blob: af09ecc3381378a84d58959b76d75bbb59faa739 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright Jim Bosch 2010-2012.
// Copyright Stefan Seefeld 2016.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_PYTHON_NUMPY_INTERNAL
#include <boost/python/numpy/internal.hpp>
#include <boost/scoped_array.hpp>

namespace boost { namespace python {
namespace converter 
{
NUMPY_OBJECT_MANAGER_TRAITS_IMPL(PyArray_Type, numpy::ndarray)
} // namespace boost::python::converter

namespace numpy 
{
namespace detail 
{

ndarray::bitflag numpy_to_bitflag(int const f)
{
  ndarray::bitflag r = ndarray::NONE;
  if (f & NPY_ARRAY_C_CONTIGUOUS) r = (r | ndarray::C_CONTIGUOUS);
  if (f & NPY_ARRAY_F_CONTIGUOUS) r = (r | ndarray::F_CONTIGUOUS);
  if (f & NPY_ARRAY_ALIGNED) r = (r | ndarray::ALIGNED);
  if (f & NPY_ARRAY_WRITEABLE) r = (r | ndarray::WRITEABLE);
  return r;
}

int bitflag_to_numpy(ndarray::bitflag f)
{
  int r = 0;
  if (f & ndarray::C_CONTIGUOUS) r |= NPY_ARRAY_C_CONTIGUOUS;
  if (f & ndarray::F_CONTIGUOUS) r |= NPY_ARRAY_F_CONTIGUOUS;
  if (f & ndarray::ALIGNED) r |= NPY_ARRAY_ALIGNED;
  if (f & ndarray::WRITEABLE) r |= NPY_ARRAY_WRITEABLE;
  return r;
}

bool is_c_contiguous(std::vector<Py_intptr_t> const & shape,
		     std::vector<Py_intptr_t> const & strides,
		     int itemsize)
{
  std::vector<Py_intptr_t>::const_reverse_iterator j = strides.rbegin();
  int total = itemsize;
  for (std::vector<Py_intptr_t>::const_reverse_iterator i = shape.rbegin(); i != shape.rend(); ++i, ++j) 
  {
    if (total != *j) return false;
    total *= (*i);
  }
  return true;
}

bool is_f_contiguous(std::vector<Py_intptr_t> const & shape,
		     std::vector<Py_intptr_t> const & strides,
		     int itemsize)
{
  std::vector<Py_intptr_t>::const_iterator j = strides.begin();
  int total = itemsize;
  for (std::vector<Py_intptr_t>::const_iterator i = shape.begin(); i != shape.end(); ++i, ++j)
  {
    if (total != *j) return false;
    total *= (*i);
  }
  return true;
}

bool is_aligned(std::vector<Py_intptr_t> const & strides,
		int itemsize)
{
  for (std::vector<Py_intptr_t>::const_iterator i = strides.begin(); i != strides.end(); ++i) 
  {
    if (*i % itemsize) return false;
  }
  return true;
}

inline PyArray_Descr * incref_dtype(dtype const & dt) 
{
  Py_INCREF(dt.ptr());
  return reinterpret_cast<PyArray_Descr*>(dt.ptr());
}

ndarray from_data_impl(void * data, 
		       dtype const & dt, 
		       python::object const & shape,
		       python::object const & strides,
		       python::object const & owner,
		       bool writeable)
{
  std::vector<Py_intptr_t> shape_(len(shape));
  std::vector<Py_intptr_t> strides_(len(strides));
  if (shape_.size() != strides_.size()) 
  {
    PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match.");
    python::throw_error_already_set();
  }
  for (std::size_t i = 0; i < shape_.size(); ++i) 
  {
    shape_[i] = python::extract<Py_intptr_t>(shape[i]);
    strides_[i] = python::extract<Py_intptr_t>(strides[i]);
  }
  return from_data_impl(data, dt, shape_, strides_, owner, writeable);
}

ndarray from_data_impl(void * data, 
		       dtype const & dt, 
		       std::vector<Py_intptr_t> const & shape,
		       std::vector<Py_intptr_t> const & strides,
		       python::object const & owner,
		       bool writeable)
{
  if (shape.size() != strides.size()) 
  {
    PyErr_SetString(PyExc_ValueError, "Length of shape and strides arrays do not match.");
    python::throw_error_already_set();
  }
  int itemsize = dt.get_itemsize();
  int flags = 0;
  if (writeable) flags |= NPY_ARRAY_WRITEABLE;
  if (is_c_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_C_CONTIGUOUS;
  if (is_f_contiguous(shape, strides, itemsize)) flags |= NPY_ARRAY_F_CONTIGUOUS;
  if (is_aligned(strides, itemsize)) flags |= NPY_ARRAY_ALIGNED;
  ndarray r(python::detail::new_reference
    (PyArray_NewFromDescr(&PyArray_Type,
			  incref_dtype(dt),
			  shape.size(),
			  const_cast<Py_intptr_t*>(&shape.front()),
			  const_cast<Py_intptr_t*>(&strides.front()),
			  data,
			  flags,
			  NULL)));
    r.set_base(owner);
    return r;
}

} // namespace detail

namespace {
    int normalize_index(int n,int nlim) // wraps [-nlim:nlim) into [0:nlim), throw IndexError otherwise
    {
        if (n<0)
            n += nlim; // negative indices work backwards from end
        if (n < 0 || n >= nlim)
        {
            PyErr_SetObject(PyExc_IndexError, Py_None);
            throw_error_already_set();
        }
        return n;
    }
}

Py_intptr_t ndarray::shape(int n) const
{
    return get_shape()[normalize_index(n,get_nd())];
}

Py_intptr_t ndarray::strides(int n) const
{
    return get_strides()[normalize_index(n,get_nd())];
}

ndarray ndarray::view(dtype const & dt) const
{
  return ndarray(python::detail::new_reference
    (PyObject_CallMethod(this->ptr(), const_cast<char*>("view"), const_cast<char*>("O"), dt.ptr())));
}
    
ndarray ndarray::astype(dtype const & dt) const
{
  return ndarray(python::detail::new_reference
    (PyObject_CallMethod(this->ptr(), const_cast<char*>("astype"), const_cast<char*>("O"), dt.ptr())));
}

ndarray ndarray::copy() const 
{
  return ndarray(python::detail::new_reference
    (PyObject_CallMethod(this->ptr(), const_cast<char*>("copy"), const_cast<char*>(""))));
}

dtype ndarray::get_dtype() const 
{
  return dtype(python::detail::borrowed_reference(get_struct()->descr));
}

python::object ndarray::get_base() const 
{
  if (get_struct()->base == NULL) return object();
  return python::object(python::detail::borrowed_reference(get_struct()->base));
}

void ndarray::set_base(object const & base) 
{
  Py_XDECREF(get_struct()->base);
  if (base.ptr())
  {
    Py_INCREF(base.ptr());
    get_struct()->base = base.ptr();
  }
  else
  {
    get_struct()->base = NULL;
  }
}

ndarray::bitflag ndarray::get_flags() const
{
  return numpy::detail::numpy_to_bitflag(get_struct()->flags);
}

ndarray ndarray::transpose() const 
{
  return ndarray(python::detail::new_reference
    (PyArray_Transpose(reinterpret_cast<PyArrayObject*>(this->ptr()), NULL)));
}

ndarray ndarray::squeeze() const 
{
  return ndarray(python::detail::new_reference
    (PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(this->ptr()))));
}

ndarray ndarray::reshape(python::tuple const & shape) const 
{
  return ndarray(python::detail::new_reference
    (PyArray_Reshape(reinterpret_cast<PyArrayObject*>(this->ptr()), shape.ptr())));
}

python::object ndarray::scalarize() const 
{
  Py_INCREF(ptr());
  return python::object(python::detail::new_reference(PyArray_Return(reinterpret_cast<PyArrayObject*>(ptr()))));
}

ndarray zeros(python::tuple const & shape, dtype const & dt) 
{
  int nd = len(shape);
  boost::scoped_array<Py_intptr_t> dims(new Py_intptr_t[nd]);
  for (int n=0; n<nd; ++n) dims[n] = python::extract<Py_intptr_t>(shape[n]);
  return ndarray(python::detail::new_reference
                 (PyArray_Zeros(nd, dims.get(), detail::incref_dtype(dt), 0)));
}

ndarray zeros(int nd, Py_intptr_t const * shape, dtype const & dt) 
{
  return ndarray(python::detail::new_reference
    (PyArray_Zeros(nd, const_cast<Py_intptr_t*>(shape), detail::incref_dtype(dt), 0)));
}

ndarray empty(python::tuple const & shape, dtype const & dt) 
{
  int nd = len(shape);
  boost::scoped_array<Py_intptr_t> dims(new Py_intptr_t[nd]);
  for (int n=0; n<nd; ++n) dims[n] = python::extract<Py_intptr_t>(shape[n]);
  return ndarray(python::detail::new_reference
                 (PyArray_Empty(nd, dims.get(), detail::incref_dtype(dt), 0)));    
}

ndarray empty(int nd, Py_intptr_t const * shape, dtype const & dt)
{
  return ndarray(python::detail::new_reference
    (PyArray_Empty(nd, const_cast<Py_intptr_t*>(shape), detail::incref_dtype(dt), 0)));
}

ndarray array(python::object const & obj) 
{
  return ndarray(python::detail::new_reference
    (PyArray_FromAny(obj.ptr(), NULL, 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)));
}

ndarray array(python::object const & obj, dtype const & dt) 
{
  return ndarray(python::detail::new_reference
    (PyArray_FromAny(obj.ptr(), detail::incref_dtype(dt), 0, 0, NPY_ARRAY_ENSUREARRAY, NULL)));
}

ndarray from_object(python::object const & obj, dtype const & dt, int nd_min, int nd_max, ndarray::bitflag flags)
{
  int requirements = detail::bitflag_to_numpy(flags);
  return ndarray(python::detail::new_reference
    (PyArray_FromAny(obj.ptr(),
		     detail::incref_dtype(dt),
		     nd_min, nd_max,
		     requirements,
		     NULL)));
}

ndarray from_object(python::object const & obj, int nd_min, int nd_max, ndarray::bitflag flags) 
{
  int requirements = detail::bitflag_to_numpy(flags);
  return ndarray(python::detail::new_reference
    (PyArray_FromAny(obj.ptr(),
		     NULL,
		     nd_min, nd_max,
		     requirements,
		     NULL)));
}

}}} // namespace boost::python::numpy