Skip to content

Commit 3937800

Browse files
committed
Fix cycling inferrability
1 parent fe178d7 commit 3937800

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

docs/src/dev.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ SparseMatrixColorings.efficient_fig_4
6969
## Misc
7070

7171
```@docs
72-
SparseMatrixColorings.cycle_until
73-
```
72+
SparseMatrixColorings.cycle_range
73+
```

src/structured.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ Question: when decompressing, should we always assume that the coloring was opti
66
=#
77

88
"""
9-
cycle_until(iterator, max_length::Integer)
9+
cycle_range(k::Integer, n::Integer)
1010
11-
Concatenate copies of `iterator` to fill a vector of length `max_length` (with one partial copy allowed at the end).
11+
Concatenate copies of `1:k` to fill a vector of length `n` (with one partial copy allowed at the end).
1212
"""
13-
function cycle_until(iterator, max_length::Integer)
14-
a = repeat(iterator, div(max_length, length(iterator)) + 1)
15-
return resize!(a, max_length)
13+
function cycle_range(k::Integer, n::Integer)
14+
color = Vector{Int}(undef, n)
15+
for i in eachindex(color)
16+
color[i] = 1 + (i - 1) % k
17+
end
18+
return color
1619
end
1720

1821
## Diagonal
@@ -67,7 +70,7 @@ function coloring(
6770
algo::GreedyColoringAlgorithm;
6871
kwargs...,
6972
)
70-
color = cycle_until(1:2, size(A, 2))
73+
color = cycle_range(1:2, size(A, 2))
7174
bg = BipartiteGraph(A)
7275
return ColumnColoringResult(A, bg, color)
7376
end
@@ -78,7 +81,7 @@ function coloring(
7881
algo::GreedyColoringAlgorithm;
7982
kwargs...,
8083
)
81-
color = cycle_until(1:2, size(A, 1))
84+
color = cycle_range(1:2, size(A, 1))
8285
bg = BipartiteGraph(A)
8386
return RowColoringResult(A, bg, color)
8487
end
@@ -123,7 +126,7 @@ function coloring(
123126
algo::GreedyColoringAlgorithm;
124127
kwargs...,
125128
)
126-
color = cycle_until(1:3, size(A, 2))
129+
color = cycle_range(1:3, size(A, 2))
127130
bg = BipartiteGraph(A)
128131
return ColumnColoringResult(A, bg, color)
129132
end
@@ -134,7 +137,7 @@ function coloring(
134137
algo::GreedyColoringAlgorithm;
135138
kwargs...,
136139
)
137-
color = cycle_until(1:3, size(A, 1))
140+
color = cycle_range(1:3, size(A, 1))
138141
bg = BipartiteGraph(A)
139142
return RowColoringResult(A, bg, color)
140143
end

test/structured.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
using LinearAlgebra
22
using SparseMatrixColorings
3+
using SparseMatrixColorings: cycle_range
34
using Test
45

56
column_problem = ColoringProblem(; structure=:nonsymmetric, partition=:column)
67
row_problem = ColoringProblem(; structure=:nonsymmetric, partition=:row)
78

89
algo = GreedyColoringAlgorithm()
910

11+
@testset "Utils" begin
12+
@test cycle_range(2, 3) == [1, 2, 1]
13+
@test cycle_range(2, 4) == [1, 2, 1, 2]
14+
@test cycle_range(2, 5) == [1, 2, 1, 2, 1]
15+
@test cycle_range(3, 5) == [1, 2, 3, 1, 2]
16+
@test cycle_range(3, 6) == [1, 2, 3, 1, 2, 3]
17+
@test cycle_range(2, 1) == [1]
18+
@test cycle_range(3, 1) == [1]
19+
@test cycle_range(3, 2) == [1, 2]
20+
end
21+
1022
@testset "Diagonal" begin
1123
for n in (1, 2, 10, 100, 1000)
1224
A = Diagonal(rand(n))

0 commit comments

Comments
 (0)