Skip to content

Commit b83b601

Browse files
committed
Added BlockSkylineMatrix–Diagonal arithmetic (fixes #57)
1 parent ce444e3 commit b83b601

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/BlockSkylineMatrix.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,24 @@ end
449449
throw(BandError(A, J-K))
450450
end
451451
end
452+
453+
for op in (:-, :+)
454+
@eval begin
455+
function $op(A::BlockSkylineMatrix, D::Diagonal)
456+
checksquareblocks(A)
457+
B = LinearAlgebra.copy_oftype(A, Base._return_type(+, Tuple{eltype(A), eltype(D)}))
458+
@inbounds for i in axes(A, 1)
459+
B[i,i] = $op(B[i,i], D.diag[i])
460+
end
461+
B
462+
end
463+
function $op(D::Diagonal, A::BlockSkylineMatrix)
464+
checksquareblocks(A)
465+
B = LinearAlgebra.copy_oftype($op(A), Base._return_type(+, Tuple{eltype(A), eltype(D)}))
466+
@inbounds for i in axes(A, 1)
467+
B[i,i] += D.diag[i]
468+
end
469+
B
470+
end
471+
end
472+
end

test/test_blockskyline.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,27 @@ Random.seed!(0)
104104
@test Matrix(AC) == Matrix(A) + Matrix(C)
105105
@test blockisequal(axes(AC),axes(C))
106106
end
107+
108+
@testset "BlockSkylineMatrix–Diagonal arithmetic" begin
109+
l,u = 2,1
110+
N = M = 4
111+
cols = rows = 1:N
112+
A = BlockBandedMatrix(Ones(sum(rows),sum(cols)), rows,cols, (l,u))
113+
V = Diagonal(1:size(A,1))
114+
115+
@testset for (label,op,a,b) in [("(A + V",+,A,V),
116+
("(V + A",+,V,A),
117+
("(A - V",-,A,V),
118+
("(V - A",-,V,A)]
119+
C = op(a,b)
120+
@test C isa BlockSkylineMatrix
121+
@test C.block_sizes == A.block_sizes
122+
@test Matrix(C) == op(Matrix(a),Matrix(b))
123+
end
124+
125+
# This matrix has no blocks on the main diagonal
126+
B = BlockBandedMatrix(Ones(sum(rows),sum(cols)), rows,cols, (l,-1))
127+
@test_throws BandedMatrices.BandError B+V
128+
@test_throws BandedMatrices.BandError V-B
129+
end
107130
end

0 commit comments

Comments
 (0)