xtensor-python icon indicating copy to clipboard operation
xtensor-python copied to clipboard

Doc: add a section for code exposed to both C++ and python

Open JohanMabille opened this issue 7 years ago • 5 comments

This section should describe the best practices when functions need to be exposed to both C++ (operating on xtensor / xarray) and Python (operating on pytensor / pyarray)

JohanMabille avatar Jul 26 '18 14:07 JohanMabille

Are there any examples of doing this anywhere I could take a look at?

pelson avatar Sep 17 '18 05:09 pelson

You can find an example here. The idea is to put the code in dedicated headers and cpp files that can be compiled in C++ mode only, and to expose them to the python in a single cpp module file.

JohanMabille avatar Sep 17 '18 06:09 JohanMabille

I am a new xtensor-python user and I found the above example code is nice. However, most of them are related to pyvectorize.

It would be nice if more use cases could be included. For example, I want to modify an numpy.array in-place and I have the following piece of code:

template<typename T>
inline void movingAverage(xt::pyarray<T>& ma, const xt::pyarray<T>& data, size_t count) {
  ma = ma + (data - ma) / T(count);
}

However, on the Python side, I cannot see the change. If I change xt::pyarray to xt::xarray and run the function only in C++, it works. Could you please let me know how to make the Python binding work? Thank you!

zhujun98 avatar Jul 17 '19 12:07 zhujun98

However, on the Python side, I cannot see the change

This appears to be a legitimate bug. Will be getting back to you shortly with a fix.

SylvainCorlay avatar Jul 17 '19 12:07 SylvainCorlay

I have the same problem. Have we solved this question? How to change pyarray from numpy, then see the changes in python

OUCyf avatar Jan 24 '23 00:01 OUCyf