Michele De Stefano's C++ Utilities
matrix_sparse_usage.i
1 // matrix_sparse_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 matrix_sparse_usage_wrap.cpp matrix_sparse_usage.i
12  *
13  * 2. python setup.py build
14  */
15 
16 /**
17  * \example matrix_sparse_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::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.
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 matrix_sparse_usage
31 
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"
35 
36 %header %{
37 #include <boost/numeric/ublas/io.hpp>
38 #include <algorithm>
39 #include <iostream>
40 #include <complex>
41 
42 namespace mdspy = mds_utils::python;
43 using namespace std;
44 %}
45 
46 %feature("autodoc","3");
47 
48 %inline %{
49 
50 // Tests the "from Python converter" for real double values
51 void get_matrix(ublas_convert::compressed_matrix<double> m) {
52  using namespace std;
53 
54  cout << "Matrix got:" << endl << m << endl;
55 }
56 
57 // Tests the "from Python converter" for complex double values
58 void get_matrix_cplx(ublas_convert::compressed_matrix< std::complex<double> > m) {
59  using namespace std;
60 
61  cout << "Matrix got:" << endl << m << endl;
62 }
63 
64 
65 // Tests the "from Python converter" for complex float values
66 void get_matrix_fcplx(ublas_convert::compressed_matrix< std::complex<float> > m) {
67  using namespace std;
68 
69  cout << "Matrix got:" << endl << m << endl;
70 }
71 
72 // Tests the "from Python converter" for integer values
73 void get_matrix_int(ublas_convert::compressed_matrix<int> m) {
74  using namespace std;
75 
76  cout << "Matrix got:" << endl << m << endl;
77 }
78 
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) {
81  using namespace std;
82 
83  cout << "Matrix got:" << endl << m << endl;
84 }
85 
86 
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() {
90 
91  namespace ub = boost::numeric::ublas;
92 
93  ub::compressed_matrix<double>*
94  pM(new ub::compressed_matrix<double>(4,3));
95 
96  ub::compressed_matrix<double>& M(*pM);
97 
98  M(0,1) = 1.1; M(3,0) = 2.2; M(2,2) = 3.3;
99 
100  return pM;
101 }
102 
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) {
105  delete pM;
106 }
107 
108 
109 // Tests building and returning a csr matrix
110 ublas_convert::compressed_matrix<double>
111  create_csr_matrix() {
112 
113  namespace ub = boost::numeric::ublas;
114 
115  ub::compressed_matrix<double>*
116  pM(create_ublas_compressed_matrix());
117 
118  ub::compressed_matrix<double>
119  out(*pM);
120 
121  destroy_ublas_compressed_matrix(pM); pM = NULL;
122 
123  return out;
124 }
125 
126 
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() {
130 
131  namespace ub = boost::numeric::ublas;
132 
133  ub::compressed_matrix< std::complex<float>,ub::column_major >
134  M(4,3);
135 
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);
139 
140  return M;
141 }
142 
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) {
145  using namespace std;
146 
147  cout << "Matrix got:" << endl << m << endl;
148 }
149 
150 %}
151 
152 %init %{
153  // The following instruction probably performs a dlopen for loading the
154  // numpy library. Without it, nothing works.
155  import_array();
156 %}
157