Skip to content

Commit 5a65151

Browse files
committed
remove restriction on scalar type for multiplication & division
1 parent d6dc9a4 commit 5a65151

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/arithmetic.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ end
4646
-(d1::U, d2::U) where U <: UnivariateFiniteArray =
4747
_minus(d1, d2, UnivariateFiniteArray)
4848

49-
# TODO: remove type restrction on `x` in the following methods if
50-
# https://github.com/JuliaStats/Distributions.jl/issues/1438 is
51-
# resolved. Currently we'd have a method ambiguity
49+
# It seems that the restriction `x::Number` below (applying only to the
50+
# array case) is unavoidable because of a method ambiguity with
51+
# `Base.*(::AbstractArray, ::Number)`.
5252

5353
function _times(d, x, T)
5454
S = d.scitype
@@ -59,8 +59,10 @@ function _times(d, x, T)
5959
end
6060
return T(d.scitype, decoder, prob_given_ref)
6161
end
62-
*(d::UnivariateFinite, x::Real) = _times(d, x, UnivariateFinite)
63-
*(d::UnivariateFiniteArray, x::Real) = _times(d, x, UnivariateFiniteArray)
62+
*(d::UnivariateFinite, x) = _times(d, x, UnivariateFinite)
63+
*(d::UnivariateFiniteArray, x::Number) = _times(d, x, UnivariateFiniteArray)
6464

65-
*(x::Real, d::SingletonOrArray) = d*x
66-
/(d::SingletonOrArray, x::Real) = d*inv(x)
65+
*(x, d::UnivariateFinite) = d*x
66+
*(x::Number, d::UnivariateFiniteArray) = d*x
67+
/(d::UnivariateFinite, x) = d*inv(x)
68+
/(d::UnivariateFiniteArray, x::Number) = d*inv(x)

src/methods.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,6 @@ end
333333
# # BROADCASTING OVER SINGLE UNIVARIATE FINITE
334334

335335
# This mirrors behaviour assigned Distributions.Distribution objects,
336-
# which allows `pdf.(d::UnivariateFinite, support(d))` to work.
336+
# which allows `pdf.(d::UnivariateFinite, support(d))` to work.
337337

338338
Broadcast.broadcastable(d::UnivariateFinite) = Ref(d)

test/arithmetic.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ d2 = UnivariateFinite(L, rand(rng, 2), pool=missing)
3232
# division by scalar:
3333
d3 = d1/42
3434
@test pdf.(d3, L) pdf.(d1, L)/42
35+
36+
# "probabilities" that aren't:
37+
d = UnivariateFinite(L, randn(rng, 2) + im*randn(2), pool=missing)
38+
@test pdf.(3*d -(4*d)/2, L) pdf.(d, L)
3539
end
3640

3741
p = [0.1, 0.9]

0 commit comments

Comments
 (0)