Skip to content

Commit d05bf6c

Browse files
authored
Change signature of pairwise! and colwise! (#239)
1 parent 3b90e79 commit d05bf6c

File tree

7 files changed

+243
-79
lines changed

7 files changed

+243
-79
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,29 @@ If the vector/matrix to store the results are pre-allocated, you may use the
132132
storage (without creating a new array) using the following syntax
133133
(`i` being either `1` or `2`):
134134

135+
```julia
136+
colwise!(dist, r, X, Y)
137+
pairwise!(dist, R, X, Y, dims=i)
138+
pairwise!(dist, R, X, dims=i)
139+
```
140+
141+
Please pay attention to the difference, the functions for inplace computation are
142+
`colwise!` and `pairwise!` (instead of `colwise` and `pairwise`).
143+
144+
#### Deprecated alternative syntax
145+
146+
The syntax
147+
135148
```julia
136149
colwise!(r, dist, X, Y)
137150
pairwise!(R, dist, X, Y, dims=i)
138151
pairwise!(R, dist, X, dims=i)
139152
```
140153

141-
Please pay attention to the difference, the functions for inplace computation are
142-
`colwise!` and `pairwise!` (instead of `colwise` and `pairwise`).
154+
with the first two arguments (metric and results) interchanged is supported as well.
155+
However, its use is discouraged since
156+
[it is deprecated](https://github.com/JuliaStats/Distances.jl/pull/239) and will be
157+
removed in a future release.
143158

144159
## Distance type hierarchy
145160

src/Distances.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,6 @@ include("mahalanobis.jl")
118118
include("bhattacharyya.jl")
119119
include("bregman.jl")
120120

121+
include("deprecated.jl")
122+
121123
end # module end

src/deprecated.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Base.@deprecate pairwise!(r::AbstractMatrix, dist::PreMetric, a) pairwise!(dist, r, a)
2+
Base.@deprecate pairwise!(r::AbstractMatrix, dist::PreMetric, a, b) pairwise!(dist, r, a, b)
3+
4+
Base.@deprecate pairwise!(
5+
r::AbstractMatrix, dist::PreMetric, a::AbstractMatrix;
6+
dims::Union{Nothing,Integer}=nothing
7+
) pairwise!(dist, r, a; dims=dims)
8+
Base.@deprecate pairwise!(
9+
r::AbstractMatrix, dist::PreMetric, a::AbstractMatrix, b::AbstractMatrix;
10+
dims::Union{Nothing,Integer}=nothing
11+
) pairwise!(dist, r, a, b; dims=dims)
12+
13+
Base.@deprecate colwise!(r::AbstractArray, dist::PreMetric, a, b) colwise!(dist, r, a, b)
14+
15+
# docstrings for deprecated methods
16+
@doc """
17+
pairwise!(r::AbstractMatrix, dist::PreMetric, a)
18+
19+
Same as `pairwise!(dist, r, a)`.
20+
21+
!!! warning
22+
Since this alternative syntax is deprecated and will be removed in a future release of
23+
Distances.jl, its use is discouraged. Please call `pairwise!(dist, r, a)` instead.
24+
""" pairwise!(r::AbstractMatrix, dist::PreMetric, a)
25+
@doc """
26+
pairwise!(r::AbstractMatrix, dist::PreMetric, a, b)
27+
28+
Same as `pairwise!(dist, r, a, b)`.
29+
30+
!!! warning
31+
Since this alternative syntax is deprecated and will be removed in a future release of
32+
Distances.jl, its use is discouraged. Please call `pairwise!(dist, r, a, b)` instead.
33+
""" pairwise!(r::AbstractMatrix, dist::PreMetric, a, b)
34+
35+
@doc """
36+
pairwise!(r::AbstractMatrix, dist::PreMetric, a::AbstractMatrix; dims)
37+
38+
Same as `pairwise!(dist, r, a; dims)`.
39+
40+
!!! warning
41+
Since this alternative syntax is deprecated and will be removed in a future release of
42+
Distances.jl, its use is discouraged. Please call `pairwise!(dist, r, a; dims)` instead.
43+
""" pairwise!(
44+
r::AbstractMatrix, dist::PreMetric, a::AbstractMatrix;
45+
dims::Union{Nothing,Integer}
46+
)
47+
@doc """
48+
pairwise!(r::AbstractMatrix, dist::PreMetric, a::AbstractMatrix, b::AbstractMatrix; dims)
49+
50+
Same as `pairwise!(dist, r, a, b; dims)`.
51+
52+
!!! warning
53+
Since this alternative syntax is deprecated and will be removed in a future release of
54+
Distances.jl, its use is discouraged. Please call `pairwise!(dist, r, a, b; dims)`
55+
instead.
56+
""" pairwise!(
57+
r::AbstractMatrix, dist::PreMetric, a::AbstractMatrix, b::AbstractMatrix;
58+
dims::Union{Nothing,Integer}
59+
)
60+
61+
@doc """
62+
colwise!(r::AbstractArray, dist::PreMetric, a, b)
63+
64+
Same as `colwise!(dist, r, a, b)`.
65+
66+
!!! warning
67+
Since this alternative syntax is deprecated and will be removed in a future release of
68+
Distances.jl, its use is discouraged. Please call `colwise!(dist, r, a, b)` instead.
69+
""" colwise!(r::AbstractArray, dist::PreMetric, a, b)

src/generic.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ __eltype(::Base.EltypeUnknown, a) = _eltype(typeof(first(a)))
4646
# Generic column-wise evaluation
4747

4848
"""
49-
colwise!(r::AbstractArray, metric::PreMetric, a, b)
49+
colwise!(metric::PreMetric, r::AbstractArray, a, b)
5050
5151
Compute distances between corresponding elements of the iterable collections
5252
`a` and `b` according to distance `metric`, and store the result in `r`.
5353
5454
`a` and `b` must have the same number of elements, `r` must be an array of length
5555
`length(a) == length(b)`.
5656
"""
57-
function colwise!(r::AbstractArray, metric::PreMetric, a, b)
57+
function colwise!(metric::PreMetric, r::AbstractArray, a, b)
5858
require_one_based_indexing(r)
5959
n = length(a)
6060
length(b) == n || throw(DimensionMismatch("iterators have different lengths"))
@@ -65,7 +65,7 @@ function colwise!(r::AbstractArray, metric::PreMetric, a, b)
6565
r
6666
end
6767

68-
function colwise!(r::AbstractArray, metric::PreMetric, a::AbstractVector, b::AbstractMatrix)
68+
function colwise!(metric::PreMetric, r::AbstractArray, a::AbstractVector, b::AbstractMatrix)
6969
require_one_based_indexing(r)
7070
n = size(b, 2)
7171
length(r) == n || throw(DimensionMismatch("Incorrect size of r."))
@@ -75,7 +75,7 @@ function colwise!(r::AbstractArray, metric::PreMetric, a::AbstractVector, b::Abs
7575
r
7676
end
7777

78-
function colwise!(r::AbstractArray, metric::PreMetric, a::AbstractMatrix, b::AbstractVector)
78+
function colwise!(metric::PreMetric, r::AbstractArray, a::AbstractMatrix, b::AbstractVector)
7979
require_one_based_indexing(r)
8080
n = size(a, 2)
8181
length(r) == n || throw(DimensionMismatch("Incorrect size of r."))
@@ -86,11 +86,11 @@ function colwise!(r::AbstractArray, metric::PreMetric, a::AbstractMatrix, b::Abs
8686
end
8787

8888
"""
89-
colwise!(r::AbstractArray, metric::PreMetric,
89+
colwise!(metric::PreMetric, r::AbstractArray,
9090
a::AbstractMatrix, b::AbstractMatrix)
91-
colwise!(r::AbstractArray, metric::PreMetric,
91+
colwise!(metric::PreMetric, r::AbstractArray,
9292
a::AbstractVector, b::AbstractMatrix)
93-
colwise!(r::AbstractArray, metric::PreMetric,
93+
colwise!(metric::PreMetric, r::AbstractArray,
9494
a::AbstractMatrix, b::AbstractVector)
9595
9696
Compute distances between each corresponding columns of `a` and `b` according
@@ -105,7 +105,7 @@ vector. `r` must be an array of length `maximum(size(a, 2), size(b, 2))`.
105105
If both `a` and `b` are vectors, the generic, iterator-based method of
106106
`colwise` applies.
107107
"""
108-
function colwise!(r::AbstractArray, metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix)
108+
function colwise!(metric::PreMetric, r::AbstractArray, a::AbstractMatrix, b::AbstractMatrix)
109109
require_one_based_indexing(r, a, b)
110110
n = get_common_ncols(a, b)
111111
length(r) == n || throw(DimensionMismatch("Incorrect size of r."))
@@ -126,7 +126,7 @@ Compute distances between corresponding elements of the iterable collections
126126
function colwise(metric::PreMetric, a, b)
127127
n = get_common_length(a, b)
128128
r = Vector{result_type(metric, a, b)}(undef, n)
129-
colwise!(r, metric, a, b)
129+
colwise!(metric, r, a, b)
130130
end
131131

132132
"""
@@ -148,25 +148,25 @@ vector.
148148
function colwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix)
149149
n = get_common_ncols(a, b)
150150
r = Vector{result_type(metric, a, b)}(undef, n)
151-
colwise!(r, metric, a, b)
151+
colwise!(metric, r, a, b)
152152
end
153153

154154
function colwise(metric::PreMetric, a::AbstractVector, b::AbstractMatrix)
155155
n = size(b, 2)
156156
r = Vector{result_type(metric, a, b)}(undef, n)
157-
colwise!(r, metric, a, b)
157+
colwise!(metric, r, a, b)
158158
end
159159

160160
function colwise(metric::PreMetric, a::AbstractMatrix, b::AbstractVector)
161161
n = size(a, 2)
162162
r = Vector{result_type(metric, a, b)}(undef, n)
163-
colwise!(r, metric, a, b)
163+
colwise!(metric, r, a, b)
164164
end
165165

166166

167167
# Generic pairwise evaluation
168168

169-
function _pairwise!(r::AbstractMatrix, metric::PreMetric, a, b=a)
169+
function _pairwise!(metric::PreMetric, r::AbstractMatrix, a, b=a)
170170
require_one_based_indexing(r)
171171
na = length(a)
172172
nb = length(b)
@@ -177,7 +177,7 @@ function _pairwise!(r::AbstractMatrix, metric::PreMetric, a, b=a)
177177
r
178178
end
179179

180-
function _pairwise!(r::AbstractMatrix, metric::PreMetric,
180+
function _pairwise!(metric::PreMetric, r::AbstractMatrix,
181181
a::AbstractMatrix, b::AbstractMatrix=a)
182182
require_one_based_indexing(r, a, b)
183183
na = size(a, 2)
@@ -192,7 +192,7 @@ function _pairwise!(r::AbstractMatrix, metric::PreMetric,
192192
r
193193
end
194194

195-
function _pairwise!(r::AbstractMatrix, metric::SemiMetric, a)
195+
function _pairwise!(metric::SemiMetric, r::AbstractMatrix, a)
196196
require_one_based_indexing(r)
197197
n = length(a)
198198
size(r) == (n, n) || throw(DimensionMismatch("Incorrect size of r."))
@@ -208,7 +208,7 @@ function _pairwise!(r::AbstractMatrix, metric::SemiMetric, a)
208208
r
209209
end
210210

211-
function _pairwise!(r::AbstractMatrix, metric::SemiMetric, a::AbstractMatrix)
211+
function _pairwise!(metric::SemiMetric, r::AbstractMatrix, a::AbstractMatrix)
212212
require_one_based_indexing(r)
213213
n = size(a, 2)
214214
size(r) == (n, n) || throw(DimensionMismatch("Incorrect size of r."))
@@ -237,7 +237,7 @@ function deprecated_dims(dims::Union{Nothing,Integer})
237237
end
238238

239239
"""
240-
pairwise!(r::AbstractMatrix, metric::PreMetric,
240+
pairwise!(metric::PreMetric, r::AbstractMatrix,
241241
a::AbstractMatrix, b::AbstractMatrix=a; dims)
242242
243243
Compute distances between each pair of rows (if `dims=1`) or columns (if `dims=2`)
@@ -247,7 +247,7 @@ If a single matrix `a` is provided, compute distances between its rows or column
247247
`a` and `b` must have the same numbers of columns if `dims=1`, or of rows if `dims=2`.
248248
`r` must be a matrix with size `size(a, dims) × size(b, dims)`.
249249
"""
250-
function pairwise!(r::AbstractMatrix, metric::PreMetric,
250+
function pairwise!(metric::PreMetric, r::AbstractMatrix,
251251
a::AbstractMatrix, b::AbstractMatrix;
252252
dims::Union{Nothing,Integer}=nothing)
253253
dims = deprecated_dims(dims)
@@ -266,13 +266,13 @@ function pairwise!(r::AbstractMatrix, metric::PreMetric,
266266
size(r) == (na, nb) ||
267267
throw(DimensionMismatch("Incorrect size of r (got $(size(r)), expected $((na, nb)))."))
268268
if dims == 1
269-
_pairwise!(r, metric, permutedims(a), permutedims(b))
269+
_pairwise!(metric, r, permutedims(a), permutedims(b))
270270
else
271-
_pairwise!(r, metric, a, b)
271+
_pairwise!(metric, r, a, b)
272272
end
273273
end
274274

275-
function pairwise!(r::AbstractMatrix, metric::PreMetric, a::AbstractMatrix;
275+
function pairwise!(metric::PreMetric, r::AbstractMatrix, a::AbstractMatrix;
276276
dims::Union{Nothing,Integer}=nothing)
277277
dims = deprecated_dims(dims)
278278
dims in (1, 2) || throw(ArgumentError("dims should be 1 or 2 (got $dims)"))
@@ -284,23 +284,23 @@ function pairwise!(r::AbstractMatrix, metric::PreMetric, a::AbstractMatrix;
284284
size(r) == (n, n) ||
285285
throw(DimensionMismatch("Incorrect size of r (got $(size(r)), expected $((n, n)))."))
286286
if dims == 1
287-
_pairwise!(r, metric, permutedims(a))
287+
_pairwise!(metric, r, permutedims(a))
288288
else
289-
_pairwise!(r, metric, a)
289+
_pairwise!(metric, r, a)
290290
end
291291
end
292292

293293
"""
294-
pairwise!(r::AbstractMatrix, metric::PreMetric, a, b=a)
294+
pairwise!(metric::PreMetric, r::AbstractMatrix, a, b=a)
295295
296296
Compute distances between each element of collection `a` and each element of
297297
collection `b` according to distance `metric`, and store the result in `r`.
298298
If a single iterable `a` is provided, compute distances between its elements.
299299
300300
`r` must be a matrix with size `length(a) × length(b)`.
301301
"""
302-
pairwise!(r::AbstractMatrix, metric::PreMetric, a, b) = _pairwise!(r, metric, a, b)
303-
pairwise!(r::AbstractMatrix, metric::PreMetric, a) = _pairwise!(r, metric, a)
302+
pairwise!(metric::PreMetric, r::AbstractMatrix, a, b) = _pairwise!(metric, r, a, b)
303+
pairwise!(metric::PreMetric, r::AbstractMatrix, a) = _pairwise!(metric, r, a)
304304

305305
"""
306306
pairwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix=a; dims)
@@ -318,7 +318,7 @@ function pairwise(metric::PreMetric, a::AbstractMatrix, b::AbstractMatrix;
318318
m = size(a, dims)
319319
n = size(b, dims)
320320
r = Matrix{result_type(metric, a, b)}(undef, m, n)
321-
pairwise!(r, metric, a, b, dims=dims)
321+
pairwise!(metric, r, a, b, dims=dims)
322322
end
323323

324324
function pairwise(metric::PreMetric, a::AbstractMatrix;
@@ -327,7 +327,7 @@ function pairwise(metric::PreMetric, a::AbstractMatrix;
327327
dims in (1, 2) || throw(ArgumentError("dims should be 1 or 2 (got $dims)"))
328328
n = size(a, dims)
329329
r = Matrix{result_type(metric, a, a)}(undef, n, n)
330-
pairwise!(r, metric, a, dims=dims)
330+
pairwise!(metric, r, a, dims=dims)
331331
end
332332

333333
"""
@@ -341,11 +341,11 @@ function pairwise(metric::PreMetric, a, b)
341341
m = length(a)
342342
n = length(b)
343343
r = Matrix{result_type(metric, a, b)}(undef, m, n)
344-
_pairwise!(r, metric, a, b)
344+
_pairwise!(metric, r, a, b)
345345
end
346346

347347
function pairwise(metric::PreMetric, a)
348348
n = length(a)
349349
r = Matrix{result_type(metric, a, a)}(undef, n, n)
350-
_pairwise!(r, metric, a)
350+
_pairwise!(metric, r, a)
351351
end

src/mahalanobis.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,34 @@ end
9595
sqmahalanobis(a::AbstractVector, b::AbstractVector, Q::AbstractMatrix) = SqMahalanobis(Q)(a, b)
9696
mahalanobis(a::AbstractVector, b::AbstractVector, Q::AbstractMatrix) = Mahalanobis(Q)(a, b)
9797

98-
function _colwise!(r, dist, a, b)
98+
function _colwise!(dist, r, a, b)
9999
Q = dist.qmat
100100
get_colwise_dims(size(Q, 1), r, a, b)
101101
z = a .- b
102102
dot_percol!(r, Q * z, z)
103103
end
104104

105-
function colwise!(r::AbstractArray, dist::SqMahalanobis, a::AbstractMatrix, b::AbstractMatrix)
106-
_colwise!(r, dist, a, b)
105+
function colwise!(dist::SqMahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractMatrix)
106+
_colwise!(dist, r, a, b)
107107
end
108-
function colwise!(r::AbstractArray, dist::SqMahalanobis, a::AbstractVector, b::AbstractMatrix)
109-
_colwise!(r, dist, a, b)
108+
function colwise!(dist::SqMahalanobis, r::AbstractArray, a::AbstractVector, b::AbstractMatrix)
109+
_colwise!(dist, r, a, b)
110110
end
111-
function colwise!(r::AbstractArray, dist::SqMahalanobis, a::AbstractMatrix, b::AbstractVector)
112-
_colwise!(r, dist, a, b)
111+
function colwise!(dist::SqMahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractVector)
112+
_colwise!(dist, r, a, b)
113113
end
114114

115-
function colwise!(r::AbstractArray, dist::Mahalanobis, a::AbstractMatrix, b::AbstractMatrix)
116-
sqrt!(_colwise!(r, dist, a, b))
115+
function colwise!(dist::Mahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractMatrix)
116+
sqrt!(_colwise!(dist, r, a, b))
117117
end
118-
function colwise!(r::AbstractArray, dist::Mahalanobis, a::AbstractVector, b::AbstractMatrix)
119-
sqrt!(_colwise!(r, dist, a, b))
118+
function colwise!(dist::Mahalanobis, r::AbstractArray, a::AbstractVector, b::AbstractMatrix)
119+
sqrt!(_colwise!(dist, r, a, b))
120120
end
121-
function colwise!(r::AbstractArray, dist::Mahalanobis, a::AbstractMatrix, b::AbstractVector)
122-
sqrt!(_colwise!(r, dist, a, b))
121+
function colwise!(dist::Mahalanobis, r::AbstractArray, a::AbstractMatrix, b::AbstractVector)
122+
sqrt!(_colwise!(dist, r, a, b))
123123
end
124124

125-
function _pairwise!(r::AbstractMatrix, dist::Union{SqMahalanobis,Mahalanobis}, a::AbstractMatrix, b::AbstractMatrix)
125+
function _pairwise!(dist::Union{SqMahalanobis,Mahalanobis}, r::AbstractMatrix, a::AbstractMatrix, b::AbstractMatrix)
126126
Q = dist.qmat
127127
m, na, nb = get_pairwise_dims(size(Q, 1), r, a, b)
128128

@@ -140,7 +140,7 @@ function _pairwise!(r::AbstractMatrix, dist::Union{SqMahalanobis,Mahalanobis}, a
140140
r
141141
end
142142

143-
function _pairwise!(r::AbstractMatrix, dist::Union{SqMahalanobis,Mahalanobis}, a::AbstractMatrix)
143+
function _pairwise!(dist::Union{SqMahalanobis,Mahalanobis}, r::AbstractMatrix, a::AbstractMatrix)
144144
Q = dist.qmat
145145
m, n = get_pairwise_dims(size(Q, 1), r, a)
146146

0 commit comments

Comments
 (0)