Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/xtensor/views/xview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,8 +1645,9 @@ namespace xt
{
if constexpr (lesser_condition<I>::value)
{
return sliced_access<I - integral_count_before<S...>(I) + newaxis_count_before<S...>(I + 1)>(
std::get<I + newaxis_count_before<S...>(I + 1)>(m_slices),
constexpr size_type slice_index = newaxis_skip<S...>(I);
return sliced_access<slice_index - integral_count_before<S...>(slice_index)>(
std::get<slice_index>(m_slices),
args...
);
}
Expand Down
39 changes: 39 additions & 0 deletions test/test_xview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,45 @@ namespace xt
EXPECT_EQ(a, b);
}

TEST(xview, assign_through_multiple_leading_newaxis)
{
SUBCASE("updates the underlying tensor for every element")
{
xt::xtensor<uint8_t, 2> tensor = xt::zeros<uint8_t>({4, 3});
auto view = xt::view(tensor, xt::newaxis(), xt::newaxis(), xt::newaxis(), xt::all(), xt::all());

uint8_t value = 0;
for (std::size_t row = 0; row < 4; ++row)
{
for (std::size_t col = 0; col < 3; ++col)
{
view(std::size_t{0}, std::size_t{0}, std::size_t{0}, row, col) = value;
EXPECT_EQ(tensor(row, col), value);
++value;
}
}

EXPECT_EQ(tensor, xt::arange<uint8_t>(12).reshape({4, 3}));
}

SUBCASE("preserves bool assignment semantics")
{
xt::xtensor<bool, 2> tensor = xt::zeros<bool>({4, 3});
auto view = xt::view(tensor, xt::newaxis(), xt::newaxis(), xt::newaxis(), xt::all(), xt::all());

for (std::size_t row = 0; row < 4; ++row)
{
for (std::size_t col = 0; col < 3; ++col)
{
view(std::size_t{0}, std::size_t{0}, std::size_t{0}, row, col) = true;
EXPECT_TRUE(tensor(row, col));
}
}

EXPECT_EQ(tensor, xt::ones<bool>({4, 3}));
}
}

TEST(xview, in_bounds)
{
xt::xtensor<size_t, 2> a = {{0, 1, 2}, {3, 4, 5}};
Expand Down
Loading