| 287 | |
| 288 | template<typename Numeric> |
| 289 | PyObject* get_2darray(const std::vector<::std::vector<Numeric>>& v) |
| 290 | { |
| 291 | detail::_interpreter::get(); //interpreter needs to be initialized for the numpy commands to work |
| 292 | if (v.size() < 1) throw std::runtime_error("get_2d_array v too small"); |
| 293 | |
| 294 | npy_intp vsize[2] = {static_cast<npy_intp>(v.size()), |
| 295 | static_cast<npy_intp>(v[0].size())}; |
| 296 | |
| 297 | PyArrayObject *varray = |
| 298 | (PyArrayObject *)PyArray_SimpleNew(2, vsize, NPY_DOUBLE); |
| 299 | |
| 300 | double *vd_begin = static_cast<double *>(PyArray_DATA(varray)); |
| 301 | |
| 302 | for (const ::std::vector<Numeric> &v_row : v) { |
| 303 | if (v_row.size() != static_cast<size_t>(vsize[1])) |
| 304 | throw std::runtime_error("Missmatched array size"); |
| 305 | std::copy(v_row.begin(), v_row.end(), vd_begin); |
| 306 | vd_begin += vsize[1]; |
| 307 | } |
| 308 | |
| 309 | return reinterpret_cast<PyObject *>(varray); |
| 310 | } |
| 311 | |
| 312 | #else // fallback if we don't have numpy: copy every element of the given vector |
| 313 |
no test coverage detected