-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheck.jl
More file actions
574 lines (521 loc) · 20.4 KB
/
check.jl
File metadata and controls
574 lines (521 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
function proper_length_coloring(
A::AbstractMatrix, color::AbstractVector{<:Integer}; verbose::Bool=false
)
m, n = size(A)
if length(color) != n
if verbose
@warn "$(length(color)) colors provided for $n columns."
end
return false
end
return true
end
function proper_length_bicoloring(
A::AbstractMatrix,
row_color::AbstractVector{<:Integer},
column_color::AbstractVector{<:Integer};
verbose::Bool=false,
)
m, n = size(A)
bool = true
if length(row_color) != m
if verbose
@warn "$(length(row_color)) colors provided for $m rows."
end
bool = false
end
if length(column_color) != n
if verbose
@warn "$(length(column_color)) colors provided for $n columns."
end
bool = false
end
return bool
end
"""
structurally_orthogonal_columns(
A::AbstractMatrix, color::AbstractVector{<:Integer};
verbose=false
)
Return `true` if coloring the columns of the matrix `A` with the vector `color` results in a partition that is structurally orthogonal, and `false` otherwise.
A partition of the columns of a matrix `A` is _structurally orthogonal_ if, for every nonzero element `A[i, j]`, the group containing column `A[:, j]` has no other column with a nonzero in row `i`.
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
# References
> [_What Color Is Your Jacobian? Graph Coloring for Computing Derivatives_](https://epubs.siam.org/doi/10.1137/S0036144504444711), Gebremedhin et al. (2005)
"""
function structurally_orthogonal_columns(
A::AbstractMatrix, color::AbstractVector{<:Integer}; verbose::Bool=false
)
if !proper_length_coloring(A, color; verbose)
return false
end
group = group_by_color(color)
for (c, g) in enumerate(group)
Ag = view(A, :, g)
nonzeros_per_row = only(eachcol(count(!iszero, Ag; dims=2)))
max_nonzeros_per_row, i = findmax(nonzeros_per_row)
if max_nonzeros_per_row > 1
if verbose
incompatible_columns = g[findall(!iszero, view(Ag, i, :))]
@warn "In color $c, columns $incompatible_columns all have nonzeros in row $i."
end
return false
end
end
return true
end
"""
symmetrically_orthogonal_columns(
A::AbstractMatrix, color::AbstractVector{<:Integer};
verbose=false
)
Return `true` if coloring the columns of the symmetric matrix `A` with the vector `color` results in a partition that is symmetrically orthogonal, and `false` otherwise.
A partition of the columns of a symmetric matrix `A` is _symmetrically orthogonal_ if, for every nonzero element `A[i, j]`, either of the following statements holds:
1. the group containing the column `A[:, j]` has no other column with a nonzero in row `i`
2. the group containing the column `A[:, i]` has no other column with a nonzero in row `j`
It is equivalent to a __star coloring__.
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
# References
> [_On the Estimation of Sparse Hessian Matrices_](https://doi.org/10.1137/0716078), Powell and Toint (1979)
> [_Estimation of sparse hessian matrices and graph coloring problems_](https://doi.org/10.1007/BF02612334), Coleman and Moré (1984)
> [_What Color Is Your Jacobian? Graph Coloring for Computing Derivatives_](https://epubs.siam.org/doi/10.1137/S0036144504444711), Gebremedhin et al. (2005)
"""
function symmetrically_orthogonal_columns(
A::AbstractMatrix, color::AbstractVector{<:Integer}; verbose::Bool=false
)
checksquare(A)
if !proper_length_coloring(A, color; verbose)
return false
end
issymmetric(A) || return false
group = group_by_color(color)
for i in axes(A, 1), j in axes(A, 2)
iszero(A[i, j]) && continue
ci, cj = color[i], color[j]
check = _bilateral_check(
A; i, j, ci, cj, row_group=group, column_group=group, verbose
)
!check && return false
end
return true
end
"""
structurally_biorthogonal(
A::AbstractMatrix, row_color::AbstractVector{<:Integer}, column_color::AbstractVector{<:Integer};
verbose=false
)
Return `true` if bicoloring of the matrix `A` with the vectors `row_color` and `column_color` results in a bipartition that is structurally biorthogonal, and `false` otherwise.
A bipartition of the rows and columns of a matrix `A` is _structurally biorthogonal_ if, for every nonzero element `A[i, j]`, either of the following statements holds:
1. the group containing the column `A[:, j]` has no other column with a nonzero in row `i`
2. the group containing the row `A[i, :]` has no other row with a nonzero in column `j`
It is equivalent to a __star bicoloring__.
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
"""
function structurally_biorthogonal(
A::AbstractMatrix,
row_color::AbstractVector{<:Integer},
column_color::AbstractVector{<:Integer};
verbose::Bool=false,
)
if !proper_length_bicoloring(A, row_color, column_color; verbose)
return false
end
row_group = group_by_color(row_color)
column_group = group_by_color(column_color)
for i in axes(A, 1), j in axes(A, 2)
iszero(A[i, j]) && continue
ci, cj = row_color[i], column_color[j]
check = _bilateral_check(A; i, j, ci, cj, row_group, column_group, verbose)
!check && return false
end
return true
end
function _bilateral_check(
A::AbstractMatrix;
i::Integer,
j::Integer,
ci::Integer,
cj::Integer,
row_group::AbstractVector,
column_group::AbstractVector,
verbose::Bool,
)
if ci == 0 && cj == 0
if verbose
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- Row color ci=$ci is neutral.
- Column color cj=$cj is neutral.
"""
end
return false
elseif ci == 0 && cj != 0
gj = column_group[cj]
A_gj_rowi = view(A, i, gj)
nonzeros_gj_rowi = count(!iszero, A_gj_rowi)
if nonzeros_gj_rowi > 1
if verbose
gj_incompatible_columns = gj[findall(!iszero, A_gj_rowi)]
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- Row color ci=$ci is neutral.
- In column color cj=$cj, columns $gj_incompatible_columns all have nonzeros in row i=$i.
"""
end
return false
end
elseif ci != 0 && cj == 0
gi = row_group[ci]
A_gi_columnj = view(A, gi, j)
nonzeros_gi_columnj = count(!iszero, A_gi_columnj)
if nonzeros_gi_columnj > 1
if verbose
gi_incompatible_rows = gi[findall(!iszero, A_gi_columnj)]
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- In row color ci=$ci, rows $gi_incompatible_rows all have nonzeros in column j=$j.
- Column color cj=$cj is neutral.
"""
end
return false
end
else
gi, gj = row_group[ci], column_group[cj]
A_gj_rowi = view(A, i, gj)
A_gi_columnj = view(A, gi, j)
nonzeros_gj_rowi = count(!iszero, A_gj_rowi)
nonzeros_gi_columnj = count(!iszero, A_gi_columnj)
if nonzeros_gj_rowi > 1 && nonzeros_gi_columnj > 1
if verbose
gj_incompatible_columns = gj[findall(!iszero, A_gj_rowi)]
gi_incompatible_rows = gi[findall(!iszero, A_gi_columnj)]
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- In row color ci=$ci, rows $gi_incompatible_rows all have nonzeros in column j=$j.
- In column color cj=$cj, columns $gj_incompatible_columns all have nonzeros in row i=$i.
"""
end
return false
end
end
return true
end
"""
directly_recoverable_columns(
A::AbstractMatrix, color::AbstractVector{<:Integer}
verbose=false
)
Return `true` if coloring the columns of the symmetric matrix `A` with the vector `color` results in a column-compressed representation that preserves every unique value, thus making direct recovery possible.
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
# References
> [_What Color Is Your Jacobian? Graph Coloring for Computing Derivatives_](https://epubs.siam.org/doi/10.1137/S0036144504444711), Gebremedhin et al. (2005)
"""
function directly_recoverable_columns(
A::AbstractMatrix, color::AbstractVector{<:Integer}; verbose::Bool=false
)
if !proper_length_coloring(A, color; verbose)
return false
end
group = group_by_color(color)
B = if isempty(group)
similar(A, size(A, 1), 0)
else
stack(group; dims=2) do g
dropdims(sum(A[:, g]; dims=2); dims=2)
end
end
A_unique = Set(unique(A))
B_unique = Set(unique(B))
if !issubset(A_unique, push!(B_unique, zero(eltype(B))))
if verbose
@warn "Coefficients $(sort(collect(setdiff(A_unique, B_unique)))) are not directly recoverable."
return false
end
return false
end
return true
end
"""
substitutable_columns(
A::AbstractMatrix, rank_nonzeros::AbstractMatrix, color::AbstractVector{<:Integer};
verbose=false
)
Return `true` if coloring the columns of the symmetric matrix `A` with the vector `color` results in a partition that is substitutable, and `false` otherwise.
For all nonzeros `A[i, j]`, `rank_nonzeros[i, j]` provides its rank of recovery.
A partition of the columns of a symmetric matrix `A` is _substitutable_ if, for every nonzero element `A[i, j]`, either of the following statements holds:
1. the group containing the column `A[:, j]` has all nonzeros in row `i` ordered before `A[i, j]`
2. the group containing the column `A[:, i]` has all nonzeros in row `j` ordered before `A[i, j]`
It is equivalent to an __acyclic coloring__.
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
# References
> [_On the Estimation of Sparse Hessian Matrices_](https://doi.org/10.1137/0716078), Powell and Toint (1979)
> [_The Cyclic Coloring Problem and Estimation of Sparse Hessian Matrices_](https://doi.org/10.1137/0607026), Coleman and Cai (1986)
> [_What Color Is Your Jacobian? Graph Coloring for Computing Derivatives_](https://epubs.siam.org/doi/10.1137/S0036144504444711), Gebremedhin et al. (2005)
"""
function substitutable_columns(
A::AbstractMatrix,
rank_nonzeros::AbstractMatrix,
color::AbstractVector{<:Integer};
verbose::Bool=false,
)
checksquare(A)
if !proper_length_coloring(A, color; verbose)
return false
end
issymmetric(A) || return false
group = group_by_color(color)
for i in axes(A, 1), j in axes(A, 2)
iszero(A[i, j]) && continue
ci, cj = color[i], color[j]
check = _substitutable_check(
A, rank_nonzeros; i, j, ci, cj, row_group=group, column_group=group, verbose
)
!check && return false
end
return true
end
"""
substitutable_bidirectional(
A::AbstractMatrix, rank_nonzeros::AbstractMatrix, row_color::AbstractVector{<:Integer}, column_color::AbstractVector{<:Integer};
verbose=false
)
Return `true` if bicoloring of the matrix `A` with the vectors `row_color` and `column_color` results in a bipartition that is substitutable, and `false` otherwise.
For all nonzeros `A[i, j]`, `rank_nonzeros[i, j]` provides its rank of recovery.
A bipartition of the rows and columns of a matrix `A` is _substitutable_ if, for every nonzero element `A[i, j]`, either of the following statements holds:
1. the group containing the column `A[:, j]` has all nonzeros in row `i` ordered before `A[i, j]`
2. the group containing the row `A[i, :]` has all nonzeros in column `j` ordered before `A[i, j]`
It is equivalent to an __acyclic bicoloring__.
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
"""
function substitutable_bidirectional(
A::AbstractMatrix,
rank_nonzeros::AbstractMatrix,
row_color::AbstractVector{<:Integer},
column_color::AbstractVector{<:Integer};
verbose::Bool=false,
)
if !proper_length_bicoloring(A, row_color, column_color; verbose)
return false
end
row_group = group_by_color(row_color)
column_group = group_by_color(column_color)
for i in axes(A, 1), j in axes(A, 2)
iszero(A[i, j]) && continue
ci, cj = row_color[i], column_color[j]
check = _substitutable_check(
A, rank_nonzeros; i, j, ci, cj, row_group, column_group, verbose
)
!check && return false
end
return true
end
function _substitutable_check(
A::AbstractMatrix,
rank_nonzeros::AbstractMatrix;
i::Integer,
j::Integer,
ci::Integer,
cj::Integer,
row_group::AbstractVector,
column_group::AbstractVector,
verbose::Bool,
)
order_ij = rank_nonzeros[i, j]
k_row = 0
k_column = 0
if ci != 0
for k in row_group[ci]
(k == i) && continue
if !iszero(A[k, j])
order_kj = rank_nonzeros[k, j]
@assert !iszero(order_kj)
if order_kj > order_ij
k_row = k
end
end
end
end
if cj != 0
for k in column_group[cj]
(k == j) && continue
if !iszero(A[i, k])
order_ik = rank_nonzeros[i, k]
@assert !iszero(order_ik)
if order_ik > order_ij
k_column = k
end
end
end
end
if ci == 0 && cj == 0
if verbose
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- Row color ci=$ci is neutral.
- Column color cj=$cj is neutral.
"""
end
return false
elseif ci == 0 && !iszero(k_column)
if verbose
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- Row color ci=$ci is neutral.
- For the column $k_column in column color cj=$cj, A[$i, $k_column] is ordered after A[$i, $j].
"""
end
return false
elseif cj == 0 && !iszero(k_row)
if verbose
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- For the row $k_row in row color ci=$ci, A[$k_row, $j] is ordered after A[$i, $j].
- Column color cj=$cj is neutral.
"""
end
return false
elseif !iszero(k_row) && !iszero(k_column)
if verbose
@warn """
For coefficient (i=$i, j=$j) with colors (ci=$ci, cj=$cj):
- For the row $k_row in row color ci=$ci, A[$k_row, $j] is ordered after A[$i, $j].
- For the column $k_column in column color cj=$cj, A[$i, $k_column] is ordered after A[$i, $j].
"""
end
return false
end
return true
end
"""
valid_dynamic_order(g::AdjacencyGraph, π::AbstractVector{<:Integer}, order::DynamicDegreeBasedOrder)
valid_dynamic_order(bg::AdjacencyGraph, ::Val{side}, π::AbstractVector{<:Integer}, order::DynamicDegreeBasedOrder)
Check that a permutation `π` corresponds to a valid application of a [`DynamicDegreeBasedOrder`](@ref).
This is done by checking, for each ordered vertex, that its back- or forward-degree was the smallest or largest among the remaining vertices (the specifics depend on the order parameters).
!!! warning
This function is not coded with efficiency in mind, it is designed for small-scale tests.
"""
function valid_dynamic_order(
g::AdjacencyGraph,
π::AbstractVector{<:Integer},
::DynamicDegreeBasedOrder{degtype,direction},
) where {degtype,direction}
length(π) != nb_vertices(g) && return false
length(unique(π)) != nb_vertices(g) && return false
for i in eachindex(π)
vi = π[i]
yet_to_be_ordered = direction == :low2high ? π[i:end] : π[begin:i]
considered_for_degree = degtype == :back ? π[begin:(i - 1)] : π[(i + 1):end]
di = degree_in_subset(g, vi, considered_for_degree)
considered_for_degree_switched = copy(considered_for_degree)
for vj in yet_to_be_ordered
replace!(considered_for_degree_switched, vj => vi)
dj = degree_in_subset(g, vj, considered_for_degree_switched)
replace!(considered_for_degree_switched, vi => vj)
if direction == :low2high
dj > di && return false
else
dj < di && return false
end
end
end
return true
end
function valid_dynamic_order(
g::BipartiteGraph,
::Val{side},
π::AbstractVector{<:Integer},
::DynamicDegreeBasedOrder{degtype,direction},
) where {side,degtype,direction}
length(π) != nb_vertices(g, Val(side)) && return false
length(unique(π)) != nb_vertices(g, Val(side)) && return false
for i in eachindex(π)
vi = π[i]
yet_to_be_ordered = direction == :low2high ? π[i:end] : π[begin:i]
considered_for_degree = degtype == :back ? π[begin:(i - 1)] : π[(i + 1):end]
di = degree_dist2_in_subset(g, Val(side), vi, considered_for_degree)
considered_for_degree_switched = copy(considered_for_degree)
for vj in yet_to_be_ordered
replace!(considered_for_degree_switched, vj => vi)
dj = degree_dist2_in_subset(g, Val(side), vj, considered_for_degree_switched)
replace!(considered_for_degree_switched, vi => vj)
if direction == :low2high
dj > di && return false
else
dj < di && return false
end
end
end
return true
end
"""
rank_nonzeros_from_trees(result::TreeSetColoringResult)
rank_nonzeros_from_trees(result::BicoloringResult)
Construct a sparse matrix `rank_nonzeros` that assigns a unique recovery rank
to each nonzero coefficient associated with an acyclic coloring or bicoloring.
For every nonzero entry `result.A[i, j]`, `rank_nonzeros[i, j]` stores a strictly positive
integer representing the order in which this coefficient is recovered during the decompression.
A larger value means the coefficient is recovered later.
This ranking is used to test substitutability (acyclicity) of colorings:
for a given nonzero `result.A[i, j]`, the ranks allow one to check whether all competing
nonzeros in the same row or column (within a color group) are recovered before it.
"""
function rank_nonzeros_from_trees end
function rank_nonzeros_from_trees(result::TreeSetColoringResult)
(; A, ag, reverse_bfs_orders, diagonal_indices, tree_edge_indices, nt) = result
(; S) = ag
m, n = size(A)
nnzS = nnz(S)
nzval = zeros(Int, nnzS)
rank_nonzeros = SparseMatrixCSC(n, n, S.colptr, S.rowval, nzval)
counter = 0
for i in diagonal_indices
counter += 1
rank_nonzeros[i, i] = counter
end
for k in 1:nt
first = tree_edge_indices[k]
last = tree_edge_indices[k + 1] - 1
for pos in first:last
(i, j) = reverse_bfs_orders[pos]
counter += 1
rank_nonzeros[i, j] = counter
rank_nonzeros[j, i] = counter
end
end
return rank_nonzeros
end
function rank_nonzeros_from_trees(result::BicoloringResult)
(; A, abg, row_color, column_color, symmetric_result, large_colptr, large_rowval) =
result
@assert symmetric_result isa TreeSetColoringResult
(; ag, reverse_bfs_orders, tree_edge_indices, nt) = symmetric_result
(; S) = ag
m, n = size(A)
nnzA = nnz(S) ÷ 2
nzval = zeros(Int, nnzA)
colptr = large_colptr[1:(n + 1)]
rowval = large_rowval[1:nnzA]
rowval .-= n
rank_nonzeros = SparseMatrixCSC(m, n, colptr, rowval, nzval)
counter = 0
for k in 1:nt
first = tree_edge_indices[k]
last = tree_edge_indices[k + 1] - 1
for pos in first:last
(i, j) = reverse_bfs_orders[pos]
counter += 1
if i > j
rank_nonzeros[i - n, j] = counter
else
rank_nonzeros[j - n, i] = counter
end
end
end
return rank_nonzeros
end