From bfa01f9c482a7338c5dfcd4768080e52abd716ad Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Tue, 23 Apr 2024 18:51:20 +0200 Subject: [PATCH] Fix generic triangular solves with empty matrices (#54201) (cherry picked from commit 8945914d4f86cd38f1b079cfc916f881eddbb3c2) --- stdlib/LinearAlgebra/src/triangular.jl | 2 ++ stdlib/LinearAlgebra/test/lu.jl | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/stdlib/LinearAlgebra/src/triangular.jl b/stdlib/LinearAlgebra/src/triangular.jl index e8291b1081df2..3e87f6d346e2c 100644 --- a/stdlib/LinearAlgebra/src/triangular.jl +++ b/stdlib/LinearAlgebra/src/triangular.jl @@ -1131,6 +1131,7 @@ function generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, tfun::Function, if size(C) != size(B) throw(DimensionMismatch("size of output, $(size(C)), does not match size of right hand side, $(size(B))")) end + iszero(mA) && return C oA = oneunit(eltype(A)) @inbounds if uploc == 'U' if isunitc == 'N' @@ -1267,6 +1268,7 @@ function generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, ::Function, xA: if size(C) != size(B) throw(DimensionMismatch("size of output, $(size(C)), does not match size of right hand side, $(size(B))")) end + iszero(mA) && return C oA = oneunit(eltype(A)) @inbounds if uploc == 'U' if isunitc == 'N' diff --git a/stdlib/LinearAlgebra/test/lu.jl b/stdlib/LinearAlgebra/test/lu.jl index 744cc543b993e..1797d8b5a9017 100644 --- a/stdlib/LinearAlgebra/test/lu.jl +++ b/stdlib/LinearAlgebra/test/lu.jl @@ -480,4 +480,17 @@ end LinearAlgebra.generic_lufact!(fill(Inf, 2, 2), check=false) end +@testset "lu for empty matrices" begin + for T in (Float64, BigFloat) + A = fill(T(0.0), 0, 0) + v = fill(T(1.0), 0, 10) + @test A \ v ≈ lu(A) \ v + vt = permutedims(v) + @test vt / A ≈ vt / lu(A) + B = UpperTriangular(transpose(fill(complex(T(0.0)), 0, 0)')) + @test B \ v ≈ v + @test vt / B ≈ vt + end +end + end # module TestLU