Skip to content

Commit 371823d

Browse files
Merge pull request #34 from huanglangwen/BBBmatrix
add BandedBlockBandedMatrix
2 parents 6b5a64b + 1d1af62 commit 371823d

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/coloring/high_level.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ function matrix_colors(A::BandedMatrix)
5050
end
5151

5252
function matrix_colors(A::BlockBandedMatrix)
53-
u,l=blockbandwidths(A)
54-
blockwidth=u+l+1
53+
l,u=blockbandwidths(A)
54+
blockwidth=l+u+1
5555
nblock=nblocks(A,2)
5656
cols=[blocksize(A,(1,i))[2] for i in 1:nblock]
5757
blockcolors=_cycle(1:blockwidth,nblock)
@@ -61,4 +61,20 @@ function matrix_colors(A::BlockBandedMatrix)
6161
startinds=[endinds[i]-ncolors[i]+1 for i in 1:blockwidth]
6262
colors=[(startinds[blockcolors[i]]:endinds[blockcolors[i]])[1:cols[i]] for i in 1:nblock]
6363
vcat(colors...)
64+
end
65+
66+
function matrix_colors(A::BandedBlockBandedMatrix)
67+
l,u=blockbandwidths(A)
68+
lambda,mu=subblockbandwidths(A)
69+
blockwidth=l+u+1
70+
subblockwidth=lambda+mu+1
71+
nblock=nblocks(A,2)
72+
cols=[blocksize(A,(1,i))[2] for i in 1:nblock]
73+
blockcolors=_cycle(1:blockwidth,nblock)
74+
#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
75+
ncolors=[min(subblockwidth,maximum(cols[i:blockwidth:nblock])) for i in 1:blockwidth]
76+
endinds=cumsum(ncolors)
77+
startinds=[endinds[i]-ncolors[i]+1 for i in 1:blockwidth]
78+
colors=[_cycle(startinds[blockcolors[i]]:endinds[blockcolors[i]],cols[i]) for i in 1:nblock]
79+
vcat(colors...)
6480
end

test/test_specialmatrices.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ symtridiagonal=SymTridiagonal(dense)
1717
banded=BandedMatrix(dense,(1,2))
1818
blockbanded1=BlockBandedMatrix(dense,([1,2,3,4],[4,3,2,1]),(1,0))
1919
blockbanded2=BlockBandedMatrix(dense,([4,3,2,1],[1,2,3,4]),(1,1))
20+
bandedblockbanded1=BandedBlockBandedMatrix(dense,([1,2,3,4],[4,3,2,1]),(1,0),(1,1))
21+
bandedblockbanded2=BandedBlockBandedMatrix(dense,([4,3,2,1],[1,2,3,4]),(1,1),(1,0))
2022

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

3133
@test matrix_colors(banded)==[1,2,3,4,1,2,3,4,1,2]
3234
@test matrix_colors(blockbanded1)==[1,2,3,4,5,6,7,1,2,5]
33-
@test matrix_colors(blockbanded2)==[1,5,6,7,8,9,1,2,3,4]
35+
@test matrix_colors(blockbanded2)==[1,5,6,7,8,9,1,2,3,4]
36+
@test matrix_colors(bandedblockbanded1)==[1,2,3,1,4,5,6,1,2,4]
37+
@test matrix_colors(bandedblockbanded2)==[1,3,4,5,6,5,1,2,1,2]
38+
39+
function _testvalidity(A)
40+
colorvec=matrix_colors(A)
41+
ncolor=maximum(colorvec)
42+
for color in 1:ncolor
43+
subA=A[:,findall(x->x==color,colorvec)]
44+
@test maximum(sum(subA,dims=2))<=1.0
45+
end
46+
end
47+
48+
_testvalidity(dense)
49+
_testvalidity(uptri)
50+
_testvalidity(lotri)
51+
_testvalidity(diagonal)
52+
_testvalidity(bidiagonalU)
53+
_testvalidity(bidiagonalL)
54+
_testvalidity(tridiagonal)
55+
_testvalidity(symtridiagonal)
56+
_testvalidity(banded)
57+
_testvalidity(blockbanded1)
58+
_testvalidity(blockbanded2)
59+
_testvalidity(bandedblockbanded1)
60+
_testvalidity(bandedblockbanded2)

0 commit comments

Comments
 (0)