Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issymmetric and is hermitian for Symmetric and Hermitian matrices #21994

Merged
merged 1 commit into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions base/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,33 @@ function copy!(dest::Hermitian, src::Hermitian)
return dest
end

function Base.isreal(A::HermOrSym)
n = size(A, 1)
@inbounds if A.uplo == 'U'
for j in 1:n
for i in 1:(j - (A isa Hermitian))
if !isreal(A.data[i,j])
return false
end
end
end
else
for j in 1:n
for i in (j + (A isa Hermitian)):n
if !isreal(A.data[i,j])
return false
end
end
end
end
return true
end

ishermitian(A::Hermitian) = true
ishermitian(A::Symmetric{<:Real}) = true
ishermitian(A::Symmetric{<:Complex}) = isreal(A.data)
ishermitian(A::Symmetric{<:Complex}) = isreal(A)
issymmetric(A::Hermitian{<:Real}) = true
issymmetric(A::Hermitian{<:Complex}) = isreal(A.data)
issymmetric(A::Hermitian{<:Complex}) = isreal(A)
issymmetric(A::Symmetric) = true
transpose(A::Symmetric) = A
ctranspose(A::Symmetric{<:Real}) = A
Expand Down
11 changes: 11 additions & 0 deletions test/linalg/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,14 @@ let X = [1 -1; -1 1]
@test_throws ArgumentError Hermitian(X) + 2im*I
@test_throws ArgumentError Hermitian(X) - 2im*I
end

@testset "Issue #21981" begin
B = complex(rand(4,4))
B[4,1] += 1im;
@test ishermitian(Symmetric(B, :U))
@test issymmetric(Hermitian(B, :U))
B[4,1] = real(B[4,1])
B[1,4] += 1im
@test ishermitian(Symmetric(B, :L))
@test issymmetric(Hermitian(B, :L))
end