Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e87d133

Browse files
author
Rue Yokaze
committed
add comments
1 parent 5406372 commit e87d133

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

‎source/python_multi_array.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ static python::object float64 = numpy.attr("float64");
3232

3333
namespace python_multi_array
3434
{
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+
3549
namespace impl
3650
{
3751
template<class T>
@@ -86,6 +100,21 @@ namespace python_multi_array
86100
else { throw std::invalid_argument("dtype"); }
87101
}
88102

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+
89118
namespace impl
90119
{
91120
template<class T, size_t N>
@@ -152,12 +181,38 @@ namespace python_multi_array
152181
impl::setitem_impl(t, s, value);
153182
}
154183

184+
//
185+
// [Python]
186+
// x.reset()
187+
//
188+
// This function resets every element of x with zero.
189+
//
155190
template<class T, size_t N>
156191
void reset(shared_ptr<multi_array<T, N>> t)
157192
{
158193
std::fill(t->origin(), t->origin() + t->num_elements(), 0);
159194
}
160195

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+
//
161216
template<class T, size_t N>
162217
python::object shape(shared_ptr<multi_array<T, N>> t)
163218
{
@@ -176,12 +231,26 @@ namespace python_multi_array
176231
}
177232
}
178233

234+
//
235+
// [Python]
236+
// int x.num_dimensions()
237+
//
238+
// return: the number of dimensions of the array.
239+
//
179240
template<class T, size_t N>
180241
size_t num_dimensions(shared_ptr<multi_array<T, N>>)
181242
{
182243
return N;
183244
}
184245

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+
//
185254
template<class T, size_t N>
186255
size_t num_elements(shared_ptr<multi_array<T, N>> t)
187256
{
@@ -193,6 +262,12 @@ namespace python_multi_array
193262
return n;
194263
}
195264

265+
//
266+
// [Python]
267+
// numpy.ndarray x.get()
268+
//
269+
// return: a copy of the array stored in numpy.ndarray.
270+
//
196271
template<class T, size_t N>
197272
python::object get(shared_ptr<multi_array<T, N>> t)
198273
{
@@ -220,6 +295,17 @@ namespace python_multi_array
220295
return boost::python::numpy::from_data(t->origin(), dt, shape, strides, boost::python::object());
221296
}
222297

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+
223309
namespace impl
224310
{
225311
template<class T, size_t N, class S>
@@ -326,6 +412,10 @@ namespace python_multi_array
326412
else { throw std::invalid_argument("nd"); }
327413
}
328414

415+
//
416+
// [Internal-usage only]
417+
// let python interpreter to export types from this module.
418+
//
329419
class array_template
330420
{
331421
public:
@@ -336,6 +426,7 @@ namespace python_multi_array
336426
.def("__getitem__", &getitem<T, N>)
337427
.def("__setitem__", &setitem<T, N>)
338428
.def("reset", &reset<T, N>)
429+
.def("element", &element<T, N>)
339430
.def("shape", &shape<T, N>)
340431
.def("num_dimensions", &num_dimensions<T, N>)
341432
.def("num_elements", &num_elements<T, N>)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /