From 3546c6326a400def1f7d9d7afabf483999856cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 26 Jun 2019 08:31:35 +0100 Subject: [PATCH] Fix element type of -Symmetric(A) for e.g. Bool or JuMP.VariableRef, fixes #32374 (#32375) (cherry picked from commit d95841934a66530ab54229aea2ed5d80dbc8c070) --- stdlib/LinearAlgebra/src/symmetric.jl | 4 ++-- stdlib/LinearAlgebra/test/symmetric.jl | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stdlib/LinearAlgebra/src/symmetric.jl b/stdlib/LinearAlgebra/src/symmetric.jl index e9317bc207e7c..995a2e42dcdb5 100644 --- a/stdlib/LinearAlgebra/src/symmetric.jl +++ b/stdlib/LinearAlgebra/src/symmetric.jl @@ -414,8 +414,8 @@ function triu(A::Symmetric, k::Integer=0) end end -(-)(A::Symmetric{Tv,S}) where {Tv,S} = Symmetric{Tv,S}(-A.data, A.uplo) -(-)(A::Hermitian{Tv,S}) where {Tv,S} = Hermitian{Tv,S}(-A.data, A.uplo) +(-)(A::Symmetric) = Symmetric(-A.data, sym_uplo(A.uplo)) +(-)(A::Hermitian) = Hermitian(-A.data, sym_uplo(A.uplo)) ## Addition/subtraction for f in (:+, :-) diff --git a/stdlib/LinearAlgebra/test/symmetric.jl b/stdlib/LinearAlgebra/test/symmetric.jl index 6d9c95a1a3ff2..5a6d38cdb2c36 100644 --- a/stdlib/LinearAlgebra/test/symmetric.jl +++ b/stdlib/LinearAlgebra/test/symmetric.jl @@ -88,6 +88,8 @@ end @testset "Unary minus for Symmetric/Hermitian matrices" begin @test (-Symmetric(asym))::typeof(Symmetric(asym)) == -asym @test (-Hermitian(aherm))::typeof(Hermitian(aherm)) == -aherm + @test (-Symmetric([true true; false false]))::Symmetric{Int,Matrix{Int}} == [-1 -1; -1 0] + @test (-Hermitian([true false; true false]))::Hermitian{Int,Matrix{Int}} == [-1 0; 0 0] end @testset "Addition and subtraction for Symmetric/Hermitian matrices" begin