-
-
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
Generalize f.(A)
to tuples?
#17411
Comments
Related #16986 |
Actually I have just a couple of lines over that PR that should cover the most relevant cases @inline broadcast(f, a::Tuple) = map(f, a)
@inline broadcast{N}(f, a::NTuple{N}, bs::Vararg{NTuple{N}}) = map(f, a, bs...) |
Supporting tuples sounds like a good idea at first glance, but before rushing into things, we should consider what we want to do with mixed cases. E.g. |
My first instinct would be to just treat a tuple as a 1d array, and produce array results. Anything else would be hard to generalize to mixed arguments. |
What I had in mind for
|
I was about to object that we don't have n-dimensional tuples, but then I realized that's not actually a problem since tuples are only 1-dimensional and if they're broadcastable as 1-d objects, then they must all be the same length. So @pabloferz's proposal seems sane to me. |
Let me make an alternative suggestion: if the arguments are all tuples, then we fall through to (Among other things, this will greatly simplify the implementation — I'd hate to have to write a specialized |
When I use tuples, I usually want to write code that is fast in the sense that no memory is allocated; data can be kept in registers or on the stack. If none of the input types is an array, then I would find it annoying if the result was an array. If the inputs consisted of sparse arrays and scalars, what should the result be? A dense array, or a sparse array? What if the input consisted of distributed arrays and scalars? Should all the combinations "X + scalar" lead to a dense array? It would certainly simplify the implementation, but would it be useful? |
If you're doing anything "interesting" with tuples, usually you want pretty specialized behavior: in the array code, for example, there are many cases where you need customized rules for what to do when one tuple is shorter than the other, or when you are "consuming" two tuples but the rules for advancing on to the next argument depend on the types of the first items in each tuple. I doubt that any generic function could replace very much of that logic. So I vote for keeping it dirt simple: insist that all inputs are tuples (and fall through to |
The current proposal over #16986 handles thing as per my #17411 (comment) above, in case someone wants to help review it. |
I also wonder whether RROR: MethodError: no method matching indices(::Base.Generator{UnitRange{Int64},##11#12})
Closest candidates are:
indices(::SimpleVector)
indices(::SimpleVector, ::Any)
indices(::Number)
...
in broadcast_shape(::Base.Generator{UnitRange{Int64},##11#12}) at ./broadcast.jl:21
in broadcast_t(::Function, ::Type{Any}, ::Base.Generator{UnitRange{Int64},##11#12}, ::Vararg{Base.Generator{UnitRange{Int64},##11#12},N}) at ./broadcast.jl:204
in broadcast(::Function, ::Base.Generator{UnitRange{Int64},##11#12}) at ./broadcast.jl:221
in eval(::Module, ::Any) at ./boot.jl:234
in macro expansion at ./REPL.jl:92 [inlined]
in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46 I couldn't find a good description of the meaning difference of |
At least |
One use case for tuples with |
Should be closed by #16986 |
@stevengj Why was this closed? The following example does not work:
Doesn't it fall under this umbrella? |
Not precisely. Please file a separate issue. Thanks! :) |
f.(A)
is shorthand forbroadcast(f, A)
, but also equivalent tomap(f, A)
. Since we supportmap
for both arrays and tuples, should we also supportf.(t)
for tuples? I may be missing it, but I can't find any place this has been discussed previously (e.g., #15032).The text was updated successfully, but these errors were encountered: