Distances.jl
Distances.jl copied to clipboard
Euclidean on Cartesian indices
Is there a way to compute the euclidean distance between arrays that are of the type CartesianIndex?
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.
Could you explain why this would be useful? Since CartesianIndex
isn't iterable (on purpose), this would require specific support in Distances.
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
Yes but we decided to revert that change until we have a clearer view of why this would be needed.
@nalimilan convinced me that supporting CartesianIndex
es directly (at least in the context of #194) is awkward, because it targets iterable objects, and CartesianIndex
es 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 CartesianIndex
es 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.