-
Notifications
You must be signed in to change notification settings - Fork 33
Create blockcholesky.jl #126
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9c47f78
Create blockcholesky.jl
felixw98 030a054
Add files via upload
felixw98 b977d8d
overload cholesk!
dlfivefifty 0ff3e67
Including the test file in REPL will do the test
felixw98 dc54673
Including test_cholesky.jl in runtests.jl
felixw98 5605d8f
Added lower Cholesky factorization
felixw98 bb7355b
Added PD check
felixw98 2d782fe
Changed the function name and types
felixw98 72259da
Added blockbanded case
felixw98 883da65
Rearrange
felixw98 09a8a54
Capable on both BlockArray and BlockBandedArray
felixw98 b6f7d3a
Changes
felixw98 dc6e020
Merge branch 'master' into pr/126
dlfivefifty 8793acd
Update Choleksy
dlfivefifty f994fc0
tests pass
dlfivefifty 44b1490
Support sparse block structure
dlfivefifty 2d43bda
Update blockcholesky.jl
dlfivefifty 025a4ce
Fix tests on 1.8, use ArrayLayouts.ldiv!
dlfivefifty 04af833
Speed up BlockKron construction
dlfivefifty f1059ef
Update test_blockproduct.jl
dlfivefifty 99f54d5
increase cov
dlfivefifty 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
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,37 +1,33 @@ | ||
|
||
|
||
########################## | ||
# Cholesky Factorization # | ||
########################## | ||
########################################## | ||
# Cholesky Factorization on BlockMatrices# | ||
########################################## | ||
|
||
""" | ||
Funtions to do | ||
2.check square | ||
3.check positive definite | ||
""" | ||
|
||
cholesky(A::Symmetric{<:Real,<:BlockArray}, | ||
::Val{false}=Val(false); check::Bool = true) = cholesky!(cholcopy(A); check = check) | ||
::Val{false}=Val(false); check::Bool = false) = cholesky!(cholcopy(A); check = check) | ||
|
||
|
||
function b_chol!(A::BlockArray{T}, ::Type{UpperTriangular}) where T<:Real | ||
n = blocksize(A)[1] | ||
|
||
@inbounds begin | ||
# Initializing the first role of blocks | ||
cholesky!(Symmetric(getblock(A,1,1))) | ||
for j = 2:n | ||
ldiv!(UpperTriangular(getblock(A,1,1))', getblock(A,1,j)) | ||
end | ||
|
||
# For the left blocks | ||
for i = 2:n | ||
for i = 1:n | ||
Pii = getblock(A,i,i) | ||
for k = 1:i-1 | ||
muladd!(-1.0, getblock(A,k,i)', getblock(A,k,i), 1.0, Pii) | ||
end | ||
cholesky!(Symmetric(Pii)) | ||
|
||
Aii, info = LinearAlgebra._chol!(Pii, UpperTriangular) | ||
if !iszero(info) | ||
@assert info > 0 | ||
if i == 1 | ||
return UpperTriangular(A), info | ||
end | ||
info += sum(size(A[Block(l,l)])[1] for l=1:i-1) | ||
return UpperTriangular(A), info | ||
end | ||
|
||
for j = i+1:n | ||
Pij = getblock(A,i,j) | ||
for k = 1:i-1 | ||
|
@@ -41,26 +37,29 @@ function b_chol!(A::BlockArray{T}, ::Type{UpperTriangular}) where T<:Real | |
end | ||
end | ||
end | ||
|
||
return UpperTriangular(A) | ||
return UpperTriangular(A), 0 | ||
end | ||
|
||
|
||
function b_chol!(A::BlockArray{T}, ::Type{LowerTriangular}) where T<:Real | ||
n = blocksize(A)[1] | ||
|
||
@inbounds begin | ||
# Initializing the first role of blocks | ||
cholesky!(Symmetric(getblock(A,1,1), :L)) | ||
for j = 2:n | ||
rdiv!(getblock(A,j,1), LowerTriangular(getblock(A,1,1))') | ||
end | ||
|
||
# For the left blocks | ||
for i = 2:n | ||
for i = 1:n | ||
Pii = getblock(A,i,i) | ||
for k = 1:i-1 | ||
muladd!(-1.0, getblock(A,i,k), getblock(A,i,k)', 1.0, Pii) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
cholesky!(Symmetric(Pii, :L)) | ||
Aii, info = LinearAlgebra._chol!(Pii, LowerTriangular) | ||
if !iszero(info) | ||
@assert info > 0 | ||
if i == 1 | ||
return UpperTriangular(A), info | ||
end | ||
info += sum(size(A[Block(l,l)])[1] for l=1:i-1) | ||
return LowerTriangular(A), info | ||
end | ||
|
||
for j = i+1:n | ||
Pij = getblock(A,j,i) | ||
|
@@ -72,12 +71,12 @@ function b_chol!(A::BlockArray{T}, ::Type{LowerTriangular}) where T<:Real | |
end | ||
end | ||
|
||
return LowerTriangular(A) | ||
return LowerTriangular(A), 0 | ||
end | ||
|
||
function cholesky!(A::Symmetric{<:Real,<:BlockArray}, ::Val{false}=Val(false); check::Bool = true) | ||
C = b_chol!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular) | ||
#check && checkpositivedefinite(info) | ||
return Cholesky(C.data, A.uplo, 0) | ||
function cholesky!(A::Symmetric{<:Real,<:BlockArray}, ::Val{false}=Val(false); check::Bool = false) | ||
C, info = b_chol!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular) | ||
#check && LinearAlgebra.checkpositivedefinite(info) | ||
return Cholesky(C.data, A.uplo, info) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,20 @@ using BlockArrays, Test, LinearAlgebra | |
B = BlockArray{Float64}(randn(55,55)+100I, 1:10, 1:10); B = Symmetric(B) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests with |
||
C = BlockArray{Float64}(randn(9,9)+100I, fill(3,3), fill(3,3)); C = Symmetric(C, :L) | ||
D = BlockArray{Float64}(randn(55,55)+100I, 1:10, 1:10); D = Symmetric(D, :L) | ||
E = BlockArray{Float64}(randn(9,9)+100I, fill(3,3), fill(3,3)); E = Symmetric(E) | ||
E2 = copy(E); E2[2,2] = 0 | ||
E5 = copy(E); E5[5,5] = 0 | ||
E8 = copy(E); E8[8,8] = 0 | ||
nsym = BlockArray{Float64}(randn(6,8), fill(2,3), fill(2,4)) | ||
|
||
A_T = Matrix(A) | ||
B_T = Matrix(B) | ||
C_T = Matrix(C) | ||
D_T = Matrix(D) | ||
|
||
#Test on nonsymmetric matrix | ||
@test_throws MethodError cholesky(nsym) | ||
|
||
#Tests on A | ||
@test cholesky(A).U ≈ cholesky(A_T).U | ||
@test cholesky(A).U'cholesky(A).U ≈ A | ||
|
@@ -27,9 +35,14 @@ using BlockArrays, Test, LinearAlgebra | |
@test cholesky(C).L ≈ cholesky(C_T).L | ||
@test cholesky(C).L*cholesky(C).L' ≈ C | ||
|
||
#tests on D | ||
#Tests on D | ||
@test cholesky(D).L ≈ cholesky(D_T).L | ||
@test cholesky(D).L*cholesky(D).L' ≈ D | ||
|
||
#Tests on non-PD matrices | ||
@test cholesky(E2).info == 2 | ||
@test cholesky(E5).info == 5 | ||
@test cholesky(E8).info == 8 | ||
|
||
end | ||
|
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.
Convention in Julia is that internal routines start with
_
. So perhaps rename this function_block_chol!
?