-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathblockdiag.jl
37 lines (34 loc) · 1011 Bytes
/
blockdiag.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright (c) 2016: CSDP.jl contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
abstract type AbstractBlockMatrix{T} <: AbstractMatrix{T} end
function nblocks end
function block end
function Base.size(bm::AbstractBlockMatrix)
n = mapreduce(
blk -> LinearAlgebra.checksquare(block(bm, blk)),
+,
1:nblocks(bm),
init = 0,
)
return (n, n)
end
function Base.getindex(bm::AbstractBlockMatrix, i::Integer, j::Integer)
(i < 0 || j < 0) && throw(BoundsError(i, j))
for k in 1:nblocks(bm)
blk = block(bm, k)
n = size(blk, 1)
if i <= n && j <= n
return blk[i, j]
elseif i <= n || j <= n
return 0
else
i -= n
j -= n
end
end
i, j = (i, j) .+ size(bm)
throw(BoundsError(i, j))
end
Base.getindex(A::AbstractBlockMatrix, I::Tuple) = getindex(A, I...)