@@ -32,6 +32,20 @@ static python::object float64 = numpy.attr("float64");
32
32
33
33
namespace python_multi_array
34
34
{
35
+ //
36
+ // [Python]
37
+ // [array_type] multi_array.make(shape, dtype)
38
+ //
39
+ // allocate a boost::multi_array of expected shape and data type.
40
+ //
41
+ // shape: int, list or tuple
42
+ // dtype: bool8, int8, int16, int32, int64, uint8, uint16, uint32, uint64,
43
+ // float32 or float64, all defined in numpy
44
+ //
45
+ // return: a smart-pointer of the array
46
+ //
47
+ python::object make (python::object shape, python::object dtype);
48
+
35
49
namespace impl
36
50
{
37
51
template <class T >
@@ -86,6 +100,21 @@ namespace python_multi_array
86
100
else { throw std::invalid_argument (" dtype" ); }
87
101
}
88
102
103
+ //
104
+ // [Python]
105
+ // T x[idx]
106
+ // x[idx] = T
107
+ //
108
+ // get and set one element via index operator.
109
+ // Example:
110
+ // x[2, 4] = 2.0
111
+ //
112
+ template <class T , size_t N>
113
+ T getitem (shared_ptr<multi_array<T, N>> t, python::object idx);
114
+
115
+ template <class T , size_t N>
116
+ void setitem (shared_ptr<multi_array<T, N>> t, python::object idx, T value);
117
+
89
118
namespace impl
90
119
{
91
120
template <class T , size_t N>
@@ -152,12 +181,38 @@ namespace python_multi_array
152
181
impl::setitem_impl (t, s, value);
153
182
}
154
183
184
+ //
185
+ // [Python]
186
+ // x.reset()
187
+ //
188
+ // This function resets every element of x with zero.
189
+ //
155
190
template <class T , size_t N>
156
191
void reset (shared_ptr<multi_array<T, N>> t)
157
192
{
158
193
std::fill (t->origin (), t->origin () + t->num_elements (), 0 );
159
194
}
160
195
196
+ //
197
+ // [Python]
198
+ // dtype x.element()
199
+ //
200
+ // return: data type of the array. possible values are bool8, uint8,
201
+ // uint16, uint32, uint64, int8, int16, int32, int64, float32,
202
+ // float64, all defined in numpy.
203
+ //
204
+ template <class T , size_t N>
205
+ python::object element (shared_ptr<multi_array<T, N>> t)
206
+ {
207
+ return python::numpy::dtype::get_builtin<T>();
208
+ }
209
+
210
+ //
211
+ // [Python]
212
+ // tuple x.shape()
213
+ //
214
+ // return: the shape of the array.
215
+ //
161
216
template <class T , size_t N>
162
217
python::object shape (shared_ptr<multi_array<T, N>> t)
163
218
{
@@ -176,12 +231,26 @@ namespace python_multi_array
176
231
}
177
232
}
178
233
234
+ //
235
+ // [Python]
236
+ // int x.num_dimensions()
237
+ //
238
+ // return: the number of dimensions of the array.
239
+ //
179
240
template <class T , size_t N>
180
241
size_t num_dimensions (shared_ptr<multi_array<T, N>>)
181
242
{
182
243
return N;
183
244
}
184
245
246
+ //
247
+ // [Python]
248
+ // int x.num_elements()
249
+ //
250
+ // return: the total number of elements of the array.
251
+ // example:
252
+ // It returns 8 for an array with shape (2, 4).
253
+ //
185
254
template <class T , size_t N>
186
255
size_t num_elements (shared_ptr<multi_array<T, N>> t)
187
256
{
@@ -193,6 +262,12 @@ namespace python_multi_array
193
262
return n;
194
263
}
195
264
265
+ //
266
+ // [Python]
267
+ // numpy.ndarray x.get()
268
+ //
269
+ // return: a copy of the array stored in numpy.ndarray.
270
+ //
196
271
template <class T , size_t N>
197
272
python::object get (shared_ptr<multi_array<T, N>> t)
198
273
{
@@ -220,6 +295,17 @@ namespace python_multi_array
220
295
return boost::python::numpy::from_data (t->origin (), dt, shape, strides, boost::python::object ());
221
296
}
222
297
298
+ //
299
+ // [Python]
300
+ // x.set(numpy.ndarray nd)
301
+ //
302
+ // Reset the array with values from nd.
303
+ // nd.dtype may be different from x.element() but the values are implicitly
304
+ // converted to x.element().
305
+ //
306
+ template <class T , size_t N>
307
+ void set (shared_ptr<multi_array<T, N>> t, python::numpy::ndarray nd);
308
+
223
309
namespace impl
224
310
{
225
311
template <class T , size_t N, class S >
@@ -326,6 +412,10 @@ namespace python_multi_array
326
412
else { throw std::invalid_argument (" nd" ); }
327
413
}
328
414
415
+ //
416
+ // [Internal-usage only]
417
+ // let python interpreter to export types from this module.
418
+ //
329
419
class array_template
330
420
{
331
421
public:
@@ -336,6 +426,7 @@ namespace python_multi_array
336
426
.def (" __getitem__" , &getitem<T, N>)
337
427
.def (" __setitem__" , &setitem<T, N>)
338
428
.def (" reset" , &reset<T, N>)
429
+ .def (" element" , &element<T, N>)
339
430
.def (" shape" , &shape<T, N>)
340
431
.def (" num_dimensions" , &num_dimensions<T, N>)
341
432
.def (" num_elements" , &num_elements<T, N>)
0 commit comments