Michele De Stefano's C++ Utilities
matrix.hpp
Go to the documentation of this file.
1 // mds_utils/python/ublas/matrix.hpp
2 //
3 // Copyright (c) 2009 - Michele De Stefano (micdestefano@users.sourceforge.net)
4 //
5 // Distributed under the MIT License (See accompanying file COPYING)
6 
7 #ifndef MDS_UTILS_PYTHON_UBLAS_MATRIX_HPP_INCLUDED
8 #define MDS_UTILS_PYTHON_UBLAS_MATRIX_HPP_INCLUDED
9 
31 #include <boost/numeric/ublas/matrix.hpp>
33 #include <mds_utils/python/ublas/detail/common.hpp>
34 #include <algorithm>
35 
36 namespace mds_utils { namespace python { namespace ublas {
37 
53 template<class M>
54 inline typename std::enable_if<
55  mds_utils::ublas::is_matrix<M>::value,M>::type
56  get(PyObject *po) {
57 
58  using namespace mds_utils::python::numpy;
59  using namespace mds_utils::python::ublas;
60  using namespace std;
61 
62 
63  if (!PyArray_Check(po)) {
64  throw std::invalid_argument("Cannot convert to "
65  "boost::numeric::ublas::matrix: not a numpy array.");
66  }
67 
68  PyArrayObject *m_po(reinterpret_cast<PyArrayObject*>(po));
69 
70 
71  int ndim(PyArray_NDIM(m_po));
72 
73  if (ndim != 2) {
74  throw std::invalid_argument("Cannot convert from an "
75  "array with num. of dims. different from 2.");
76  }
77 
78  npy_intp *dims(PyArray_DIMS(m_po));
79 
80  M mout(dims[0],dims[1]);
81 
83  typename M::value_type,
84  typename detail::ublas_to_npy_storage<
85  typename M::orientation_category>::tag,NPY_ITER_READONLY>
86  it_beg(m_po),it_end(it_beg,true);
87 
88  copy(it_beg,it_end,mout.data().begin());
89 
90  return mout;
91 }
92 
93 
104 template<class ublas_mat_T>
105 inline typename std::enable_if<
106  mds_utils::ublas::is_matrix<ublas_mat_T>::value,
107  PyObject*
108  >::type to_python(const ublas_mat_T& mat) {
109 
110  using namespace mds_utils::python::numpy;
111  using namespace std;
112 
113  int
114  nd(2),
115  flags(detail::numpy_flags<
116  typename ublas_mat_T::orientation_category>());
117 
118  npy_intp
119  dims[] = {
120  boost::numeric_cast<npy_intp>(mat.size1()),
121  boost::numeric_cast<npy_intp>(mat.size2())
122  };
123 
124  int dtype(numpy_dtype_traits<
125  typename ublas_mat_T::value_type>::typenum);
126 
127  PyObject *pout(PyArray_New(&PyArray_Type,nd,dims,dtype,
128  NULL,NULL,0,flags,NULL));
129 
130  if (pout == NULL) {
131  throw std::runtime_error("Could not create output numpy array.");
132  }
133 
134  PyArrayObject *m_po(reinterpret_cast<PyArrayObject*>(pout));
135 
136  typedef typename ublas_mat_T::value_type value_type;
137  typedef typename detail::ublas_to_npy_storage<
138  typename ublas_mat_T::orientation_category>::tag storage;
139 
141 
142  copy(mat.data().begin(),mat.data().end(),it_beg);
143 
144  return pout;
145 }
146 
147 
148 }}}
149 
150 #endif
Contains several utilities for interfacing Python with Boost&#39;s uBLAS.
Definition: matrix.hpp:36
Main namespace of all Michele De Stefano&#39;s C++ utilities.
Definition: endian.hpp:30
Contains utilities for the Boost uBLAS library.
Contains utilities for the creation of NumPy extensions.
std::enable_if< mds_utils::ublas::is_matrix< ublas_mat_T >::value, PyObject * >::type to_python(const ublas_mat_T &mat)
"To python" converter for Boost uBLAS matrices.
Definition: matrix.hpp:108
Provides traits for a specific C/C++ datatype.
Definition: traits.hpp:48