Skip to content

Commit 2e67bf3

Browse files
authored
Add symmetric coloring function (#43)
* Add symmetric coloring function * Fix docstring
1 parent bb38b92 commit 2e67bf3

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ ADTypes.coloring_algorithm
7878
ADTypes.AbstractColoringAlgorithm
7979
ADTypes.column_coloring
8080
ADTypes.row_coloring
81+
ADTypes.symmetric_coloring
8182
ADTypes.NoColoringAlgorithm
8283
```
8384

src/sparse.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,50 @@ Abstract supertype for Jacobian/Hessian coloring algorithms, defined for example
5353
5454
- [`column_coloring`](@ref)
5555
- [`row_coloring`](@ref)
56+
- [`symmetric_coloring`](@ref)
57+
58+
# Note
59+
60+
The terminology and definitions are taken from the following paper:
61+
62+
> "What Color Is Your Jacobian? Graph Coloring for Computing Derivatives"
63+
>
64+
> Assefaw Hadish Gebremedhin, Fredrik Manne, and Alex Pothen (2005)
65+
>
66+
> https://epubs.siam.org/doi/10.1137/S0036144504444711
5667
"""
5768
abstract type AbstractColoringAlgorithm end
5869

5970
"""
6071
column_coloring(M::AbstractMatrix, ca::ColoringAlgorithm)::AbstractVector{<:Integer}
6172
62-
Use algorithm `ca` to construct a coloring vector `c` of length `size(M, 2)` such that if two columns `j1` and `j2` satisfy `c[j1] = c[j2]`, they do not share any nonzero coefficients in `M`.
73+
Use algorithm `ca` to construct a structurally orthogonal partition of the columns of `M`.
74+
75+
The result is a coloring vector `c` of length `size(M, 2)` such that for every non-zero coefficient `M[i, j]`, column `j` is the only column of its color `c[j]` with a non-zero coefficient in row `i`.
6376
"""
6477
function column_coloring end
6578

6679
"""
6780
row_coloring(M::AbstractMatrix, ca::ColoringAlgorithm)::AbstractVector{<:Integer}
6881
69-
Use algorithm `ca` to construct a coloring vector `c` of length `size(M, 1)` such that if two rows `i1` and `i2` satisfy `c[i1] = c[i2]`, they do not share any nonzero coefficients in `M`.
82+
Use algorithm `ca` to construct a structurally orthogonal partition of the rows of `M`.
83+
84+
The result is a coloring vector `c` of length `size(M, 1)` such that for every non-zero coefficient `M[i, j]`, row `i` is the only row of its color `c[i]` with a non-zero coefficient in column `j`.
7085
"""
7186
function row_coloring end
7287

88+
"""
89+
symmetric_coloring(M::AbstractMatrix, ca::ColoringAlgorithm)::AbstractVector{<:Integer}
90+
91+
Use algorithm `ca` to construct a symetrically structurally orthogonal partition of the columns (or rows) of the symmetric matrix `M`.
92+
93+
The result is a coloring vector `c` of length `size(M, 1) == size(M, 2)` such that for every non-zero coefficient `M[i, j]`, at least one of the following conditions holds:
94+
95+
- column `j` is the only column of its color `c[j]` with a non-zero coefficient in row `i`;
96+
- column `i` is the only column of its color `c[i]` with a non-zero coefficient in row `j`.
97+
"""
98+
function symmetric_coloring end
99+
73100
"""
74101
NoColoringAlgorithm <: AbstractColoringAlgorithm
75102
@@ -83,6 +110,7 @@ struct NoColoringAlgorithm <: AbstractColoringAlgorithm end
83110

84111
column_coloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 2)
85112
row_coloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 1)
113+
symmetric_coloring(M::AbstractMatrix, ::NoColoringAlgorithm) = 1:size(M, 1)
86114

87115
## Sparse backend
88116

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ using ADTypes: dense_ad,
1313
NoColoringAlgorithm,
1414
coloring_algorithm,
1515
column_coloring,
16-
row_coloring
16+
row_coloring,
17+
symmetric_coloring
1718
using Aqua: Aqua
1819
using ChainRulesCore: ChainRulesCore, RuleConfig,
1920
HasForwardsMode, HasReverseMode,

test/sparse.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,10 @@ end
6262
@test length(rv) == size(M, 1)
6363
@test allunique(rv)
6464
end
65+
66+
M = rand(3, 3)
67+
sv = symmetric_coloring(M, ca)
68+
@test sv isa AbstractVector{<:Integer}
69+
@test length(sv) == size(M, 1) == size(M, 2)
70+
@test allunique(sv)
6571
end

0 commit comments

Comments
 (0)