/** * Here, we'll take the alternate approach of using the strides to access the array's memory directly. * This can be much faster for large arrays. */ static void copy_ndarray_to_mv2(bn::ndarray const & array, matrix2 & mat) { // Unfortunately, get_strides() can't be inlined, so it's best to call it once up-front. Py_intptr_t const * strides = array.get_strides(); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { mat(i, j) = *reinterpret_cast<double const *>(array.get_data() + i * strides[0] + j * strides[1]); } } }
NumPyArrayData<T>(const np::ndarray &arr){ np::dtype dtype = arr.get_dtype(); np::dtype dtype_expected = np::dtype::get_builtin<T>(); if(dtype != dtype_expected) { std::stringstream ss; ss << "NumPyArrayData: Unexpected data type (" << bp::extract<const char*>(dtype.attr("__str__")()) << ") received"; ss << "Expected " << bp::extract<const char*>(dtype_expected.attr("__str__")()); throw std::runtime_error(ss.str().c_str()); } data_ = arr.get_data(); strides_ = arr.get_strides(); }