Skip to content

Commit 91ef00e

Browse files
authored
log for dense diagonal matrix with negative elements (#1352)
Fixes a regression introduced in v1.12, where we have a special branch for diagonal matrices to improve performance. The issue is that log for `Diagonal` matrices acts elementwise, which, for real matrices, requires the diagonal elements to be greater than zero. This PR adds an extra branch to check this.
1 parent 335a1ee commit 91ef00e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/dense.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,12 @@ julia> log(A)
917917
"""
918918
function log(A::AbstractMatrix)
919919
# If possible, use diagonalization
920-
if isdiag(A)
921-
return applydiagonal(log, A)
920+
if isdiag(A) && eltype(A) <: Union{Real,Complex}
921+
if eltype(A) <: Real && all(>=(0), diagview(A))
922+
return applydiagonal(log, A)
923+
else
924+
return applydiagonal(logcomplex, A)
925+
end
922926
elseif ishermitian(A)
923927
logHermA = log(Hermitian(A))
924928
PH = parent(logHermA)

test/dense.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,4 +1418,15 @@ end
14181418
@test tril(GenericArray(M),-1) == res'
14191419
end
14201420

1421+
@testset "log for diagonal" begin
1422+
D = diagm([-2.0, 2.0])
1423+
@test log(D) log(UpperTriangular(D))
1424+
D = diagm([-2.0, 0.0])
1425+
@test log(D) log(UpperTriangular(D))
1426+
D = diagm([2.0, 2.0])
1427+
@test log(D) log(UpperTriangular(D))
1428+
D = diagm([2.0, 2.0*im])
1429+
@test log(D) log(UpperTriangular(D))
1430+
end
1431+
14211432
end # module TestDense

0 commit comments

Comments
 (0)