@@ -175,19 +175,34 @@ function decompress(B::AbstractMatrix, result::AbstractColoringResult)
175175 return decompress! (A, B, result)
176176end
177177
178+ function decompress (
179+ B:: AbstractMatrix ,
180+ result:: AbstractColoringResult{:symmetric,:column} )
181+ A = respectful_similar (result. A, eltype (B))
182+ if A isa SparseMatrixCSC && result. uplo != :F
183+ (result. uplo == :L ) && (A = tril (A))
184+ (result. uplo == :U ) && (A = triu (A))
185+ end
186+ return decompress! (A, B, result)
187+ end
188+
178189function decompress (
179190 Br:: AbstractMatrix ,
180191 Bc:: AbstractMatrix ,
181192 result:: AbstractColoringResult{structure,:bidirectional} ,
182193) where {structure}
183194 A = respectful_similar (result. A, Base. promote_eltype (Br, Bc))
195+ if A isa SparseMatrixCSC && result. symmetric_result. uplo != :F
196+ (result. symmetric_result. uplo == :L ) && (A = tril (A))
197+ (result. symmetric_result. uplo == :U ) && (A = triu (A))
198+ end
184199 return decompress! (A, Br, Bc, result)
185200end
186201
187202"""
188203 decompress!(
189204 A::AbstractMatrix, B::AbstractMatrix,
190- result::AbstractColoringResult{_,:column/:row}, [uplo=:F]
205+ result::AbstractColoringResult{_,:column/:row},
191206 )
192207
193208 decompress!(
@@ -204,9 +219,6 @@ The out-of-place alternative is [`decompress`](@ref).
204219Compression means summing either the columns or the rows of `A` which share the same color.
205220It is done by calling [`compress`](@ref).
206221
207- For `:symmetric` coloring results (and for those only), an optional positional argument `uplo in (:U, :L, :F)` can be passed to specify which part of the matrix `A` should be updated: the Upper triangle, the Lower triangle, or the Full matrix.
208- When `A isa SparseMatrixCSC`, using the `uplo` argument requires a target matrix which only stores the relevant triangle(s).
209-
210222!!! warning
211223 For some coloring variants, the `result` object is mutated during decompression.
212224
@@ -260,7 +272,7 @@ function decompress! end
260272"""
261273 decompress_single_color!(
262274 A::AbstractMatrix, b::AbstractVector, c::Integer,
263- result::AbstractColoringResult, [uplo=:F]
275+ result::AbstractColoringResult,
264276 )
265277
266278Decompress the vector `b` corresponding to color `c` in-place into `A`, given a `:direct` coloring `result` of the sparsity pattern of `A` (it will not work with a `:substitution` coloring).
@@ -272,9 +284,6 @@ Decompress the vector `b` corresponding to color `c` in-place into `A`, given a
272284!!! warning
273285 This function will only update some coefficients of `A`, without resetting the rest to zero.
274286
275- For `:symmetric` coloring results (and for those only), an optional positional argument `uplo in (:U, :L, :F)` can be passed to specify which part of the matrix `A` should be updated: the Upper triangle, the Lower triangle, or the Full matrix.
276- When `A isa SparseMatrixCSC`, using the `uplo` argument requires a target matrix which only stores the relevant triangle(s).
277-
278287!!! warning
279288 For some coloring variants, the `result` object is mutated during decompression.
280289
@@ -446,95 +455,68 @@ end
446455# # StarSetColoringResult
447456
448457function decompress! (
449- A:: AbstractMatrix , B:: AbstractMatrix , result:: StarSetColoringResult , uplo:: Symbol = :F
450- )
451- (; ag, compressed_indices) = result
458+ A:: AbstractMatrix , B:: AbstractMatrix , result:: StarSetColoringResult )
459+ (; ag, compressed_indices, uplo) = result
452460 (; S) = ag
453461 uplo == :F && check_same_pattern (A, S)
454462 fill! (A, zero (eltype (A)))
455463
456- rvS = rowvals (S)
464+ l = 0
465+ rvS = rowvals (A)
457466 for j in axes (S, 2 )
458467 for k in nzrange (S, j)
459468 i = rvS[k]
460469 if in_triangle (i, j, uplo)
461- A[i, j] = B[compressed_indices[k]]
470+ l += 1
471+ A[i, j] = B[compressed_indices[l]]
462472 end
463473 end
464474 end
465475 return A
466476end
467477
468478function decompress_single_color! (
469- A:: AbstractMatrix ,
479+ A:: SparseMatrixCSC ,
470480 b:: AbstractVector ,
471481 c:: Integer ,
472482 result:: StarSetColoringResult ,
473- uplo:: Symbol = :F ,
474483)
475- (; ag, compressed_indices, group) = result
484+ (; ag, compressed_indices, group, uplo ) = result
476485 (; S) = ag
486+ println (uplo)
477487 uplo == :F && check_same_pattern (A, S)
478488
479489 lower_index = (c - 1 ) * S. n + 1
480490 upper_index = c * S. n
481- rvS = rowvals (S)
491+ rvA = rowvals (A)
492+ nzA = nonzeros (A)
482493 for j in group[c]
483- for k in nzrange (S , j)
484- # Check if the color c is used to recover A[i,j] / A[j,i]
494+ for k in nzrange (A , j)
495+ # Check if the color c is used to recover A[i,j]
485496 if lower_index <= compressed_indices[k] <= upper_index
486- i = rvS[k]
487- if i == j
488- # Recover the diagonal coefficients of A
489- A[i, i] = b[i]
490- else
491- # Recover the off-diagonal coefficients of A
492- if in_triangle (i, j, uplo)
493- A[i, j] = b[i]
494- end
495- if in_triangle (j, i, uplo)
496- A[j, i] = b[i]
497- end
498- end
497+ i = rvA[k]
498+ nzA[k] = b[i]
499499 end
500500 end
501501 end
502502 return A
503503end
504504
505- function decompress! (
506- A:: SparseMatrixCSC , B:: AbstractMatrix , result:: StarSetColoringResult , uplo:: Symbol = :F
507- )
508- (; ag, compressed_indices) = result
505+ function decompress! (A:: SparseMatrixCSC , B:: AbstractMatrix , result:: StarSetColoringResult )
506+ (; ag, compressed_indices, uplo) = result
509507 (; S) = ag
510508 nzA = nonzeros (A)
511- if uplo == :F
512- check_same_pattern (A, S)
513- for k in eachindex (nzA, compressed_indices)
514- nzA[k] = B[compressed_indices[k]]
515- end
516- else
517- rvS = rowvals (S)
518- l = 0 # assume A has the same pattern as the triangle
519- for j in axes (S, 2 )
520- for k in nzrange (S, j)
521- i = rvS[k]
522- if in_triangle (i, j, uplo)
523- l += 1
524- nzA[l] = B[compressed_indices[k]]
525- end
526- end
527- end
509+ uplo == :F && check_same_pattern (A, S)
510+ for k in eachindex (nzA, compressed_indices)
511+ nzA[k] = B[compressed_indices[k]]
528512 end
529513 return A
530514end
531515
532516# # TreeSetColoringResult
533517
534- function decompress! (
535- A:: AbstractMatrix , B:: AbstractMatrix , result:: TreeSetColoringResult , uplo:: Symbol = :F
536- )
537- (; ag, color, reverse_bfs_orders, tree_edge_indices, nt, buffer) = result
518+ function decompress! (A:: AbstractMatrix , B:: AbstractMatrix , result:: TreeSetColoringResult )
519+ (; ag, color, reverse_bfs_orders, tree_edge_indices, nt, diagonal_indices, buffer, uplo) = result
538520 (; S) = ag
539521 uplo == :F && check_same_pattern (A, S)
540522 R = eltype (A)
@@ -548,10 +530,8 @@ function decompress!(
548530
549531 # Recover the diagonal coefficients of A
550532 if ! augmented_graph (ag)
551- for i in axes (S, 1 )
552- if ! iszero (S[i, i])
553- A[i, i] = B[i, color[i]]
554- end
533+ for i in diagonal_indices
534+ A[i, i] = B[i, color[i]]
555535 end
556536 end
557537
@@ -590,7 +570,6 @@ function decompress!(
590570 A:: SparseMatrixCSC{R} ,
591571 B:: AbstractMatrix{R} ,
592572 result:: TreeSetColoringResult ,
593- uplo:: Symbol = :F ,
594573) where {R<: Real }
595574 (;
596575 ag,
@@ -603,6 +582,7 @@ function decompress!(
603582 lower_triangle_offsets,
604583 upper_triangle_offsets,
605584 buffer,
585+ uplo,
606586 ) = result
607587 (; S) = ag
608588 A_colptr = A. colptr
@@ -706,9 +686,8 @@ function decompress!(
706686 A:: AbstractMatrix ,
707687 B:: AbstractMatrix ,
708688 result:: LinearSystemColoringResult ,
709- uplo:: Symbol = :F ,
710689)
711- (; color, strict_upper_nonzero_inds, M_factorization, strict_upper_nonzeros_A) = result
690+ (; color, strict_upper_nonzero_inds, M_factorization, strict_upper_nonzeros_A, uplo ) = result
712691 S = result. ag. S
713692 uplo == :F && check_same_pattern (A, S)
714693
@@ -770,10 +749,20 @@ end
770749function decompress! (
771750 A:: AbstractMatrix , Br:: AbstractMatrix , Bc:: AbstractMatrix , result:: BicoloringResult
772751)
752+ (; large_colptr, large_rowval, symmetric_result) = result
773753 m, n = size (A)
774754 Br_and_Bc = _join_compressed! (result, Br, Bc)
775- A_and_Aᵀ = decompress (Br_and_Bc, result. symmetric_result)
776- copyto! (A, A_and_Aᵀ[(n + 1 ): (n + m), 1 : n]) # original matrix in bottom left corner
755+ nzval = Vector {eltype(A)} (undef, length (large_rowval))
756+ A_and_noAᵀ = SparseMatrixCSC (m + n, m + n, large_colptr, large_rowval, nzval)
757+ decompress! (A_and_noAᵀ, Br_and_Bc, symmetric_result)
758+ rvA = rowvals (A_and_noAᵀ)
759+ nzA = nonzeros (A_and_noAᵀ)
760+ for j in axes (A_and_noAᵀ, 2 )
761+ for k in nzrange (A_and_noAᵀ, j)
762+ i = rvA[k]
763+ A[i- n, j] = nzA[k]
764+ end
765+ end
777766 return A
778767end
779768
@@ -786,6 +775,6 @@ function decompress!(
786775 # pretend A is larger
787776 A_and_noAᵀ = SparseMatrixCSC (m + n, m + n, large_colptr, large_rowval, A. nzval)
788777 # decompress lower triangle only
789- decompress! (A_and_noAᵀ, Br_and_Bc, symmetric_result, :L )
778+ decompress! (A_and_noAᵀ, Br_and_Bc, symmetric_result)
790779 return A
791780end
0 commit comments