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

Optimize pairwise for Sinus and scalar inputs #538

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
12 changes: 12 additions & 0 deletions src/distances/sinus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@
end
return sum(abs2, sinpi.(a - b) ./ d.r)
end

# Optimizations for scalar inputs (avoiding allocations)
pairwise(d::Sinus, x::AbstractVector{<:Real}) = pairwise(d, x, x)
function pairwise(d::Sinus, x::AbstractVector{<:Real}, y::AbstractVector{<:Real})
return abs2.(sinpi.(x .- y') ./ only(d.r))

Check warning on line 28 in src/distances/sinus.jl

View check run for this annotation

Codecov / codecov/patch

src/distances/sinus.jl#L26-L28

Added lines #L26 - L28 were not covered by tests
end
pairwise(d::Sinus, x::RowVecs) = Distances_pairwise(d, x.X; dims=1)
pairwise(d::Sinus, x::ColVecs) = Distances_pairwise(d, x.X; dims=2)
pairwise(d::Sinus, x::RowVecs, y::RowVecs) = Distances_pairwise(d, x.X, y.X; dims=1)
pairwise(d::Sinus, x::ColVecs, y::RowVecs) = pairwise(d, x, ColVecs(permutedims(y.X)))
pairwise(d::Sinus, x::RowVecs, y::ColVecs) = pairwise(d, ColVecs(permutedims(x.X)), y)
pairwise(d::Sinus, x::ColVecs, y::ColVecs) = Distances_pairwise(d, x.X, y.X; dims=2)

Check warning on line 35 in src/distances/sinus.jl

View check run for this annotation

Codecov / codecov/patch

src/distances/sinus.jl#L30-L35

Added lines #L30 - L35 were not covered by tests
Loading