Michele De Stefano's C++ Utilities
array_iter_usage.i
1 // array_iter_usage.i
2 //
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file LICENSE)
6 
7 
8 /*
9  * Instructions for generating and building the extension:
10  *
11  * 1. swig -c++ -Wall -python -I../../../../include -o array_iter_usage_wrap.cpp array_iter_usage.i
12  *
13  * 2. python setup.py build
14  */
15 
16 /**
17  * \example array_iter_usage.i
18  *
19  * A simple SWIG interface for a Python extension module that shows the
20  * mds_utils::python::numpy::NDArrayIterator
21  *
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.
24  */
25 
26 %module array_iter_usage
27 
28 %include "mds_utils/python/common.i"
29 
30 %header %{
31 #include <mds_utils/python/numpy/array_iterator.hpp>
32 #include <iostream>
33 #include <algorithm>
34 #include <complex>
35 
36 using namespace std;
37 
38 namespace mds_npy = mds_utils::python::numpy;
39 %}
40 
41 %feature("autodoc","3");
42 
43 %inline %{
44 
45 // Tests accessing array elements for reading (FORTRAN ordering)
46 void print_array_elements(PyObject *array) {
47 
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);
51 
52  cout << "\nPrint array:" << endl;
53 
54  for_each(arr_it,arr_it_end,
55  [](decltype(*arr_it) x) {
56  cout << static_cast<double>(x) << endl;
57  });
58 
59  cout << "------------" << endl;
60 
61 
62  cout << static_cast<double>(*arr_it) << endl;
63  ++arr_it;
64  cout << static_cast<double>(*arr_it) << endl;
65  ++arr_it;
66  cout << static_cast<double>(*arr_it) << endl;
67  ++arr_it;
68 
69  // Tests decrement
70  --arr_it;
71  cout << static_cast<double>(*arr_it) << endl;
72 
73  // Tests random access
74  arr_it -= 1;
75  cout << static_cast<double>(*arr_it) << endl;
76 
77 }
78 
79 
80 // Tests modification of some elements (FORTRAN ordering)
81 void modify_array_elements(PyObject *array) {
82 
83  mds_npy::NDArrayIterator<double,mds_npy::fortran_storage>
84  arr_it(reinterpret_cast<PyArrayObject*>(array));
85 
86  *arr_it++ = 7.;
87  *arr_it++ = 9.;
88 }
89 
90 
91 // Tests accessing elements with cast (C ordering)
92 void print_int_elements(PyObject *array) {
93 
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);
97 
98  cout << "\nPrint array:" << endl;
99 
100 
101  for_each(arr_it,arr_it_end,
102  [](decltype(*arr_it) x) {
103  cout << static_cast<int>(x) << endl;
104  });
105 
106  cout << "------------" << endl;
107 
108 }
109 
110 
111 // Tests modification of array elements with cast (C ordering)
112 void modify_int_elements(PyObject *array) {
113 
114  mds_npy::NDArrayIterator<int,mds_npy::c_storage>
115  arr_it(reinterpret_cast<PyArrayObject*>(array));
116 
117  *arr_it++ = 10;
118  *arr_it++ = 11;
119 }
120 
121 
122 // Tests accessing complex elements with cast (C ordering)
123 void print_fcplx_elements(PyObject *array) {
124 
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);
128 
129  cout << "\nPrint array:" << endl;
130 
131 
132  for_each(arr_it,arr_it_end,
133  [](decltype(*arr_it) x) {
134  cout << static_cast< complex<float> >(x) << endl;
135  });
136 
137  cout << "------------" << endl;
138 
139 }
140 
141 
142 // Tests modification of complex array elements with cast (C ordering)
143 void modify_fcplx_elements(PyObject *array) {
144 
145  mds_npy::NDArrayIterator<complex<float>,mds_npy::c_storage>
146  arr_it(reinterpret_cast<PyArrayObject*>(array));
147 
148  arr_it += 2;
149  *arr_it++ = complex<float>(10.1,30.1);
150  *arr_it++ = complex<float>(14.8,9.7);
151 }
152 
153 %}
154 
155 
156 %init %{
157  // The following instruction probably performs a dlopen for loading the
158  // numpy library. Without it, nothing works.
159  import_array();
160 %}
161 
162 
163 
164