From e6a902ec53b195c218de7642f85485f77dc3819b Mon Sep 17 00:00:00 2001 From: Moelf Date: Mon, 16 May 2022 15:15:43 -0400 Subject: [PATCH 1/4] move to modern indexing style --- src/deviation.jl | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/src/deviation.jl b/src/deviation.jl index dd393afc3..eaa1022fe 100644 --- a/src/deviation.jl +++ b/src/deviation.jl @@ -9,13 +9,10 @@ Count the number of indices at which the elements of the arrays `a` and `b` are equal. """ function counteq(a::AbstractArray, b::AbstractArray) - n = length(a) - length(b) == n || throw(DimensionMismatch("Inconsistent lengths.")) + length(a) == length(b) || throw(DimensionMismatch("Inconsistent lengths.")) c = 0 - for i = 1:n - @inbounds if a[i] == b[i] - c += 1 - end + for i = eachindex(a, b) + c += (a[i] == b[i]) end return c end @@ -28,13 +25,10 @@ Count the number of indices at which the elements of the arrays `a` and `b` are not equal. """ function countne(a::AbstractArray, b::AbstractArray) - n = length(a) - length(b) == n || throw(DimensionMismatch("Inconsistent lengths.")) + length(a) == length(b) || throw(DimensionMismatch("Inconsistent lengths.")) c = 0 - for i = 1:n - @inbounds if a[i] != b[i] - c += 1 - end + for i = eachindex(a, b) + c += (a[i] != b[i]) end return c end @@ -47,11 +41,10 @@ Compute the squared L2 distance between two arrays: ``\\sum_{i=1}^n |a_i - b_i|^ Efficient equivalent of `sumabs2(a - b)`. """ function sqL2dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number - n = length(a) - length(b) == n || throw(DimensionMismatch("Input dimension mismatch")) + length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - for i = 1:n - @inbounds r += abs2(a[i] - b[i]) + for i = eachindex(a, b) + r += abs2(a[i] - b[i]) end return r end @@ -75,11 +68,10 @@ Compute the L1 distance between two arrays: ``\\sum_{i=1}^n |a_i - b_i|``. Efficient equivalent of `sum(abs, a - b)`. """ function L1dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number - n = length(a) - length(b) == n || throw(DimensionMismatch("Input dimension mismatch")) + length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - for i = 1:n - @inbounds r += abs(a[i] - b[i]) + for i = eachindex(a, b) + r += abs(a[i] - b[i]) end return r end @@ -94,11 +86,10 @@ two arrays: ``\\max_{i\\in1:n} |a_i - b_i|``. Efficient equivalent of `maxabs(a - b)`. """ function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number - n = length(a) - length(b) == n || throw(DimensionMismatch("Input dimension mismatch")) + length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - for i = 1:n - @inbounds v = abs(a[i] - b[i]) + for i = eachindex(a, b) + v = abs(a[i] - b[i]) if r < v r = v end @@ -118,9 +109,9 @@ Efficient equivalent of `sum(a*log(a/b)-a+b)`. function gkldiv(a::AbstractArray{T}, b::AbstractArray{T}) where T<:AbstractFloat n = length(a) r = 0.0 - for i = 1:n - @inbounds ai = a[i] - @inbounds bi = b[i] + for i = eachindex(a, b) + ai = a[i] + bi = b[i] if ai > 0 r += (ai * log(ai / bi) - ai + bi) else From e105e92b0f442e6c3b51616e454e3b24ef0c7565 Mon Sep 17 00:00:00 2001 From: Jerry Ling Date: Mon, 16 May 2022 18:23:52 -0400 Subject: [PATCH 2/4] add back inbounts --- src/deviation.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/deviation.jl b/src/deviation.jl index eaa1022fe..03ffbb776 100644 --- a/src/deviation.jl +++ b/src/deviation.jl @@ -11,7 +11,7 @@ Count the number of indices at which the elements of the arrays function counteq(a::AbstractArray, b::AbstractArray) length(a) == length(b) || throw(DimensionMismatch("Inconsistent lengths.")) c = 0 - for i = eachindex(a, b) + @inbounds for i = eachindex(a, b) c += (a[i] == b[i]) end return c @@ -27,7 +27,7 @@ Count the number of indices at which the elements of the arrays function countne(a::AbstractArray, b::AbstractArray) length(a) == length(b) || throw(DimensionMismatch("Inconsistent lengths.")) c = 0 - for i = eachindex(a, b) + @inbounds for i = eachindex(a, b) c += (a[i] != b[i]) end return c @@ -43,7 +43,7 @@ Efficient equivalent of `sumabs2(a - b)`. function sqL2dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - for i = eachindex(a, b) + @inbounds for i = eachindex(a, b) r += abs2(a[i] - b[i]) end return r @@ -70,7 +70,7 @@ Efficient equivalent of `sum(abs, a - b)`. function L1dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - for i = eachindex(a, b) + @inbounds for i = eachindex(a, b) r += abs(a[i] - b[i]) end return r @@ -88,7 +88,7 @@ Efficient equivalent of `maxabs(a - b)`. function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - for i = eachindex(a, b) + @inbounds for i = eachindex(a, b) v = abs(a[i] - b[i]) if r < v r = v @@ -109,7 +109,7 @@ Efficient equivalent of `sum(a*log(a/b)-a+b)`. function gkldiv(a::AbstractArray{T}, b::AbstractArray{T}) where T<:AbstractFloat n = length(a) r = 0.0 - for i = eachindex(a, b) + @inbounds for i = eachindex(a, b) ai = a[i] bi = b[i] if ai > 0 From f7201b8f4b0ce364d788c5c7f591b4eee1d9818b Mon Sep 17 00:00:00 2001 From: Moelf Date: Mon, 16 May 2022 22:30:30 -0400 Subject: [PATCH 3/4] use `zip` --- src/deviation.jl | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/deviation.jl b/src/deviation.jl index 03ffbb776..61eec9eae 100644 --- a/src/deviation.jl +++ b/src/deviation.jl @@ -11,8 +11,8 @@ Count the number of indices at which the elements of the arrays function counteq(a::AbstractArray, b::AbstractArray) length(a) == length(b) || throw(DimensionMismatch("Inconsistent lengths.")) c = 0 - @inbounds for i = eachindex(a, b) - c += (a[i] == b[i]) + for (ai, bi) = zip(a, b) + c += (ai == bi) end return c end @@ -27,8 +27,8 @@ Count the number of indices at which the elements of the arrays function countne(a::AbstractArray, b::AbstractArray) length(a) == length(b) || throw(DimensionMismatch("Inconsistent lengths.")) c = 0 - @inbounds for i = eachindex(a, b) - c += (a[i] != b[i]) + for (ai, bi) = zip(a, b) + c += (ai != bi) end return c end @@ -43,8 +43,8 @@ Efficient equivalent of `sumabs2(a - b)`. function sqL2dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - @inbounds for i = eachindex(a, b) - r += abs2(a[i] - b[i]) + for (ai, bi) = zip(a, b) + r += abs2(ai - bi) end return r end @@ -70,8 +70,8 @@ Efficient equivalent of `sum(abs, a - b)`. function L1dist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - @inbounds for i = eachindex(a, b) - r += abs(a[i] - b[i]) + for (ai, bi) = zip(a, b) + r += abs(ai - bi) end return r end @@ -88,7 +88,7 @@ Efficient equivalent of `maxabs(a - b)`. function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 - @inbounds for i = eachindex(a, b) + for (ai, bi) = zip(a, b) v = abs(a[i] - b[i]) if r < v r = v @@ -109,9 +109,7 @@ Efficient equivalent of `sum(a*log(a/b)-a+b)`. function gkldiv(a::AbstractArray{T}, b::AbstractArray{T}) where T<:AbstractFloat n = length(a) r = 0.0 - @inbounds for i = eachindex(a, b) - ai = a[i] - bi = b[i] + for (ai, bi) = zip(a, b) if ai > 0 r += (ai * log(ai / bi) - ai + bi) else From f7418722cba1f1bceb7ceecf4a44402a8a06666b Mon Sep 17 00:00:00 2001 From: David Widmann Date: Tue, 17 May 2022 13:05:30 +0200 Subject: [PATCH 4/4] Update src/deviation.jl Co-authored-by: Kristoffer Carlsson --- src/deviation.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/deviation.jl b/src/deviation.jl index 61eec9eae..0fe62a3bd 100644 --- a/src/deviation.jl +++ b/src/deviation.jl @@ -89,7 +89,7 @@ function Linfdist(a::AbstractArray{T}, b::AbstractArray{T}) where T<:Number length(a) == length(b) || throw(DimensionMismatch("Input dimension mismatch")) r = 0.0 for (ai, bi) = zip(a, b) - v = abs(a[i] - b[i]) + v = abs(ai - bi) if r < v r = v end