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

move to modern indexing style #790

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
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
45 changes: 18 additions & 27 deletions src/deviation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Moelf marked this conversation as resolved.
Show resolved Hide resolved
end
return c
end
Expand All @@ -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])
Moelf marked this conversation as resolved.
Show resolved Hide resolved
end
return c
end
Expand All @@ -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])
Moelf marked this conversation as resolved.
Show resolved Hide resolved
end
return r
end
Expand All @@ -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
Moelf marked this conversation as resolved.
Show resolved Hide resolved
for i = 1:n
@inbounds r += abs(a[i] - b[i])
for i = eachindex(a, b)
r += abs(a[i] - b[i])
Moelf marked this conversation as resolved.
Show resolved Hide resolved
end
return r
end
Expand All @@ -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])
Moelf marked this conversation as resolved.
Show resolved Hide resolved
devmotion marked this conversation as resolved.
Show resolved Hide resolved
if r < v
r = v
end
Expand All @@ -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]
Moelf marked this conversation as resolved.
Show resolved Hide resolved
if ai > 0
r += (ai * log(ai / bi) - ai + bi)
else
Expand Down