Skip to content

Commit

Permalink
Add pairwise distance computation in condensed form for symmetrical m…
Browse files Browse the repository at this point in the history
…etrics.
  • Loading branch information
lucabrivio committed Oct 20, 2017
1 parent 2b0ab92 commit 5d36c4e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ R = pairwise(dist, X)
```

This statement will result in an ``m-by-m`` matrix, where ``R[i,j]`` is the distance between ``X[:,i]`` and ``X[:,j]``.
``pairwise(dist, X)`` is typically more efficient than ``pairwise(dist, X, X)``, as the former will take advantage of the symmetry when ``dist`` is a semi-metric (including metric).
``pairwise(dist, X)`` is typically more efficient than ``pairwise(dist, X, X)``, as the former will take advantage of the symmetry when ``dist`` is a semi-metric (including metric). You can also compute symmetrical distances in a condensed vector representation:

```julia
r = cond_pairwise(dist, X)
```

This will return a vector ``r`` of length ``n * (n - 1) / 2``, containing the corresponding matrix elements arranged in the order ``(2,1), (3,1), ..., (m,1), (3,2), ..., (m,2), ..., (m,m–1)``.

#### Computing column-wise and pairwise distances inplace

Expand All @@ -103,9 +109,10 @@ If the vector/matrix to store the results are pre-allocated, you may use the sto
colwise!(r, dist, X, Y)
pairwise!(R, dist, X, Y)
pairwise!(R, dist, X)
pairwise!(r, dist, X)
```

Please pay attention to the difference, the functions for inplace computation are ``colwise!`` and ``pairwise!`` (instead of ``colwise`` and ``pairwise``).
Please pay attention to the difference, the functions for inplace computation are ``colwise!`` and ``pairwise!`` (instead of ``colwise`` and ``pairwise``/``cond_pairwise``).


## Distance type hierarchy
Expand Down
1 change: 1 addition & 0 deletions src/Distances.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export
result_type,
colwise,
pairwise,
cond_pairwise,
colwise!,
pairwise!,
evaluate,
Expand Down
22 changes: 22 additions & 0 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,25 @@ function pairwise(metric::PreMetric, a::AbstractMatrix)
r = Matrix{result_type(metric, a, a)}(n, n)
pairwise!(r, metric, a)
end

# Generic pairwise evaluation to a condensed form, for symmetrical distances

function pairwise!(r::AbstractVector, metric::SemiMetric, a::AbstractMatrix)
n = size(a, 2)
size(r) == n * (n - 1) / 2 || throw(DimensionMismatch("Incorrect size of r."))
k = 1
for j = 1:n
aj = view(a, :, j)
for i = (j + 1):n
@inbounds r[k] = evaluate(metric, view(a, :, i), aj)
k += 1
end
end
r
end

function cond_pairwise(metric::SemiMetric, a::AbstractMatrix)
n = size(a, 2)
r = Vector{result_type(metric, a, a)}(n * (n - 1) / 2)
pairwise!(r, metric, a)
end

0 comments on commit 5d36c4e

Please sign in to comment.