From abbd086eb7263ab2d2ef270eec0ec1f9be5a02a7 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 8 Apr 2025 17:35:42 +0530 Subject: [PATCH 1/2] Avoid copy in empty bidiag `ldiv!` --- src/bidiag.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/bidiag.jl b/src/bidiag.jl index 1a34adde..3df8c4db 100644 --- a/src/bidiag.jl +++ b/src/bidiag.jl @@ -1250,19 +1250,17 @@ end ldiv!(A::Bidiagonal, b::AbstractVecOrMat) = @inline ldiv!(b, A, b) function ldiv!(c::AbstractVecOrMat, A::Bidiagonal, b::AbstractVecOrMat) require_one_based_indexing(c, A, b) - N = size(A, 2) + N = size(A, 1) mb, nb = size(b, 1), size(b, 2) if N != mb - throw(DimensionMismatch(lazy"second dimension of A, $N, does not match first dimension of b, $mb")) + throw(DimensionMismatch(lazy"first dimension of A, $N, does not match first dimension of b, $mb")) end mc, nc = size(c, 1), size(c, 2) if mc != mb || nc != nb - throw(DimensionMismatch(lazy"size of result, ($mc, $nc), does not match the size of b, ($mb, $nb)")) + throw(DimensionMismatch(lazy"size of result, $(size(c)), does not match the size of b, $(size(b))")) end - if N == 0 - return copyto!(c, b) - end + N == 0 && return c # in this case c and b are also empty zi = findfirst(iszero, A.dv) isnothing(zi) || throw(SingularException(zi)) From 02045d49dd98769308fb53bba34ae0eacbc22fb9 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 8 Apr 2025 19:27:18 +0530 Subject: [PATCH 2/2] Update error messages --- src/bidiag.jl | 4 +++- test/bidiag.jl | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/bidiag.jl b/src/bidiag.jl index 3df8c4db..326fbb51 100644 --- a/src/bidiag.jl +++ b/src/bidiag.jl @@ -1253,7 +1253,9 @@ function ldiv!(c::AbstractVecOrMat, A::Bidiagonal, b::AbstractVecOrMat) N = size(A, 1) mb, nb = size(b, 1), size(b, 2) if N != mb - throw(DimensionMismatch(lazy"first dimension of A, $N, does not match first dimension of b, $mb")) + dimstr = b isa AbstractVector ? "length" : "first dimension" + throw(DimensionMismatch(LazyString(lazy"the first dimension of the Bidiagonal matrix, $N, ", + lazy"does not match the $dimstr of the right-hand-side, $mb"))) end mc, nc = size(c, 1), size(c, 2) if mc != mb || nc != nb diff --git a/test/bidiag.jl b/test/bidiag.jl index 7d07640d..1c1edd37 100644 --- a/test/bidiag.jl +++ b/test/bidiag.jl @@ -1180,4 +1180,15 @@ end @test !isreal(im*M) end +@testset "ldiv! error message" begin + C = zeros(2) + B = Bidiagonal(1:0, 1:0, :U) + msg = "size of result, (2,), does not match the size of b, (0, 1)" + @test_throws msg ldiv!(C, B, zeros(0,1)) + msg = "the first dimension of the Bidiagonal matrix, 0, does not match the length of the right-hand-side, 2" + @test_throws msg ldiv!(C, B, zeros(2)) + msg = "the first dimension of the Bidiagonal matrix, 0, does not match the first dimension of the right-hand-side, 2" + @test_throws msg ldiv!(C, B, zeros(2,1)) +end + end # module TestBidiagonal