-
Notifications
You must be signed in to change notification settings - Fork 148
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
Broadcast of SArray with Array gives Array but we'd like SArray #744
Comments
|
So I've spent some time trying to do this. Overall I'm finding that it's really tricky to know exactly what to overload. I think the right way is to rework
I'll try to introduce some new kind of trait to control dispatch so that Some relevant links:
|
I thought this would work but I just realized that the problem might be that BroadcastStyle(
::AbstractArrayStyle{2},
BroadcastStyle(
StaticArrayStyle(Size(1, 3)), # StaticArrayStyle embeds Size
StaticArrayStyle(Size(4, 1)),
), # => StaticArrayStyle(Size(4, 3))
) # => StaticArrayStyle(Size(4, 3)) and BroadcastStyle(
BroadcastStyle(
::AbstractArrayStyle{2},
StaticArrayStyle(Size(1, 3)),
), # => ::AbstractArrayStyle{2}
StaticArrayStyle(Size(4, 1)),
) # => ::AbstractArrayStyle{2} yield different results if you want to handle everything in Is this what happened? Anyway, from this thinking, I think I agree |
Interesting point, but this isn't what happened! I had an alternative formulation where I converted all After doing all that, I realized that Base assigns The other point is that it's long been a goal to support more general static axes than |
By the way, I think the woes with |
I guess this might have worked (when ignoring tuples). I was thinking that you'd want to keep other array styles if you ended up not using the static style. For example, producing a sparse matrix from But I agree about the point that coding with interface where you can "touch" the original object is much easier. |
I’ve definitely moved towards instance traits computed on instances, for example in Dictionaries.jl. |
Yes thanks for bringing this up, I'm not really sure what to do with other array styles in this circumstance. I guess we could wrap them to extract later but that seems a bit heavy handed. A related issue here is in identifying the output static array type. Currently it's a leftmost rule but that seems fairly arbitrary. Perhaps a promotion-like system would make most sense. |
Maybe mixing |
Yeah that sounds pretty safe. I think the current version only captures |
Yes, currently only |
I meant to bring up sparse matrix just as an example of other custom styles. I don't think the solution (whatever it is) should refer to a concrete style (like
|
Hmm, a funny/annoying thing here is that — due to lazy fusing — the inferrability of static axes is not nested in the same way as the syntax tree. That's in contrast to the reduction of broadcast styles which follows the AST. I think this leads to the desire to rerun Side note: there's also the option of making all purely-StaticArray broadcasts eager rather than lazy by overriding |
Making Another disadvantage may be that it makes impossible to create computation tree using, e.g., |
As noted in #740, we currently have the following annoyance that broadcast with static and normal arrays doesn't result in static arrays:
I think this can be fixed in principle but the big difficulty with broadcasting is that the output
SArray
Size
may depend on the dynamicsize
of theArray
, for example we cannot infer the size of the second dimension inHowever, there's many circumstances in which we could make this work: basically any time the
StaticArray
has all dimensions withSize
greater than than 1, broadcast semantics require that theArray
which is being broadcast together with it match those dimensions (provided theArray
as number of dimensions less or equal to theStaticArray
).I'm not sure how to fix this, but one thought was to try stashing the
Size
trait within theBroadcastStyle
and use that to infer the output shape as part of the recursive reduction ofBroadCastStyle(a,b)
.@mateuszbaran's HybridArrays.jl may also have some code to deal with this kind of thing (https://github.com/mateuszbaran/HybridArrays.jl/blob/master/src/broadcast.jl)
The text was updated successfully, but these errors were encountered: