-
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 9 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,91 @@ | ||
|
||
|
||
sparsecholesky(A::Symmetric{<:Real,<:BlockArray}, | ||
::Val{false}=Val(false); check::Bool = true) = sparsecholesky!(cholcopy(A); check = check) | ||
|
||
|
||
|
||
function _blockbandedcholesky!(A::BlockArray{T}, ::Type{UpperTriangular}) where T<:Real | ||
n = blocksize(A)[1] | ||
k_end = 0 | ||
|
||
@inbounds begin | ||
for i = 1:n | ||
Pii = getblock(A,i,i) | ||
for k = 1:i-1 | ||
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. This should loop over 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.
The code 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. It's overloaded for banded matrices: https://github.com/JuliaMatrices/BandedMatrices.jl/blob/3eb9c0d77f471db67b87b7dc5f9832c1d0beb0b2/src/generic/AbstractBandedMatrix.jl#L89 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. Since we only care about the blockbanded cases, maybe we can move the file into the package BlockBandedMatrices? 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. No, it should be here. It will do the right thing for both dense and banded formats. 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. Another thing is that why |
||
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,i), one(T), Pii) | ||
end | ||
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 | ||
|
||
k_start = n | ||
while getblock(A,i,k_start) ≈ zeros(Float32, size(getblock(A,i,k_start))) && k_start > k_end | ||
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. This is strange |
||
k_start -= 1 | ||
end | ||
k_end = k_start | ||
|
||
for j = k_start:-1:i+1 | ||
Pij = getblock(A,i,j) | ||
for k = 1:i-1 | ||
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,j), one(T), Pij) | ||
end | ||
ldiv!(UpperTriangular(getblock(A,i,i))', Pij) | ||
end | ||
end | ||
end | ||
|
||
return UpperTriangular(A), 0 | ||
end | ||
|
||
function _blockbandedcholesky!(A::BlockArray{T}, ::Type{LowerTriangular}) where T<:Real | ||
n = blocksize(A)[1] | ||
k_end = 0 | ||
|
||
@inbounds begin | ||
for i = 1:n | ||
Pii = getblock(A,i,i) | ||
for k = 1:i-1 | ||
muladd!(-one(T), getblock(A,i,k), getblock(A,i,k)', one(T), Pii) | ||
end | ||
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 | ||
|
||
k_start = n | ||
while getblock(A,k_start,i) ≈ zeros(Float32, size(getblock(A,k_start,i))) && k_start > k_end | ||
k_start -= 1 | ||
end | ||
k_end = k_start | ||
|
||
for j = k_start:-1:i+1 | ||
Pij = getblock(A,j,i) | ||
for k = 1:i-1 | ||
muladd!(-one(T), getblock(A,j,k), getblock(A,i,k)', one(T), Pij) | ||
end | ||
rdiv!(Pij, LowerTriangular(getblock(A,i,i))') | ||
end | ||
end | ||
end | ||
|
||
return LowerTriangular(A), 0 | ||
end | ||
|
||
|
||
function sparsecholesky!(A::Symmetric{<:Real,<:BlockArray}, ::Val{false}=Val(false); check::Bool = true) | ||
C, info = _blockbandedcholesky!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular) | ||
#check && LinearAlgebra.checkpositivedefinite(info) | ||
return Cholesky(C.data, A.uplo, info) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
|
||
|
||
########################################## | ||
# Cholesky Factorization on BlockMatrices# | ||
########################################## | ||
|
||
|
||
cholesky(A::Symmetric{<:Real,<:BlockArray}, | ||
::Val{false}=Val(false); check::Bool = false) = cholesky!(cholcopy(A); check = check) | ||
|
||
|
||
function _block_chol!(A::BlockArray{T}, ::Type{UpperTriangular}) where T<:Real | ||
n = blocksize(A)[1] | ||
|
||
@inbounds begin | ||
for i = 1:n | ||
Pii = getblock(A,i,i) | ||
for k = 1:i-1 | ||
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,i), one(T), Pii) | ||
end | ||
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 | ||
muladd!(-one(T), getblock(A,k,i)', getblock(A,k,j), one(T), Pij) | ||
end | ||
ldiv!(UpperTriangular(getblock(A,i,i))', Pij) | ||
end | ||
end | ||
end | ||
|
||
return UpperTriangular(A), 0 | ||
end | ||
|
||
|
||
function _block_chol!(A::BlockArray{T}, ::Type{LowerTriangular}) where T<:Real | ||
n = blocksize(A)[1] | ||
|
||
@inbounds begin | ||
for i = 1:n | ||
Pii = getblock(A,i,i) | ||
for k = 1:i-1 | ||
muladd!(-one(T), getblock(A,i,k), getblock(A,i,k)', one(T), Pii) | ||
end | ||
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) | ||
for k = 1:i-1 | ||
muladd!(-one(T), getblock(A,j,k), getblock(A,i,k)', one(T), Pij) | ||
end | ||
rdiv!(Pij, LowerTriangular(getblock(A,i,i))') | ||
end | ||
end | ||
end | ||
|
||
return LowerTriangular(A), 0 | ||
end | ||
|
||
function cholesky!(A::Symmetric{<:Real,<:BlockArray}, ::Val{false}=Val(false); check::Bool = false) | ||
C, info = _block_chol!(A.data, A.uplo == 'U' ? UpperTriangular : LowerTriangular) | ||
#check && LinearAlgebra.checkpositivedefinite(info) | ||
return Cholesky(C.data, A.uplo, info) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using BlockArrays, Test, LinearAlgebra | ||
|
||
|
||
|
||
@testset "Block cholesky" begin | ||
|
||
# Generating random positive definite and symmetric matrices | ||
A = BlockArray{Float32}(randn(9,9)+100I, fill(3,3), fill(3,3)); A = Symmetric(A) | ||
B = BlockArray{Float32}(randn(55,55)+100I, 1:10, 1:10); B = Symmetric(B) | ||
C = BlockArray{Float32}(randn(9,9)+100I, fill(3,3), fill(3,3)); C = Symmetric(C, :L) | ||
D = BlockArray{Float32}(randn(55,55)+100I, 1:10, 1:10); D = Symmetric(D, :L) | ||
E = BlockArray{Float32}(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{Float32}(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 | ||
|
||
#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 | ||
|
||
#Tests on non-PD matrices | ||
@test cholesky(E2).info == 2 | ||
@test cholesky(E5).info == 5 | ||
@test cholesky(E8).info == 8 | ||
|
||
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.
I don't understand why this is a separate function, not just
block_chol!
from beliw.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.
The only difference is that I added a check of trivial blocks from backwards. It actually do the same thing on dense matrices. We can have another parameter for activating the 'check'.