Skip to content

Commit 9604a69

Browse files
authored
Merge pull request #117 from invenia/mjp/fix-empty-boundscheck
Correctly handle `size` when `blocks` is empty
2 parents 932b020 + 83c183e commit 9604a69

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockDiagonals"
22
uuid = "0a1fb500-61f7-11e9-3c65-f5ef3456f9f0"
33
authors = ["Invenia Technical Computing Corporation"]
4-
version = "0.1.36"
4+
version = "0.1.37"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/blockdiagonal.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,20 @@ end
119119

120120
Base.collect(B::BlockDiagonal) = Matrix(B)
121121

122-
Base.size(B::BlockDiagonal) = sum(firstsize, blocks(B)), sum(lastsize, blocks(B))
122+
# init kwarg for sum introduced in v1.6
123+
# see https://github.com/JuliaLang/julia/blob/master/base/reduce.jl#L492
124+
@static if VERSION < v"1.6"
125+
function Base.size(B::BlockDiagonal)
126+
if isempty(blocks(B))
127+
return 0, 0
128+
else
129+
return sum(firstsize, blocks(B)), sum(lastsize, blocks(B))
130+
end
131+
end
132+
else
133+
Base.size(B::BlockDiagonal) = sum(firstsize, blocks(B); init=0), sum(lastsize, blocks(B); init=0)
134+
end
135+
123136
Base.similar(B::BlockDiagonal) = BlockDiagonal(map(similar, blocks(B)))
124137
Base.similar(B::BlockDiagonal, ::Type{T}) where T = BlockDiagonal(map(b -> similar(b, T), blocks(B)))
125138
Base.parent(B::BlockDiagonal) = B.blocks

test/blockdiagonal.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ using Test
8484
@test blocksize(B, 2) == blocksizes(B)[2] == blocksize(B, 2, 2)
8585
end
8686

87+
@testset "no blocks" begin
88+
B = BlockDiagonal(Matrix{Float64}[]);
89+
@test size(B) == (0, 0)
90+
end
91+
8792
@testset "Equality" begin
8893
# Equality
8994
@test b1 == b1

0 commit comments

Comments
 (0)