1+ function check_valid_problem (structure:: Symbol , partition:: Symbol )
2+ valid = (
3+ (structure == :nonsymmetric && partition in (:column , :row )) ||
4+ (structure == :symmetric && partition == :column )
5+ )
6+ if ! valid
7+ throw (
8+ ArgumentError (
9+ " The combination `($(repr (structure)) , $(repr (partition)) )` is not supported by `ColoringProblem`." ,
10+ ),
11+ )
12+ end
13+ end
14+
15+ function check_valid_algorithm (decompression:: Symbol )
16+ valid = decompression in (:direct , :substitution )
17+ if ! valid
18+ throw (
19+ ArgumentError (
20+ " The setting `decompression=$(repr (decompression)) ` is not supported by `GreedyColoringAlgorithm`." ,
21+ ),
22+ )
23+ end
24+ end
25+
126"""
227 ColoringProblem{structure,partition}
328
429Selector type for the coloring problem to solve, enabling multiple dispatch.
530
631It is passed as an argument to the main function [`coloring`](@ref).
732
8- # Constructor
33+ # Constructors
934
10- ColoringProblem(; structure::Symbol=:nonsymmetric, partition::Symbol=:column)
35+ ColoringProblem{structure,partition}()
36+ ColoringProblem(; structure=:nonsymmetric, partition=:column)
1137
1238- `structure::Symbol`: either `:nonsymmetric` or `:symmetric`
1339- `partition::Symbol`: either `:column`, `:row` or `:bidirectional`
1440
41+ !!! warning
42+ The second constructor (based on keyword arguments) is type-unstable.
43+
1544# Link to automatic differentiation
1645
1746Matrix coloring is often used in automatic differentiation, and here is the translation guide:
1847
19- | matrix | mode | `structure` | `partition` |
20- | -------- | -------------------- | --------------- | -----------------|
21- | Jacobian | forward | `:nonsymmetric` | `:column` |
22- | Jacobian | reverse | `:nonsymmetric` | `:row` |
23- | Jacobian | forward + reverse | `:nonsymmetric` | `:bidirectional` |
24- | Hessian | any | `:symmetric` | `:column` |
25-
26- !!! warning
27- With a `:symmetric` structure, you have to use a `:column` partition.
28-
29- !!! warning
30- At the moment, `:bidirectional` partitions are not implemented.
48+ | matrix | mode | `structure` | `partition` | implemented |
49+ | -------- | ------- | --------------- | ---------------- | ----------- |
50+ | Jacobian | forward | `:nonsymmetric` | `:column` | yes |
51+ | Jacobian | reverse | `:nonsymmetric` | `:row` | yes |
52+ | Jacobian | mixed | `:nonsymmetric` | `:bidirectional` | no |
53+ | Hessian | - | `:symmetric` | `:column` | yes |
54+ | Hessian | - | `:symmetric` | `:row` | no |
3155"""
3256struct ColoringProblem{structure,partition} end
3357
3458function ColoringProblem (; structure:: Symbol = :nonsymmetric , partition:: Symbol = :column )
35- @assert structure in (:nonsymmetric , :symmetric )
36- @assert partition in (:column , :row , :bidirectional )
59+ check_valid_problem (structure, partition)
3760 return ColoringProblem {structure,partition} ()
3861end
3962
@@ -44,16 +67,17 @@ Greedy coloring algorithm for sparse matrices which colors columns or rows one a
4467
4568It is passed as an argument to the main function [`coloring`](@ref).
4669
47- # Constructor
70+ # Constructors
4871
49- GreedyColoringAlgorithm(
50- order::AbstractOrder=NaturalOrder();
51- decompression::Symbol=:direct
52- )
72+ GreedyColoringAlgorithm{decompression}(order=NaturalOrder())
73+ GreedyColoringAlgorithm(order=NaturalOrder(); decompression=:direct)
5374
5475- `order::AbstractOrder`: the order in which the columns or rows are colored, which can impact the number of colors.
5576- `decompression::Symbol`: either `:direct` or `:substitution`. Usually `:substitution` leads to fewer colors, at the cost of a more expensive coloring (and decompression). When `:substitution` is not applicable, it falls back on `:direct` decompression.
5677
78+ !!! warning
79+ The second constructor (based on keyword arguments) is type-unstable.
80+
5781# ADTypes coloring interface
5882
5983`GreedyColoringAlgorithm` is a subtype of [`ADTypes.AbstractColoringAlgorithm`](@extref ADTypes.AbstractColoringAlgorithm), which means the following methods are also applicable:
@@ -74,10 +98,17 @@ struct GreedyColoringAlgorithm{decompression,O<:AbstractOrder} <:
7498 order:: O
7599end
76100
101+ function GreedyColoringAlgorithm {decompression} (
102+ order:: AbstractOrder = NaturalOrder ()
103+ ) where {decompression}
104+ check_valid_algorithm (decompression)
105+ return GreedyColoringAlgorithm {decompression,typeof(order)} (order)
106+ end
107+
77108function GreedyColoringAlgorithm (
78109 order:: AbstractOrder = NaturalOrder (); decompression:: Symbol = :direct
79110)
80- @assert decompression in ( :direct , :substitution )
111+ check_valid_algorithm (decompression )
81112 return GreedyColoringAlgorithm {decompression,typeof(order)} (order)
82113end
83114
@@ -106,9 +137,9 @@ julia> S = sparse([
106137 0 1 1 0 0 0
107138 ]);
108139
109- julia> problem = ColoringProblem(structure=:nonsymmetric, partition=:column);
140+ julia> problem = ColoringProblem(; structure=:nonsymmetric, partition=:column);
110141
111- julia> algo = GreedyColoringAlgorithm();
142+ julia> algo = GreedyColoringAlgorithm(; decompression=:direct );
112143
113144julia> result = coloring(S, problem, algo);
114145
0 commit comments