Skip to content

Commit

Permalink
Merge pull request #27 from JuliaSIMD/non-inplace
Browse files Browse the repository at this point in the history
Add convencience non-inplace methods for median and percentiles
  • Loading branch information
brenhinkeller committed Mar 24, 2023
2 parents 02e024b + 37efc39 commit 446ac1a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "VectorizedStatistics"
uuid = "3b853605-1c98-4422-8364-4bd93ee0529e"
authors = ["C. Brenhin Keller", "Chris Elrod"]
version = "0.5.2"
version = "0.5.3"

[deps]
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
Expand Down
3 changes: 3 additions & 0 deletions src/vmedian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ _vmedian!(A, ::Colon, region) = _vmedian!(A, region)
_vmedian!(A, region, ::Colon) = reducedims(_vmedian!(A, region), region)
export vmedian!

vmedian(A; kwargs...) = vmedian!(copy(A); kwargs...)
export vmedian

# Reduce one dim
_vmedian!(A, dims::Int) = _vmedian!(A, (dims,))

Expand Down
5 changes: 5 additions & 0 deletions src/vquantile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ julia> A = [1 2 3; 4 5 6; 7 8 9]
vpercentile!(A, p::Number; dim=:, dims=:) = _vquantile!(A, p/100, dim, dims)
export vpercentile!

vpercentile(A, p; kwargs...) = vpercentile!(copy(A), p; kwargs...)
export vpercentile

"""
```julia
Expand Down Expand Up @@ -106,6 +108,9 @@ _vquantile!(A, q, ::Colon, region) = _vquantile!(A, q, region)
_vquantile!(A, q, region, ::Colon) = reducedims(_vquantile!(A, q, region), region)
export vquantile!

vquantile(A, q; kwargs...) = vquantile!(copy(A), q; kwargs...)
export vquantile

# Reduce one dim
_vquantile!(A, q::Real, dims::Int) = _vquantile!(A, q, (dims,))

Expand Down
24 changes: 12 additions & 12 deletions test/testSorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,22 @@
@test vmedian!(1:10) == 5.5

A = rand(100)
@test vmedian!(copy(A)) == median(A)
@test vmedian!(copy(A)) == vmedian(A) == median(A)
A = rand(10_000)
@test vmedian!(copy(A)) == median(A)

A = rand(55,82)
@test vmedian!(copy(A)) == median(A)
@test vmedian!(copy(A)) == vmedian(A) == median(A)
@test vmedian!(copy(A), dims=1) == median(A, dims=1)
@test vmedian!(copy(A), dims=2) == median(A, dims=2)

A = rand(10,11,12)
@test vmedian!(copy(A)) == median(A)
@test vmedian!(copy(A), dims=1) == median(A, dims=1)
@test vmedian!(copy(A), dims=2) == median(A, dims=2)
@test vmedian!(copy(A), dims=3) == median(A, dims=3)
@test vmedian!(copy(A), dims=(1,2)) == median(A, dims=(1,2))
@test vmedian!(copy(A), dims=(2,3)) == median(A, dims=(2,3))
@test vmedian!(copy(A)) == vmedian(A) == median(A)
@test vmedian!(copy(A), dims=1) == vmedian(A, dims=1) == median(A, dims=1)
@test vmedian!(copy(A), dims=2) == vmedian(A, dims=2) == median(A, dims=2)
@test vmedian!(copy(A), dims=3) == vmedian(A, dims=3) == median(A, dims=3)
@test vmedian!(copy(A), dims=(1,2)) == vmedian(A, dims=(1,2)) == median(A, dims=(1,2))
@test vmedian!(copy(A), dims=(2,3)) == vmedian(A, dims=(2,3)) == median(A, dims=(2,3))

## --- Test vpercentile! / vquantile!

Expand All @@ -169,25 +169,25 @@
@test vpercentile!(collect(1:10), 50) == 5.5

A = rand(100)
@test vpercentile!(copy(A), 50) == median(A)
@test vpercentile!(copy(A), 50) == vpercentile(A, 50) == median(A)
A = rand(10_000)
@test vpercentile!(copy(A), 50) == median(A)

A = rand(55,82)
@test vpercentile!(copy(A), 50) == median(A)
@test vpercentile!(copy(A), 50) == vpercentile(A, 50) == median(A)
@test vpercentile!(copy(A), 50, dims=1) == median(A, dims=1)
@test vpercentile!(copy(A), 50, dims=2) == median(A, dims=2)

A = rand(10,11,12)
@test vpercentile!(copy(A), 50) == median(A)
@test vpercentile!(copy(A), 50) == vpercentile(A, 50) == median(A)
@test vpercentile!(copy(A), 50, dims=1) == median(A, dims=1)
@test vpercentile!(copy(A), 50, dims=2) == median(A, dims=2)
@test vpercentile!(copy(A), 50, dims=3) == median(A, dims=3)
@test vpercentile!(copy(A), 50, dims=(1,2)) == median(A, dims=(1,2))
@test vpercentile!(copy(A), 50, dims=(2,3)) == median(A, dims=(2,3))

A = rand(100)
@test median(A) == vpercentile!(copy(A), 50) == vquantile!(copy(A), 0.5)
@test median(A) == vpercentile!(copy(A), 50) == vquantile!(copy(A), 0.5) == vquantile(A, 0.5)
@test vpercentile!(copy(A), 50) == vquantile!(copy(A), 0.5)

## ---

2 comments on commit 446ac1a

@brenhinkeller
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register
Release notes:

  • Add non-inplace convenience methods for medians and percentiles/quantiles

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/80230

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" 446ac1a8b4d8015960bfec3e65a1f0429d62ebb2
git push origin v0.5.3

Please sign in to comment.