Skip to content

Commit

Permalink
A few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chriselrod committed Sep 21, 2023
1 parent 1a65afe commit 801eb29
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/pdiagmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ Positive definite diagonal matrix.
struct PDiagMat{T<:Real,V<:AbstractVector{T}} <: AbstractPDMat{T}
diag::V
end
function PDiagMat(dim::Int, diag::V) where {T,V<:AbstractVector{T}}
if length(diag) != dim
throw(DimensionMismatch("Dimensions of diag and dim are inconsistent."))
end
return PDiagMat{T,V}(diag)
end

function Base.getproperty(a::PDiagMat, s::Symbol)
s === :dim && return length(getfield(a, :diag))
if s === :dim
return length(getfield(a, :diag))
end
return getfield(a, s)
end
Base.propertynames(::PDiagMat) = (:diag, :dim)
Expand Down
11 changes: 8 additions & 3 deletions src/pdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ struct PDMat{T<:Real,S<:AbstractMatrix} <: AbstractPDMat{T}

PDMat{T,S}(m::AbstractMatrix{T},c::Cholesky{T,S}) where {T,S} = new{T,S}(m,c)
function PDMat{T,S}(d::Int, m::AbstractMatrix{T},c::Cholesky{T,S}) where {T,S}
LinearAlgebra.checksquare(m) == d || throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
if LinearAlgebra.checksquare(m) != d || d != size(c,1)
throw(DimensionMismatch("dim `d`=$d, size(m) = $(size(m)), size(c) = $(size(c))"))
end
new{T,S}(m,c)
end
end

function PDMat(mat::AbstractMatrix,chol::Cholesky{T,S}) where {T,S}
d = LinearAlgebra.checksquare(mat)
size(chol, 1) == d ||
if size(chol, 1) != d
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
end
PDMat{T,S}(convert(S, mat), chol)
end

PDMat(mat::AbstractMatrix) = PDMat(mat, cholesky(mat))
PDMat(fac::Cholesky) = PDMat(AbstractMatrix(fac), fac)

function Base.getproperty(a::PDMat, s::Symbol)
s === :dim && return size(getfield(a, :mat), 1)
if s === :dim
return size(getfield(a, :mat), 1)
end
return getfield(a, s)
end
Base.propertynames(::PDMat) = (:mat, :chol, :dim)
Expand Down
9 changes: 6 additions & 3 deletions src/pdsparsemat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ struct PDSparseMat{T<:Real,S<:AbstractSparseMatrix} <: AbstractPDMat{T}
chol::CholTypeSparse

function PDSparseMat{T,S}(d::Int,m::AbstractSparseMatrix{T},c::CholTypeSparse) where {T,S}
LinearAlgebra.checksquare(m) == d ||
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
if LinearAlgebra.checksquare(m) != d || d != size(c,1)
throw(DimensionMismatch("dim `d`=$d, size(m) = $(size(m)), size(c) = $(size(c))"))
end
new{T,S}(m,c) #add {T} to CholTypeSparse argument once #14076 is implemented
end
PDSparseMat{T,S}(m::AbstractSparseMatrix{T},c::CholTypeSparse) where {T,S} =
Expand All @@ -25,7 +26,9 @@ PDSparseMat(mat::SparseMatrixCSC) = PDSparseMat(mat, cholesky(mat))
PDSparseMat(fac::CholTypeSparse) = PDSparseMat(sparse(fac), fac)

function Base.getproperty(a::PDSparseMat, s::Symbol)
s == :dim && return size(getfield(a, :mat), 1)
if s === :dim
return size(getfield(a, :mat), 1)
end
return getfield(a, s)
end
Base.propertynames(::PDSparseMat) = (:mat, :chol, :dim)
Expand Down

0 comments on commit 801eb29

Please sign in to comment.