Skip to content

vector indexing and view for matrix only accept Int indices instead of Integer #2424

Description

@matthias314

view for Julia arrays accepts arbitrary integers and integer vectors. In AbstractAlgebra, only Int is allowed:

julia> a = ones(Int, 2, 3);

julia> view(matrix(a), :, Int8(1))
ERROR: MethodError: no method matching _view(::AbstractAlgebra.Generic.MatSpaceElem{Int64}, ::UnitRange{Int64}, ::Int8)
The function `_view` exists, but no method is defined for this combination of argument types.

julia> view(matrix(a), :, 1)
2-element AbstractAlgebra.Generic.MatSpaceVecView{Int64, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}:
 1
 1

julia> view(a, :, Int8(1))
2-element view(::Matrix{Int64}, :, 1) with eltype Int64:
 1
 1

julia> view(matrix(a), :, Int8[1, 3])
ERROR: MethodError: no method matching _view(::AbstractAlgebra.Generic.MatSpaceElem{Int64}, ::UnitRange{Int64}, ::Vector{Int8})
The function `_view` exists, but no method is defined for this combination of argument types.

julia> view(matrix(a), :, [1, 3])
[1   1]
[1   1]

julia> view(a, :, Int8[1, 3])
2×2 view(::Matrix{Int64}, :, Int8[1, 3]) with eltype Int64:
 1  1
 1  1

The problem is the restriction to AbstractVector{Int} instead of AbstractVector{<:Integer} in the relevant methods:

function Base.view(M::Mat{T}, rows::Union{Colon, AbstractVector{Int}}, cols::Union{Colon, AbstractVector{Int}}) where T <: NCRingElement
return MatSpaceView(view(M.entries, rows, cols), M.base_ring)
end
function Base.view(M::Mat{T}, rows::Int, cols::Union{Colon, AbstractVector{Int}}) where T <: NCRingElement
return MatSpaceVecView(view(M.entries, rows, cols), M.base_ring)
end
function Base.view(M::Mat{T}, rows::Union{Colon, AbstractVector{Int}}, cols::Int) where T <: NCRingElement
return MatSpaceVecView(view(M.entries, rows, cols), M.base_ring)
end
function Base.view(M::Mat{T}, rows::Int, cols::Int) where T <: NCRingElement
return MatSpacePointView(view(M.entries, rows, cols), M.base_ring)
end

EDIT: Ditto for vector indexing proper:

julia> matrix(a)[:, Int8(1)]
ERROR: MethodError: no method matching getindex(::AbstractAlgebra.Generic.MatSpaceElem{Int64}, ::UnitRange{Int64}, ::Int8)
The function `getindex` exists, but no method is defined for this combination of argument types.

julia> matrix(a)[:, Int8[1,3]]
ERROR: MethodError: no method matching sub(::AbstractAlgebra.Generic.MatSpaceElem{Int64}, ::UnitRange{Int64}, ::Vector{Int8})
The function `sub` exists, but no method is defined for this combination of argument types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions