-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
[WIP] added definitions for lp-distances. Faster than just using norm #1234
Draft
ajinkya-k
wants to merge
4
commits into
JuliaLang:master
Choose a base branch
from
ajinkya-k:ahk/lp-dist
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,10 @@ export | |
lyap, | ||
mul!, | ||
norm, | ||
lpdist, | ||
l1dist, | ||
euclidean, | ||
l2dist, | ||
normalize!, | ||
normalize, | ||
nullspace, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -892,6 +892,89 @@ | |
|
||
norm(v::AdjOrTrans, p::Real) = norm(v.parent, p) | ||
|
||
""" | ||
lpdist(x::T, y::T, p::Int) where {T <: AbstractVector} | ||
lpdist(x::T, y::T, p::Real) where {T <: AbstractVector} | ||
|
||
Computes the Lₚ distance between two vectors `x` and `y`, defined as: | ||
for 0 < p < Inf: Lₚ dist = ᵖ√(Σ|xᵢ - yᵢ|ᵖ) | ||
for p = 0: count of mismatches for p = 0 | ||
for p + Inf: L_∞ = maxᵢ(abs(xᵢ - yᵢ)) | ||
""" | ||
|
||
function _lpunnorm(x::T, y::T, fn) where {T} | ||
r = 0.0 | ||
for i in eachindex(x,y) | ||
@inbounds r += fn(x[i] - y[i]) | ||
end | ||
|
||
return r | ||
end | ||
|
||
|
||
function lpdist(x::T, y::T, p::Int) where {T <: AbstractVector} | ||
p < 0 && throw(DomainError("p must be non-negative")) | ||
length(x) == length(y) || throw(DimensionMismatch("x and y have different lenghts")) | ||
if p == 0 | ||
@warn("Technically not a distance metric for p = 0") | ||
fn = !iszero # simply count number of nonzeros | ||
elseif p == 1 | ||
fn = abs | ||
elseif p == 2 | ||
fn = abs2 | ||
Comment on lines
+922
to
+924
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
else | ||
fn = u -> abs(u)^p | ||
end | ||
r = _lpunnorm(x, y, fn) | ||
|
||
p <= 1 && return r | ||
p == 2 && return √r | ||
|
||
return r^(1/p) | ||
end | ||
|
||
function lpdist(x::T, y::T, p::Real) where {T <: AbstractVector} | ||
length(x) == length(y) || throw(DimensionMismatch("x and y have different lenghts")) | ||
p < 0 && throw(DomainError("p must be non-negative")) | ||
p < 1 && @warn("Technically not a distance metric for 0 < p < 1") | ||
|
||
# handle inf norm separatey | ||
if p == Inf | ||
r = 0.0 | ||
for i in eachindex(x,y) | ||
@inbounds r = max(abs(x[i] - y[i]), r) | ||
end | ||
return r | ||
elseif iszero(p) | ||
return lpdist(x, y, 0) | ||
else | ||
fn = u -> abs(u)^p | ||
end | ||
r = _lpunnorm(x, y, fn) | ||
|
||
return r^(1/p) | ||
end | ||
|
||
""" | ||
euclidean(x::T, y::T) | ||
l2dist(x::T, y::T) | ||
|
||
Compute the L₂ distance between vectors x and y. | ||
Basically just aliases for lpdist(x, y, 2) | ||
""" | ||
euclidean(x::T, y::T) where {T <: AbstractVector} = lpdist(x, y, 2) | ||
l2dist(x::T, y::T) where {T <: AbstractVector} = lpdist(x, y, 2) | ||
|
||
""" | ||
l1dist(x::T, y::T) | ||
|
||
Compute the L₁ distance between vectors x and y. | ||
Basically just an alias for lpdist(x, y, 1) | ||
""" | ||
l1dist(x::T, y::T) where {T <: AbstractVector} = lpdist(x, y, 1) | ||
|
||
|
||
## dot products | ||
""" | ||
dot(x, y) | ||
x ⋅ y | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this algorithm suffers from spurious overflow if the elements of
x
ory
are very large.e.g.
lpdist([1e300],[0.0])
should return1e300
, but this will returnInf
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch! Any pointers on how I can fix this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps divide each vector by the
max
of the maximum absolute value of each vector before computing the distance, and rescale the result later?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that rescaling is not needed for the L1 norm or the maximum norm.
Note also that you should be using
norm
, notabs
, everywhere, in order to handle vectors whose entries are themselves vectors. In fact, arguably you should be calling the distance function recursively for such cases.In general, I would study the
generic_norm
implementation, which already deals with such issues.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I intended this to be an implementation of vectors of reals (and maybe complex) only. Would it be better to specialise for real/complex vectors and have a separate implementation for recursive structures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See how it's done for
norm
. There's no need to have two implementations, sincenorm_sqr
just callsabs2
for scalars, andnorm
for scalars just callsabs
.You should really study the
norm
implementation and build off that, as otherwise you're re-inventing the wheel here.