Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different from numpy? #1716

Closed
msnh2012 opened this issue Aug 23, 2019 · 4 comments
Closed

Different from numpy? #1716

msnh2012 opened this issue Aug 23, 2019 · 4 comments
Labels

Comments

@msnh2012
Copy link

c++ code:

    xt::xarray<int> aad ={{1, 2},{4, 5}};
    xt::xarray<bool> v = {{true,false}};
    xt::filter(aad,v) = 0;
    std::cout<<aad;

result:

{{0, 2},
 {4, 5}}

in numpy:

{{0, 0},
 {4, 5}}
@JohanMabille
Copy link
Member

Can you post your numpy code please?

@msnh2012
Copy link
Author

msnh2012 commented Aug 23, 2019

@JohanMabille numpy code

import numpy as np
a = np.array(((1,2),(4,5)))
b = np.array((True,False))
a[b]=0

@JohanMabille
Copy link
Member

xt::filter is not boolean indexing (like you do in Numpy), it returns a one-dimensional views holding elements of an xexpression that verify a given condition. Internally, the implementation is:

auto indices = argwhere<L>(std::forward<O>(condition));
using view_type = xindex_view<xclosure_t<E>, decltype(indices)>;
return view_type(std::forward<E>(e), std::move(indices));

That is, the selected elements in the view have the same indices as the true elements in the condition. Usually you don't need to bother with the implementation detial because the condition depends on the expression you want to filter:

auto f = filter(a, a >= 5);

So here the condition (a >= 5) and a have the same shape and the result is intuitive.

In your case, the condition and the expression that you want to filter do not have the same shape, and there is a bug in the filter implementation where the condition is not broadcasted (see #963 ).
Therefore, the vector of indices for selecting elements contains only only one index (i.e. (0, 0)). When the broadcasting issue is fixed, your example will be equivalent to:

xt::filter(aad, {{true, false}, {true, false}}) = 0;

resulting in

{{0, 2},
 {0, 5}}

So it will still be different from Numpy.

@msnh2012
Copy link
Author

I got it. Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants