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 numpy_array_usage_wrap.cpp numpy_array_usage.i
13 * 2. python setup.py build
17 * \example numpy_array_usage.i
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.
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.
28 %module numpy_array_usage
30 // The following instruction includes all the required typemaps
31 %include "mds_utils/python/ublas/numpy_array.i"
34 #include <boost/numeric/ublas/io.hpp>
42 %feature("autodoc","3");
46 // Tests building and returning a NumPy1DArray without converting to NumPy
47 boost::numeric::ublas::NumPy1DArray<double>*
48 create_NumPy1DArray() {
50 namespace ub = boost::numeric::ublas;
52 double buf[] = {1.1,2.2,3.3,4.4,5.5,6.6};
54 size_t Nel(sizeof(buf)/sizeof(buf[0]));
56 ub::NumPy1DArray<double>
57 *pv(new ub::NumPy1DArray<double>(buf,buf+Nel));
62 void destroy_NumPy1DArray(boost::numeric::ublas::NumPy1DArray<double>* pv) {
67 // Tests building and returning a NumPy1DArray, with conversion to NumPY
68 ublas_convert::NumPy1DArray<double>
71 namespace ub = boost::numeric::ublas;
73 ub::NumPy1DArray<double>*
74 pv(create_NumPy1DArray());
76 ub::NumPy1DArray<double>
85 // Tests building and returning a NumPy1DArray with complex elements
86 ublas_convert::NumPy1DArray< std::complex<double> >
87 create_npy_array_cplx() {
89 namespace ub = boost::numeric::ublas;
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)
102 ub::NumPy1DArray< complex<double> >
109 // Tests building and returning a NumPy1DArray of shorts with conversion to NumPy
110 ublas_convert::NumPy1DArray<short>
111 create_npy_array_short() {
113 namespace ub = boost::numeric::ublas;
115 short buf[] = {-1,2,-3,4,5,-6};
117 size_t Nel(sizeof(buf)/sizeof(buf[0]));
119 ub::NumPy1DArray<short>
127 // Tests the "from Python converter"
128 void get_vec(ublas_convert::NumPy1DArray<double> v) {
131 cout << "Vector got:" << endl << v << endl;
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) {
139 cout << "Vector got:" << endl << v << endl;
143 // Tests the "from Python converter" for complex numbers
144 void get_vec_cplx(ublas_convert::NumPy1DArray< std::complex<double> > v) {
147 cout << "Vector got:" << endl << v << endl;
150 // Tests the "from Python converter" for complex float numbers
151 void get_vec_fcplx(ublas_convert::NumPy1DArray< std::complex<float> > v) {
154 cout << "Vector got:" << endl << v << endl;
157 // Tests the "from Python converter", with conversion cast
158 void get_vec_cast(ublas_convert::NumPy1DArray<short> v) {
161 cout << "Vector got:" << endl << v << endl;
165 // Tests a simple uBLAS operation on the NumPy array, modifying the argument
167 void doublevec(ublas_convert::NumPy1DArray<double>& v) {
172 // The same as before, but with pointer argument
174 void doublevecp(ublas_convert::NumPy1DArray<double>* v) {
184 // The following instruction probably performs a dlopen for loading the
185 // numpy library. Without it, nothing works.