Skip to content

Commit 1c25c49

Browse files
dkarraschKristofferC
authored andcommitted
Fix solve for complex Hermitian with non-vanishing imaginary part on diagonal (#54276)
(cherry picked from commit e80a880)
1 parent 99340fa commit 1c25c49

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

stdlib/LinearAlgebra/src/lu.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ end
9595
function lu!(A::HermOrSym{T}, pivot::Union{RowMaximum,NoPivot,RowNonZero} = lupivottype(T);
9696
check::Bool = true, allowsingular::Bool = false) where {T}
9797
copytri!(A.data, A.uplo, isa(A, Hermitian))
98+
@inbounds if isa(A, Hermitian) # realify diagonal
99+
for i in axes(A, 1)
100+
A.data[i,i] = A[i,i]
101+
end
102+
end
98103
lu!(A.data, pivot; check, allowsingular)
99104
end
100105
# for backward compatibility

stdlib/LinearAlgebra/test/symmetric.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,11 @@ end
264264
@testset "inverse edge case with complex Hermitian" begin
265265
# Hermitian matrix, where inv(lu(A)) generates non-real diagonal elements
266266
for T in (ComplexF32, ComplexF64)
267-
A = T[0.650488+0.0im 0.826686+0.667447im; 0.826686-0.667447im 1.81707+0.0im]
268-
H = Hermitian(A)
267+
# data should have nonvanishing imaginary parts on the diagonal
268+
M = T[0.279982+0.988074im 0.770011+0.870555im
269+
0.138001+0.889728im 0.177242+0.701413im]
270+
H = Hermitian(M)
271+
A = Matrix(H)
269272
@test inv(H) inv(A)
270273
@test ishermitian(Matrix(inv(H)))
271274
end

0 commit comments

Comments
 (0)