pythonc++numpyxtensor

xtensor : How to write an vector to an array


What is the equivalent in xtensor or the most optimized way to write a vector to an array.

Thanks

import numpy as np

array = np.zeros((4, 4))

array[0] = np.array([1, 2, 3, 4]) # this


Solution

  • The easiest way is

    #include <xtensor/xtensor.hpp>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xview.hpp>
    #include <xtensor/xio.hpp>
    
    int main()
    {
        xt::xtensor<double, 2> array = xt::xtensor<double>({4, 4});
        xt::xtensor<double, 1> row = {1, 2, 3, 4};
        xt::view(array, 0) = row;
    
        return 0;
    }
    

    You can use xt::xarray for flexibility, but it is less efficient.