-
Notifications
You must be signed in to change notification settings - Fork 45
Unify PDMat and PDSparseMat + move SparseArrays support to an extension
#188
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
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.
Open
Changes from 1 commit
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 hidden or 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 |
|---|---|---|
|
|
@@ -12,8 +12,7 @@ jobs: | |
| fail-fast: false | ||
| matrix: | ||
| version: | ||
| - '1.0' | ||
| - '1.6' | ||
| - '1.9' | ||
| - '1' | ||
| - nightly | ||
| os: | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,19 +1,24 @@ | ||
| name = "PDMats" | ||
| uuid = "90014a1f-27ba-587c-ab20-58faa44d9150" | ||
| version = "0.11.22" | ||
| version = "0.12.0" | ||
|
|
||
| [deps] | ||
| LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
| SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
| SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9" | ||
|
|
||
| [compat] | ||
| julia = "1" | ||
| julia = "1.9" | ||
|
|
||
| [extensions] | ||
| PDMatsSparseArraysExt = "SparseArrays" | ||
|
|
||
| [extras] | ||
| BandedMatrices = "aae01518-5342-5314-be14-df237901396f" | ||
| SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
| StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
| Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
|
||
| [targets] | ||
| test = ["BandedMatrices", "StaticArrays", "Test"] | ||
| test = ["BandedMatrices", "SparseArrays", "StaticArrays", "Test"] | ||
|
|
||
| [weakdeps] | ||
| SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" |
This file contains hidden or 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 hidden or 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,15 @@ | ||
| module PDMatsSparseArraysExt | ||
|
|
||
| using PDMats | ||
| using SparseArrays | ||
|
|
||
| using PDMats.LinearAlgebra | ||
|
|
||
| const HAVE_CHOLMOD = isdefined(SparseArrays, :CHOLMOD) | ||
|
|
||
| if HAVE_CHOLMOD | ||
| include("chol.jl") | ||
| include("pdsparsemat.jl") | ||
| end | ||
|
|
||
| end # module | ||
This file contains hidden or 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,5 @@ | ||
| const CholTypeSparse{T} = SparseArrays.CHOLMOD.Factor{T} | ||
|
|
||
| # Take into account pivoting! | ||
| PDMats.chol_lower(cf::CholTypeSparse) = cf.PtL | ||
| PDMats.chol_upper(cf::CholTypeSparse) = cf.UP |
This file contains hidden or 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,109 @@ | ||
| """ | ||
| Sparse positive definite matrix together with a Cholesky factorization object. | ||
| """ | ||
| const PDSparseMat{T<:Real,S<:AbstractSparseMatrix,C<:CholTypeSparse} = PDMat{T,S,C} | ||
|
|
||
| function PDMats.PDMat(mat::AbstractSparseMatrix, chol::CholTypeSparse) | ||
| d = size(mat, 1) | ||
| size(chol, 1) == d || | ||
| throw(DimensionMismatch("Dimensions of mat and chol are inconsistent.")) | ||
| PDMat{eltype(mat),typeof(mat),typeof(chol)}(d, mat, chol) | ||
| end | ||
|
|
||
| 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.chol)}(a.dim, mat, a.chol) | ||
| end | ||
|
|
||
| ### Arithmetics | ||
|
|
||
| Base.:\(a::PDSparseMat{T}, x::AbstractVecOrMat{T}) where {T<:Real} = convert(Array{T},a.chol \ 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.chol ) | ||
|
|
||
| ### Algebra | ||
|
|
||
| Base.inv(a::PDSparseMat{T}) where {T<:Real} = PDMat(inv(a.mat)) | ||
| LinearAlgebra.det(a::PDSparseMat) = det(a.chol) | ||
| LinearAlgebra.logdet(a::PDSparseMat) = logdet(a.chol) | ||
| LinearAlgebra.sqrt(A::PDSparseMat) = PDMat(sqrt(Hermitian(Matrix(A)))) | ||
|
|
||
| ### whiten and unwhiten | ||
|
|
||
| function PDMats.whiten!(r::AbstractVecOrMat, a::PDSparseMat, x::AbstractVecOrMat) | ||
| # Can't use `ldiv!` due to missing support in SparseArrays | ||
| return copyto!(r, PDMats.chol_lower(a.chol) \ x) | ||
| end | ||
|
|
||
| function PDMats.unwhiten!(r::AbstractVecOrMat, a::PDSparseMat, x::AbstractVecOrMat) | ||
| # `*` is not defined for `PtL` factor components, | ||
| # so we can't use `chol_lower(a.chol) * x` | ||
| C = a.chol | ||
| PtL = sparse(C.L)[C.p, :] | ||
| # Can't use `lmul!` due to missing support in SparseArrays | ||
| return copyto!(r, PtL * x) | ||
| end | ||
|
|
||
|
|
||
| ### quadratic forms | ||
|
|
||
| PDMats.quad(a::PDSparseMat, x::AbstractVector) = dot(x, a * x) | ||
| PDMats.invquad(a::PDSparseMat, x::AbstractVector) = dot(x, a \ x) | ||
|
|
||
| function PDMats.quad!(r::AbstractArray, a::PDSparseMat, x::AbstractMatrix) | ||
| PDMats.@check_argdims eachindex(r) == axes(x, 2) | ||
| for i in axes(x, 2) | ||
| r[i] = quad(a, x[:,i]) | ||
| end | ||
| return r | ||
| end | ||
|
|
||
| function PDMats.invquad!(r::AbstractArray, a::PDSparseMat, x::AbstractMatrix) | ||
| PDMats.@check_argdims eachindex(r) == axes(x, 2) | ||
| for i in axes(x, 2) | ||
| r[i] = invquad(a, x[:,i]) | ||
| end | ||
| return r | ||
| end | ||
|
|
||
|
|
||
| ### tri products | ||
|
|
||
| function PDMats.X_A_Xt(a::PDSparseMat, x::AbstractMatrix) | ||
| # `*` is not defined for `PtL` factor components, | ||
| # so we can't use `x * chol_lower(a.chol)` | ||
| C = a.chol | ||
| PtL = sparse(C.L)[C.p, :] | ||
| z = x * PtL | ||
| z * transpose(z) | ||
| end | ||
|
|
||
|
|
||
| function PDMats.Xt_A_X(a::PDSparseMat, x::AbstractMatrix) | ||
| # `*` is not defined for `UP` factor components, | ||
| # so we can't use `chol_upper(a.chol) * x` | ||
| # Moreover, `sparse` is only defined for `L` factor components | ||
| C = a.chol | ||
| UP = transpose(sparse(C.L))[:, C.p] | ||
| z = UP * x | ||
| transpose(z) * z | ||
| end | ||
|
|
||
|
|
||
| function PDMats.X_invA_Xt(a::PDSparseMat, x::AbstractMatrix) | ||
| z = a.chol \ collect(transpose(x)) | ||
| x * z | ||
| end | ||
|
|
||
| function PDMats.Xt_invA_X(a::PDSparseMat, x::AbstractMatrix) | ||
| z = a.chol \ x | ||
| transpose(x) * z | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.