xtensor-stack / xtensor-python

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

Broadcasting problem on basic operations? #279

Closed ThibHlln closed 2 years ago

ThibHlln commented 2 years ago

Hi there,

It seems that there may be some broadcasting problems with xt::pyarray on basic operations with operator+, operator-, operator*, and operator/.

For example, with operator-:

#define FORCE_IMPORT_ARRAY
#include "xtensor-python/pyarray.hpp"

xt::pyarray<double> arr1
        {{ 4., 5., 6., 7. }};
xt::pyarray<double> arr2
        {{ 4., 3., 2., 1. },
         { 4., 3., 2., 1. },
         { 4., 3., 2., 1. }};

std::cout << arr1 - arr2 << std::endl;

returns

{{ 0.000000e+000,  2.000000e+000,  4.000000e+000,  6.000000e+000},
 { 2.889806e+204, -3.000000e+000, -2.000000e+000,  1.000000e+000},
 { 0.000000e+000,  3.000000e+000,  1.722192e+097, -1.000000e+000}}

For simplicity, I have added some tests to your existing test suite in a fork of mine, so that you can try to reproduce the problem yourself. See: https://github.com/ThibHlln/xtensor-python/blob/broadcasting-problem/test_python/main.cpp#L114-L132 https://github.com/ThibHlln/xtensor-python/blob/broadcasting-problem/test_python/test_pyarray.py#L66-L108

All 4 tests fail for me.

Could you tell me if these tests pass for you please? And if so, any idea what might be wrong here?

Thank you for your help, Thibault

Note: I am building/running this on Windows 10, version 1803 with pybind11==2.9.2, numpy==1.22.3, xtensor==0.24.1, xtl==0.7.4 and using xtensor-python from the master branch.

EDIT If this helps, it works fine if I use xt::pytensor<double, 2> in place of xt::pyarray<double>.

tdegeus commented 2 years ago

That sounds like a bug. Thanks for reporting.

JohanMabille commented 2 years ago

Hi,

Does the following work?

py::array<double> res = arr1 - arr2;
std::cout << res << std::endl;
ThibHlln commented 2 years ago

Hi,

Sorry, my example was misleading: this is when the Python interface is used that the problem occurs as I tried to demonstrate in your existing testing suite with: https://github.com/ThibHlln/xtensor-python/blob/broadcasting-problem/test_python/main.cpp#L114-L132 https://github.com/ThibHlln/xtensor-python/blob/broadcasting-problem/test_python/test_pyarray.py#L66-L108

If the pyarrays are defined directly in C++, the broadcasting works. This is when working with pyarrays defined via the Python bindings that the problem occurs (observable both in the returned numpy array and if I do a std::cout << arr1 - arr2 << std::endl; in C++).

JohanMabille commented 2 years ago

I can reproduce the problem, I am investigating.

ThibHlln commented 2 years ago

Thank you! This works perfectly now.

By the way, in https://github.com/xtensor-stack/xtensor-python/blob/6a286681c48c35d3df342f291938f4825b20c0a3/test_python/test_pyarray.py, perhaps you did not mean to comment out all the other tests?