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 vector_usage_wrap.cpp vector_usage.i
13 * 2. python setup.py build
17 * \example vector_usage.i
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.
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.
32 // The following instruction includes all the required typemaps
33 %include "mds_utils/python/ublas/vector.i"
36 #include <boost/numeric/ublas/io.hpp>
44 %feature("autodoc","3");
48 // Tests building and returning a vector without converting to NumPy
49 boost::numeric::ublas::vector<double>*
50 create_ublas_vector() {
52 namespace ub = boost::numeric::ublas;
54 double buf[] = {1.1,2.2,3.3,4.4,5.5,6.6};
56 size_t Nel(sizeof(buf)/sizeof(buf[0]));
59 *pv(new ub::vector<double>(Nel));
61 copy(buf,buf+Nel,pv->begin());
66 void destroy_ublas_vector(boost::numeric::ublas::vector<double>* pv) {
71 // Tests building and returning a vector, with conversion to NumPY
72 ublas_convert::vector<double>
75 namespace ub = boost::numeric::ublas;
78 pv(create_ublas_vector());
89 // Tests building and returning a vector with complex elements
90 ublas_convert::vector< std::complex<double> >
91 create_npy_array_cplx() {
93 namespace ub = boost::numeric::ublas;
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)
106 ub::vector< complex<double> >
109 copy(buf,buf+Nel,v.data().begin());
115 // Tests building and returning a vector with conversion to NumPy
116 ublas_convert::vector<short>
117 create_npy_array_short() {
119 namespace ub = boost::numeric::ublas;
121 short buf[] = {-1,2,-3,4,5,-6};
123 size_t Nel(sizeof(buf)/sizeof(buf[0]));
128 copy(buf,buf+Nel,v.data().begin());
135 // Tests the "from Python converter"
136 void get_vec(ublas_convert::vector<double> v) {
139 cout << "Vector got:" << endl << v << endl;
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) {
147 cout << "Vector got:" << endl << v << endl;
151 // Tests the "from Python converter" for complex numbers
152 void get_vec_cplx(ublas_convert::vector< std::complex<double> > v) {
155 cout << "Vector got:" << endl << v << endl;
158 // Tests the "from Python converter" for complex float numbers
159 void get_vec_fcplx(ublas_convert::vector< std::complex<float> > v) {
162 cout << "Vector got:" << endl << v << endl;
165 // Tests the "from Python converter", with conversion cast
166 void get_vec_cast(ublas_convert::vector<short> v) {
169 cout << "Vector got:" << endl << v << endl;
178 // The following instruction probably performs a dlopen for loading the
179 // numpy library. Without it, nothing works.