xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

Assigning to a view of a row of a matrix does not change the matrix

Open Kevinpgalligan opened this issue 4 years ago • 0 comments

I want to pass around a reference to the row of a matrix and use that reference to edit the row. This can be done in numpy:

>>> X = np.array([[1, 2], [3, 4]])
>>> r = X[0]
>>> r[0] = 5
>>> X
array([[5, 2],
       [3, 4]])

But when I try to do the same thing with xtensor...

int main() {
    xt::xarray<int> X = xt::zeros<int>({3, 3});
    xt::xarray<int> r = xt::row(X, 0);
    r(0) = 1;
    r(2) = 1;
    std::cout << X << std::endl;
    std::cout << r << std::endl;
}

...the output is:

{{0, 0, 0},
 {0, 0, 0},
 {0, 0, 0}}
{1, 0, 1}

Am I misusing views, somehow? I thought that views don't make a copy and can be used to modify the underlying data.

Kevinpgalligan avatar Jan 20 '22 13:01 Kevinpgalligan