Michele De Stefano's C++ Utilities
numpy_array_usage.i
1 // numpy_array_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 numpy_array_usage_wrap.cpp numpy_array_usage.i
12  *
13  * 2. python setup.py build
14  */
15 
16 /**
17  * \example numpy_array_usage.i
18  *
19  * A simple SWIG interface for a Python extension module that shows the
20  * mds_utils::python::ublas::NumPy1DArray wrapper usage.
21  * Proper typemaps are included with the numpy_array.i interface.
22  * The output from the Python command line is shown in numpy_array_usage.pycmd.
23  *
24  * \remarks Here I've used SWIG for convenience only. The C++ code of
25  * mds-utils does not require you to make this choice.
26  */
27 
28 %module numpy_array_usage
29 
30 // The following instruction includes all the required typemaps
31 %include "mds_utils/python/ublas/numpy_array.i"
32 
33 %header %{
34 #include <boost/numeric/ublas/io.hpp>
35 #include <algorithm>
36 #include <iostream>
37 #include <complex>
38 
39 using namespace std;
40 %}
41 
42 %feature("autodoc","3");
43 
44 %inline %{
45 
46 // Tests building and returning a NumPy1DArray without converting to NumPy
47 boost::numeric::ublas::NumPy1DArray<double>*
48  create_NumPy1DArray() {
49 
50  namespace ub = boost::numeric::ublas;
51 
52  double buf[] = {1.1,2.2,3.3,4.4,5.5,6.6};
53 
54  size_t Nel(sizeof(buf)/sizeof(buf[0]));
55 
56  ub::NumPy1DArray<double>
57  *pv(new ub::NumPy1DArray<double>(buf,buf+Nel));
58 
59  return pv;
60 }
61 
62 void destroy_NumPy1DArray(boost::numeric::ublas::NumPy1DArray<double>* pv) {
63 
64  delete pv;
65 }
66 
67 // Tests building and returning a NumPy1DArray, with conversion to NumPY
68 ublas_convert::NumPy1DArray<double>
69  create_npy_array() {
70 
71  namespace ub = boost::numeric::ublas;
72 
73  ub::NumPy1DArray<double>*
74  pv(create_NumPy1DArray());
75 
76  ub::NumPy1DArray<double>
77  out(*pv);
78 
79  delete pv; pv = NULL;
80 
81  return out;
82 }
83 
84 
85 // Tests building and returning a NumPy1DArray with complex elements
86 ublas_convert::NumPy1DArray< std::complex<double> >
87  create_npy_array_cplx() {
88 
89  namespace ub = boost::numeric::ublas;
90 
91  complex<double> buf[] = {
92  complex<double>(1.1,1.1),
93  complex<double>(2.2,2.2),
94  complex<double>(3.3,3.3),
95  complex<double>(4.4,4.4),
96  complex<double>(5.5,5.5),
97  complex<double>(6.6,6.6)
98  };
99 
100  size_t Nel(6);
101 
102  ub::NumPy1DArray< complex<double> >
103  v(buf,buf+Nel);
104 
105  return v;
106 }
107 
108 
109 // Tests building and returning a NumPy1DArray of shorts with conversion to NumPy
110 ublas_convert::NumPy1DArray<short>
111  create_npy_array_short() {
112 
113  namespace ub = boost::numeric::ublas;
114 
115  short buf[] = {-1,2,-3,4,5,-6};
116 
117  size_t Nel(sizeof(buf)/sizeof(buf[0]));
118 
119  ub::NumPy1DArray<short>
120  v(buf,buf+Nel);
121 
122  return v;
123 }
124 
125 
126 
127 // Tests the "from Python converter"
128 void get_vec(ublas_convert::NumPy1DArray<double> v) {
129  using namespace std;
130 
131  cout << "Vector got:" << endl << v << endl;
132 }
133 
134 
135 // Tests getting a NumPy1DArray directly (i.w. without conversion from NumPy)
136 void get_vec_no_conversion(const boost::numeric::ublas::NumPy1DArray<double>& v) {
137  using namespace std;
138 
139  cout << "Vector got:" << endl << v << endl;
140 }
141 
142 
143 // Tests the "from Python converter" for complex numbers
144 void get_vec_cplx(ublas_convert::NumPy1DArray< std::complex<double> > v) {
145  using namespace std;
146 
147  cout << "Vector got:" << endl << v << endl;
148 }
149 
150 // Tests the "from Python converter" for complex float numbers
151 void get_vec_fcplx(ublas_convert::NumPy1DArray< std::complex<float> > v) {
152  using namespace std;
153 
154  cout << "Vector got:" << endl << v << endl;
155 }
156 
157 // Tests the "from Python converter", with conversion cast
158 void get_vec_cast(ublas_convert::NumPy1DArray<short> v) {
159  using namespace std;
160 
161  cout << "Vector got:" << endl << v << endl;
162 }
163 
164 
165 // Tests a simple uBLAS operation on the NumPy array, modifying the argument
166 
167 void doublevec(ublas_convert::NumPy1DArray<double>& v) {
168 
169  v *= 2.;
170 }
171 
172 // The same as before, but with pointer argument
173 
174 void doublevecp(ublas_convert::NumPy1DArray<double>* v) {
175 
176  *v *= 2.;
177 }
178 
179 
180 %}
181 
182 
183 %init %{
184  // The following instruction probably performs a dlopen for loading the
185  // numpy library. Without it, nothing works.
186  import_array();
187 %}
188 
189 
190