-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from invenia/gm/submat
Define submat for PDMat subtypes
- Loading branch information
Showing
6 changed files
with
116 additions
and
23 deletions.
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,16 @@ | ||
""" | ||
submat(A::AbstractPDMat, inds) | ||
Select a square submatrix of an `AbstractPDMat` specified by `inds`. | ||
Returns a `AbstractPDMat` of the same type. | ||
""" | ||
submat(A::PDMat, inds) = PDMat(A[inds, inds]) | ||
submat(A::PSDMat, inds) = PSDMat(A[inds, inds]) | ||
submat(A::PDiagMat, inds) = PDiagMat(A.diag[inds]) | ||
submat(A::PDSparseMat, inds) = PDSparseMat(sparse(A[inds, inds])) | ||
submat(A::WoodburyPDMat, inds) = WoodburyPDMat(A.A[inds, :], A.D, Diagonal(A.S.diag[inds])) | ||
function submat(A::ScalMat, inds) | ||
# inds should not be able to "access" non-existent indices | ||
checkbounds(Bool, A, inds) || throw(BoundsError(A, inds)) | ||
return ScalMat(length(inds), A.value) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
@testset "utils.jl" begin | ||
@testset "submat" begin | ||
|
||
@testset "PDMat" begin | ||
M = PDMat(TEST_MATRICES["Positive definite"]) | ||
M_dense = Matrix(M) | ||
|
||
subM = submat(M, [1, 3]) | ||
@test subM isa PDMat | ||
@test Matrix(subM) == M_dense[[1, 3], [1, 3]] | ||
end | ||
|
||
@testset "PDDiagMat" begin | ||
M = PDiagMat(diag(TEST_MATRICES["Positive definite"])) | ||
M_dense = Matrix(M) | ||
|
||
subM = submat(M, [1, 3]) | ||
@test subM isa PDiagMat | ||
@test Matrix(subM) == M_dense[[1, 3], [1, 3]] | ||
end | ||
|
||
@testset "PDSparseMat" begin | ||
M = PDSparseMat(sparse(TEST_MATRICES["Positive definite"])) | ||
M_dense = Matrix(M) | ||
|
||
subM = submat(M, [1, 3]) | ||
@test subM isa PDSparseMat | ||
@test Matrix(subM) == M_dense[[1, 3], [1, 3]] | ||
end | ||
|
||
@testset "ScalMat" begin | ||
M = ScalMat(3, 0.1) | ||
M_dense = Matrix(M) | ||
|
||
subM = submat(M, [1, 3]) | ||
@test subM isa ScalMat | ||
@test Matrix(subM) == M_dense[[1, 3], [1, 3]] | ||
@test_throws BoundsError submat(M, [1, 10]) | ||
end | ||
|
||
@testset "PSDMat" begin | ||
SM = TEST_MATRICES["Positive semi-definite"] | ||
pivoted = cholesky(SM, Val(true); check=false) | ||
M = PSDMat(SM, pivoted) | ||
M_dense = Matrix(M) | ||
|
||
subM = submat(M, [1, 3]) | ||
@test subM isa PSDMat | ||
@test Matrix(subM) == M_dense[[1, 3], [1, 3]] | ||
end | ||
|
||
@testset "WoodburyPDMat" begin | ||
A = randn(4, 2) | ||
D = Diagonal(randn(2).^2 .+ 1) | ||
S = Diagonal(randn(4).^2 .+ 1) | ||
x = randn(size(A, 1)) | ||
|
||
W = WoodburyPDMat(A, D, S) | ||
W_dense = PDMat(Symmetric(Matrix(W))) | ||
|
||
subW = submat(W, [1, 3]) | ||
@test subW isa WoodburyPDMat | ||
@test subW.A == W.A[[1, 3], :] | ||
@test subW.D == W.D | ||
@test subW.S == Diagonal(W.S.diag[[1, 3]]) | ||
@test Matrix(subW) == W_dense[[1, 3], [1, 3]] | ||
end | ||
end | ||
end |
5879fe4
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.
@JuliaRegistrator register
5879fe4
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.
Registration pull request created: JuliaRegistries/General/40004
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:
5879fe4
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.
Registration pull request updated: JuliaRegistries/General/40004
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: