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