Skip to content

add BandedBlockBandedMatrix #34

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 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/coloring/high_level.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function matrix_colors(A::BandedMatrix)
end

function matrix_colors(A::BlockBandedMatrix)
u,l=blockbandwidths(A)
blockwidth=u+l+1
l,u=blockbandwidths(A)
blockwidth=l+u+1
nblock=nblocks(A,2)
cols=[blocksize(A,(1,i))[2] for i in 1:nblock]
blockcolors=_cycle(1:blockwidth,nblock)
Expand All @@ -61,4 +61,20 @@ function matrix_colors(A::BlockBandedMatrix)
startinds=[endinds[i]-ncolors[i]+1 for i in 1:blockwidth]
colors=[(startinds[blockcolors[i]]:endinds[blockcolors[i]])[1:cols[i]] for i in 1:nblock]
vcat(colors...)
end

function matrix_colors(A::BandedBlockBandedMatrix)
l,u=blockbandwidths(A)
lambda,mu=subblockbandwidths(A)
blockwidth=l+u+1
subblockwidth=lambda+mu+1
nblock=nblocks(A,2)
cols=[blocksize(A,(1,i))[2] for i in 1:nblock]
blockcolors=_cycle(1:blockwidth,nblock)
#the reserved number of colors of a block is the min of subblockwidth and the largest length of columns of blocks with the same block color
ncolors=[min(subblockwidth,maximum(cols[i:blockwidth:nblock])) for i in 1:blockwidth]
endinds=cumsum(ncolors)
startinds=[endinds[i]-ncolors[i]+1 for i in 1:blockwidth]
colors=[_cycle(startinds[blockcolors[i]]:endinds[blockcolors[i]],cols[i]) for i in 1:nblock]
vcat(colors...)
end
29 changes: 28 additions & 1 deletion test/test_specialmatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ symtridiagonal=SymTridiagonal(dense)
banded=BandedMatrix(dense,(1,2))
blockbanded1=BlockBandedMatrix(dense,([1,2,3,4],[4,3,2,1]),(1,0))
blockbanded2=BlockBandedMatrix(dense,([4,3,2,1],[1,2,3,4]),(1,1))
bandedblockbanded1=BandedBlockBandedMatrix(dense,([1,2,3,4],[4,3,2,1]),(1,0),(1,1))
bandedblockbanded2=BandedBlockBandedMatrix(dense,([4,3,2,1],[1,2,3,4]),(1,1),(1,0))

@test matrix_colors(dense)==1:n
@test matrix_colors(uptri)==1:n
Expand All @@ -30,4 +32,29 @@ blockbanded2=BlockBandedMatrix(dense,([4,3,2,1],[1,2,3,4]),(1,1))

@test matrix_colors(banded)==[1,2,3,4,1,2,3,4,1,2]
@test matrix_colors(blockbanded1)==[1,2,3,4,5,6,7,1,2,5]
@test matrix_colors(blockbanded2)==[1,5,6,7,8,9,1,2,3,4]
@test matrix_colors(blockbanded2)==[1,5,6,7,8,9,1,2,3,4]
@test matrix_colors(bandedblockbanded1)==[1,2,3,1,4,5,6,1,2,4]
@test matrix_colors(bandedblockbanded2)==[1,3,4,5,6,5,1,2,1,2]

function _testvalidity(A)
colorvec=matrix_colors(A)
ncolor=maximum(colorvec)
for color in 1:ncolor
subA=A[:,findall(x->x==color,colorvec)]
@test maximum(sum(subA,dims=2))<=1.0
end
end

_testvalidity(dense)
_testvalidity(uptri)
_testvalidity(lotri)
_testvalidity(diagonal)
_testvalidity(bidiagonalU)
_testvalidity(bidiagonalL)
_testvalidity(tridiagonal)
_testvalidity(symtridiagonal)
_testvalidity(banded)
_testvalidity(blockbanded1)
_testvalidity(blockbanded2)
_testvalidity(bandedblockbanded1)
_testvalidity(bandedblockbanded2)