3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
5 // Distributed under the MIT License (See accompanying file LICENSE)
9 * Instructions for generating and building the extension:
11 * 1. swig -c++ -Wall -python -I../../../../include -o array_iter_usage_wrap.cpp array_iter_usage.i
13 * 2. python setup.py build
17 * \example array_iter_usage.i
19 * A simple SWIG interface for a Python extension module that shows the
20 * mds_utils::python::numpy::NDArrayIterator
22 * \remarks Here I've used SWIG for convenience only. The C++ code of
23 * mds-utils does not require you to make this choice.
26 %module array_iter_usage
28 %include "mds_utils/python/common.i"
31 #include <mds_utils/python/numpy/array_iterator.hpp>
38 namespace mds_npy = mds_utils::python::numpy;
41 %feature("autodoc","3");
45 // Tests accessing array elements for reading (FORTRAN ordering)
46 void print_array_elements(PyObject *array) {
48 mds_npy::NDArrayIterator<double,mds_npy::fortran_storage,NPY_ITER_READONLY>
49 arr_it(reinterpret_cast<PyArrayObject*>(array)),
50 arr_it_end(reinterpret_cast<PyArrayObject*>(array),true);
52 cout << "\nPrint array:" << endl;
54 for_each(arr_it,arr_it_end,
55 [](decltype(*arr_it) x) {
56 cout << static_cast<double>(x) << endl;
59 cout << "------------" << endl;
62 cout << static_cast<double>(*arr_it) << endl;
64 cout << static_cast<double>(*arr_it) << endl;
66 cout << static_cast<double>(*arr_it) << endl;
71 cout << static_cast<double>(*arr_it) << endl;
73 // Tests random access
75 cout << static_cast<double>(*arr_it) << endl;
80 // Tests modification of some elements (FORTRAN ordering)
81 void modify_array_elements(PyObject *array) {
83 mds_npy::NDArrayIterator<double,mds_npy::fortran_storage>
84 arr_it(reinterpret_cast<PyArrayObject*>(array));
91 // Tests accessing elements with cast (C ordering)
92 void print_int_elements(PyObject *array) {
94 mds_npy::NDArrayIterator<int,mds_npy::c_storage,NPY_ITER_READONLY>
95 arr_it(reinterpret_cast<PyArrayObject*>(array)),
96 arr_it_end(arr_it,true);
98 cout << "\nPrint array:" << endl;
101 for_each(arr_it,arr_it_end,
102 [](decltype(*arr_it) x) {
103 cout << static_cast<int>(x) << endl;
106 cout << "------------" << endl;
111 // Tests modification of array elements with cast (C ordering)
112 void modify_int_elements(PyObject *array) {
114 mds_npy::NDArrayIterator<int,mds_npy::c_storage>
115 arr_it(reinterpret_cast<PyArrayObject*>(array));
122 // Tests accessing complex elements with cast (C ordering)
123 void print_fcplx_elements(PyObject *array) {
125 mds_npy::NDArrayIterator<complex<float>,mds_npy::c_storage,NPY_ITER_READONLY>
126 arr_it(reinterpret_cast<PyArrayObject*>(array)),
127 arr_it_end(arr_it,true);
129 cout << "\nPrint array:" << endl;
132 for_each(arr_it,arr_it_end,
133 [](decltype(*arr_it) x) {
134 cout << static_cast< complex<float> >(x) << endl;
137 cout << "------------" << endl;
142 // Tests modification of complex array elements with cast (C ordering)
143 void modify_fcplx_elements(PyObject *array) {
145 mds_npy::NDArrayIterator<complex<float>,mds_npy::c_storage>
146 arr_it(reinterpret_cast<PyArrayObject*>(array));
149 *arr_it++ = complex<float>(10.1,30.1);
150 *arr_it++ = complex<float>(14.8,9.7);
157 // The following instruction probably performs a dlopen for loading the
158 // numpy library. Without it, nothing works.