Skip to content

Commit 8c904d1

Browse files
st--devmotion
andauthored
Fix logdet(PDiagMat(zeros(0))) (#139)
* fix #138 * Update src/pdiagmat.jl Co-authored-by: David Widmann <[email protected]> * make _randPDMat more reliable Co-authored-by: David Widmann <[email protected]>
1 parent 213c37a commit 8c904d1

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/pdiagmat.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ Base.kron(A::PDiagMat, B::PDiagMat) = PDiagMat( vcat([A.diag[i] * B.diag for i i
5858
### Algebra
5959

6060
Base.inv(a::PDiagMat) = PDiagMat(a.inv_diag, a.diag)
61-
LinearAlgebra.logdet(a::PDiagMat) = sum(log, a.diag)
61+
function LinearAlgebra.logdet(a::PDiagMat)
62+
diag = a.diag
63+
return isempty(diag) ? zero(log(zero(eltype(diag)))) : sum(log, diag)
64+
end
6265
LinearAlgebra.eigmax(a::PDiagMat) = maximum(a.diag)
6366
LinearAlgebra.eigmin(a::PDiagMat) = minimum(a.diag)
6467

test/kron.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using PDMats
22
using Test
3+
using LinearAlgebra: LinearAlgebra
34

4-
_randPDMat(T, n) = (X = randn(T, n, n); PDMat(X * X'))
5+
_randPDMat(T, n) = (X = randn(T, n, n); PDMat(X * X' + LinearAlgebra.I))
56
_randPDiagMat(T, n) = PDiagMat(rand(T, n))
67
_randScalMat(T, n) = ScalMat(n, rand(T))
78
function _pd_compare(A::AbstractPDMat, B::AbstractPDMat)

test/pdmtypes.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ x = one(Float32); d = 4
3838
s = SparseMatrixCSC{Float32}(I, 2, 2)
3939
@test convert(PDSparseMat{Float64}, PDSparseMat(s)).mat == PDSparseMat(convert(SparseMatrixCSC{Float64}, s)).mat
4040

41+
Z = zeros(0, 0)
42+
test_pdmat(PDMat(Z), Z; t_eig=false)
43+
test_pdmat(PDiagMat(diag(Z)), Z; t_eig=false)
44+
4145
# no-op conversion with correct eltype (#101)
4246
X = PDMat((Y->Y'Y)(randn(Float32, 4, 4)))
4347
@test convert(AbstractArray{Float32}, X) === X
@@ -51,4 +55,4 @@ a = PDMat([1 0.5; 0.5 1])
5155
@inferred unwhiten(a, ones(2))
5256

5357
# convert Matrix type to the same Cholesky type (#117)
54-
@test PDMat([1 0; 0 1]) == [1.0 0.0; 0.0 1.0]
58+
@test PDMat([1 0; 0 1]) == [1.0 0.0; 0.0 1.0]

0 commit comments

Comments
 (0)