-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Element-wise in
operator
#5212
Comments
I don't think vectorizing everything is the way forward. For one thing, you might mean either
or
Adding a dot is not expressive enough. |
Agreed. Vectorization is generally only safe when the base definition only makes sense for scalars. For operations where one of more arguments can be containers, it's a nightmare. |
I can't find any context where |
Apologies, but I don't think there is going to be a |
Sure, you can see that it doesn't make sense, but it's still syntactically ambiguous. |
Syntactically it's not ambiguous, since you could define |
Yes, let's see if it's a real pain-point and at that point, it will be clearer what to do. |
I just bumped up against this while working with filtering a DataFrame. I spent 20 minutes or so searching online for a way to make it work because I didn't believe it wasn't possible. When I write |
@tristanmarkwell I would do something like this: julia> using DataFrames
julia> events = DataFrame(EVENT_TYPE = rand('a':'z', 100));
julia> function Base.in{T1,T2<:Integer}(xs::AbstractArray{T1}, ys::Range{T2})
Bool[any(x in ys) for x in xs]
end
in (generic function with 19 methods)
julia> function Base.in{T1,T2}(xs::AbstractArray{T1}, ys::Range{T2})
Bool[any(x in ys) for x in xs]
end
in (generic function with 20 methods)
julia> function Base.in{T1,T2}(xs::AbstractArray{T1}, ys::AbstractArray{T2})
Bool[any(x in ys) for x in xs]
end
in (generic function with 21 methods)
julia> @time events[(events[:EVENT_TYPE] .== 'a') | (events[:EVENT_TYPE] .== 'z'), :];
0.370084 seconds (96.39 k allocations: 4.353 MB, 8.23% gc time)
julia> @time events[(events[:EVENT_TYPE] .== 'a') | (events[:EVENT_TYPE] .== 'z'), :];
0.000048 seconds (36 allocations: 10.109 KB)
julia> @time events[events[:EVENT_TYPE] in ['a', 'z'], :];
0.046778 seconds (32.42 k allocations: 1.471 MB)
julia> @time events[events[:EVENT_TYPE] in ['a', 'z'], :];
0.000028 seconds (24 allocations: 1.656 KB)
julia> @time events[events[:EVENT_TYPE] in 'a':'z', :];
0.047705 seconds (70.72 k allocations: 2.992 MB)
julia> @time events[events[:EVENT_TYPE] in 'a':'z', :];
0.000040 seconds (124 allocations: 5.891 KB) Now imagine its |
It is a a bit cumbersome with large expressions and slower because of extra memory allocations. @JeffBezanson for
With out the So it's not ambiguous with neither:
Please excuse me if I'm wrong. |
This occurred to me while reading mbauman@d83dad3:
What do you think about having an element-wise
in
operator? Despite not having a dedicated symbol, I thinkin
is a true operator, and it's the only one without an element-wise version.This would allow writing e.g.
A .in (1, 2)
instead of the convoluted form above (and thus reduce priority mistakes). It's very useful when working with data where you often need to test for equality against a longer series of values; or simply when testing for equality against a set of values stored in a variable.I grant you
.in
looks a little weird, but not more than.!=
. ;-)The text was updated successfully, but these errors were encountered: