xtensor-stack / xtensor-python

Python bindings for xtensor
BSD 3-Clause "New" or "Revised" License
345 stars 58 forks source link

from pytensor to xtensor without copying the underlying data and vice-versa #161

Closed bordingj closed 6 years ago

bordingj commented 6 years ago

Is there a way to go from a xt::pytensor to xt::xtensor and back again without copying the underlying data ? If so, it would be nice with some documentation on how to do it. If not, then I have a feature request :) .

wolfv commented 6 years ago

Hi bordingj,

no, there is no way to do that. but you can use all functions that you have on xtensor on the pytensor as well.

If you want to write expression agnostic code, writing templated code in this fashion is recommended:

template <class T>
auto func(xexpression<T>& expr)
{
    auto& de = expr.derived_cast();
    de[0] += 123;
    return de;
}
JohanMabille commented 6 years ago

xtensor owns its data buffer and manages the memory with C++ allocators whereas the memory of the pytensor is managed by python allocators, so you cannot transfer ownership from one world to the other.

As mentioned by @wolfv, pytensor is a valid expression so it supports all the operations / features of xtensor and you can write code agnostic from the data backend.

bordingj commented 6 years ago

I understand. Thank you.