c++vectornanxtensor

Xtensor append values to bottom of tensor?


Suppose I'm trying to shift (not bitwise) a tensor of values up by n and then add NaNs at the bottom to fill the shift offset.

int n = 2;

  1. original {{ 1., 2., 3., 4. } }
xt::xtensor_fixed<float, xt::xshape<1, 4>, xt::layout_type::column_major> original = {{ 1., 2., 3., 4. } };
  1. values to keep following shift of n {{3., 4. } }
auto shiftvalues = xt::view(original, xt::all(), xt::range(n, original.size()));
  1. after inserting nans to match original size {{3., 4. , nan, nan} }
(?)shiftvalues.appendBottom(std::numeric_limits<double>::quiet_NaN());
(?)shiftvalues.appendBottom(std::numeric_limits<double>::quiet_NaN());
int n = 2;
xt::xtensor_fixed<float, xt::xshape<1, 4>, xt::layout_type::column_major> original = {{ 1., 2., 3., 4. } };
auto shiftvalues = xt::view(original, xt::all(), xt::range(n, original.size()));

How would I go about inserting the NaN values into the tensor following steps 1 and 2? I haven't been able to find any methods in the documentation to append values to the bottom of a tensor.

Or would it be better to create a tensor of the same size as original, fill it with NaNs, then set the values at appropriate indexes to shiftvalues?

Thanks


Solution

  • Nevermind figured it out

    int n = 2;
    xt::xtensor_fixed<float, xt::xshape<1, 4>, xt::layout_type::column_major> original = {{ 1., 2., 3., 4. } };
    xt::xtensor<float, 2>::shape_type sh1 = {1, n};
    
    auto offsetfill = xt::full_like(xt::empty<float>(sh1), std::numeric_limits<double>::quiet_NaN());
    auto shiftvalues = xt::view(original, xt::all(), xt::range(n, original.size()));
    
    auto result = xt::concatenate(xt::xtuple(shiftvalues, offsetfill), 1);
    std::cout << result << std::endl;
    
    

    {{ 3., 4., nan., nan.}}

    Its a bit of a workaround I suppose, there's no appending values in the traditional way.

    1. Create a shape with specified offset size n
    2. Put shape into temp tensor. Fill the temp tensor with value of choice.
    3. Slice original tensor to keep values you want.
    4. Concatenate 3 and 2 to get desired result