-
-
Notifications
You must be signed in to change notification settings - Fork 377
Add boolean-based indexing (e.g., Y[Z == 1]
)
#3300
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
Comments
Yes, please. I've wanted this for a long time. Working from Stan itself, I think we'd like to be able to do this: vector[N] y = ...;
...
array[N] int<lower=0, upper=1> bool_idx = y > 0;
y[bool_idx] This would require two things:
In C++, There are many more things like this that would be useful. It'd be useful to do a survey of operations provided by the tidyverse and numpy/pandas to see what'd be useful---I often feel while writing Stan code that I want more of these operators and wind up having to write a lot of loops. |
I don't think we'd be able to treat an array of |
That's a good point. But we don't have a specific boolean type in Stan, do we? And I think in C++ it'd wind up being ambiguous because a The other place I want something like select is when I have matrix[M, N] x;
array[J] int<lower=1, upper=M> row_idxs;
int<lower=1, upper=N>[J] col_idxs;
vector[J] y = pairwise_index(x, row_idxs, col_idxs); where matrix[M, N] x;
array[J] tuple(int<lower=1, upper=M>, int<lower=1, upper=N>) idxs;
vector[J] = x[idxs]; |
Is adding a native stan |
Everything's fair for discussion.
I would personally be opposed to breaking backward compatibility for the sake of adding a proper boolean type. I would prefer to just add helper functions to do the indexing as I suggested above. I'm also not sure how the type of overload you're suggesting would play out in the C++ implementation, where there's a lot of fluidity between numerical and boolean types. |
There's some previous discussion around a |
Thanks for the pointer, @andrjohns. If we took the overloading route, which would be straightforward, it wouldn't allow us to support boolean-based indexing because applying an array to an array of int is already defined. That's why I've been thinking separate function like |
Summary:
A useful addition for simpler expressions with more complex indexing/subsetting would be to support logical indices. On the backend, this could just be syntactic sugar which first constructs an array of indices of the
true
values before passing to the existing indexing implementations.C++ example:
A simpler alternative, we could add an
std::vector<bool>
constructor forindex_multi
which performs the same logic above.This also has a bit of overlap with the use of
bool
-ish types in the Stan languages, so there might be some edge-case/interaction that I'm not thinking of. Thoughts?Current Version:
v2.35.0
The text was updated successfully, but these errors were encountered: