1 // mds_utils/python/ublas/matrix_sparse.i
3 // Copyright (c) 2014 - Michele De Stefano (micdestefano@users.sourceforge.net)
5 // Distributed under the MIT License (See accompanying file LICENSE)
8 #include <mds_utils/python/ublas/matrix_sparse.hpp>
10 namespace ublas_convert = boost::numeric::ublas;
15 %define MATRIX_SPARSE_IN_TYPEMAPS_GEN(type)
17 %typemap(in) (ublas_convert::compressed_matrix< type >) {
19 $1 = mds_utils::python::ublas::get< boost::numeric::ublas::compressed_matrix< type > >($input);
20 } catch (std::exception& e) {
21 PyErr_SetString(PyExc_RuntimeError,e.what());
26 %typecheck(SWIG_TYPECHECK_POINTER) ublas_convert::compressed_matrix< type > {
28 mds_utils::python::Obj module(PyImport_ImportModule("scipy.sparse"));
30 module.get_ownership();
32 mds_utils::python::Obj type_obj(module.attr("csr_matrix");
34 $1 = PyType_Check(type_obj) &&
35 PyObject_TypeCheck($input,
36 reinterpret_cast<PyTypeObject*>(
37 static_cast<PyObject*>(type_obj)));
43 %typemap(in) (ublas_convert::compressed_matrix< type ,boost::numeric::ublas::column_major>) {
45 $1 = mds_utils::python::ublas::get< boost::numeric::ublas::compressed_matrix< type ,boost::numeric::ublas::column_major> >($input);
46 } catch (std::exception& e) {
47 PyErr_SetString(PyExc_RuntimeError,e.what());
52 %typecheck(SWIG_TYPECHECK_POINTER) ublas_convert::compressed_matrix< type ,boost::numeric::ublas::column_major> {
54 mds_utils::python::Obj module(PyImport_ImportModule("scipy.sparse"));
56 module.get_ownership();
58 mds_utils::python::Obj type_obj(module.attr("csc_matrix");
60 $1 = PyType_Check(type_obj) &&
61 PyObject_TypeCheck($input,
62 reinterpret_cast<PyTypeObject*>(
63 static_cast<PyObject*>(type_obj)));
71 MATRIX_SPARSE_IN_TYPEMAPS_GEN(char)
72 MATRIX_SPARSE_IN_TYPEMAPS_GEN(unsigned char)
73 MATRIX_SPARSE_IN_TYPEMAPS_GEN(short)
74 MATRIX_SPARSE_IN_TYPEMAPS_GEN(unsigned short)
75 MATRIX_SPARSE_IN_TYPEMAPS_GEN(int)
76 MATRIX_SPARSE_IN_TYPEMAPS_GEN(unsigned int)
77 MATRIX_SPARSE_IN_TYPEMAPS_GEN(long)
78 MATRIX_SPARSE_IN_TYPEMAPS_GEN(unsigned long)
79 MATRIX_SPARSE_IN_TYPEMAPS_GEN(long long)
80 MATRIX_SPARSE_IN_TYPEMAPS_GEN(unsigned long long)
81 MATRIX_SPARSE_IN_TYPEMAPS_GEN(size_t)
82 MATRIX_SPARSE_IN_TYPEMAPS_GEN(float)
83 MATRIX_SPARSE_IN_TYPEMAPS_GEN(double)
84 MATRIX_SPARSE_IN_TYPEMAPS_GEN(long double)
85 MATRIX_SPARSE_IN_TYPEMAPS_GEN(std::complex<float>)
86 MATRIX_SPARSE_IN_TYPEMAPS_GEN(std::complex<double>)
87 MATRIX_SPARSE_IN_TYPEMAPS_GEN(std::complex<long double>)
90 %typemap(out) ublas_convert::compressed_matrix<double> {
91 $result = mds_utils::python::ublas::to_python($1);
94 %typemap(out) ublas_convert::compressed_matrix<double,boost::numeric::ublas::row_major> = ublas_convert::compressed_matrix<double>;
95 %typemap(out) ublas_convert::matrix<double,boost::numeric::ublas::column_major> = ublas_convert::compressed_matrix<double>;
98 %define MATRIX_SPARSE_OUT_TYPEMAPS_GEN(type)
100 %typemap(out) ublas_convert::compressed_matrix< type > = ublas_convert::compressed_matrix<double>;
101 %typemap(out) ublas_convert::compressed_matrix< type,boost::numeric::ublas::row_major> = ublas_convert::compressed_matrix<double>;
102 %typemap(out) ublas_convert::compressed_matrix< type,boost::numeric::ublas::column_major> = ublas_convert::compressed_matrix<double>;
106 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(char)
107 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(unsigned char)
108 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(short)
109 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(unsigned short)
110 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(int)
111 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(unsigned int)
112 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(long)
113 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(unsigned long)
114 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(long long)
115 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(unsigned long long)
116 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(size_t)
117 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(float)
118 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(long double)
119 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(std::complex<float>)
120 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(std::complex<double>)
121 MATRIX_SPARSE_OUT_TYPEMAPS_GEN(std::complex<long double>)