| 340 | |
| 341 | template<typename Numeric> |
| 342 | PyObject* get_2darray(const std::vector<::std::vector<Numeric>>& v) |
| 343 | { |
| 344 | if (v.size() < 1) throw std::runtime_error("get_2d_array v too small"); |
| 345 | |
| 346 | npy_intp vsize[2] = {static_cast<npy_intp>(v.size()), |
| 347 | static_cast<npy_intp>(v[0].size())}; |
| 348 | |
| 349 | PyArrayObject *varray = |
| 350 | (PyArrayObject *)PyArray_SimpleNew(2, vsize, NPY_DOUBLE); |
| 351 | |
| 352 | double *vd_begin = static_cast<double *>(PyArray_DATA(varray)); |
| 353 | |
| 354 | for (const ::std::vector<Numeric> &v_row : v) { |
| 355 | if (v_row.size() != static_cast<size_t>(vsize[1])) |
| 356 | throw std::runtime_error("Missmatched array size"); |
| 357 | std::copy(v_row.begin(), v_row.end(), vd_begin); |
| 358 | vd_begin += vsize[1]; |
| 359 | } |
| 360 | |
| 361 | return reinterpret_cast<PyObject *>(varray); |
| 362 | } |
| 363 | |
| 364 | #else // fallback if we don't have numpy: copy every element of the given vector |
| 365 | |