Line data Source code
1 9 : spblockdiag(A::SparseOp; count::Int=1) =
2 : count == 1 ? copy(A) : spblockdiag((A for i in 1:count)...)
3 :
4 5 : function spblockdiag(A::SparseOp...)
5 19 : m = sum(S -> size(S, 1), A)
6 19 : n = sum(S -> size(S, 2), A)
7 5 : nz = sum(map(nnz, A))
8 5 : T = Base.promote_type(map(eltype, A)...)
9 :
10 24 : i = zeros(Int, nz)
11 24 : j = zeros(Int, nz)
12 24 : v = zeros(T, nz)
13 :
14 5 : ofs = 0
15 5 : ofsi = 0
16 5 : ofsj = 0
17 :
18 5 : for S in A
19 14 : nzs = nnz(S)
20 15 : let (is, js, vs) = spfind(S)
21 14 : for k in 1:nzs
22 164 : i[k + ofs] = is[k] + ofsi
23 164 : j[k + ofs] = js[k] + ofsj
24 164 : v[k + ofs] = vs[k]
25 314 : end
26 : end
27 14 : ofs += nzs
28 15 : ofsi += size(S, 1)
29 15 : ofsj += size(S, 2)
30 19 : end
31 :
32 5 : sparse(i, j, v, m, n)
33 : end
34 :
35 : # spblockdiag(A::Diagonal; count::Int=1) =
36 : # count == 1 ? copy(A) : Diagonal(repeat(diag(A), count))
37 :
38 : # spblockdiag(A::Diagonal...) = Diagonal(vcat(map(diag,A)...))
39 :
40 : """
41 : M = spblockdiag(A, B, C,...)
42 : M = spblockdiag(A, count=N)
43 :
44 : Create a block diagonal matrix from sparse matrices (including
45 : adjoints) or `Diagonal` matrices `A, B, C,...` .
46 :
47 : !!! note
48 : `SparseArrays.blockdiag` is available and more efficient,
49 : but does not handle adjoints/transposed matrices.
50 :
51 : """ spblockdiag
52 :
53 : """
54 : M = blockdiag(A; count=n)
55 : M = blockdiag(A; count=Val(N))
56 :
57 : Extends `SparseArrays.blockdiag` by passing an n-tuple of `A`s.
58 : """
59 6 : SparseArrays.blockdiag(A; count) = blockdiag(ntuple(_ -> A, count)...)
|