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 matrix_usage_wrap.cpp matrix_usage.i
13 * 2. python setup.py build
17 * \example matrix_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::numeric::ublas::matrix objects.
22 * These functions are indirectly
23 * used through typemaps included with the matrix.i interface.
24 * The output from the Python command line is shown in matrix_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/matrix.i"
36 #include <boost/numeric/ublas/io.hpp>
41 namespace mdspy = mds_utils::python;
46 %feature("autodoc","3");
50 // Tests building and returning a matrix with column major ordering, without direct conversion to NumPy
51 boost::numeric::ublas::matrix<double,boost::numeric::ublas::column_major>*
52 create_ublas_matrix_column_major() {
54 namespace ub = boost::numeric::ublas;
56 double buf[] = {1.1,2.2,3.3,4.4,5.5,6.6};
58 size_t Nel(sizeof(buf)/sizeof(buf[0]));
60 ub::matrix<double,ub::column_major>
61 *pM(new ub::matrix<double,ub::column_major>(3,2));
63 copy(buf,buf+Nel,pM->data().begin());
69 void destroy_ublas_matrix_column_major(boost::numeric::ublas::matrix<double,boost::numeric::ublas::column_major>* pM) {
75 // Tests building and returning a matrix with column major ordering
76 ublas_convert::matrix<double,boost::numeric::ublas::column_major>
79 namespace ub = boost::numeric::ublas;
81 ub::matrix<double,ub::column_major>*
82 pM(create_ublas_matrix_column_major());
84 ub::matrix<double,ub::column_major>
93 // Tests building and returning a matrix with row major ordering
94 ublas_convert::matrix<double>
95 create_npy_array_row() {
97 namespace ub = boost::numeric::ublas;
99 double buf[] = {1.1,2.2,3.3,4.4,5.5,6.6};
101 size_t Nel(sizeof(buf)/sizeof(buf[0]));
106 copy(buf,buf+Nel,M.data().begin());
112 // Tests building and returning a matrix with complex elements
113 ublas_convert::matrix< std::complex<double> >
114 create_npy_array_cplx() {
116 namespace ub = boost::numeric::ublas;
118 complex<double> buf[] = {
119 complex<double>(1.1,1.1),
120 complex<double>(2.2,2.2),
121 complex<double>(3.3,3.3),
122 complex<double>(4.4,4.4),
123 complex<double>(5.5,5.5),
124 complex<double>(6.6,6.6)
129 ub::matrix< complex<double> >
132 copy(buf,buf+Nel,M.data().begin());
138 // Tests building and returning a matrix of short, without conversion
139 boost::numeric::ublas::matrix<short>
140 create_ublas_matrix_short() {
142 namespace ub = boost::numeric::ublas;
144 short buf[] = {-1,2,-3,4,5,-6};
146 size_t Nel(sizeof(buf)/sizeof(buf[0]));
151 copy(buf,buf+Nel,M.data().begin());
157 // Tests building and returning a matrix of short
158 ublas_convert::matrix<short>
159 create_npy_array_short() {
161 return create_ublas_matrix_short();
166 // Tests the "from Python converter"
167 void get_matrix(ublas_convert::matrix<double> m) {
170 cout << "Matrix got:" << endl << m << endl;
174 // Tests getting a matrix directly (i.e. without conversion from NumPy)
175 void get_matrix_no_conversion(const boost::numeric::ublas::matrix<double,boost::numeric::ublas::column_major>& m) {
178 cout << "Matrix got:" << endl << m << endl;
182 // Tests the "from Python converter" (opposite storage)
183 void get_matrix2(ublas_convert::matrix<double,boost::numeric::ublas::column_major> m) {
186 cout << "Matrix got:" << endl << m << endl;
189 // Tests the "from Python converter" for complex numbers
190 void get_matrix3(ublas_convert::matrix< std::complex<double> > m) {
193 cout << "Matrix got:" << endl << m << endl;
196 // Tests the "from Python converter", with conversion cast
197 void get_matrix4(ublas_convert::matrix<char> m) {
200 cout << "Matrix got:" << endl << m << endl;
209 // The following instruction probably performs a dlopen for loading the
210 // numpy library. Without it, nothing works.