diff --git a/stdlib/LinearAlgebra/src/lapack.jl b/stdlib/LinearAlgebra/src/lapack.jl index 02dfa0079038b..875ed3ed4e3da 100644 --- a/stdlib/LinearAlgebra/src/lapack.jl +++ b/stdlib/LinearAlgebra/src/lapack.jl @@ -470,7 +470,7 @@ for (gebrd, gelqf, geqlf, geqrf, geqp3, geqrt, geqrt3, gerqf, getrf, elty, relty end lda = max(1, stride(A,2)) work = Vector{$elty}(undef, nb*n) - if n > 0 + if minmn > 0 info = Ref{BlasInt}() ccall((@blasfunc($geqrt), libblastrampoline), Cvoid, (Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, @@ -496,7 +496,7 @@ for (gebrd, gelqf, geqlf, geqrf, geqp3, geqrt, geqrt3, gerqf, getrf, elty, relty if p != n || q != n throw(DimensionMismatch("block reflector T has dimensions ($p,$q), but should have dimensions ($n,$n)")) end - if n > 0 + if n > 0 # this implies `m > 0` because of `m >= n` info = Ref{BlasInt}() ccall((@blasfunc($geqrt3), libblastrampoline), Cvoid, (Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, diff --git a/stdlib/LinearAlgebra/test/qr.jl b/stdlib/LinearAlgebra/test/qr.jl index 184971da304f7..e339706598a8a 100644 --- a/stdlib/LinearAlgebra/test/qr.jl +++ b/stdlib/LinearAlgebra/test/qr.jl @@ -504,4 +504,28 @@ end @test x ≈ xf end +@testset "issue #53451" begin + # in the issue it was noted that QR factorizations of zero-column matrices + # were possible, but zero row-matrices errored, because LAPACK does not + # accept these empty matrices. now, the `geqrt!` call should be forwarded only + # if both matrix dimensions are positive. + + for dimA in (0, 1, 2, 4) + for F in (Float32, Float64, ComplexF32, ComplexF64, BigFloat) + # this should have worked before, Q is square, and R is 0 × 0: + A_zero_cols = rand(F, dimA, 0) + qr_zero_cols = qr(A_zero_cols) + @test size(qr_zero_cols.Q) == (dimA, dimA) + @test size(qr_zero_cols.R) == (0, 0) + @test qr_zero_cols.Q == LinearAlgebra.I(dimA) + + # this should work now, Q is 0 × 0, and R has `dimA` columns: + A_zero_rows = rand(F, 0, dimA) + qr_zero_rows = qr(A_zero_rows) + @test size(qr_zero_rows.Q) == (0, 0) + @test size(qr_zero_rows.R) == (0, dimA) + end + end +end + end # module TestQR