Skip to content

Commit edca559

Browse files
committed
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 e6e7449 commit edca559

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
@@ -901,8 +901,12 @@ julia> log(A)
901901
"""
902902
function log(A::AbstractMatrix)
903903
# If possible, use diagonalization
904-
if isdiag(A)
905-
return applydiagonal(log, A)
904+
if isdiag(A) && eltype(A) <: Union{Real,Complex}
905+
if eltype(A) <: Real && all(>=(0), diagview(A))
906+
return applydiagonal(log, A)
907+
else
908+
return applydiagonal(logcomplex, A)
909+
end
906910
elseif ishermitian(A)
907911
logHermA = log(Hermitian(A))
908912
return ishermitian(logHermA) ? copytri!(parent(logHermA), 'U', true) : parent(logHermA)

test/dense.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,4 +1384,15 @@ end
13841384
@test tril(GenericArray(M),-1) == res'
13851385
end
13861386

1387+
@testset "log for diagonal" begin
1388+
D = diagm([-2.0, 2.0])
1389+
@test log(D) log(UpperTriangular(D))
1390+
D = diagm([-2.0, 0.0])
1391+
@test log(D) log(UpperTriangular(D))
1392+
D = diagm([2.0, 2.0])
1393+
@test log(D) log(UpperTriangular(D))
1394+
D = diagm([2.0, 2.0*im])
1395+
@test log(D) log(UpperTriangular(D))
1396+
end
1397+
13871398
end # module TestDense

0 commit comments

Comments
 (0)