Skip to content

Commit d0b2be1

Browse files
authored
Faster blockdiag for uniform input value-index types (#36013)
1 parent 9a431f3 commit d0b2be1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,16 +3318,25 @@ julia> blockdiag(sparse(2I, 3, 3), sparse(4I, 2, 2))
33183318
⋅ ⋅ ⋅ ⋅ 4
33193319
```
33203320
"""
3321+
blockdiag() = spzeros(promote_type(), Int, 0, 0)
3322+
3323+
function blockdiag(X::AbstractSparseMatrixCSC{Tv, Ti}...) where {Tv, Ti <: Integer}
3324+
_blockdiag(Tv, Ti, X...)
3325+
end
3326+
33213327
function blockdiag(X::AbstractSparseMatrixCSC...)
3328+
Tv = promote_type(map(x->eltype(nonzeros(x)), X)...)
3329+
Ti = promote_type(map(x->eltype(rowvals(x)), X)...)
3330+
_blockdiag(Tv, Ti, X...)
3331+
end
3332+
3333+
function _blockdiag(::Type{Tv}, ::Type{Ti}, X::AbstractSparseMatrixCSC...) where {Tv, Ti <: Integer}
33223334
num = length(X)
33233335
mX = Int[ size(x, 1) for x in X ]
33243336
nX = Int[ size(x, 2) for x in X ]
33253337
m = sum(mX)
33263338
n = sum(nX)
33273339

3328-
Tv = promote_type(map(x->eltype(nonzeros(x)), X)...)
3329-
Ti = isempty(X) ? Int : promote_type(map(x->eltype(rowvals(x)), X)...)
3330-
33313340
colptr = Vector{Ti}(undef, n+1)
33323341
nnzX = Int[ nnz(x) for x in X ]
33333342
nnz_res = sum(nnzX)

0 commit comments

Comments
 (0)