-
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
Changes from 6 commits
9c47f78
030a054
b977d8d
0ff3e67
dc54673
5605d8f
bb7355b
2d782fe
72259da
883da65
09a8a54
b6f7d3a
dc6e020
8793acd
f994fc0
44b1490
2d43bda
025a4ce
04af833
f1059ef
99f54d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
|
||
########################## | ||
# Cholesky Factorization # | ||
########################## | ||
|
||
""" | ||
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) | ||
|
||
|
||
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 | ||
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)) | ||
|
||
for j = i+1:n | ||
Pij = getblock(A,i,j) | ||
for k = 1:i-1 | ||
muladd!(-1.0, getblock(A,k,i)', getblock(A,k,j), 1.0, Pij) | ||
end | ||
ldiv!(UpperTriangular(getblock(A,i,i))', Pij) | ||
end | ||
end | ||
end | ||
|
||
return UpperTriangular(A) | ||
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. Please have this return info = 0
for ...
...
info = cholesky!(Symmetric(Pii))
iszero(info) || break
...
end
Cholesky(A, :U, info) though we should double check what Similarly for lower below. 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. In the document of 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. I think you mean indicates that there exists a negative eigenvalue, not that it finds one. Here is the doc for LAPACK.potrf!: http://www.netlib.org/lapack/explore-html/d1/d7a/group__double_p_ocomputational_ga2f55f604a6003d03b5cd4a0adcfb74d6.html
So I think we want if !iszero(info)
@assert info > 0 # should never have illegal values
info += sum(blockaxes(A,2)[Block.(1:i-1)]) # add columns before we got to block to indicate correct minor
break
end |
||
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 | ||
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)) | ||
|
||
for j = i+1:n | ||
Pij = getblock(A,j,i) | ||
for k = 1:i-1 | ||
muladd!(-1.0, getblock(A,j,k), getblock(A,i,k)', 1.0, Pij) | ||
end | ||
rdiv!(Pij, LowerTriangular(getblock(A,i,i))') | ||
end | ||
end | ||
end | ||
|
||
return LowerTriangular(A) | ||
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) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using BlockArrays, Test, LinearAlgebra | ||
|
||
|
||
|
||
@testset "Block cholesky" begin | ||
|
||
# Generating random positive definite and symmetric matrices | ||
A = BlockArray{Float64}(randn(9,9)+100I, fill(3,3), fill(3,3)); A = Symmetric(A) | ||
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) | ||
|
||
A_T = Matrix(A) | ||
B_T = Matrix(B) | ||
C_T = Matrix(C) | ||
D_T = Matrix(D) | ||
|
||
#Tests on A | ||
@test cholesky(A).U ≈ cholesky(A_T).U | ||
@test cholesky(A).U'cholesky(A).U ≈ A | ||
|
||
#Tests on B | ||
@test cholesky(B).U ≈ cholesky(B_T).U | ||
@test cholesky(B).U'cholesky(B).U ≈ B | ||
|
||
#Tests on C | ||
@test cholesky(C).L ≈ cholesky(C_T).L | ||
@test cholesky(C).L*cholesky(C).L' ≈ C | ||
|
||
#tests on D | ||
@test cholesky(D).L ≈ cholesky(D_T).L | ||
@test cholesky(D).L*cholesky(D).L' ≈ D | ||
|
||
end | ||
|
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!
?