|
| 1 | +transpose_respecting_similar(A::AbstractMatrix, ::Type{T}) where {T} = similar(A, T) |
| 2 | + |
| 3 | +function transpose_respecting_similar(A::Transpose, ::Type{T}) where {T} |
| 4 | + return transpose(similar(parent(A), T)) |
| 5 | +end |
| 6 | + |
| 7 | +""" |
| 8 | + color_groups(colors) |
| 9 | +
|
| 10 | +Return `groups::Vector{Vector{Int}}` such that `i ∈ groups[c]` iff `colors[i] == c`. |
| 11 | +
|
| 12 | +Assumes the colors are contiguously numbered from `1` to some `cmax`. |
| 13 | +""" |
| 14 | +function color_groups(colors::AbstractVector{<:Integer}) |
| 15 | + cmin, cmax = extrema(colors) |
| 16 | + @assert cmin == 1 |
| 17 | + groups = [Int[] for c in 1:cmax] |
| 18 | + for (k, c) in enumerate(colors) |
| 19 | + push!(groups[c], k) |
| 20 | + end |
| 21 | + return groups |
| 22 | +end |
| 23 | + |
| 24 | +## Column decompression |
| 25 | + |
| 26 | +""" |
| 27 | + decompress_columns!( |
| 28 | + A::AbstractMatrix{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 29 | + ) where {R<:Real} |
| 30 | +
|
| 31 | +Decompress the thin matrix `C` into the fat matrix `A`. |
| 32 | +
|
| 33 | +Here, `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color in `colors`. |
| 34 | +""" |
| 35 | +function decompress_columns! end |
| 36 | + |
| 37 | +#= |
| 38 | +function decompress_columns!( |
| 39 | + A::AbstractMatrix{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 40 | +) where {R<:Real} |
| 41 | + A .= zero(R) |
| 42 | + @views for j in axes(A, 2) |
| 43 | + k = colors[j] |
| 44 | + rows_j = (!iszero).(A[:, j]) |
| 45 | + copyto!(A[rows_j, j], C[rows_j, k]) |
| 46 | + end |
| 47 | + return A |
| 48 | +end |
| 49 | +=# |
| 50 | + |
| 51 | +function decompress_columns!( |
| 52 | + A::SparseMatrixCSC{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 53 | +) where {R<:Real} |
| 54 | + Anz, Arv = nonzeros(A), rowvals(A) |
| 55 | + Anz .= zero(R) |
| 56 | + @views for j in axes(A, 2) |
| 57 | + k = colors[j] |
| 58 | + nzrange_j = nzrange(A, j) |
| 59 | + rows_j = Arv[nzrange_j] |
| 60 | + copyto!(Anz[nzrange_j], C[rows_j, k]) |
| 61 | + end |
| 62 | + return A |
| 63 | +end |
| 64 | + |
| 65 | +""" |
| 66 | + decompress_columns( |
| 67 | + S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 68 | + ) where {R<:Real} |
| 69 | +
|
| 70 | +Decompress the thin matrix `C` into a new fat matrix `A` with the same sparsity pattern as `S`. |
| 71 | +
|
| 72 | +Here, `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color in `colors`. |
| 73 | +""" |
| 74 | +function decompress_columns( |
| 75 | + S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 76 | +) where {R<:Real} |
| 77 | + A = transpose_respecting_similar(S, R) |
| 78 | + return decompress_columns!(A, C, colors) |
| 79 | +end |
| 80 | + |
| 81 | +## Row decompression |
| 82 | + |
| 83 | +""" |
| 84 | + decompress_rows!( |
| 85 | + A::AbstractMatrix{R}, |
| 86 | + C::AbstractMatrix{R}, S::AbstractMatrix{Bool}, |
| 87 | + colors::AbstractVector{<:Integer} |
| 88 | + ) where {R<:Real} |
| 89 | +
|
| 90 | +Decompress the small matrix `C` into the tall matrix `A`. |
| 91 | +
|
| 92 | +Here, `C` is a compressed representation of matrix `A` obtained by summing the rows that share the same color in `colors`. |
| 93 | +""" |
| 94 | +function decompress_rows! end |
| 95 | + |
| 96 | +#= |
| 97 | +function decompress_rows!( |
| 98 | + A::AbstractMatrix{R}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 99 | +) where {R<:Real} |
| 100 | + A .= zero(R) |
| 101 | + @views for i in axes(A, 1) |
| 102 | + k = colors[i] |
| 103 | + cols_i = (!iszero).(A[i, :]) |
| 104 | + copyto!(A[i, cols_i], C[k, cols_i]) |
| 105 | + end |
| 106 | + return A |
| 107 | +end |
| 108 | +=# |
| 109 | + |
| 110 | +function decompress_rows!( |
| 111 | + A::Transpose{R,<:SparseMatrixCSC{R}}, |
| 112 | + C::AbstractMatrix{R}, |
| 113 | + colors::AbstractVector{<:Integer}, |
| 114 | +) where {R<:Real} |
| 115 | + PA = parent(A) |
| 116 | + PAnz, PArv = nonzeros(PA), rowvals(PA) |
| 117 | + PAnz .= zero(R) |
| 118 | + @views for i in axes(A, 1) |
| 119 | + k = colors[i] |
| 120 | + nzrange_i = nzrange(PA, i) |
| 121 | + cols_i = PArv[nzrange_i] |
| 122 | + copyto!(PAnz[nzrange_i], C[k, cols_i]) |
| 123 | + end |
| 124 | + return A |
| 125 | +end |
| 126 | + |
| 127 | +""" |
| 128 | + decompress_rows( |
| 129 | + S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 130 | + ) where {R<:Real} |
| 131 | +
|
| 132 | +Decompress the small matrix `C` into a new tall matrix `A` with the same sparsity pattern as `S`. |
| 133 | +
|
| 134 | +Here, `C` is a compressed representation of matrix `A` obtained by summing the rows that share the same color in `colors`. |
| 135 | +""" |
| 136 | +function decompress_rows( |
| 137 | + S::AbstractMatrix{Bool}, C::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 138 | +) where {R<:Real} |
| 139 | + A = transpose_respecting_similar(S, R) |
| 140 | + return decompress_rows!(A, C, colors) |
| 141 | +end |
0 commit comments