Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant dimension wastes space and allows disagreement #178

Merged
merged 8 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PDMats"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.11.17"
version = "0.11.18"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
10 changes: 7 additions & 3 deletions src/pdiagmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
Positive definite diagonal matrix.
"""
struct PDiagMat{T<:Real,V<:AbstractVector{T}} <: AbstractPDMat{T}
dim::Int
diag::V
devmotion marked this conversation as resolved.
Show resolved Hide resolved
end

PDiagMat(v::AbstractVector{<:Real}) = PDiagMat{eltype(v),typeof(v)}(length(v), v)
function Base.getproperty(a::PDiagMat, s::Symbol)
s === :dim && return length(getfield(a, :diag))
devmotion marked this conversation as resolved.
Show resolved Hide resolved
return getfield(a, s)
end
Base.propertynames(::PDiagMat) = (:diag, :dim)

AbstractPDMat(A::Diagonal{<:Real}) = PDiagMat(A.diag)
AbstractPDMat(A::Symmetric{<:Real,<:Diagonal{<:Real}}) = PDiagMat(A.data.diag)
Expand Down Expand Up @@ -58,7 +61,8 @@ function \(a::PDiagMat, x::AbstractVecOrMat)
end
function /(x::AbstractVecOrMat, a::PDiagMat)
@check_argdims a.dim == size(x, 2)
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
(VERSION >= v"1.9" && x isa AbstractVector) && return x ./ a.diag
chriselrod marked this conversation as resolved.
Show resolved Hide resolved
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra on old Julia
return reshape(x, Val(2)) ./ permutedims(a.diag) # = (a' \ x')'
end
Base.kron(A::PDiagMat, B::PDiagMat) = PDiagMat(vec(permutedims(A.diag) .* B.diag))
Expand Down
31 changes: 24 additions & 7 deletions src/pdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@
Full positive definite matrix together with a Cholesky factorization object.
"""
struct PDMat{T<:Real,S<:AbstractMatrix} <: AbstractPDMat{T}
dim::Int
mat::S
chol::Cholesky{T,S}

PDMat{T,S}(d::Int,m::AbstractMatrix{T},c::Cholesky{T,S}) where {T,S} = new{T,S}(d,m,c)
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}
chriselrod marked this conversation as resolved.
Show resolved Hide resolved
LinearAlgebra.checksquare(m) == d || throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
chriselrod marked this conversation as resolved.
Show resolved Hide resolved
new{T,S}(m,c)
end
end

function PDMat(mat::AbstractMatrix,chol::Cholesky{T,S}) where {T,S}
d = size(mat, 1)
d = LinearAlgebra.checksquare(mat)
size(chol, 1) == d ||
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
PDMat{T,S}(d, convert(S, mat), chol)
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)
devmotion marked this conversation as resolved.
Show resolved Hide resolved
return getfield(a, s)
end
Base.propertynames(::PDMat) = (:mat, :chol, :dim)

AbstractPDMat(A::Cholesky) = PDMat(A)

### Conversion
Expand Down Expand Up @@ -49,9 +58,17 @@ end
*(a::PDMat, x::AbstractVector) = a.mat * x
*(a::PDMat, x::AbstractMatrix) = a.mat * x
\(a::PDMat, x::AbstractVecOrMat) = a.chol \ x
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
/(x::AbstractVecOrMat, a::PDMat) = reshape(x, Val(2)) / a.chol

function /(x::AbstractVecOrMat, a::PDMat)
if VERSION >= v"1.9" && x isa AbstractVector
chriselrod marked this conversation as resolved.
Show resolved Hide resolved
# either size(x) == 1, or we error
if length(x) != 1 || size(a) != (1,1)
throw(DimensionMismatch("size of A is $(size(a)), size of B is ($(length(x)), 1)"))
end
return x ./ a[1]
end
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
reshape(x, Val(2)) / a.chol
end
### Algebra

Base.inv(a::PDMat) = PDMat(inv(a.chol))
Expand Down
20 changes: 15 additions & 5 deletions src/pdsparsemat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,34 @@
Sparse positive definite matrix together with a Cholesky factorization object.
"""
struct PDSparseMat{T<:Real,S<:AbstractSparseMatrix} <: AbstractPDMat{T}
dim::Int
mat::S
chol::CholTypeSparse

PDSparseMat{T,S}(d::Int,m::AbstractSparseMatrix{T},c::CholTypeSparse) where {T,S} =
new{T,S}(d,m,c) #add {T} to CholTypeSparse argument once #14076 is implemented
function PDSparseMat{T,S}(d::Int,m::AbstractSparseMatrix{T},c::CholTypeSparse) where {T,S}
devmotion marked this conversation as resolved.
Show resolved Hide resolved
LinearAlgebra.checksquare(m) == d ||
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
devmotion marked this conversation as resolved.
Show resolved Hide resolved
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} =
new{T,S}(m,c) #add {T} to CholTypeSparse argument once #14076 is implemented
end

function PDSparseMat(mat::AbstractSparseMatrix,chol::CholTypeSparse)
d = size(mat, 1)
d = LinearAlgebra.checksquare(mat)
size(chol, 1) == d ||
throw(DimensionMismatch("Dimensions of mat and chol are inconsistent."))
PDSparseMat{eltype(mat),typeof(mat)}(d, mat, chol)
PDSparseMat{eltype(mat),typeof(mat)}(mat, chol)
end

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)
chriselrod marked this conversation as resolved.
Show resolved Hide resolved
return getfield(a, s)
end
Base.propertynames(::PDSparseMat) = (:mat, :chol, :dim)

AbstractPDMat(A::SparseMatrixCSC) = PDSparseMat(A)
AbstractPDMat(A::CholTypeSparse) = PDSparseMat(A)

Expand Down
1 change: 1 addition & 0 deletions src/scalmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function \(a::ScalMat, x::AbstractVecOrMat)
end
function /(x::AbstractVecOrMat, a::ScalMat)
@check_argdims a.dim == size(x, 2)
VERSION >= v"1.9" && return x .* inv(a.value)
devmotion marked this conversation as resolved.
Show resolved Hide resolved
# return matrix for 1-element vectors `x`, consistent with LinearAlgebra
return reshape(x, Val(2)) / a.value
end
Expand Down
11 changes: 6 additions & 5 deletions test/pdmtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,18 @@ using Test
A = rand(1, 1)
x = randn(1)
y = x / A
@assert x / A isa Matrix{Float64}
@assert size(y) == (1, 1)

T = VERSION >= v"1.9" ? Vector{Float64} : Matrix{Float64}
@assert x / A isa T
@assert size(y) == (VERSION >= v"1.9" ? (1,) : (1, 1))

for M in (PDiagMat(vec(A)), ScalMat(1, first(A)))
@test x / M isa Matrix{Float64}
@test x / M isa T
@test x / M ≈ y
end

# requires https://github.com/JuliaLang/julia/pull/32594
if VERSION >= v"1.3.0-DEV.562"
@test x / PDMat(A) isa Matrix{Float64}
@test x / PDMat(A) isa T
@test x / PDMat(A) ≈ y
end

Expand Down
Loading