Skip to content

Commit a946bb3

Browse files
committed
fix multiplication of BlockSkylineMatrix
1 parent 11c28ef commit a946bb3

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

src/linalg.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,25 @@ function add_bandwidths(A::AbstractBlockBandedMatrix,B::AbstractBlockBandedMatri
103103
Al,Au = colblockbandwidths(A)
104104
Bl,Bu = colblockbandwidths(B)
105105

106-
l = copy(Al)
107-
u = copy(Au)
106+
l = copy(Bl)
107+
u = copy(Bu)
108108

109-
for (v,Bv) in [(l,Bl),(u,Bu)]
109+
for (v,Av) in [(l,Al),(u,Au)]
110110
n = length(v)
111111
for i = 1:n
112-
sel = max(i-Au[i],1):min(i+Al[i],n)
112+
sel = max(i-Bu[i],1):min(i+Bl[i],length(Av))
113113
isempty(sel) && continue
114-
v[i] += maximum(Bv[sel])
114+
v[i] += maximum(Av[sel])
115115
end
116116
end
117117

118118
l,u
119119
end
120120

121-
add_bandwidths(A::BlockBandedMatrix,B::BlockBandedMatrix) =
122-
colblockbandwidths(A) .+ colblockbandwidths(B)
121+
function add_bandwidths(A::BlockBandedMatrix,B::BlockBandedMatrix)
122+
l,u = blockbandwidths(A) .+ blockbandwidths(B)
123+
Fill(l,nblocks(B,2)), Fill(u,nblocks(B,2))
124+
end
123125

124126
function similar(M::MatMulMat{<:AbstractBlockBandedLayout,<:AbstractBlockBandedLayout}, ::Type{T}) where T
125127
A,B = M.factors

test/test_linalg.jl

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,29 @@ end
3232
@test unsafe_load(pointer(V)) == 46
3333
@test unsafe_load(pointer(V) + stride(V,2)*sizeof(Float64)) == 53
3434

35-
x = randn(size(A,2))
36-
@test A*x == (similar(x) .= Mul(A,x)) Matrix(A)*x
35+
x = randn(size(A,2))
36+
@test A*x == (similar(x) .= Mul(A,x)) Matrix(A)*x
3737

38-
z = randn(size(A,2)) + im*randn(size(A,2))
39-
A*z == (similar(z) .= Mul(A,z)) Matrix(A)*z
38+
z = randn(size(A,2)) + im*randn(size(A,2))
39+
A*z == (similar(z) .= Mul(A,z)) Matrix(A)*z
4040

41-
Matrix(A)*z
42-
z[1]
43-
view(A,Block(1,1)) * z[1] + view(A,Block(1,2))*z[2:3]
41+
Matrix(A)*z
42+
z[1]
43+
view(A,Block(1,1)) * z[1] + view(A,Block(1,2))*z[2:3]
4444

45-
A*z
45+
A*z
4646

47-
X = randn(size(A))
48-
@test A*X == (similar(X) .= Mul(A,X)) Matrix(A)*X
49-
@test X*A == (similar(X) .= Mul(X,A)) Matrix(X)*A
47+
X = randn(size(A))
48+
@test A*X == (similar(X) .= Mul(A,X)) Matrix(A)*X
49+
@test X*A == (similar(X) .= Mul(X,A)) Matrix(X)*A
5050

5151

52-
Z = randn(size(A)) + im*randn(size(A))
52+
Z = randn(size(A)) + im*randn(size(A))
5353

54-
A*Z
54+
A*Z
5555

5656

57-
v = fill(1.0,4)
57+
v = fill(1.0,4)
5858
U = UpperTriangular(view(A, Block(N,N)))
5959
@test Matrix(U) == U
6060
w = Matrix(U) \ v
@@ -72,8 +72,6 @@ v = fill(1.0,4)
7272
@test v == fill(1.0,size(A,1))
7373
@test ldiv!(U, v) === v
7474
@test v w
75-
76-
7775
end
7876

7977
@testset "BandedBlockBandedMatrix linear algebra" begin
@@ -155,3 +153,26 @@ end
155153
@time BLAS.axpy!(1.0, A, B)
156154
@test B AB
157155
end
156+
157+
@testset "Rectangular block *" begin
158+
A = BlockBandedMatrix{Float64}(undef, (Ones{Int}(2), Ones{Int}(2)), (0,2))
159+
A.data .= randn.()
160+
B = BlockBandedMatrix{Float64}(undef, (Ones{Int}(2), Ones{Int}(3)), (0,2))
161+
B.data .= randn.()
162+
163+
@test A*B Matrix(A)*Matrix(B)
164+
165+
166+
rows = rand(1:10, 5)
167+
l = rand(0:2, 5)
168+
u = rand(0:2, 5)
169+
170+
m = sum(rows)
171+
172+
A = BlockSkylineMatrix(Zeros(m,m), (rows,rows), (l,u))
173+
A.data .= randn.()
174+
175+
B = BlockSkylineMatrix(Zeros(m,m+1), (rows,[rows;1]), ([l;1],[u;1]))
176+
B.data .= randn.()
177+
@test A*B Matrix(A)*Matrix(B)
178+
end

0 commit comments

Comments
 (0)