Skip to content

Aliased vector slices #8795

@gummif

Description

@gummif

What I think is a missing feature in Julia is the ability to work with unit-stride slices of Arrays, like Matrix columns or sections of Vectors, as Vectors with perfect compatibility and equal performance as Vectors. SubArrays and ArrayViews are more general and don't quite have the compatibility or performance yet.

I could make a PR if there is interest. Sample code:

# return aliased vector slice A[i:i-1+n]
function vectorslice(A::Array, i::Int, n::Int)
    @assert 0 < i <= length(A)
    @assert 0 <= n
    @assert i-1+n <= length(A)
    return pointer_to_array(pointer(A, i), n, true)::Vector
end
julia> x = randn(3,4)
3x4 Array{Float64,2}:
 -1.0475    1.44511    -0.103864   -0.395006
 -1.20736   0.935344    0.0891084   1.18014 
  0.725243  0.0379082   0.986568   -0.204709

julia> y = vectorslice(x,4,3)
3-element Array{Float64,1}:
 1.44511  
 0.935344 
 0.0379082

julia> scale!(2,y)
3-element Array{Float64,1}:
 2.89022  
 1.87069  
 0.0758164

julia> x
3x4 Array{Float64,2}:
 -1.0475    2.89022    -0.103864   -0.395006
 -1.20736   1.87069     0.0891084   1.18014 
  0.725243  0.0758164   0.986568   -0.204709

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