Skip to content

Commit 56a46e3

Browse files
martinholtersandreasnoack
authored andcommitted
Make blockdiag return a 0×0 SparseMatrixCSC{Union{},Int} (#28053)
Fixes #14850.
1 parent 5f19151 commit 56a46e3

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3139,7 +3139,7 @@ function blockdiag(X::SparseMatrixCSC...)
31393139
n = sum(nX)
31403140

31413141
Tv = promote_type(map(x->eltype(x.nzval), X)...)
3142-
Ti = promote_type(map(x->eltype(x.rowval), X)...)
3142+
Ti = isempty(X) ? Int : promote_type(map(x->eltype(x.rowval), X)...)
31433143

31443144
colptr = Vector{Ti}(undef, n+1)
31453145
nnzX = Int[ nnz(x) for x in X ]
@@ -3158,6 +3158,7 @@ function blockdiag(X::SparseMatrixCSC...)
31583158
nX_sofar += nX[i]
31593159
mX_sofar += mX[i]
31603160
end
3161+
colptr[n+1] = nnz_sofar + 1
31613162

31623163
SparseMatrixCSC(m, n, colptr, rowval, nzval)
31633164
end

stdlib/SparseArrays/test/sparse.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ end
118118

119119
@testset "blockdiag concatenation" begin
120120
@test blockdiag(se33, se33) == sparse(1:6,1:6,fill(1.,6))
121+
@test blockdiag() == spzeros(0, 0)
122+
@test nnz(blockdiag()) == 0
121123
end
122124

123125
@testset "concatenation promotion" begin

0 commit comments

Comments
 (0)