Skip to content

Commit

Permalink
Merge pull request #37 from brenhinkeller/check-empty
Browse files Browse the repository at this point in the history
`@turbo check_empty=true` in most all cases
  • Loading branch information
brenhinkeller authored May 13, 2023
2 parents 35b45f3 + 8abadfd commit 3a0f83f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NaNStatistics"
uuid = "b946abbf-3ea7-4610-9019-9858bfdeaf2d"
authors = ["C. Brenhin Keller"]
version = "0.6.27"
version = "0.6.28"

[deps]
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
Expand All @@ -11,7 +11,7 @@ Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"

[compat]
IfElse = "0.1"
LoopVectorization = "0.12.159"
LoopVectorization = "0.12.113"
PrecompileTools = "1"
Static = "0.8"
julia = "1.8"
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayStats/ArrayStats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"""
function countnans(A)
n = 0
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
n += A[i]!=A[i]
end
return n
Expand All @@ -28,7 +28,7 @@
"""
function countnotnans(A)
n = 0
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
n += A[i]==A[i]
end
return n
Expand Down
8 changes: 4 additions & 4 deletions src/ArrayStats/nancov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function _nancov(x::AbstractVector, y::AbstractVector, corrected::Bool, μᵪ::N
# Calculate covariance
σᵪᵧ == zero(promote_type(typeof(μᵪ), typeof(μᵧ), Int))
n = 0
@turbo for i eachindex(x,y)
@turbo check_empty=true for i eachindex(x,y)
δᵪ = x[i] - μᵪ
δᵧ = y[i] - μᵧ
δ² = δᵪ * δᵧ
Expand All @@ -19,7 +19,7 @@ function _nancov(x::AbstractVector, y::AbstractVector, corrected::Bool)
n = 0
Σᵪ = ∅ᵪ = zero(eltype(x))
Σᵧ = ∅ᵧ = zero(eltype(y))
@turbo for i eachindex(x,y)
@turbo check_empty=true for i eachindex(x,y)
xᵢ, yᵢ = x[i], y[i]
notnan = (xᵢ==xᵢ) & (yᵢ==yᵢ)
n += notnan
Expand Down Expand Up @@ -110,7 +110,7 @@ function _nancor(x::AbstractVector, y::AbstractVector, corrected::Bool)
n = 0
Σᵪ = ∅ᵪ = zero(eltype(x))
Σᵧ = ∅ᵧ = zero(eltype(y))
@turbo for i eachindex(x,y)
@turbo check_empty=true for i eachindex(x,y)
xᵢ, yᵢ = x[i], y[i]
notnan = (xᵢ==xᵢ) & (yᵢ==yᵢ)
n += notnan
Expand All @@ -124,7 +124,7 @@ function _nancor(x::AbstractVector, y::AbstractVector, corrected::Bool)
# Pairwise nan-variances
σ²ᵪ = ∅ᵪ = zero(typeof(μᵪ))
σ²ᵧ = ∅ᵧ = zero(typeof(μᵧ))
@turbo for i eachindex(x,y)
@turbo check_empty=true for i eachindex(x,y)
δᵪ = x[i] - μᵪ
δᵧ = y[i] - μᵧ
notnan = (δᵪ==δᵪ) & (δᵧ==δᵧ)
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayStats/nanmean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function _nanmean(A::StridedArray, ::Colon)
Tₒ = Base.promote_op(/, eltype(A), Int)
n = 0
Σ == zero(Tₒ)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
Aᵢ = A[i]
notnan = Aᵢ==Aᵢ
n += notnan
Expand All @@ -65,7 +65,7 @@ end
function _nanmean(A::StridedArray{<:Integer}, ::Colon)
Tₒ = Base.promote_op(/, eltype(A), Int)
Σ = zero(Tₒ)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
Σ += A[i]
end
return Σ / length(A)
Expand Down
2 changes: 1 addition & 1 deletion src/ArrayStats/nanstd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export nanstd

sqrt!(x::Number) = sqrt(x)
function sqrt!(A::StridedArray)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
A[i] = sqrt(A[i])
end
return A
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayStats/nansum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end
function _nansum(A::StridedArray, ::Colon)
Tₒ = Base.promote_op(+, eltype(A), Int)
Σ == zero(Tₒ)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
Aᵢ = A[i]
notnan = Aᵢ==Aᵢ
Σ += ifelse(notnan, Aᵢ, ∅)
Expand All @@ -63,7 +63,7 @@ end
function _nansum(A::StridedArray{<:Integer}, ::Colon)
Tₒ = Base.promote_op(+, eltype(A), Int)
Σ = zero(Tₒ)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
Σ += A[i]
end
return Σ
Expand Down
16 changes: 8 additions & 8 deletions src/ArrayStats/nanvar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ function _nanvar(::Nothing, corrected::Bool, A::StridedArray, ::Colon)
Tₒ = Base.promote_op(/, eltype(A), Int)
n = 0
Σ == zero(Tₒ)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
Aᵢ = A[i]
notnan = Aᵢ==Aᵢ
n += notnan
Σ += ifelse(notnan, Aᵢ, ∅)
end
μ = Σ / n
σ² == zero(typeof(μ))
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
δ = A[i] - μ
notnan = δ==δ
σ² += ifelse(notnan, δ * δ, ∅)
Expand All @@ -68,12 +68,12 @@ function _nanvar(::Nothing, corrected::Bool, A::StridedArray{<:Integer}, ::Colon
Tₒ = Base.promote_op(/, eltype(A), Int)
n = length(A)
Σ = zero(Tₒ)
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
Σ += A[i]
end
μ = Σ / n
σ² = zero(typeof(μ))
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
δ = A[i] - μ
σ² += δ * δ
end
Expand Down Expand Up @@ -109,7 +109,7 @@ _nanvar(μ::Number, corrected::Bool, A, dims::Tuple) = _nanvar!([μ], corrected,
function _nanvar::Number, corrected::Bool, A::AbstractArray, ::Colon)
n = 0
σ² == zero(typeof(μ))
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
δ = A[i] - μ
notnan = δ==δ
n += notnan
Expand All @@ -120,7 +120,7 @@ end
function _nanvar::Number, corrected::Bool, A::AbstractArray{<:Integer}, ::Colon)
σ² = zero(typeof(μ))
if μ==μ
@turbo for i eachindex(A)
@turbo check_empty=true for i eachindex(A)
δ = A[i] - μ
σ² += δ * δ
end
Expand Down Expand Up @@ -149,12 +149,12 @@ end
# N = sum(mask, dims=region)
# Σ = sum(A.*mask, dims=region)./N
# δ = A .- Σ # Subtract mean, using broadcasting
# @turbo for i ∈ eachindex(δ)
# @turbo check_empty=true for i ∈ eachindex(δ)
# δᵢ = δ[i]
# δ[i] = ifelse(mask[i], δᵢ * δᵢ, 0)
# end
# B .= sum(δ, dims=region)
# @turbo for i ∈ eachindex(B)
# @turbo check_empty=true for i ∈ eachindex(B)
# B[i] = B[i] / max(N[i] - corrected, 0)
# end
# return B
Expand Down
20 changes: 11 additions & 9 deletions src/Sorting/quicksort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ function quickselect!(A::AbstractArray, iₗ=firstindex(A), iᵤ=lastindex(A), k

# Count up elements that must be moved to upper partition
Nᵤ = 0
# @turbo
@inbounds for i = (iₗ+1):iᵤ
if A[i] >= pivot
Nᵤ += 1
end
# Nᵤ += Int(A[i] >= pivot)
@turbo check_empty=true for i = (iₗ+1):iᵤ
Nᵤ += Int(A[i] >= pivot)
end
Nₗ = N - Nᵤ

Expand All @@ -109,9 +105,15 @@ function quickselect!(A::AbstractArray, iₗ=firstindex(A), iᵤ=lastindex(A), k
iₚ = iₗ + Nₗ - 1
A[iₗ], A[iₚ] = A[iₚ], A[iₗ]
# Recurse: select from partition containing k
(iₗ <= k < iₚ) && quickselect!(A, iₗ, iₚ, k)
(iₚ < k <= iᵤ) && quickselect!(A, iₚ+1, iᵤ, k)
return A[k]
if iₚ==k
return A[k]
elseif k < iₚ
Nₗ == 2 && return A[iₗ]
quickselect!(A, iₗ, iₚ, k)
else
Nᵤ == 2 && return A[iᵤ]
quickselect!(A, iₚ+1, iᵤ, k)
end
end


Expand Down

2 comments on commit 3a0f83f

@brenhinkeller
Copy link
Owner Author

Choose a reason for hiding this comment

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

@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/83519

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.6.28 -m "<description of version>" 3a0f83ff86ab10539cfb7c356e94b6521e2309e3
git push origin v0.6.28

Please sign in to comment.