Skip to content

Commit 773b491

Browse files
committed
Add a function compute_tree_value
1 parent bbf63a2 commit 773b491

1 file changed

Lines changed: 65 additions & 101 deletions

File tree

src/decompression.jl

Lines changed: 65 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,19 @@ end
530530

531531
## TreeSetColoringResult
532532

533+
function compute_tree_value(is_star::Val{false}, B::AbstractMatrix, i::Integer, j::Integer, color::AbstractVector{<:Integer}, buffer_right_type::AbstractVector{<:Real})
534+
# The tree is not a star
535+
val = B[i, color[j]] - buffer_right_type[i]
536+
buffer_right_type[j] = buffer_right_type[j] + val
537+
return val
538+
end
539+
540+
function compute_tree_value(is_star::Val{true}, B::AbstractMatrix, i::Integer, j::Integer, color::AbstractVector{<:Integer}, buffer_right_type::AbstractVector{<:Real})
541+
# The tree is a star (trivial or non-trivial)
542+
val = B[i, color[j]]
543+
return val
544+
end
545+
533546
function decompress!(
534547
A::AbstractMatrix, B::AbstractMatrix, result::TreeSetColoringResult, uplo::Symbol=:F
535548
)
@@ -556,37 +569,28 @@ function decompress!(
556569

557570
# Recover the off-diagonal coefficients of A
558571
for k in eachindex(reverse_bfs_orders)
572+
is_star_k = is_star[k]
573+
val_is_star_k = Val(is_star_k)
574+
559575
# We need the buffer only when the tree is not a star (trivial or non-trivial)
560-
if !is_star[k]
576+
if !is_star_k
561577
# Reset the buffer to zero for all vertices in a tree (except the root)
562578
for (vertex, _) in reverse_bfs_orders[k]
563579
buffer_right_type[vertex] = zero(R)
564580
end
565581
# Reset the buffer to zero for the root vertex
566582
(_, root) = reverse_bfs_orders[k][end]
567583
buffer_right_type[root] = zero(R)
584+
end
568585

569-
# The tree is not a star
570-
for (i, j) in reverse_bfs_orders[k]
571-
val = B[i, color[j]] - buffer_right_type[i]
572-
buffer_right_type[j] = buffer_right_type[j] + val
573-
if in_triangle(i, j, uplo)
574-
A[i, j] = val
575-
end
576-
if in_triangle(j, i, uplo)
577-
A[j, i] = val
578-
end
586+
for (i, j) in reverse_bfs_orders[k]
587+
val = compute_tree_value(val_is_star_k, B, i, j, color, buffer_right_type)
588+
589+
if in_triangle(i, j, uplo)
590+
A[i, j] = val
579591
end
580-
else
581-
# The tree is a star (trivial or non-trivial)
582-
for (i, j) in reverse_bfs_orders[k]
583-
val = B[i, color[j]]
584-
if in_triangle(i, j, uplo)
585-
A[i, j] = val
586-
end
587-
if in_triangle(j, i, uplo)
588-
A[j, i] = val
589-
end
592+
if in_triangle(j, i, uplo)
593+
A[j, i] = val
590594
end
591595
end
592596
end
@@ -648,98 +652,58 @@ function decompress!(
648652

649653
# Recover the off-diagonal coefficients of A
650654
for k in eachindex(reverse_bfs_orders)
655+
is_star_k = is_star[k]
656+
val_is_star_k = Val(is_star_k)
657+
651658
# We need the buffer only when the tree is not a star (trivial or non-trivial)
652-
if !is_star[k]
659+
if !is_star_k
653660
# Reset the buffer to zero for all vertices in a tree (except the root)
654661
for (vertex, _) in reverse_bfs_orders[k]
655662
buffer_right_type[vertex] = zero(R)
656663
end
657664
# Reset the buffer to zero for the root vertex
658665
(_, root) = reverse_bfs_orders[k][end]
659666
buffer_right_type[root] = zero(R)
667+
end
660668

661-
# The tree is not a star
662-
for (i, j) in reverse_bfs_orders[k]
663-
counter += 1
664-
val = B[i, color[j]] - buffer_right_type[i]
665-
buffer_right_type[j] = buffer_right_type[j] + val
666-
667-
#! format: off
668-
# A[i,j] is in the lower triangular part of A
669-
if in_triangle(i, j, :L)
670-
# uplo = :L or uplo = :F
671-
# A[i,j] is stored at index_ij = (A.colptr[j+1] - offset_L) in A.nzval
672-
if uplo != :U
673-
nzind = A_colptr[j + 1] - lower_triangle_offsets[counter]
674-
nzA[nzind] = val
675-
end
676-
677-
# uplo = :U or uplo = :F
678-
# A[j,i] is stored at index_ji = (A.colptr[i] + offset_U) in A.nzval
679-
if uplo != :L
680-
nzind = A_colptr[i] + upper_triangle_offsets[counter]
681-
nzA[nzind] = val
682-
end
683-
684-
# A[i,j] is in the upper triangular part of A
685-
else
686-
# uplo = :U or uplo = :F
687-
# A[i,j] is stored at index_ij = (A.colptr[j] + offset_U) in A.nzval
688-
if uplo != :L
689-
nzind = A_colptr[j] + upper_triangle_offsets[counter]
690-
nzA[nzind] = val
691-
end
692-
693-
# uplo = :L or uplo = :F
694-
# A[j,i] is stored at index_ji = (A.colptr[i+1] - offset_L) in A.nzval
695-
if uplo != :U
696-
nzind = A_colptr[i + 1] - lower_triangle_offsets[counter]
697-
nzA[nzind] = val
698-
end
669+
for (i, j) in reverse_bfs_orders[k]
670+
counter += 1
671+
compute_tree_value(val_is_star_k, B, i, j, color, buffer_right_type)
672+
673+
#! format: off
674+
# A[i,j] is in the lower triangular part of A
675+
if in_triangle(i, j, :L)
676+
# uplo = :L or uplo = :F
677+
# A[i,j] is stored at index_ij = (A.colptr[j+1] - offset_L) in A.nzval
678+
if uplo != :U
679+
nzind = A_colptr[j + 1] - lower_triangle_offsets[counter]
680+
nzA[nzind] = val
699681
end
700-
#! format: on
701-
end
702-
else
703-
# The tree is a star (trivial or non-trivial)
704-
for (i, j) in reverse_bfs_orders[k]
705-
counter += 1
706-
val = B[i, color[j]]
707-
708-
#! format: off
709-
# A[i,j] is in the lower triangular part of A
710-
if in_triangle(i, j, :L)
711-
# uplo = :L or uplo = :F
712-
# A[i,j] is stored at index_ij = (A.colptr[j+1] - offset_L) in A.nzval
713-
if uplo != :U
714-
nzind = A_colptr[j + 1] - lower_triangle_offsets[counter]
715-
nzA[nzind] = val
716-
end
717-
718-
# uplo = :U or uplo = :F
719-
# A[j,i] is stored at index_ji = (A.colptr[i] + offset_U) in A.nzval
720-
if uplo != :L
721-
nzind = A_colptr[i] + upper_triangle_offsets[counter]
722-
nzA[nzind] = val
723-
end
724-
725-
# A[i,j] is in the upper triangular part of A
726-
else
727-
# uplo = :U or uplo = :F
728-
# A[i,j] is stored at index_ij = (A.colptr[j] + offset_U) in A.nzval
729-
if uplo != :L
730-
nzind = A_colptr[j] + upper_triangle_offsets[counter]
731-
nzA[nzind] = val
732-
end
733-
734-
# uplo = :L or uplo = :F
735-
# A[j,i] is stored at index_ji = (A.colptr[i+1] - offset_L) in A.nzval
736-
if uplo != :U
737-
nzind = A_colptr[i + 1] - lower_triangle_offsets[counter]
738-
nzA[nzind] = val
739-
end
682+
683+
# uplo = :U or uplo = :F
684+
# A[j,i] is stored at index_ji = (A.colptr[i] + offset_U) in A.nzval
685+
if uplo != :L
686+
nzind = A_colptr[i] + upper_triangle_offsets[counter]
687+
nzA[nzind] = val
688+
end
689+
690+
# A[i,j] is in the upper triangular part of A
691+
else
692+
# uplo = :U or uplo = :F
693+
# A[i,j] is stored at index_ij = (A.colptr[j] + offset_U) in A.nzval
694+
if uplo != :L
695+
nzind = A_colptr[j] + upper_triangle_offsets[counter]
696+
nzA[nzind] = val
697+
end
698+
699+
# uplo = :L or uplo = :F
700+
# A[j,i] is stored at index_ji = (A.colptr[i+1] - offset_L) in A.nzval
701+
if uplo != :U
702+
nzind = A_colptr[i + 1] - lower_triangle_offsets[counter]
703+
nzA[nzind] = val
740704
end
741-
#! format: on
742705
end
706+
#! format: on
743707
end
744708
end
745709
return A

0 commit comments

Comments
 (0)