Skip to content

Commit

Permalink
Merge pull request #34 from vandenman/mimic_constructor_PDMats
Browse files Browse the repository at this point in the history
Mimic constructor from PDMats.jl so that PSDMat(<:Matrix{<:Integer}) does not throw
  • Loading branch information
oxinabox authored Oct 23, 2023
2 parents 918e216 + 4fee327 commit 4fc4a7e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/psd_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ struct PSDMat{T<:Real,S<:AbstractMatrix} <: AbstractPDMat{T}
PSDMat{T,S}(m::AbstractMatrix{T},c::CholType{T,S}) where {T,S} = new{T, S}(m, c)
end

function PSDMat(mat::AbstractMatrix, chol::CholType)
function PSDMat(mat::AbstractMatrix, chol::CholType{T,S}) where {T,S}
d = LinearAlgebra.checksquare(mat)
size(chol, 1) == d ||
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
PSDMat{eltype(mat),typeof(mat)}(mat, chol)
PSDMat{T, S}(d, convert(S, mat), chol)
end

PSDMat(mat::Matrix) = PSDMat(mat, cholesky(mat, VERSION >= v"1.8.0-rc1" ? RowMaximum() : Val(true); check=false))
Expand Down
14 changes: 14 additions & 0 deletions test/psd_mat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ end
@test whiten!( similar(x), pd, x) whiten!( similar(x), psd, x)
@test unwhiten!(similar(x), pd, x) unwhiten!(similar(x), psd, x)
end

@testset "Constructing from Matrix{<:Integer} works" begin

m = [
1 0
0 1
]

for type in (UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt, Int)
@test PSDMat{Float64, Matrix{Float64}} == typeof(PSDMat(type.(m)))
end

end

end

@testset "Degenerate MvNormal" begin
Expand Down

0 comments on commit 4fc4a7e

Please sign in to comment.