Skip to content

Commit

Permalink
Merge pull request #72 from JuliaAI/dev
Browse files Browse the repository at this point in the history
For a 0.1.14 release
  • Loading branch information
ablaom authored Jan 24, 2024
2 parents 759bedc + 6559cb5 commit 79ee322
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CategoricalDistributions"
uuid = "af321ab8-2d2e-40a6-b165-3d674595d28e"
authors = ["Anthony D. Blaom <[email protected]>"]
version = "0.1.13"
version = "0.1.14"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
14 changes: 12 additions & 2 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import Base: +, *, /, -
pdf_matrix(d::UnivariateFinite, L) = pdf.(d, L)
pdf_matrix(d::AbstractArray{<:UnivariateFinite}, L) = pdf(d, L)

function +(d1::U, d2::U) where U <: SingletonOrArray
function +(d1::UnivariateFinite{S, V}, d2::UnivariateFinite{S, V}) where {S, V}
L = classes(d1)
L == classes(d2) || throw(ERR_DIFFERENT_SAMPLE_SPACES)
return UnivariateFinite(L, pdf_matrix(d1, L) + pdf_matrix(d2, L))
end
function +(d1::UnivariateFiniteArray{S, V}, d2::UnivariateFiniteArray{S, V}) where {S, V}
L = classes(d1)
L == classes(d2) || throw(ERR_DIFFERENT_SAMPLE_SPACES)
return UnivariateFinite(L, pdf_matrix(d1, L) + pdf_matrix(d2, L))
Expand All @@ -27,7 +32,12 @@ end
-(d::UnivariateFinite) = _minus(d, UnivariateFinite)
-(d::UnivariateFiniteArray) = _minus(d, UnivariateFiniteArray)

function -(d1::U, d2::U) where U <: SingletonOrArray
function -(d1::UnivariateFinite{S, V}, d2::UnivariateFinite{S, V}) where {S, V}
L = classes(d1)
L == classes(d2) || throw(ERR_DIFFERENT_SAMPLE_SPACES)
return UnivariateFinite(L, pdf_matrix(d1, L) - pdf_matrix(d2, L))
end
function -(d1::UnivariateFiniteArray{S, V}, d2::UnivariateFiniteArray{S, V}) where {S, V}
L = classes(d1)
L == classes(d2) || throw(ERR_DIFFERENT_SAMPLE_SPACES)
return UnivariateFinite(L, pdf_matrix(d1, L) - pdf_matrix(d2, L))
Expand Down
3 changes: 3 additions & 0 deletions test/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ end
L = ["yes", "no"]
d1 = UnivariateFinite(L, rand(rng, 2), pool=missing)
d2 = UnivariateFinite(L, rand(rng, 2), pool=missing)
df32 = UnivariateFinite(L, rand(rng, Float32, 2), pool=missing)

@testset "arithmetic" begin

# addition and subtraction:
for op in [:+, :-]
quote
s = $op(d1, d2 )
s2 = $op(d1, df32 )
@test $op(pdf.(d1, L), pdf.(d2, L)) pdf.(s, L)
@test $op(pdf.(d1, L), pdf.(df32, L)) pdf.(s2, L)
end |> eval
end

Expand Down

0 comments on commit 79ee322

Please sign in to comment.