Replace broadcasting over distributions with broadcasting with partially applied functions #1818
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Broadcasting treats
UnivariateDistribution
s as scalars due toDistributions.jl/src/common.jl
Line 175 in 9e72f1f
Ref
s are suboptimal sinceUnfortunately, replacing
Distributions.jl/src/common.jl
Line 175 in 9e72f1f
broadcastable(d::UnivariateDistribution) = (d,)
is not an option since broadcasting with tuples is not identical to broadcasting withRef
s (see, e.g., https://discourse.julialang.org/t/add-scalar-macro-to-base/50255/5).However, broadcasting over distributions is actually not needed as one can just broadcast with a partially applied function that captures the distribution instead. Therefore this PR changes all broadcasts over distributions in Distributions.jl to broadcasts with such partially applied functions (and quite often this implies that broadcasting is not needed anymore and a simple
map
would be sufficient).Fixes #1815.
As a side note, IMO it would be more convenient for users and downstream packages that would like to follow the same pattern if we would define
pdf(d::Distribution) = Base.Fix1(pdf, d)
etc. (similar to==(x)
etc. in Base) (ref #966). Unfortunately,pdf(d::DiscreteUnivariateDistribution)
is already defined in a different way:Distributions.jl/src/deprecates.jl
Line 43 in 9e72f1f