Description
I hesitate to open this issue because gosh this behavior is so very useful. But it's probably worth thinking about a better name/idiom here. In short min
and max
for CartesianIndex break the function's contract: they don't return one of the arguments.
julia> min(CartesianIndex(5, 1), CartesianIndex(1, 5))
CartesianIndex(1, 1)
Of course this means that minimum
also returns an element that is different from first(sort(idxs))
(where idxs
is a vector of cartesian indices).
While strange, it is a hugely helpful behavior — e.g., to select the rectangular extents of a found object, you can just do: idxs = findall(...); region = minimum(idxs):maximum(idxs)
. I similarly don't really want to change sorting of cartesian indices — it'll put them in column-major order for you! So if we change this, we'll need to find a new way of spelling min
/minimum
/max
/maximum
.
One possible alternative is broadcasting: it's currently acting as though we've broadcast min
across the elements. We don't support broadcasting over CartesianIndex, but perhaps we should implement tuple-like broadcasting semantics:
julia> Broadcast.broadcastable(c::CartesianIndex) = c.I
julia> min.(CartesianIndex(5, 1), CartesianIndex(1, 5))
(1, 1) # would need a style and tuple-like implementation to preserve the type
That would probably also be helpful in other situations, but it's of no help for minimum
and maximum
.
Activity