Skip to content

Commit 20478c6

Browse files
Speed up logdet for diagonal and triangular matrices (#50950)
1 parent 2c5406b commit 20478c6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/generic.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,8 +1678,12 @@ julia> logabsdet(B)
16781678
(0.6931471805599453, 1.0)
16791679
```
16801680
"""
1681-
logabsdet(A::AbstractMatrix) = logabsdet(lu(A, check=false))
1682-
1681+
function logabsdet(A::AbstractMatrix)
1682+
if istriu(A) || istril(A)
1683+
return logabsdet(UpperTriangular(A))
1684+
end
1685+
return logabsdet(lu(A, check=false))
1686+
end
16831687
logabsdet(a::Number) = log(abs(a)), sign(a)
16841688

16851689
"""

test/generic.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ n = 5 # should be odd
7777
X = fill(x, 1, 1)
7878
@test logabsdet(x)[1] logabsdet(X)[1]
7979
@test logabsdet(x)[2] logabsdet(X)[2]
80+
# Diagonal, upper, and lower triangular matrices
81+
chksign(s1, s2) = if elty <: Real s1 == s2 else s1 s2 end
82+
D = Matrix(Diagonal(A))
83+
v, s = logabsdet(D)
84+
@test v log(abs(det(D))) && chksign(s, sign(det(D)))
85+
R = triu(A)
86+
v, s = logabsdet(R)
87+
@test v log(abs(det(R))) && chksign(s, sign(det(R)))
88+
L = tril(A)
89+
v, s = logabsdet(L)
90+
@test v log(abs(det(L))) && chksign(s, sign(det(L)))
8091
end
8192

8293
@testset "det with nonstandard Number type" begin

0 commit comments

Comments
 (0)