From 40d5a4f118a973f7a9f7469ed63f2bef51e3fa16 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 6 Sep 2025 14:58:19 -0400 Subject: [PATCH 1/2] `isnan` check in `issymetric`/`ishermitian` for `Number`s --- src/generic.jl | 4 ++-- test/generic.jl | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/generic.jl b/src/generic.jl index 4b78ee90..9b9a4509 100644 --- a/src/generic.jl +++ b/src/generic.jl @@ -1350,7 +1350,7 @@ function issymmetric(A::AbstractMatrix) return true end -issymmetric(x::Number) = x == x +issymmetric(x::Number) = !isnan(x) """ ishermitian(A) -> Bool @@ -1389,7 +1389,7 @@ function ishermitian(A::AbstractMatrix) return true end -ishermitian(x::Number) = (x == conj(x)) +ishermitian(x::Number) = issymmetric(x) && isreal(x) # helper function equivalent to `iszero(v)`, but potentially without the fast exit feature # of `all` if this improves performance diff --git a/test/generic.jl b/test/generic.jl index 0570b8db..3b029d0c 100644 --- a/test/generic.jl +++ b/test/generic.jl @@ -943,4 +943,12 @@ end @test B == A2 end +@testset "issymmetric/ishermitian for Numbers" begin + fsym(x) = Val(issymmetric(x)) + @test @inferred(fsym(2)) isa Val{true} + @test @inferred(fsym(2im)) isa Val{true} + fherm(x) = Val(ishermitian(x)) + @test @inferred(fherm(2)) isa Val{true} +end + end # module TestGeneric From ca9a31c6a33e5b46b8c353c08142d59b75f558c7 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 20 Sep 2025 20:06:58 +0530 Subject: [PATCH 2/2] Dispatch on type in `issymmetric` --- src/generic.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generic.jl b/src/generic.jl index 9b9a4509..4547f918 100644 --- a/src/generic.jl +++ b/src/generic.jl @@ -1350,7 +1350,7 @@ function issymmetric(A::AbstractMatrix) return true end -issymmetric(x::Number) = !isnan(x) +issymmetric(x::Number) = true """ ishermitian(A) -> Bool @@ -1389,7 +1389,7 @@ function ishermitian(A::AbstractMatrix) return true end -ishermitian(x::Number) = issymmetric(x) && isreal(x) +ishermitian(x::Number) = isreal(x) # helper function equivalent to `iszero(v)`, but potentially without the fast exit feature # of `all` if this improves performance