In this short snippet, I want to create an Eigen::Tensor (in the unsupported module), with aribtrary dimensions.
template
<typename T>
Tensor<T, 2> convertNPToEigen2D(np::ndarray const & arr)
{
//Some checking...
T* raw_arr_data = reinterpret_cast<T*>(arr.get_data());
TensorMap<Tensor<T, 2>> arr_eigen(raw_arr_data,
arr.shape(0), arr.shape(1));
//...
return arr_eigen;
}
You can of course see that without variadic templates, I have to duplicate this function for every possible number of dimensions. This seems like a pretty basic examples, where variadic templates can avoid a lot of code duplication:
template
<typename T, uint64_t dims>
Tensor<T, dims> convertNPToEigenND(np::ndarray const & arr)
{
//Some checking...
T* raw_arr_data = reinterpret_cast<T*>(arr.get_data());
TensorMap<Tensor<T, dims>> arr_eigen(raw_arr_data,
/*arr.shape(0), ..., arr.shape(dims-1)*/);
//...
return arr_eigen;
}
I have several problems translating this into variadic template code,since I don't have an argument pack. I guess it would also be possible to let the caller do the work, i.e.
convertNPToEigenND<float, arr.shape(0), arr.shape(1), arr.shape(2)>(arr)
but I would prefer the above solution if it is possible. I thought it would be easy to look this problem up, but most of the questions I have found deal with an existing argument pack instead of creating one.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire