Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make blocks(randn(2, 2))[1, 1] return the original array #403

Open
mtfishman opened this issue Jun 5, 2024 · 0 comments
Open

Make blocks(randn(2, 2))[1, 1] return the original array #403

mtfishman opened this issue Jun 5, 2024 · 0 comments

Comments

@mtfishman
Copy link
Collaborator

Currently, blocks has this behavior:

julia> using BlockArrays

julia> blocks(randn(2, 2))[1, 1]
2×2 view(::Matrix{Float64}, BlockSlice(Block(1),Base.OneTo(2)), BlockSlice(Block(1),Base.OneTo(2))) with eltype Float64:
 -1.50189  2.04898
  1.76046  0.382146

julia> using Pkg; Pkg.status("BlockArrays")
Status `~/Simons Foundation Dropbox/Matthew Fishman/Documents/workdir/BlockArrays.jl/Project.toml`
  [8e7c35d0] BlockArrays v1.0.1

I think it could be nice to have it return the input array instead of a view when it can be known from the type of the original array that it only has one block. Working with the view, while not too bad, is a bit more complicated than just working with the original array.

A design could be an alternative type to BlocksView:

struct OneBlockView{T,N,A<:AbstractArray{T,N}} <: AbstractArray{T,N}
    array::A
end
Base.size(a::OneBlockView) = ntuple(Returns(1), ndims(a))
function Base.getindex(a::OneBlockView{T,N}, i::Vararg{Int,N}) where {N}
    @boundscheck checkbounds(a, i...)
    return a.array
end
function Base.setindex!(a::OneBlockView{T,N}, b, i::Vararg{Int,N}) where {N}
    @boundscheck checkbounds(a, i...)
    copyto!(a.array, b)
    return a
end

blocks(a::Array) = OneBlockView(a)

It may warrant having an is_blocked trait (say to detect if wrapped arrays are blocked or not) but I think that is a different story.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant