Skip to content

Commit 643492d

Browse files
authored
Fix BandedMatrix pad for fewer rows/cols (#446)
1 parent b2c80db commit 643492d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/LinearAlgebra/helper.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ end
261261

262262
function pad(A::BandedMatrix, n::Integer, m::Integer)
263263
B = BandedMatrix{eltype(A)}(undef, (n,m), bandwidths(A))
264-
copyto!(B.data, A.data)
264+
copyto!(B.data, firstindex(B.data), A.data, firstindex(A.data), min(length(A.data), length(B.data)))
265265
B.data[length(A.data)+1:end] .= 0
266266
return B
267267
end

test/runtests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ end
8484
@test pad(a, 2) == @view(a[1:2])
8585
end
8686
end
87+
88+
@testset "BandedMatrix" begin
89+
B = BandedMatrix(0=>[1:4;])
90+
B11 = pad(B, 1, 1)
91+
@test B11 isa BandedMatrix
92+
@test B11 == BandedMatrix(0=>[1])
93+
B16 = pad(B, 1, 6)
94+
@test B16 isa BandedMatrix
95+
@test B16 == BandedMatrix((0=>[1],), (1,6), (0,0))
96+
B61 = pad(B, 6, 1)
97+
@test B61 isa BandedMatrix
98+
@test B61 == BandedMatrix((0=>[1],), (6,1), (0,0))
99+
B66 = pad(B, 6, 6)
100+
@test B66 isa BandedMatrix
101+
@test B66 == BandedMatrix(0=>[1:4;0;0])
102+
B23 = pad(B, 2, 3)
103+
@test B23 isa BandedMatrix
104+
@test B23 == BandedMatrix((0=>[1,2],), (2,3), (0,0))
105+
end
87106
end
88107
@testset "nocat" begin
89108
D = Derivative()

0 commit comments

Comments
 (0)