-
Notifications
You must be signed in to change notification settings - Fork 43
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
Unify PDMat
and PDSparseMat
+ move SparseArrays support to an extension
#188
Open
devmotion
wants to merge
18
commits into
master
Choose a base branch
from
dw/unify_pdmat_pdsparsemat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+333
−372
Open
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5ab9584
Unify `PDMat` and `PDSparseMat`
devmotion e4ed4ed
Replace tab with whitespace
devmotion 91f6d14
Try to add back support for Julia < 1.9
devmotion e817ceb
Fix support on older Julia versions
devmotion dee9225
Merge branch 'master' into dw/unify_pdmat_pdsparsemat
devmotion dd947d6
Fix test errors on Julia 1.0
devmotion 2dc005d
Update README.md
devmotion deb575d
Improve type parameter
devmotion c27aab2
Rename field `chol` to `fact`
devmotion 578e459
Merge branch 'master' into dw/unify_pdmat_pdsparsemat
devmotion 6ce4b77
Fix type parameter
devmotion bbedcb2
Fix variable conflicts with type parameters
devmotion dca9273
Clean up switch to `fact`
devmotion 33a80f3
Specialize `sqrt` for `PDSparseMat`
devmotion 60df321
Merge branch 'master' into dw/unify_pdmat_pdsparsemat
devmotion 6678dd3
Merge branch 'master' into dw/unify_pdmat_pdsparsemat
devmotion a0acc49
Add test from #207
devmotion 8c160df
Run `SymTridiagonal` tests only on Julia >= 1.8
devmotion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module PDMatsSparseArraysExt | ||
|
||
using PDMats | ||
using SparseArrays | ||
|
||
using PDMats.LinearAlgebra | ||
dkarrasch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if isdefined(Base, :get_extension) | ||
const HAVE_CHOLMOD = isdefined(SparseArrays, :CHOLMOD) | ||
else | ||
import SuiteSparse | ||
const HAVE_CHOLMOD = isdefined(SuiteSparse, :CHOLMOD) | ||
end | ||
|
||
# https://github.com/JuliaLang/julia/pull/29749 | ||
if VERSION < v"1.1.0-DEV.792" | ||
eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) | ||
end | ||
|
||
if HAVE_CHOLMOD | ||
include("chol.jl") | ||
include("pdsparsemat.jl") | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if isdefined(Base, :get_extension) | ||
const CholTypeSparse{T} = SparseArrays.CHOLMOD.Factor{T} | ||
else | ||
const CholTypeSparse{T} = SuiteSparse.CHOLMOD.Factor{T} | ||
end | ||
|
||
# Take into account pivoting! | ||
PDMats.chol_lower(cf::CholTypeSparse) = cf.PtL | ||
PDMats.chol_upper(cf::CholTypeSparse) = cf.UP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
""" | ||
Sparse positive definite matrix together with a Cholesky factorization object. | ||
""" | ||
const PDSparseMat{T<:Real,S<:AbstractSparseMatrix{T},C<:CholTypeSparse} = PDMat{T,S,C} | ||
|
||
function PDMats.PDMat(mat::AbstractSparseMatrix, chol::CholTypeSparse) | ||
PDMat{eltype(mat),typeof(mat),typeof(chol)}(mat, chol) | ||
end | ||
Base.@deprecate PDMat{T,S}(d::Int, m::AbstractSparseMatrix{T}, c::CholTypeSparse) where {T,S} PDSparseMat{T,S,typeof(c)}(m, c) | ||
|
||
PDMats.PDMat(mat::SparseMatrixCSC) = PDMat(mat, cholesky(mat)) | ||
PDMats.PDMat(fac::CholTypeSparse) = PDMat(sparse(fac), fac) | ||
|
||
PDMats.AbstractPDMat(A::CholTypeSparse) = PDMat(A) | ||
|
||
### Conversion | ||
function Base.convert(::Type{PDMat{T}}, a::PDSparseMat) where {T<:Real} | ||
# CholTypeSparse only supports Float64 and ComplexF64 type parameters! | ||
# So there is no point in recomputing `cholesky(mat)` and we just reuse | ||
# the existing Cholesky factorization | ||
mat = convert(AbstractMatrix{T}, a.mat) | ||
return PDMat{T,typeof(mat),typeof(a.fact)}(mat, a.fact) | ||
end | ||
|
||
### Arithmetics | ||
|
||
Base.:\(a::PDSparseMat{T}, x::AbstractVecOrMat{T}) where {T<:Real} = convert(Array{T},a.fact \ convert(Array{Float64},x)) #to avoid limitations in sparse factorization library CHOLMOD, see e.g., julia issue #14076 | ||
Base.:/(x::AbstractVecOrMat{T}, a::PDSparseMat{T}) where {T<:Real} = convert(Array{T},convert(Array{Float64},x) / a.fact ) | ||
|
||
### Algebra | ||
|
||
Base.inv(a::PDSparseMat) = PDMat(inv(a.mat)) | ||
LinearAlgebra.cholesky(a::PDSparseMat) = a.fact | ||
Base.sqrt(A::PDSparseMat) = PDMat(sqrt(Hermitian(Matrix(A)))) | ||
|
||
### whiten and unwhiten | ||
|
||
function PDMats.whiten!(r::AbstractVecOrMat, a::PDSparseMat, x::AbstractVecOrMat) | ||
PDMats.@check_argdims axes(r) == axes(x) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
# Can't use `ldiv!` due to missing support in SparseArrays | ||
return copyto!(r, PDMats.chol_lower(cholesky(a)) \ x) | ||
end | ||
|
||
function PDMats.unwhiten!(r::AbstractVecOrMat, a::PDSparseMat, x::AbstractVecOrMat) | ||
PDMats.@check_argdims axes(r) == axes(x) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
# `*` is not defined for `PtL` factor components, | ||
# so we can't use `chol_lower(cholesky(a)) * x` | ||
C = cholesky(a) | ||
PtL = sparse(C.L)[C.p, :] | ||
return copyto!(r, PtL * x) | ||
end | ||
|
||
function PDMats.whiten(a::PDSparseMat, x::AbstractVecOrMat) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
return PDMats.chol_lower(cholesky(a)) \ x | ||
end | ||
|
||
function PDMats.unwhiten(a::PDSparseMat, x::AbstractVecOrMat) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
# `*` is not defined for `PtL` factor components, | ||
# so we can't use `chol_lower(cholesky(a)) * x` | ||
C = cholesky(a) | ||
PtL = sparse(C.L)[C.p, :] | ||
return PtL * x | ||
end | ||
|
||
### quadratic forms | ||
|
||
function PDMats.quad(a::PDSparseMat, x::AbstractVecOrMat) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73 | ||
if VERSION < v"1.4.0-DEV.92" | ||
z = a.mat * x | ||
return x isa AbstractVector ? dot(x, z) : map(dot, eachcol(x), eachcol(z)) | ||
else | ||
return x isa AbstractVector ? dot(x, a.mat, x) : map(Base.Fix1(quad, a), eachcol(x)) | ||
end | ||
end | ||
|
||
function PDMats.quad!(r::AbstractArray, a::PDSparseMat, x::AbstractMatrix) | ||
PDMats.@check_argdims eachindex(r) == axes(x, 2) | ||
@inbounds for i in axes(x, 2) | ||
xi = view(x, :, i) | ||
# https://github.com/JuliaLang/julia/commit/2425ae760fb5151c5c7dd0554e87c5fc9e24de73 | ||
if VERSION < v"1.4.0-DEV.92" | ||
# Can't use `lmul!` with buffer due to missing support in SparseArrays | ||
r[i] = dot(xi, a.mat * xi) | ||
else | ||
r[i] = dot(xi, a.mat, xi) | ||
end | ||
end | ||
return r | ||
end | ||
|
||
function PDMats.invquad(a::PDSparseMat, x::AbstractVecOrMat) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
z = cholesky(a) \ x | ||
return x isa AbstractVector ? dot(x, z) : map(dot, eachcol(x), eachcol(z)) | ||
end | ||
|
||
function PDMats.invquad!(r::AbstractArray, a::PDSparseMat, x::AbstractMatrix) | ||
PDMats.@check_argdims eachindex(r) == axes(x, 2) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
# Can't use `ldiv!` with buffer due to missing support in SparseArrays | ||
C = cholesky(a) | ||
@inbounds for i in axes(x, 2) | ||
xi = view(x, :, i) | ||
r[i] = dot(xi, C \ xi) | ||
end | ||
return r | ||
end | ||
|
||
|
||
### tri products | ||
|
||
function PDMats.X_A_Xt(a::PDSparseMat, x::AbstractMatrix{<:Real}) | ||
PDMats.@check_argdims a.dim == size(x, 2) | ||
z = a.mat * transpose(x) | ||
return Symmetric(x * z) | ||
end | ||
|
||
|
||
function PDMats.Xt_A_X(a::PDSparseMat, x::AbstractMatrix{<:Real}) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
z = a.mat * x | ||
return Symmetric(transpose(x) * z) | ||
end | ||
|
||
|
||
function PDMats.X_invA_Xt(a::PDSparseMat, x::AbstractMatrix{<:Real}) | ||
PDMats.@check_argdims a.dim == size(x, 2) | ||
z = cholesky(a) \ collect(transpose(x)) | ||
return Symmetric(x * z) | ||
end | ||
|
||
function PDMats.Xt_invA_X(a::PDSparseMat, x::AbstractMatrix{<:Real}) | ||
PDMats.@check_argdims a.dim == size(x, 1) | ||
z = cholesky(a) \ x | ||
return Symmetric(transpose(x) * z) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One needs an inner constructor that checks
isposdef(fact)
before callingnew
. Otherwise I could calleven if
A
is not posdef.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently none of the other constructors enforces positive semi-definite matrices: #22
So ideally this should be addressed more generally, to ensure that such checks are performed by all
AbstractPDMat
types. But I wonder if it should be left for a separate PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. But it's not a hard change, we should just do it once other dust has settled. Given that the PDMat wrapper promises positive-definiteness, it is kinda important to enforce this, especially if we widen the type definition so that it can no longer be guaranteed from the input types alone.