Skip to content

Commit

Permalink
Reject Symmetric(Hermitian(A)) if diagonal of A is not real, fixes J…
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC authored and fredrikekre committed Mar 7, 2019
1 parent ac8c5b7 commit 05ee672
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ for (S, H) in ((:Symmetric, :Hermitian), (:Hermitian, :Symmetric))
throw(ArgumentError("Cannot construct $($S); uplo doesn't match"))
end
end
$S(A::$H) = $S(A.data, sym_uplo(A.uplo))
$S(A::$H) = $S(A, sym_uplo(A.uplo))
function $S(A::$H, uplo::Symbol)
if A.uplo == char_uplo(uplo)
if $H === Hermitian && !(eltype(A) <: Real) &&
any(!isreal, A.data[i] for i in diagind(A.data))

throw(ArgumentError("Cannot construct $($S)($($H))); diagonal contains complex values"))
end
return $S(A.data, sym_uplo(A.uplo))
else
throw(ArgumentError("Cannot construct $($S); uplo doesn't match"))
Expand Down
8 changes: 8 additions & 0 deletions stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -584,4 +584,12 @@ end
@test (A / x)::Matrix == Matrix(A) / x
end

@testset "issue #30814: Symmetric of Hermitian if diag is not real" begin
A = [1 2; 3 4] * (1 + im)
B = Hermitian(A)
@test_throws ArgumentError Symmetric(B) == Symmetric(Matrix(B))
A[1,1] = 1; A[2,2] = 4
@test Symmetric(B) == Symmetric(Matrix(B))
end

end # module TestSymmetric

0 comments on commit 05ee672

Please sign in to comment.