Skip to content
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

Implement Base.allequal and Base.allunique for weight vectors #894

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StatsBase"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
authors = ["JuliaStats"]
version = "0.34.1"
version = "0.34.2"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
8 changes: 8 additions & 0 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ Base.:(==)(x::UnitWeights, y::UnitWeights) = (x.len == y.len)
Base.isequal(x::AbstractWeights, y::AbstractWeights) = false
Base.:(==)(x::AbstractWeights, y::AbstractWeights) = false

# https://github.com/JuliaLang/julia/pull/43354
if VERSION >= v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
Base.allequal(wv::AbstractWeights) = allequal(wv.values)
Base.allequal(::UnitWeights) = true
end
Base.allunique(wv::AbstractWeights) = allunique(wv.values)
Base.allunique(wv::UnitWeights) = length(wv) <= 1

##### Weighted sum #####

## weighted sum over vectors
Expand Down
36 changes: 36 additions & 0 deletions test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,40 @@ end
end
end

@testset "allequal and allunique" begin
# General weights
for f in (weights, aweights, fweights, pweights)
@test allunique(f(Float64[]))
@test allunique(f([0.4]))
@test allunique(f([0.4, 0.3]))
@test !allunique(f([0.4, 0.4]))
@test allunique(f([0.4, 0.3, 0.5]))
@test !allunique(f([0.4, 0.4, 0.5]))
@test allunique(f([0.4, 0.3, 0.5, 0.35]))
@test !allunique(f([0.4, 0.3, 0.5, 0.4]))

if isdefined(Base, :allequal)
@test allequal(f(Float64[]))
@test allequal(f([0.4]))
@test allequal(f([0.4, 0.4]))
@test !allequal(f([0.4, 0.3]))
@test allequal(f([0.4, 0.4, 0.4, 0.4]))
@test !allunique(f([0.4, 0.4, 0.3, 0.4]))
end
end

# Uniform weights
@test allunique(uweights(0))
@test allunique(uweights(1))
@test !allunique(uweights(2))
@test !allunique(uweights(5))

if isdefined(Base, :allequal)
@test allequal(uweights(0))
@test allequal(uweights(1))
@test allequal(uweights(2))
@test allequal(uweights(5))
end
end

end # @testset StatsBase.Weights