Skip to content

Commit

Permalink
Add specialized pairwise methods to *msd (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarrasch authored Oct 30, 2021
1 parent c63dc14 commit 9e23809
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Distances"
uuid = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
version = "0.10.5"
version = "0.10.6"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
51 changes: 44 additions & 7 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,50 @@ function _pairwise!(r::AbstractMatrix, dist::Union{WeightedSqEuclidean,WeightedE
r
end

# MeanSqDeviation, RMSDeviation, NormRMSDeviation
function _pairwise!(r::AbstractMatrix, dist::MeanSqDeviation, a::AbstractMatrix, b::AbstractMatrix)
_pairwise!(r, SqEuclidean(), a, b)
# TODO: Replace by rdiv!(r, size(a, 1)) once julia compat ≥v1.2
s = size(a, 1)
@simd for I in eachindex(r)
@inbounds r[I] /= s
end
return r
end
_pairwise!(r::AbstractMatrix, dist::RMSDeviation, a::AbstractMatrix, b::AbstractMatrix) =
sqrt!(_pairwise!(r, MeanSqDeviation(), a, b))
function _pairwise!(r::AbstractMatrix, dist::NormRMSDeviation, a::AbstractMatrix, b::AbstractMatrix)
_pairwise!(r, RMSDeviation(), a, b)
@views for (i, j) in zip(axes(r, 1), axes(a, 2))
amin, amax = extrema(a[:,j])
r[i,:] ./= amax - amin
end
return r
end

function _pairwise!(r::AbstractMatrix, dist::MeanSqDeviation, a::AbstractMatrix)
_pairwise!(r, SqEuclidean(), a)
# TODO: Replace by rdiv!(r, size(a, 1)) once julia compat ≥v1.2
s = size(a, 1)
@simd for I in eachindex(r)
@inbounds r[I] /= s
end
return r
end
_pairwise!(r::AbstractMatrix, dist::RMSDeviation, a::AbstractMatrix) =
sqrt!(_pairwise!(r, MeanSqDeviation(), a))
function _pairwise!(r::AbstractMatrix, dist::NormRMSDeviation, a::AbstractMatrix)
_pairwise!(r, RMSDeviation(), a)
@views for (i, j) in zip(axes(r, 1), axes(a, 2))
amin, amax = extrema(a[:,j])
r[i,:] ./= amax - amin
end
return r
end

# CosineDist

function _pairwise!(r::AbstractMatrix, ::CosineDist,
a::AbstractMatrix, b::AbstractMatrix)
function _pairwise!(r::AbstractMatrix, ::CosineDist, a::AbstractMatrix, b::AbstractMatrix)
require_one_based_indexing(r, a, b)
m, na, nb = get_pairwise_dims(r, a, b)
inplace = promote_type(eltype(r), typeof(oneunit(eltype(a))'oneunit(eltype(b)))) === eltype(r)
Expand Down Expand Up @@ -772,10 +812,7 @@ end
# 2. pre-calculated `_centralize_colwise` avoids four times of redundant computations
# of `_centralize` -- ~4x speed up
_centralize_colwise(x::AbstractMatrix) = x .- mean(x, dims=1)
function _pairwise!(r::AbstractMatrix, ::CorrDist,
a::AbstractMatrix, b::AbstractMatrix)
_pairwise!(r::AbstractMatrix, ::CorrDist, a::AbstractMatrix, b::AbstractMatrix) =
_pairwise!(r, CosineDist(), _centralize_colwise(a), _centralize_colwise(b))
end
function _pairwise!(r::AbstractMatrix, ::CorrDist, a::AbstractMatrix)
_pairwise!(r::AbstractMatrix, ::CorrDist, a::AbstractMatrix) =
_pairwise!(r, CosineDist(), _centralize_colwise(a))
end
6 changes: 6 additions & 0 deletions test/test_dists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,12 @@ end
test_pairwise(cityblock, X, Y, T)
test_pairwise(TotalVariation(), X, Y, T)
test_pairwise(totalvariation, X, Y, T)
test_pairwise(MeanSqDeviation(), X, Y, T)
test_pairwise(msd, X, Y, T)
test_pairwise(RMSDeviation(), X, Y, T)
test_pairwise(rmsd, X, Y, T)
test_pairwise(NormRMSDeviation(), X, Y, T)
test_pairwise(nrmsd, X, Y, T)
test_pairwise(Chebyshev(), X, Y, T)
test_pairwise(chebyshev, X, Y, T)
test_pairwise(Minkowski(2.5), X, Y, T)
Expand Down

2 comments on commit 9e23809

@dkarrasch
Copy link
Member 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/47801

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.10.6 -m "<description of version>" 9e2380993857d9028c7106e15fb3108cfe05153d
git push origin v0.10.6

Please sign in to comment.