-
Notifications
You must be signed in to change notification settings - Fork 31
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
More convenient syntax for getting the size of a block #369
Comments
There's also However, I agree with you in general that something like this would have been convenient |
Oops, you're right, I forgot about However, from what I can tell the most compact way to get the size of a block using julia> blocksizes(a)
([2, 3], [2, 3])
julia> getindex.(blocksizes(a), Int.(Tuple(Block(1, 2))))
(2, 3) (say if you have a |
An alternative to julia> struct BlockSizes{N,A<:AbstractArray{<:Any,N}} <: AbstractArray{NTuple{N,Int},N}
array::A
end
julia> Base.getindex(a::BlockSizes, index::Integer...) = size(@view(a.array[Block(index)]))
julia> Base.size(a::BlockSizes) = blocksize(a.array)
julia> BlockSizes(a)[1, 2]
(2, 3) EDIT: In the end, I think this is my favorite one, because of the analogy with the current julia> blocksizes(a::AbstractArray) = BlockSizes(a)
julia> blocksizes(a)[1, 2]
(2, 3)
julia> blocksizes(a)
2×2 BlockSizes{2, BlockMatrix{Float64, Matrix{Matrix{Float64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}}:
(2, 2) (2, 3)
(3, 2) (3, 3) which has a nice correspondence with calling julia> getindex.(blocklengths.(axes(a)), (1, 2))
(2, 3) I would favor dropping the current julia> size.(eachblock(a))
2×2 Matrix{Tuple{Int64, Int64}}:
(2, 2) (2, 3)
(3, 2) (3, 3) A more generic julia> struct BlockLengths{N,A<:AbstractArray{<:Any,N}} <: AbstractArray{Int,N}
array::A
end
julia> Base.getindex(a::BlockLengths, index::Integer...) = length(@view(a.array[Block(index)]))
julia> Base.size(a::BlockLengths) = blocksize(a.array)
julia> BlockArrays.blocklengths(a::AbstractArray) = BlockLengths(a)
julia> blocklengths(a)[1, 2]
6
julia> blocklengths(a)
2×2 BlockLengths{2, BlockMatrix{Float64, Matrix{Matrix{Float64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}}:
4 6
6 9 with an alternative syntax julia> struct BlockAxeses{N,A<:AbstractArray{<:Any,N}} <: AbstractArray{Tuple,N}
array::A
end
julia> blockaxeses(a::AbstractArray) = BlockAxeses(a)
blockaxes (generic function with 1 method)
julia> Base.getindex(a::BlockAxeses, index::Integer...) = axes(@view(a.array[Block(index)]))
julia> Base.size(a::BlockAxeses) = blocksize(a.array)
julia> blockaxeses(a)[1, 2]
(Base.OneTo(2), Base.OneTo(3))
julia> blockaxeses(a)
2×2 BlockAxeses{2, BlockMatrix{Float64, Matrix{Matrix{Float64}}, Tuple{BlockedUnitRange{Vector{Int64}}, BlockedUnitRange{Vector{Int64}}}}}:
(Base.OneTo(2), Base.OneTo(2)) (Base.OneTo(2), Base.OneTo(3))
(Base.OneTo(3), Base.OneTo(2)) (Base.OneTo(3), Base.OneTo(3))
|
Another idea would be to define a generic function julia> viewsize(a::AbstractArray, indices...) = size(view(a, indices...))
viewsize (generic function with 1 method)
julia> viewsize(a, Block(1, 2))
(2, 3) though maybe that isn't much better than EDIT: Some other ideas are julia> blockedsize(a::AbstractArray, index...) = size(view(a, Block(index)))
blockedsize (generic function with 1 method)
julia> blockedsize(a, 1, 2)
(2, 3)
julia> getblocksize(a::AbstractArray, index...) = size(view(a, Block(index)))
getblocksize (generic function with 1 method)
julia> getblocksize(a, 1, 2)
(2, 3) But I think I like the suggestion for redefining |
Closed by #399. |
New syntax proposal for obtaining the size of a block
EDIT: TLDR: This proposal is to change the definition of
blocksizes
so that it acts like this:i.e. it acts like a collection of size
blocksize(a)
, and indexing into it outputs the size of the corresponding block.The current definition gives:
which is equivalent to:
so uses of the current
blocksizes
definition could switch to that, which I think is clearer anyway. See also the discussion in #255.Summary of the current syntax for obtaining the size of a block
From what I can tell, these are the most compact ways of getting the size of a block right now, using public APIs:
(please correct me if I am wrong).
I think it would be nice to have something more convenient. The best I can come up with is overloading
Base.size(a::AbstractArray, b::Block)
, for example:It feels like a slight abuse of
Base.size
, but seems along the same lines as being able to ask for the size in a certain dimension withsize(a, 1)
.I believe this would make sense for getting the axes of a block as well, i.e.
axes(a, Block(1, 2))
, however perhaps there is some ambiguity there if that is meant to be a slice of the axes ofa
, i.e.:or the axes of the
@view(a[Block(1, 2)])
, i.e.:It is a bit unfortunate that
blocksize
is already taken and has a different meaning, I understand why that choice was made but I found that to be confusing at first and my initial thought was that it should be a way to get the size of a block.The text was updated successfully, but these errors were encountered: