Skip to content

Commit 12c9391

Browse files
authored
“Fix” #53451 -- allow zero-row QR factorization bypassing LAPACK (#53578)
1 parent d7dc9a8 commit 12c9391

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

stdlib/LinearAlgebra/src/lapack.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ for (gebrd, gelqf, geqlf, geqrf, geqp3, geqrt, geqrt3, gerqf, getrf, elty, relty
470470
end
471471
lda = max(1, stride(A,2))
472472
work = Vector{$elty}(undef, nb*n)
473-
if n > 0
473+
if minmn > 0
474474
info = Ref{BlasInt}()
475475
ccall((@blasfunc($geqrt), libblastrampoline), Cvoid,
476476
(Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty},
@@ -496,7 +496,7 @@ for (gebrd, gelqf, geqlf, geqrf, geqp3, geqrt, geqrt3, gerqf, getrf, elty, relty
496496
if p != n || q != n
497497
throw(DimensionMismatch("block reflector T has dimensions ($p,$q), but should have dimensions ($n,$n)"))
498498
end
499-
if n > 0
499+
if n > 0 # this implies `m > 0` because of `m >= n`
500500
info = Ref{BlasInt}()
501501
ccall((@blasfunc($geqrt3), libblastrampoline), Cvoid,
502502
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},

stdlib/LinearAlgebra/test/qr.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,28 @@ end
504504
@test x xf
505505
end
506506

507+
@testset "issue #53451" begin
508+
# in the issue it was noted that QR factorizations of zero-column matrices
509+
# were possible, but zero row-matrices errored, because LAPACK does not
510+
# accept these empty matrices. now, the `geqrt!` call should be forwarded only
511+
# if both matrix dimensions are positive.
512+
513+
for dimA in (0, 1, 2, 4)
514+
for F in (Float32, Float64, ComplexF32, ComplexF64, BigFloat)
515+
# this should have worked before, Q is square, and R is 0 × 0:
516+
A_zero_cols = rand(F, dimA, 0)
517+
qr_zero_cols = qr(A_zero_cols)
518+
@test size(qr_zero_cols.Q) == (dimA, dimA)
519+
@test size(qr_zero_cols.R) == (0, 0)
520+
@test qr_zero_cols.Q == LinearAlgebra.I(dimA)
521+
522+
# this should work now, Q is 0 × 0, and R has `dimA` columns:
523+
A_zero_rows = rand(F, 0, dimA)
524+
qr_zero_rows = qr(A_zero_rows)
525+
@test size(qr_zero_rows.Q) == (0, 0)
526+
@test size(qr_zero_rows.R) == (0, dimA)
527+
end
528+
end
529+
end
530+
507531
end # module TestQR

0 commit comments

Comments
 (0)