Skip to content

Commit 5cf19a5

Browse files
authored
Allow mixed-type BlockSize containers (#86)
* Allow mixed-type BlockSize containers * Add DefaultBlockSizes * Fix DefaultBlockSizes
1 parent 6ad493b commit 5cf19a5

File tree

8 files changed

+22
-20
lines changed

8 files changed

+22
-20
lines changed

docs/src/man/blockarrays.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The `block_type` should be an array type. It specifies the internal block type,
6767

6868
```julia
6969
julia> BlockArray(undef_blocks, SparseVector{Float64, Int}, [1,2])
70-
2-blocked 3-element BlockArray{Float64,1,Array{SparseVector{Float64,Int64},1},BlockArrays.BlockSizes{1,Array{Int64,1}}}:
70+
2-blocked 3-element BlockArray{Float64,1,Array{SparseVector{Float64,Int64},1},BlockArrays.BlockSizes{1,Tuple{Array{Int64,1}}}}:
7171
#undef
7272
------
7373
#undef
@@ -146,7 +146,7 @@ We can also view and modify views of blocks of `BlockArray` using the `view` syn
146146
julia> A = BlockArray(ones(6), 1:3);
147147
148148
julia> view(A, Block(2))
149-
2-element view(::BlockArray{Float64,1,Array{Array{Float64,1},1},BlockArrays.BlockSizes{1,Array{Int64,1}}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
149+
2-element view(::BlockArray{Float64,1,Array{Array{Float64,1},1},BlockArrays.BlockSizes{1,Tuple{Array{Int64,1}}}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
150150
1.0
151151
1.0
152152
@@ -164,7 +164,7 @@ An array can be repacked into a `BlockArray` with `BlockArray(array, block_sizes
164164

165165
```jl
166166
julia> block_array_sparse = BlockArray(sprand(4, 5, 0.7), [1,3], [2,3])
167-
2×2-blocked 4×5 BlockArray{Float64,2,Array{SparseMatrixCSC{Float64,Int64},2},BlockArrays.BlockSizes{2,Array{Int64,1}}}:
167+
2×2-blocked 4×5 BlockArray{Float64,2,Array{SparseMatrixCSC{Float64,Int64},2},BlockArrays.BlockSizes{2,Tuple{Array{Int64,1}}}}:
168168
0.0341601 0.3741870.0118196 0.299058 0.0
169169
----------------------------------------------------
170170
0.0945445 0.9311150.0460428 0.0 0.0

docs/src/man/pseudoblockarrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ We can also view and modify views of blocks of `PseudoBlockArray` using the `vie
8383
julia> A = PseudoBlockArray(ones(6), 1:3);
8484
8585
julia> view(A, Block(2))
86-
2-element view(::PseudoBlockArray{Float64,1,Array{Float64,1},BlockArrays.BlockSizes{1,Array{Int64,1}}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
86+
2-element view(::PseudoBlockArray{Float64,1,Array{Float64,1},BlockArrays.BlockSizes{1,Tuple{Array{Int64,1}}}}, BlockSlice(Block{1,Int64}((2,)),2:3)) with eltype Float64:
8787
1.0
8888
1.0
8989

src/abstractblockarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ specialize this method if they need to provide custom block bounds checking beha
250250
julia> A = BlockArray(rand(2,3), [1,1], [2,1]);
251251
252252
julia> blockcheckbounds(A, 3, 2)
253-
ERROR: BlockBoundsError: attempt to access 2×2-blocked 2×3 BlockArray{Float64,2,Array{Array{Float64,2},2},BlockArrays.BlockSizes{2,Array{Int64,1}}} at block index [3,2]
253+
ERROR: BlockBoundsError: attempt to access 2×2-blocked 2×3 BlockArray{Float64,2,Array{Array{Float64,2},2},BlockArrays.BlockSizes{2,Tuple{Array{Int64,1},Array{Int64,1}}}} at block index [3,2]
254254
[...]
255255
```
256256
"""

src/blocksizes.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@
55
abstract type AbstractBlockSizes{N} end
66

77
# Keeps track of the (cumulative) sizes of all the blocks in the `BlockArray`.
8-
struct BlockSizes{N,VT<:AbstractVector{Int}} <: AbstractBlockSizes{N}
9-
cumul_sizes::NTuple{N,VT}
8+
struct BlockSizes{N,VT<:NTuple{N,AbstractVector{Int}}} <: AbstractBlockSizes{N}
9+
cumul_sizes::VT
1010
# Takes a tuple of sizes, accumulates them and create a `BlockSizes`
11-
BlockSizes{N,VT}() where {N,VT<:AbstractVector{Int}} = new{N,VT}()
12-
BlockSizes{N,VT}(cs::NTuple{N,VT}) where {N,VT<:AbstractVector{Int}} = new{N,VT}(cs)
11+
BlockSizes{N,VT}() where {N,VT<:NTuple{N,AbstractVector{Int}}} = new{N,VT}()
12+
BlockSizes{N,VT}(cs::VT) where {N,VT<:NTuple{N,AbstractVector{Int}}} = new{N,VT}(cs)
1313
end
1414

15-
BlockSizes{N}(cs::NTuple{N,VT}) where {N,VT<:AbstractVector{Int}} = BlockSizes{N,VT}(cs)
16-
BlockSizes{N}() where N = BlockSizes{N,Vector{Int}}()
15+
const DefaultBlockSizes{N} = BlockSizes{N,NTuple{N,Vector{Int}}}
16+
17+
BlockSizes{N}(cs::VT) where {N,VT<:NTuple{N,AbstractVector{Int}}} = BlockSizes{N,VT}(cs)
18+
BlockSizes{N}() where N = BlockSizes{N,NTuple{N,Vector{Int}}}()
1719
BlockSizes() = BlockSizes{0}()
1820

19-
BlockSizes(cs::NTuple{N,VT}) where {N,VT<:AbstractVector{Int}} = BlockSizes{N}(cs)
21+
BlockSizes(cs::NTuple{N,AbstractVector{Int}}) where N = BlockSizes{N}(cs)
2022

2123
function BlockSizes(sizes::Vararg{AbstractVector{Int}, N}) where {N}
2224
cumul_sizes = ntuple(k -> _cumul_vec(sizes[k]), Val(N))

src/pseudo_blockarray.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ julia> A = PseudoBlockArray(rand(2,3), [1,1], [2,1])
2929
0.849939 0.283365 │ 0.365801
3030
3131
julia> A = PseudoBlockArray(sprand(6, 0.5), [3,2,1])
32-
3-blocked 6-element PseudoBlockArray{Float64,1,SparseVector{Float64,Int64},BlockArrays.BlockSizes{1,Array{Int64,1}}}:
33-
0.0
32+
3-blocked 6-element PseudoBlockArray{Float64,1,SparseVector{Float64,Int64},BlockArrays.BlockSizes{1,Tuple{Array{Int64,1}}}}:
33+
0.0
3434
0.5865981007905481
35-
0.0
35+
0.0
3636
───────────────────
3737
0.05016684053503706
3838
0.0

src/show.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Base.print_matrix_row(io::IO,
8080
i::Integer, cols::AbstractVector, sep::AbstractString) =
8181
_blockarray_print_matrix_row(io, X, A, i, cols, sep)
8282

83-
function _show_typeof(io::IO, a::BlockArray{T,N,Array{Array{T,N},N},BlockSizes{N,Vector{Int}}}) where {T,N}
83+
function _show_typeof(io::IO, a::BlockArray{T,N,Array{Array{T,N},N},DefaultBlockSizes{N}}) where {T,N}
8484
Base.show_type_name(io, typeof(a).name)
8585
print(io, '{')
8686
show(io, T)
@@ -89,7 +89,7 @@ function _show_typeof(io::IO, a::BlockArray{T,N,Array{Array{T,N},N},BlockSizes{N
8989
print(io, '}')
9090
end
9191

92-
function _show_typeof(io::IO, a::PseudoBlockArray{T,N,Array{T,N},BlockSizes{N,Vector{Int}}}) where {T,N}
92+
function _show_typeof(io::IO, a::PseudoBlockArray{T,N,Array{T,N},DefaultBlockSizes{N}}) where {T,N}
9393
Base.show_type_name(io, typeof(a).name)
9494
print(io, '{')
9595
show(io, T)

test/test_blockarrayinterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757
@test Hermitian(A)[Block(2,3)] == A[2:3,4:6]
5858
@test Hermitian(A)[Block(3,2)] == A[2:3,4:6]'
5959

60-
@test stringmime("text/plain", UpperTriangular(A)) == "10×10 UpperTriangular{Complex{Float64},PseudoBlockArray{Complex{Float64},2,Array{Complex{Float64},2},BlockArrays.BlockSizes{2,Array{Int64,1}}}}:\n 1.0+0.0im │ 11.0+0.0im 21.0+0.0im │ 31.0+0.0im 41.0+0.0im 51.0+0.0im │ 61.0+0.0im 71.0+0.0im 81.0+0.0im 91.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ 12.0+0.0im 22.0+0.0im │ 32.0+0.0im 42.0+0.0im 52.0+0.0im │ 62.0+0.0im 72.0+0.0im 82.0+0.0im 92.0+0.0im\n ⋅ │ ⋅ 23.0+0.0im │ 33.0+0.0im 43.0+0.0im 53.0+0.0im │ 63.0+0.0im 73.0+0.0im 83.0+0.0im 93.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ 34.0+0.0im 44.0+0.0im 54.0+0.0im │ 64.0+0.0im 74.0+0.0im 84.0+0.0im 94.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ 45.0+0.0im 55.0+0.0im │ 65.0+0.0im 75.0+0.0im 85.0+0.0im 95.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ 56.0+0.0im │ 66.0+0.0im 76.0+0.0im 86.0+0.0im 96.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ 67.0+0.0im 77.0+0.0im 87.0+0.0im 97.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ 78.0+0.0im 88.0+0.0im 98.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ 89.0+0.0im 99.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ ⋅ 100.0+0.0im"
60+
@test stringmime("text/plain", UpperTriangular(A)) == "10×10 UpperTriangular{Complex{Float64},PseudoBlockArray{Complex{Float64},2,Array{Complex{Float64},2},BlockArrays.BlockSizes{2,Tuple{Array{Int64,1},Array{Int64,1}}}}}:\n 1.0+0.0im │ 11.0+0.0im 21.0+0.0im │ 31.0+0.0im 41.0+0.0im 51.0+0.0im │ 61.0+0.0im 71.0+0.0im 81.0+0.0im 91.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ 12.0+0.0im 22.0+0.0im │ 32.0+0.0im 42.0+0.0im 52.0+0.0im │ 62.0+0.0im 72.0+0.0im 82.0+0.0im 92.0+0.0im\n ⋅ │ ⋅ 23.0+0.0im │ 33.0+0.0im 43.0+0.0im 53.0+0.0im │ 63.0+0.0im 73.0+0.0im 83.0+0.0im 93.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ 34.0+0.0im 44.0+0.0im 54.0+0.0im │ 64.0+0.0im 74.0+0.0im 84.0+0.0im 94.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ 45.0+0.0im 55.0+0.0im │ 65.0+0.0im 75.0+0.0im 85.0+0.0im 95.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ 56.0+0.0im │ 66.0+0.0im 76.0+0.0im 86.0+0.0im 96.0+0.0im\n ───────────┼──────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────────────\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ 67.0+0.0im 77.0+0.0im 87.0+0.0im 97.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ 78.0+0.0im 88.0+0.0im 98.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ 89.0+0.0im 99.0+0.0im\n ⋅ │ ⋅ ⋅ │ ⋅ ⋅ ⋅ │ ⋅ ⋅ ⋅ 100.0+0.0im"
6161
end
6262

6363
@testset "Adjoint/Transpose block arrays" begin

test/test_blockarrays.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,11 @@ end
268268
A = BlockArray(rand(4, 5), [1,3], [2,3]);
269269
buf = IOBuffer()
270270
Base.showerror(buf, BlockBoundsError(A, (3,2)))
271-
@test String(take!(buf)) == "BlockBoundsError: attempt to access 2×2-blocked 4×5 BlockArray{Float64,2,Array{Array{Float64,2},2},BlockArrays.BlockSizes{2,Array{Int64,1}}} at block index [3,2]"
271+
@test String(take!(buf)) == "BlockBoundsError: attempt to access 2×2-blocked 4×5 BlockArray{Float64,2,Array{Array{Float64,2},2},BlockArrays.BlockSizes{2,Tuple{Array{Int64,1},Array{Int64,1}}}} at block index [3,2]"
272272

273273
A = PseudoBlockArray(rand(4, 5), [1,3], [2,3]);
274274
Base.showerror(buf, BlockBoundsError(A, (3,2)))
275-
@test String(take!(buf)) == "BlockBoundsError: attempt to access 2×2-blocked 4×5 PseudoBlockArray{Float64,2,Array{Float64,2},BlockArrays.BlockSizes{2,Array{Int64,1}}} at block index [3,2]"
275+
@test String(take!(buf)) == "BlockBoundsError: attempt to access 2×2-blocked 4×5 PseudoBlockArray{Float64,2,Array{Float64,2},BlockArrays.BlockSizes{2,Tuple{Array{Int64,1},Array{Int64,1}}}} at block index [3,2]"
276276
end
277277

278278

0 commit comments

Comments
 (0)