简体   繁体   中英

c++ How to flatten a view from xtensor, without deep copying?

Is it possible to directly flatten a view from xtensor? Something like:

xt::xarray<double> arr3d = xt::linspace<double>(1.0, 21970.0, 21970);
arr3d.reshape({13, 13, 130});
xt::xtensor<double, 3> data = arr3d;

xt::xtensor<double, 3>  data_view = xt::view(data, xt::range(9, 12),xt::range(9, 12),xt::range(9, 12));
xt::xtensor<double, 1>  flat_data = xt::flatten(data_view);

If I try to flatten the view directly, results differ.

xt::xtensor<double, 1>  flat_data = xt::flatten(xt::view(data, xt::range(9, 12),xt::range(9, 12),xt::range(9, 12)));

I was thinking about flattening directly, for a more clean and optimized code. Is there a way of doing it? Or should I copy the view to an auxiliary variable before flattening it?

Obs.: flattening directly outputs (wrong answer):
16390, 16391, 16392, 16393, 16394, 16395, 16396, 16397, 16398, 16399, 16400, 16401, 16402, 16403, 16404, 16405, 16406, 16407, 16408, 16409, 16410, 16411, 16412, 16413, 16414, 16415, 16416.

copying before flattening outputs what I need (a kernel window): 16390, 16391, 16392, 16520, 16521, 16522, 16650, 16651, 16652, 18080, 18081, 18082, 18210, 18211, 18212, 18340, 18341, 18342, 19770, 19771, 19772, 19900, 19901, 19902, 20030, 20031, 20032

This looks like a bug, could you open an issue on xtensor's repo please?

Also you can initialize your first tensor in a single line and avoid a copy:

xt::xtensor<double, 3> = xt::linspace<double>(1.0, 21970.0, 21970).reshape({13, 13, 130});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM