Skip to content

Incorrect getindex for CartesianIndices #308

@wangl-cc

Description

@wangl-cc

getindex for CartesianIndices whose indices are not start from 1 seems incorrect.

julia> A = reshape(1:6, 2, 3)
2×3 reshape(::UnitRange{Int64}, 2, 3) with eltype Int64:
 1  3  5
 2  4  6

julia> ArrayInterface.getindex(A, CartesianIndices((1:2, 2:3)))
2×2 Matrix{Int64}:
 1  3
 2  4

julia> ArrayInterface.getindex(A, 1:2, 2:3)
2×2 Matrix{Int64}:
 3  5
 4  6

julia> getindex(A, CartesianIndices((1:2, 2:3)))
2×2 Matrix{Int64}:
 3  5
 4  6

IMO, ArrayInterface.getindex(A, CartesianIndices((1:2, 2:3))) should be the same as ArrayInterface.getindex(A, 1:2, 2:3) which is the behavior of Base.getindex,
but it was treated as getindex(A, 1:2, 1:2).

It seems that to_index don't handle CartesianIndices properly:

@inline to_index(x, i::CartesianIndices) = axes(i)

Returning i.indices should fix it:

julia> ArrayInterface.to_index(x, i::CartesianIndices) = i.indices

julia> ArrayInterface.getindex(A, CartesianIndices((1:2, 2:3)))
2×2 Matrix{Int64}:
 3  5
 4  6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions