Skip to content

Commit

Permalink
Propagate type-constraints
Browse files Browse the repository at this point in the history
I was hoping this solves the allocations on 1.0
  • Loading branch information
timholy committed Dec 11, 2024
1 parent 42e00c3 commit a5c0ce8
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/pdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct PDMat{T<:Real,S<:AbstractMatrix{T}} <: AbstractPDMat{T}
mat::S
chol::Cholesky{T,S}

function PDMat{T,S}(m::AbstractMatrix, c::Cholesky) where {T,S}
function PDMat{T,S}(m::AbstractMatrix, c::Cholesky) where {T<:Real,S<:AbstractMatrix{T}}
d = LinearAlgebra.checksquare(m)
if size(c, 1) != d
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
Expand All @@ -15,36 +15,36 @@ struct PDMat{T<:Real,S<:AbstractMatrix{T}} <: AbstractPDMat{T}
return new{T,S}(m,c)
end
end
function PDMat{T}(m::AbstractMatrix, c::Cholesky) where T
function PDMat{T}(m::AbstractMatrix, c::Cholesky) where T<:Real
c = convert(Cholesky{T}, c)
return PDMat{T,typeof(c.factors)}(m, c)
end
PDMat(mat::AbstractMatrix,chol::Cholesky{T,S}) where {T,S} = PDMat{T,S}(mat, chol)
PDMat(mat::AbstractMatrix,chol::Cholesky{T,S}) where {T<:Real,S<:AbstractMatrix{T}} = PDMat{T,S}(mat, chol)

# Construction from another PDMat
PDMat{T,S}(pdm::PDMat{T,S}) where {T,S} = pdm # since PDMat doesn't support `setindex!` it's not mutable (xref https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Mutable-collections)
PDMat{T,S}(pdm::PDMat) where {T,S} = PDMat{T,S}(pdm.mat, pdm.chol)
PDMat{T}(pdm::PDMat{T}) where T = pdm
PDMat{T}(pdm::PDMat) where T = PDMat{T}(pdm.mat, pdm.chol)
PDMat{T,S}(pdm::PDMat{T,S}) where {T<:Real,S<:AbstractMatrix{T}} = pdm # since PDMat doesn't support `setindex!` it's not mutable (xref https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Mutable-collections)
PDMat{T,S}(pdm::PDMat) where {T<:Real,S<:AbstractMatrix{T}} = PDMat{T,S}(pdm.mat, pdm.chol)
PDMat{T}(pdm::PDMat{T}) where T<:Real = pdm
PDMat{T}(pdm::PDMat) where T<:Real = PDMat{T}(pdm.mat, pdm.chol)
PDMat(pdm::PDMat) = pdm

# Construction from an AbstractMatrix
function PDMat{T,S}(mat::AbstractMatrix) where {T,S}
function PDMat{T,S}(mat::AbstractMatrix) where {T<:Real,S<:AbstractMatrix{T}}
mat = convert(S, mat)
return PDMat{T,S}(mat, cholesky(mat))
end
function PDMat{T}(mat::AbstractMatrix) where T
function PDMat{T}(mat::AbstractMatrix) where T<:Real
mat = convert(AbstractMatrix{T}, mat)
return PDMat{T}(mat, cholesky(mat))
end
PDMat(mat::AbstractMatrix) = PDMat(mat, cholesky(mat))

# Construction from a Cholesky factorization
function PDMat{T,S}(c::Cholesky) where {T,S}
function PDMat{T,S}(c::Cholesky) where {T<:Real,S<:AbstractMatrix{T}}
c = convert(Cholesky{T,S}, c)
return PDMat{T,S}(AbstractMatrix(c), c)
end
function PDMat{T}(c::Cholesky) where T
function PDMat{T}(c::Cholesky) where T<:Real
c = convert(Cholesky{T}, c)
return PDMat{T}(AbstractMatrix(c), c)
end
Expand Down

0 comments on commit a5c0ce8

Please sign in to comment.