File tree Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Expand file tree Collapse file tree 2 files changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -972,7 +972,8 @@ sqrt(::AbstractMatrix)
972972function sqrt (A:: AbstractMatrix{T} ) where {T<: Union{Real,Complex} }
973973 if checksquare (A) == 0
974974 return copy (float (A))
975- elseif isdiag (A)
975+ elseif isdiag (A) && (T <: Complex || all (x -> x ≥ zero (x), diagview (A)))
976+ # Real Diagonal sqrt requires each diagonal element to be positive
976977 return applydiagonal (sqrt, A)
977978 elseif ishermitian (A)
978979 sqrtHermA = sqrt (Hermitian (A))
Original file line number Diff line number Diff line change 984984@testset " sqrt for diagonal" begin
985985 A = diagm (0 => [1 , 2 , 3 ])
986986 @test sqrt (A)^ 2 ≈ A
987+
988+ A = diagm (0 => [1.0 , - 1.0 ])
989+ @test sqrt (A) == diagm (0 => [1.0 , 1.0im ])
990+ @test sqrt (A)^ 2 ≈ A
991+ B = im* A
992+ @test sqrt (B)^ 2 ≈ B
987993end
988994
989995@testset " issue #40141" begin
You can’t perform that action at this time.
0 commit comments