FixedSizeArrays.jl icon indicating copy to clipboard operation
FixedSizeArrays.jl copied to clipboard

Range-indexing FixedSizeArrays must/could return other FixedSizeArrays or "SubFixedSizeArray"s

Open cdsousa opened this issue 10 years ago • 7 comments

cdsousa avatar Aug 28 '15 11:08 cdsousa

+1 will work on this in the next days

schlady avatar Dec 17 '15 16:12 schlady

I just ran into this as well. We should definitely return a fixed size array for slices, at the moment a tuple comes out:

julia> Vec(1,2,3)[1:2]
(1,2)

c42f avatar Feb 08 '16 05:02 c42f

Then, there is also the question of type stability of such indexing.

A workaround I've been using (told by @SimonDanisch ) is using Val-encapsulated indexes: Vec(1,2,3)[Val{1:2}] and generated functions:

@generated function Base.getindex{R}(v::Vec, i::Type{Val{R}})
    :(Vec(tuple($([:(v._[$i]) for i in R]...))))
end

@generated function Base.getindex{Ri, Rj}(m::Mat, ::Type{Val{Ri}}, ::Type{Val{Rj}})
    if isa(Rj, Integer)
        :(Vec(tuple($([:(m._[$Rj][$i]) for i in Ri]...))))
    else
        :(Mat(tuple($([:(tuple($([:(m._[$j][$i]) for i in Ri]...))) for j in Rj]...))))
    end
end

cdsousa avatar Feb 08 '16 11:02 cdsousa

But for a first approach we may have to forget about type stability, and just improve current getindex methods of https://github.com/SimonDanisch/FixedSizeArrays.jl/blob/master/src/indexing.jl#L1-L6.

cdsousa avatar Feb 08 '16 11:02 cdsousa

BTW, this Vec(Vec(1,2,3)[1:2]) already works. It doesn't work well for Mats though.

cdsousa avatar Feb 08 '16 11:02 cdsousa

I could give it another go now that we have a "better" similar function ;) But type instability will remain, as long as we don't use something like Val or get const propagation. Although, from vec[(1,2,3)] the type should be inferrable just fine... We could think about introducing some fixed size range type :D

SimonDanisch avatar Feb 08 '16 12:02 SimonDanisch

Good point about the type problems. The tuple approach seems like the best thing we can do at the moment IMHO, though for anything larger than 3 or 4-vectors, the Val approach would make some sense regardless of the ugly syntax.

Constant propagation has been discussed a little at JuliaLang/julia#5560 - I imagine even one pass of this would solve a whole raft of what are currently tricky design problems. Unfortunately doesn't look like it's coming any time soon!

c42f avatar Feb 09 '16 03:02 c42f