7 #ifndef MDS_UTILS_PYTHON_UBLAS_VECTOR_HPP_INCLUDED 8 #define MDS_UTILS_PYTHON_UBLAS_VECTOR_HPP_INCLUDED 29 #include <boost/numeric/ublas/vector.hpp> 30 #include <mds_utils/python/ublas/detail/common.hpp> 32 #include <type_traits> 61 template<
class ublas_vec_T>
62 inline typename std::enable_if<
63 mds_utils::ublas::is_vector<ublas_vec_T>::value,ublas_vec_T>::type
69 if (!PyArray_Check(po)) {
70 throw std::invalid_argument(
"Cannot convert to " 71 "boost::numeric::ublas::vector: not a numpy array.");
74 PyArrayObject *m_po(reinterpret_cast<PyArrayObject*>(po));
77 int ndim(PyArray_NDIM(m_po));
79 if (ndim != 1 && ndim != 2) {
80 throw std::invalid_argument(
"Cannot convert from an " 81 "array with num. of dims. different from 2.");
84 npy_intp *dims(PyArray_DIMS(m_po));
86 if (!(ndim == 1 || std::min(dims[0],dims[1]) == 1)) {
87 throw std::invalid_argument(
"Cannot convert from a non-vector");
91 vout((ndim == 1) ? *dims : *std::max_element(dims,dims+2));
94 typename ublas_vec_T::value_type,
95 typename detail::ublas_to_npy_storage<
96 boost::numeric::ublas::row_major_tag>::tag,NPY_ITER_READONLY>
97 it_beg(m_po),it_end(it_beg,
true);
99 copy(it_beg,it_end,vout.begin());
116 template<
class ublas_vec_T>
117 inline typename std::enable_if<
118 mds_utils::ublas::is_vector<ublas_vec_T>::value,
131 boost::numeric_cast<npy_intp>(v.size())
135 typename ublas_vec_T::value_type>::typenum);
137 PyObject *pout(PyArray_New(&PyArray_Type,nd,dims,dtype,
138 NULL,NULL,0,flags,NULL));
141 throw std::runtime_error(
"Could not create output numpy array.");
144 PyArrayObject *m_po(reinterpret_cast<PyArrayObject*>(pout));
146 typedef typename ublas_vec_T::value_type value_type;
147 typedef typename detail::ublas_to_npy_storage<
148 boost::numeric::ublas::row_major_tag>::tag storage;
152 copy(v.begin(),v.end(),it_beg);
Iterator on a NumPy ndarray.
Main namespace of all Michele De Stefano's C++ utilities.
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.
Provides traits for a specific C/C++ datatype.