1 // matrix_sparse_usage.i
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_sparse_usage_wrap.cpp matrix_sparse_usage.i
13 * 2. python setup.py build
17 * \example matrix_sparse_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::compressed_matrix objects.
22 * These functions are indirectly
23 * used through typemaps included with the matrix_sparse.i interface.
24 * The output from the Python command line is shown in matrix_sparse_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.
30 %module matrix_sparse_usage
32 %include "mds_utils/python/common.i"
33 // The following instruction includes all the required typemaps
34 %include "mds_utils/python/ublas/matrix_sparse.i"
37 #include <boost/numeric/ublas/io.hpp>
42 namespace mdspy = mds_utils::python;
46 %feature("autodoc","3");
50 // Tests the "from Python converter" for real double values
51 void get_matrix(ublas_convert::compressed_matrix<double> m) {
54 cout << "Matrix got:" << endl << m << endl;
57 // Tests the "from Python converter" for complex double values
58 void get_matrix_cplx(ublas_convert::compressed_matrix< std::complex<double> > m) {
61 cout << "Matrix got:" << endl << m << endl;
65 // Tests the "from Python converter" for complex float values
66 void get_matrix_fcplx(ublas_convert::compressed_matrix< std::complex<float> > m) {
69 cout << "Matrix got:" << endl << m << endl;
72 // Tests the "from Python converter" for integer values
73 void get_matrix_int(ublas_convert::compressed_matrix<int> m) {
76 cout << "Matrix got:" << endl << m << endl;
79 // Tests the "from Python converter" for real double values and csc matrices
80 void get_csc_matrix(ublas_convert::compressed_matrix<double,boost::numeric::ublas::column_major> m) {
83 cout << "Matrix got:" << endl << m << endl;
87 // Tests building and returning a compressed matrix without direct conversion to NumPy
88 boost::numeric::ublas::compressed_matrix<double>*
89 create_ublas_compressed_matrix() {
91 namespace ub = boost::numeric::ublas;
93 ub::compressed_matrix<double>*
94 pM(new ub::compressed_matrix<double>(4,3));
96 ub::compressed_matrix<double>& M(*pM);
98 M(0,1) = 1.1; M(3,0) = 2.2; M(2,2) = 3.3;
103 // Destructor needed, because I've not exposed the compressed_matrix class
104 void destroy_ublas_compressed_matrix(boost::numeric::ublas::compressed_matrix<double>* pM) {
109 // Tests building and returning a csr matrix
110 ublas_convert::compressed_matrix<double>
111 create_csr_matrix() {
113 namespace ub = boost::numeric::ublas;
115 ub::compressed_matrix<double>*
116 pM(create_ublas_compressed_matrix());
118 ub::compressed_matrix<double>
121 destroy_ublas_compressed_matrix(pM); pM = NULL;
127 // Tests building and returning a complex<float> csr matrix
128 ublas_convert::compressed_matrix< std::complex<float>,boost::numeric::ublas::column_major >
129 create_csc_matrix_fcplx() {
131 namespace ub = boost::numeric::ublas;
133 ub::compressed_matrix< std::complex<float>,ub::column_major >
136 M(0,1) = std::complex<float>(1.1,1.1);
137 M(3,0) = std::complex<float>(2.2,2.2);
138 M(2,2) = std::complex<float>(3.3,3.3);
143 // Tests getting a compressed matrix directly (i.e. without conversion from NumPy)
144 void get_matrix_no_conversion(const boost::numeric::ublas::compressed_matrix<double>& m) {
147 cout << "Matrix got:" << endl << m << endl;
153 // The following instruction probably performs a dlopen for loading the
154 // numpy library. Without it, nothing works.