1
- abstract type ColoringAlgorithm end
2
- struct GreedyD1Color <: ColoringAlgorithm end
3
- struct BacktrackingColor <: ColoringAlgorithm end
4
- struct ContractionColor <: ColoringAlgorithm end
5
- struct GreedyStar1Color <: ColoringAlgorithm end
6
- struct GreedyStar2Color <: ColoringAlgorithm end
1
+ abstract type SparseDiffToolsColoringAlgorithm <: ArrayInterface. ColoringAlgorithm end
2
+ struct GreedyD1Color <: SparseDiffToolsColoringAlgorithm end
3
+ struct BacktrackingColor <: SparseDiffToolsColoringAlgorithm end
4
+ struct ContractionColor <: SparseDiffToolsColoringAlgorithm end
5
+ struct GreedyStar1Color <: SparseDiffToolsColoringAlgorithm end
6
+ struct GreedyStar2Color <: SparseDiffToolsColoringAlgorithm end
7
7
8
8
"""
9
9
matrix_colors(A,alg::ColoringAlgorithm = GreedyD1Color())
@@ -13,70 +13,8 @@ struct GreedyStar2Color <: ColoringAlgorithm end
13
13
The coloring defaults to a greedy distance-1 coloring.
14
14
15
15
"""
16
- function matrix_colors (A:: AbstractMatrix ,alg:: ColoringAlgorithm = GreedyD1Color (); partition_by_rows:: Bool = false )
16
+ function ArrayInterface . matrix_colors (A:: AbstractMatrix ,alg:: SparseDiffToolsColoringAlgorithm = GreedyD1Color (); partition_by_rows:: Bool = false )
17
17
_A = A isa SparseMatrixCSC ? A : sparse (A) # Avoid the copy
18
18
A_graph = matrix2graph (_A, partition_by_rows)
19
19
color_graph (A_graph,alg)
20
20
end
21
-
22
- """
23
- matrix_colors(A::Union{Array,UpperTriangular,LowerTriangular})
24
-
25
- The color vector for dense matrix and triangular matrix is simply
26
- `[1,2,3,...,size(A,2)]`
27
- """
28
- function matrix_colors (A:: Union{Array,UpperTriangular,LowerTriangular} )
29
- eachindex (1 : size (A,2 )) # Vector size matches number of rows
30
- end
31
-
32
- function _cycle (repetend,len)
33
- repeat (repetend,div (len,length (repetend))+ 1 )[1 : len]
34
- end
35
-
36
- function matrix_colors (A:: Diagonal )
37
- fill (1 ,size (A,2 ))
38
- end
39
-
40
- function matrix_colors (A:: Bidiagonal )
41
- _cycle (1 : 2 ,size (A,2 ))
42
- end
43
-
44
- function matrix_colors (A:: Union{Tridiagonal,SymTridiagonal} )
45
- _cycle (1 : 3 ,size (A,2 ))
46
- end
47
-
48
- function matrix_colors (A:: BandedMatrix )
49
- u,l= bandwidths (A)
50
- width= u+ l+ 1
51
- _cycle (1 : width,size (A,2 ))
52
- end
53
-
54
- function matrix_colors (A:: BlockBandedMatrix )
55
- l,u= blockbandwidths (A)
56
- blockwidth= l+ u+ 1
57
- nblock= nblocks (A,2 )
58
- cols= [blocksize (A,(1 ,i))[2 ] for i in 1 : nblock]
59
- blockcolors= _cycle (1 : blockwidth,nblock)
60
- # the reserved number of colors of a block is the maximum length of columns of blocks with the same block color
61
- ncolors= [maximum (cols[i: blockwidth: nblock]) for i in 1 : blockwidth]
62
- endinds= cumsum (ncolors)
63
- startinds= [endinds[i]- ncolors[i]+ 1 for i in 1 : blockwidth]
64
- colors= [(startinds[blockcolors[i]]: endinds[blockcolors[i]])[1 : cols[i]] for i in 1 : nblock]
65
- vcat (colors... )
66
- end
67
-
68
- function matrix_colors (A:: BandedBlockBandedMatrix )
69
- l,u= blockbandwidths (A)
70
- lambda,mu= subblockbandwidths (A)
71
- blockwidth= l+ u+ 1
72
- subblockwidth= lambda+ mu+ 1
73
- nblock= nblocks (A,2 )
74
- cols= [blocksize (A,(1 ,i))[2 ] for i in 1 : nblock]
75
- blockcolors= _cycle (1 : blockwidth,nblock)
76
- # 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
77
- ncolors= [min (subblockwidth,maximum (cols[i: blockwidth: nblock])) for i in 1 : min (blockwidth,nblock)]
78
- endinds= cumsum (ncolors)
79
- startinds= [endinds[i]- ncolors[i]+ 1 for i in 1 : min (blockwidth,nblock)]
80
- colors= [_cycle (startinds[blockcolors[i]]: endinds[blockcolors[i]],cols[i]) for i in 1 : nblock]
81
- vcat (colors... )
82
- end
0 commit comments