Distances.jl icon indicating copy to clipboard operation
Distances.jl copied to clipboard

Euclidean on Cartesian indices

Open Dale-Black opened this issue 3 years ago • 5 comments

Is there a way to compute the euclidean distance between arrays that are of the type CartesianIndex?

Dale-Black avatar Sep 16 '20 23:09 Dale-Black

Do you mean something like this?

julia> A = reshape(collect(1:9), 3, 3)
3×3 Array{Int64,2}:
 1  4  7
 2  5  8
 3  6  9

julia> inds1 = findall(iseven, A)
4-element Array{CartesianIndex{2},1}:
 CartesianIndex(2, 1)
 CartesianIndex(1, 2)
 CartesianIndex(3, 2)
 CartesianIndex(2, 3)

julia> inds2 = findall(isodd, A)
5-element Array{CartesianIndex{2},1}:
 CartesianIndex(1, 1)
 CartesianIndex(3, 1)
 CartesianIndex(2, 2)
 CartesianIndex(1, 3)
 CartesianIndex(3, 3)

julia> pairwise(SqEuclidean(), inds1, inds2)
4×5 Array{Int64,2}:
 1  1  1  5  5
 1  5  1  1  5
 5  1  1  5  1
 5  5  1  1  1

julia> euclidean(inds1[1], inds1[1]) === 0.0
true

I have added support for this special, noniterable type in #194.

dkarrasch avatar Dec 15 '20 13:12 dkarrasch

Could you explain why this would be useful? Since CartesianIndex isn't iterable (on purpose), this would require specific support in Distances.

nalimilan avatar Dec 19 '20 22:12 nalimilan

I think what @dkarrasch implemented is what I wanted. I would have to go back to this specific project to find out for sure but from what I can tell that solves my use case

Dale-Black avatar Dec 19 '20 23:12 Dale-Black

Yes but we decided to revert that change until we have a clearer view of why this would be needed.

nalimilan avatar Dec 20 '20 11:12 nalimilan

@nalimilan convinced me that supporting CartesianIndexes directly (at least in the context of #194) is awkward, because it targets iterable objects, and CartesianIndexes are intentionally non-iterable. I believe the correct way of handling it is to pass the responsibility to the callee, asking her/him to pass iterable objects to Distances.jl. In the context here, this would mean to convert CartesianIndexes to tuples via Tuple(cartind), as suggested by the error message. Once you have that, the new iterator-based implementation will work just fast and fine.

dkarrasch avatar Dec 20 '20 11:12 dkarrasch