Skip to content

Commit ac70240

Browse files
authored
Add some functionality to the API (#7)
* Add some functionality to the API * Fix
1 parent f44549f commit ac70240

File tree

6 files changed

+99
-52
lines changed

6 files changed

+99
-52
lines changed

Project.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseMatrixColorings"
22
uuid = "0a514795-09f3-496d-8182-132a7b665d35"
33
authors = ["Guillaume Dalle <22795598+gdalle@users.noreply.github.com>"]
4-
version = "0.2.0"
4+
version = "0.3.0"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
@@ -15,7 +15,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1515
ADTypes = "1.2.1"
1616
Compat = "3,4"
1717
DocStringExtensions = "0.9"
18-
LinearAlgebra = "1"
19-
Random = "1"
20-
SparseArrays = "1"
18+
LinearAlgebra = "<0.0.1, 1"
19+
Random = "<0.0.1, 1"
20+
SparseArrays = "<0.0.1, 1"
2121
julia = "1.6"

docs/src/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ decompress_columns!
3030
decompress_columns
3131
decompress_rows!
3232
decompress_rows
33+
color_groups
3334
```
3435

3536
## Private
@@ -50,7 +51,6 @@ vertices
5051
```@docs
5152
partial_distance2_coloring
5253
star_coloring1
53-
color_groups
5454
```
5555

5656
### Testing

src/SparseMatrixColorings.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ include("decompression.jl")
3131

3232
@compat public GreedyColoringAlgorithm
3333
@compat public NaturalOrder, RandomOrder, LargestFirst
34-
@compat public decompress_columns!, decompress_columns, decompress_rows!, decompress_rows
34+
@compat public decompress_columns!, decompress_columns
35+
@compat public decompress_rows!, decompress_rows
36+
@compat public color_groups
3537

3638
export GreedyColoringAlgorithm
3739

src/decompression.jl

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,41 @@ end
2525

2626
"""
2727
decompress_columns!(
28-
A::AbstractMatrix{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
28+
A::AbstractMatrix{R},
29+
S::AbstractMatrix{Bool},
30+
C::AbstractMatrix{R},
31+
colors::AbstractVector{<:Integer}
2932
) where {R<:Real}
3033
31-
Decompress the thin matrix `C` into the fat matrix `A`.
34+
Decompress the thin matrix `C` into a fat matrix `A` with the same sparsity pattern as `S`.
3235
33-
Here, `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color in `colors`.
36+
Here, `colors` is a column coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
3437
"""
3538
function decompress_columns! end
3639

37-
#=
3840
function decompress_columns!(
39-
A::AbstractMatrix{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
41+
A::AbstractMatrix{R},
42+
S::AbstractMatrix{Bool},
43+
C::AbstractMatrix{R},
44+
colors::AbstractVector{<:Integer},
4045
) where {R<:Real}
4146
A .= zero(R)
4247
@views for j in axes(A, 2)
4348
k = colors[j]
44-
rows_j = (!iszero).(A[:, j])
49+
rows_j = map(!iszero, S[:, j])
4550
copyto!(A[rows_j, j], C[rows_j, k])
51+
A[rows_j, j] .= C[rows_j, k]
4652
end
4753
return A
4854
end
49-
=#
5055

5156
function decompress_columns!(
52-
A::SparseMatrixCSC{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
57+
A::SparseMatrixCSC{R},
58+
S::SparseMatrixCSC{Bool},
59+
C::AbstractMatrix{R},
60+
colors::AbstractVector{<:Integer},
5361
) where {R<:Real}
62+
# assume A and S have the same pattern
5463
Anz, Arv = nonzeros(A), rowvals(A)
5564
Anz .= zero(R)
5665
@views for j in axes(A, 2)
@@ -64,54 +73,60 @@ end
6473

6574
"""
6675
decompress_columns(
67-
S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
76+
S::AbstractMatrix{Bool},
77+
C::AbstractMatrix{R},
78+
colors::AbstractVector{<:Integer}
6879
) where {R<:Real}
6980
7081
Decompress the thin matrix `C` into a new fat matrix `A` with the same sparsity pattern as `S`.
7182
72-
Here, `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color in `colors`.
83+
Here, `colors` is a column coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
7384
"""
7485
function decompress_columns(
7586
S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
7687
) where {R<:Real}
7788
A = transpose_respecting_similar(S, R)
78-
return decompress_columns!(A, C, colors)
89+
return decompress_columns!(A, S, C, colors)
7990
end
8091

8192
## Row decompression
8293

8394
"""
8495
decompress_rows!(
8596
A::AbstractMatrix{R},
86-
C::AbstractMatrix{R}, S::AbstractMatrix{Bool},
97+
S::AbstractMatrix{Bool},
98+
C::AbstractMatrix{R},
8799
colors::AbstractVector{<:Integer}
88100
) where {R<:Real}
89101
90-
Decompress the small matrix `C` into the tall matrix `A`.
102+
Decompress the small matrix `C` into the tall matrix `A` with the same sparsity pattern as `S`.
91103
92-
Here, `C` is a compressed representation of matrix `A` obtained by summing the rows that share the same color in `colors`.
104+
Here, `colors` is a row coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
93105
"""
94106
function decompress_rows! end
95107

96-
#=
97108
function decompress_rows!(
98-
A::AbstractMatrix{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
109+
A::AbstractMatrix{R},
110+
S::AbstractMatrix{Bool},
111+
C::AbstractMatrix{R},
112+
colors::AbstractVector{<:Integer},
99113
) where {R<:Real}
100114
A .= zero(R)
101115
@views for i in axes(A, 1)
102116
k = colors[i]
103-
cols_i = (!iszero).(A[i, :])
117+
cols_i = map(!iszero, S[i, :])
104118
copyto!(A[i, cols_i], C[k, cols_i])
105119
end
106120
return A
107121
end
108-
=#
109122

110123
function decompress_rows!(
111124
A::Transpose{R,<:SparseMatrixCSC{R}},
125+
S::Transpose{Bool,<:SparseMatrixCSC{Bool}},
112126
C::AbstractMatrix{R},
113127
colors::AbstractVector{<:Integer},
114128
) where {R<:Real}
129+
# assume A and S have the same pattern
115130
PA = parent(A)
116131
PAnz, PArv = nonzeros(PA), rowvals(PA)
117132
PAnz .= zero(R)
@@ -126,16 +141,18 @@ end
126141

127142
"""
128143
decompress_rows(
129-
S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
144+
S::AbstractMatrix{Bool},
145+
C::AbstractMatrix{R},
146+
colors::AbstractVector{<:Integer}
130147
) where {R<:Real}
131148
132149
Decompress the small matrix `C` into a new tall matrix `A` with the same sparsity pattern as `S`.
133150
134-
Here, `C` is a compressed representation of matrix `A` obtained by summing the rows that share the same color in `colors`.
151+
Here, `colors` is a row coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
135152
"""
136153
function decompress_rows(
137154
S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer}
138155
) where {R<:Real}
139156
A = transpose_respecting_similar(S, R)
140-
return decompress_rows!(A, C, colors)
157+
return decompress_rows!(A, S, C, colors)
141158
end

test/coloring_correctness.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ using SparseMatrixColorings:
66
check_structurally_orthogonal_columns,
77
check_structurally_orthogonal_rows,
88
check_symmetrically_orthogonal
9+
using StableRNGs
910
using Test
1011

12+
rng = StableRNG(63)
13+
1114
algo = GreedyColoringAlgorithm()
1215

1316
@test startswith(string(algo), "GreedyColoringAlgorithm(")
1417

1518
@testset "Column coloring" begin
16-
for A in (sprand(Bool, 100, 200, 0.05), sprand(Bool, 200, 100, 0.05))
19+
@testset "$(typeof(A)) - $(size(A))" for A in (
20+
sprand(rng, Bool, 100, 200, 0.05),
21+
sprand(rng, Bool, 200, 100, 0.05),
22+
Matrix(sprand(rng, Bool, 100, 200, 0.05)),
23+
Matrix(sprand(rng, Bool, 200, 100, 0.05)),
24+
)
1725
column_colors = column_coloring(A, algo)
1826
@test check_structurally_orthogonal_columns(A, column_colors)
1927
@test minimum(column_colors) == 1
@@ -22,7 +30,12 @@ algo = GreedyColoringAlgorithm()
2230
end
2331

2432
@testset "Row coloring" begin
25-
for A in (sprand(Bool, 100, 200, 0.05), sprand(Bool, 200, 100, 0.05))
33+
@testset "$(typeof(A)) - $(size(A))" for A in (
34+
sprand(rng, Bool, 100, 200, 0.05),
35+
sprand(rng, Bool, 200, 100, 0.05),
36+
Matrix(sprand(rng, Bool, 100, 200, 0.05)),
37+
Matrix(sprand(rng, Bool, 200, 100, 0.05)),
38+
)
2639
row_colors = row_coloring(A, algo)
2740
@test check_structurally_orthogonal_rows(A, row_colors)
2841
@test minimum(row_colors) == 1
@@ -31,9 +44,13 @@ end
3144
end
3245

3346
@testset "Symmetric coloring" begin
34-
S = sparse(Symmetric(sprand(Bool, 100, 100, 0.05)))
35-
symmetric_colors = symmetric_coloring(S, algo)
36-
@test check_symmetrically_orthogonal(S, symmetric_colors)
37-
@test minimum(symmetric_colors) == 1
38-
@test maximum(symmetric_colors) < size(S, 2) ÷ 2
47+
@testset "$(typeof(A)) - $(size(A))" for A in (
48+
sparse(Symmetric(sprand(rng, Bool, 100, 100, 0.05))),
49+
Symmetric(Matrix(sprand(rng, Bool, 100, 100, 0.05))),
50+
)
51+
symmetric_colors = symmetric_coloring(A, algo)
52+
@test check_symmetrically_orthogonal(A, symmetric_colors)
53+
@test minimum(symmetric_colors) == 1
54+
@test maximum(symmetric_colors) < size(A, 2) ÷ 2
55+
end
3956
end

test/decompression_correctness.jl

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ADTypes: column_coloring, row_coloring, symmetric_coloring
22
using Compat
33
using SparseArrays
4+
using SparseMatrixColorings
45
using SparseMatrixColorings: color_groups, decompress_columns, decompress_rows
56
using StableRNGs
67
using Test
@@ -10,29 +11,39 @@ rng = StableRNG(63)
1011
algo = GreedyColoringAlgorithm()
1112

1213
m, n = 10, 20
13-
A = sprand(rng, Bool, m, n, 0.3)
14-
At = transpose(A)
15-
S = map(!iszero, A)
16-
St = transpose(S)
14+
15+
A0 = sprand(rng, Bool, m, n, 0.3)
16+
A1 = Matrix(A0)
17+
A0t = transpose(A0)
18+
A1t = transpose(A1)
19+
20+
S0 = map(!iszero, A0)
21+
S1 = map(!iszero, A1)
22+
S0t = transpose(S0)
23+
S1t = transpose(S1)
1724

1825
@testset "Column decompression" begin
19-
colors = column_coloring(A, algo)
20-
groups = color_groups(colors)
21-
@test length(groups[1]) > 1
22-
C = stack(groups) do group
23-
dropdims(sum(A[:, group]; dims=2); dims=2)
26+
@testset "$(typeof(A))" for (A, S) in zip((A0, A1), (S0, S1))
27+
colors = column_coloring(A, algo)
28+
groups = color_groups(colors)
29+
@test length(groups[1]) > 1
30+
C = stack(groups) do group
31+
dropdims(sum(A[:, group]; dims=2); dims=2)
32+
end
33+
A_new = decompress_columns(S, C, colors)
34+
@test A_new == A
2435
end
25-
A_new = decompress_columns(S, C, colors)
26-
@test A_new == A
2736
end
2837

2938
@testset "Row decompression" begin
30-
colors = row_coloring(At, algo)
31-
groups = color_groups(colors)
32-
@test length(groups[1]) > 1
33-
Ct = stack(groups; dims=1) do group
34-
dropdims(sum(At[group, :]; dims=1); dims=1)
39+
@testset "$(typeof(At))" for (At, St) in zip((A0t, A1t), (S0t, S1t))
40+
colors = row_coloring(At, algo)
41+
groups = color_groups(colors)
42+
@test length(groups[1]) > 1
43+
Ct = stack(groups; dims=1) do group
44+
dropdims(sum(At[group, :]; dims=1); dims=1)
45+
end
46+
At_new = decompress_rows(St, Ct, colors)
47+
@test At_new == At
3548
end
36-
At_new = decompress_rows(St, Ct, colors)
37-
@test At_new == At
3849
end

0 commit comments

Comments
 (0)