@@ -123,6 +123,34 @@ function findstructralnz(x::SparseMatrixCSC)
123
123
(rowind,colind)
124
124
end
125
125
126
+ abstract type ColoringAlgorithm end
127
+
128
+ """
129
+ matrix_colors(A::Union{Array,UpperTriangular,LowerTriangular})
130
+
131
+ The color vector for dense matrix and triangular matrix is simply
132
+ `[1,2,3,...,size(A,2)]`
133
+ """
134
+ function matrix_colors (A:: Union{Array,UpperTriangular,LowerTriangular} )
135
+ eachindex (1 : size (A,2 )) # Vector size matches number of rows
136
+ end
137
+
138
+ function _cycle (repetend,len)
139
+ repeat (repetend,div (len,length (repetend))+ 1 )[1 : len]
140
+ end
141
+
142
+ function matrix_colors (A:: Diagonal )
143
+ fill (1 ,size (A,2 ))
144
+ end
145
+
146
+ function matrix_colors (A:: Bidiagonal )
147
+ _cycle (1 : 2 ,size (A,2 ))
148
+ end
149
+
150
+ function matrix_colors (A:: Union{Tridiagonal,SymTridiagonal} )
151
+ _cycle (1 : 3 ,size (A,2 ))
152
+ end
153
+
126
154
function __init__ ()
127
155
128
156
@require StaticArrays= " 90137ffa-7385-5640-81b9-e52037218182" begin
@@ -142,11 +170,49 @@ function __init__()
142
170
143
171
@require BandedMatrices= " aae01518-5342-5314-be14-df237901396f" begin
144
172
is_structured (:: BandedMatrices.BandedMatrix ) = true
173
+
174
+ function matrix_colors (A:: BandedMatrix )
175
+ u,l= bandwidths (A)
176
+ width= u+ l+ 1
177
+ _cycle (1 : width,size (A,2 ))
178
+ end
179
+
145
180
end
146
181
147
182
@require BlockBandedMatrices= " aae01518-5342-5314-be14-df237901396f" begin
148
183
is_structured (:: BandedMatrices.BlockBandedMatrix ) = true
149
184
is_structured (:: BandedMatrices.BandedBlockBandedMatrix ) = true
185
+
186
+ function matrix_colors (A:: BlockBandedMatrix )
187
+ l,u= blockbandwidths (A)
188
+ blockwidth= l+ u+ 1
189
+ nblock= nblocks (A,2 )
190
+ cols= [blocksize (A,(1 ,i))[2 ] for i in 1 : nblock]
191
+ blockcolors= _cycle (1 : blockwidth,nblock)
192
+ # the reserved number of colors of a block is the maximum length of columns of blocks with the same block color
193
+ ncolors= [maximum (cols[i: blockwidth: nblock]) for i in 1 : blockwidth]
194
+ endinds= cumsum (ncolors)
195
+ startinds= [endinds[i]- ncolors[i]+ 1 for i in 1 : blockwidth]
196
+ colors= [(startinds[blockcolors[i]]: endinds[blockcolors[i]])[1 : cols[i]] for i in 1 : nblock]
197
+ vcat (colors... )
198
+ end
199
+
200
+ function matrix_colors (A:: BandedBlockBandedMatrix )
201
+ l,u= blockbandwidths (A)
202
+ lambda,mu= subblockbandwidths (A)
203
+ blockwidth= l+ u+ 1
204
+ subblockwidth= lambda+ mu+ 1
205
+ nblock= nblocks (A,2 )
206
+ cols= [blocksize (A,(1 ,i))[2 ] for i in 1 : nblock]
207
+ blockcolors= _cycle (1 : blockwidth,nblock)
208
+ # 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
209
+ ncolors= [min (subblockwidth,maximum (cols[i: blockwidth: nblock])) for i in 1 : min (blockwidth,nblock)]
210
+ endinds= cumsum (ncolors)
211
+ startinds= [endinds[i]- ncolors[i]+ 1 for i in 1 : min (blockwidth,nblock)]
212
+ colors= [_cycle (startinds[blockcolors[i]]: endinds[blockcolors[i]],cols[i]) for i in 1 : nblock]
213
+ vcat (colors... )
214
+ end
215
+
150
216
end
151
217
end
152
218
0 commit comments