Skip to content
Open
Show file tree
Hide file tree
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: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ Note that either of `X` and `Y` can be just a single vector -- then the `colwise

#### Computing pairwise distances

Let `X` and `Y` respectively have `m` and `n` columns. Then the `pairwise` function with the `dims=2` argument computes distances between each pair of columns in `X` and `Y`:
Let `X` and `Y` respectively have `m` and `n` columns. Then the `pairwise` function argument computes distances between each pair of columns in `X` and `Y`:

```julia
R = pairwise(dist, X, Y, dims=2)
R = pairwise(dist, X, Y)
```

In the output, `R` is a matrix of size `(m, n)`, such that `R[i,j]` is the distance between `X[:,i]` and `Y[:,j]`. Computing distances for all pairs using `pairwise` function is often remarkably faster than evaluting for each pair individually.

If you just want to just compute distances between columns of a matrix `X`, you can write

```julia
R = pairwise(dist, X, dims=2)
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]`.
Expand All @@ -105,12 +105,12 @@ matrices with observations stored in rows are also supported via the argument `d

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

If the vector/matrix to store the results are pre-allocated, you may use the storage (without creating a new array) using the following syntax (`i` being either `1` or `2`):
If the vector/matrix to store the results are pre-allocated, you may use the storage (without creating a new array) using the following syntax (`dims` being either `1` or `2`):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we don't need to specify dims here now, it was just required due to the deprecation.


```julia
colwise!(r, dist, X, Y)
pairwise!(R, dist, X, Y, dims=i)
pairwise!(R, dist, X, dims=i)
pairwise!(R, dist, X, Y, dims=2)
pairwise!(R, dist, X, dims=2)
```

Please pay attention to the difference, the functions for inplace computation are `colwise!` and `pairwise!` (instead of `colwise` and `pairwise`).
Expand Down
27 changes: 6 additions & 21 deletions src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,9 @@ function _pairwise!(r::AbstractMatrix, metric::SemiMetric, a::AbstractMatrix)
r
end

function deprecated_dims(dims::Union{Nothing,Integer})
if dims === nothing
Base.depwarn("implicit `dims=2` argument now has to be passed explicitly " *
"to specify that distances between columns should be computed",
:pairwise!)
return 2
else
return dims
end
end

"""
pairwise!(r::AbstractMatrix, metric::PreMetric,
a::AbstractMatrix, b::AbstractMatrix=a; dims)
a::AbstractMatrix, b::AbstractMatrix=a; dims=2)

Compute distances between each pair of rows (if `dims=1`) or columns (if `dims=2`)
in `a` and `b` according to distance `metric`, and store the result in `r`.
Expand All @@ -144,8 +133,7 @@ If a single matrix `a` is provided, compute distances between its rows or column
"""
function pairwise!(r::AbstractMatrix, metric::PreMetric,
a::AbstractMatrix, b::AbstractMatrix;
dims::Union{Nothing,Integer}=nothing)
dims = deprecated_dims(dims)
dims::Integer=2)
dims in (1, 2) || throw(ArgumentError("dims should be 1 or 2 (got $dims)"))
if dims == 1
na, ma = size(a)
Expand All @@ -168,8 +156,7 @@ function pairwise!(r::AbstractMatrix, metric::PreMetric,
end

function pairwise!(r::AbstractMatrix, metric::PreMetric, a::AbstractMatrix;
dims::Union{Nothing,Integer}=nothing)
dims = deprecated_dims(dims)
dims::Integer=2)
dims in (1, 2) || throw(ArgumentError("dims should be 1 or 2 (got $dims)"))
if dims == 1
n, m = size(a)
Expand All @@ -186,7 +173,7 @@ function pairwise!(r::AbstractMatrix, metric::PreMetric, a::AbstractMatrix;
end

"""
pairwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix=a; dims)
pairwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix=a; dims=2)

Compute distances between each pair of rows (if `dims=1`) or columns (if `dims=2`)
in `a` and `b` according to distance `metric`. If a single matrix `a` is provided,
Expand All @@ -195,8 +182,7 @@ compute distances between its rows or columns.
`a` and `b` must have the same numbers of columns if `dims=1`, or of rows if `dims=2`.
"""
function pairwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix;
dims::Union{Nothing,Integer}=nothing)
dims = deprecated_dims(dims)
dims::Integer=2)
dims in (1, 2) || throw(ArgumentError("dims should be 1 or 2 (got $dims)"))
m = size(a, dims)
n = size(b, dims)
Expand All @@ -205,8 +191,7 @@ function pairwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix;
end

function pairwise(metric::PreMetric, a::AbstractMatrix;
dims::Union{Nothing,Integer}=nothing)
dims = deprecated_dims(dims)
dims::Integer=2)
dims in (1, 2) || throw(ArgumentError("dims should be 1 or 2 (got $dims)"))
n = size(a, dims)
r = Matrix{result_type(metric, a, a)}(undef, n, n)
Expand Down