Skip to content

Commit 30cb5d6

Browse files
authored
Avoid copy in empty bidiag ldiv! (#1275)
If `N == 0`, the arrays are all empty, so there's nothing to copy. We may return the output array directly.
1 parent 5d3ef46 commit 30cb5d6

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/bidiag.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,19 +1250,19 @@ end
12501250
ldiv!(A::Bidiagonal, b::AbstractVecOrMat) = @inline ldiv!(b, A, b)
12511251
function ldiv!(c::AbstractVecOrMat, A::Bidiagonal, b::AbstractVecOrMat)
12521252
require_one_based_indexing(c, A, b)
1253-
N = size(A, 2)
1253+
N = size(A, 1)
12541254
mb, nb = size(b, 1), size(b, 2)
12551255
if N != mb
1256-
throw(DimensionMismatch(lazy"second dimension of A, $N, does not match first dimension of b, $mb"))
1256+
dimstr = b isa AbstractVector ? "length" : "first dimension"
1257+
throw(DimensionMismatch(LazyString(lazy"the first dimension of the Bidiagonal matrix, $N, ",
1258+
lazy"does not match the $dimstr of the right-hand-side, $mb")))
12571259
end
12581260
mc, nc = size(c, 1), size(c, 2)
12591261
if mc != mb || nc != nb
1260-
throw(DimensionMismatch(lazy"size of result, ($mc, $nc), does not match the size of b, ($mb, $nb)"))
1262+
throw(DimensionMismatch(lazy"size of result, $(size(c)), does not match the size of b, $(size(b))"))
12611263
end
12621264

1263-
if N == 0
1264-
return copyto!(c, b)
1265-
end
1265+
N == 0 && return c # in this case c and b are also empty
12661266

12671267
zi = findfirst(iszero, A.dv)
12681268
isnothing(zi) || throw(SingularException(zi))

test/bidiag.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,4 +1180,15 @@ end
11801180
@test !isreal(im*M)
11811181
end
11821182

1183+
@testset "ldiv! error message" begin
1184+
C = zeros(2)
1185+
B = Bidiagonal(1:0, 1:0, :U)
1186+
msg = "size of result, (2,), does not match the size of b, (0, 1)"
1187+
@test_throws msg ldiv!(C, B, zeros(0,1))
1188+
msg = "the first dimension of the Bidiagonal matrix, 0, does not match the length of the right-hand-side, 2"
1189+
@test_throws msg ldiv!(C, B, zeros(2))
1190+
msg = "the first dimension of the Bidiagonal matrix, 0, does not match the first dimension of the right-hand-side, 2"
1191+
@test_throws msg ldiv!(C, B, zeros(2,1))
1192+
end
1193+
11831194
end # module TestBidiagonal

0 commit comments

Comments
 (0)