Skip to content

Commit 2d6e88f

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 56e1364 commit 2d6e88f

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
@@ -85,6 +85,11 @@ function lu!(A::StridedMatrix{T}, ::RowMaximum; check::Bool = true) where {T<:Bl
8585
end
8686
function lu!(A::HermOrSym{T}, pivot::Union{RowMaximum,NoPivot,RowNonZero} = lupivottype(T); check::Bool = true) where {T}
8787
copytri!(A.data, A.uplo, isa(A, Hermitian))
88+
@inbounds if isa(A, Hermitian) # realify diagonal
89+
for i in axes(A, 1)
90+
A.data[i,i] = A[i,i]
91+
end
92+
end
8893
lu!(A.data, pivot; check = check)
8994
end
9095
# for backward compatibility

stdlib/LinearAlgebra/test/symmetric.jl

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

0 commit comments

Comments
 (0)