Skip to content

Min/max of CartesianIndex are implicitly "vectorized" and conflict with ordering #31208

Open
@mbauman

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]broadcastApplying a function over a collectiondesignDesign of APIs or of the language itself

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions