Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <boost/python.hpp>
- #include <boost/python/numpy.hpp>
- namespace py = boost::python;
- namespace np = py::numpy;
- template<typename T, std::size_t ...I>
- auto make_shape_impl(const T &data, std::index_sequence<I...> ndxs)
- {
- return std::array<std::size_t, ndxs.size()> { std::get<I+1>(data)... };
- }
- template<typename T>
- auto make_shape(const T &data)
- {
- return make_shape_impl(data, std::make_index_sequence<std::tuple_size_v<T> - 1>());
- }
- template<typename T, std::size_t ...I>
- auto make_strides_impl(const T &data, std::index_sequence<I...> ndxs)
- {
- }
- template<typename T>
- auto make_strides(const T &data)
- {
- return make_strides_impl(data, std::make_index_sequence<std::tuple_size_v<T> - 1>());
- }
- template<typename T>
- np::ndarray make_ndarray(const T &data)
- {
- auto shape = make_shape(data);
- auto strides = shape;
- std::reverse(strides.begin(), strides.end());
- for(auto v : strides)
- std::cout << v << std::endl;
- auto dtype = np::dtype::get_builtin<std::remove_pointer_t<std::tuple_element_t<0, T>>>();
- return np::from_data(std::get<0>(data), dtype, shape, strides, py::object());
- }
- int main()
- {
- Py_Initialize();
- np::initialize();
- std::tuple<float*, std::size_t, std::size_t, std::size_t> data { new float[3 * 2 * 1], 3, 2, 1 };
- std::get<0>(data)[0] = -1.0f;
- np::ndarray py_array = make_ndarray(data);
- std::cout << py::extract<std::string>(py::str(py_array))() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement