|
8 | 8 | color::AbstractVector{<:Integer} |
9 | 9 | ) where {R<:Real} |
10 | 10 |
|
11 | | -Decompress the thin matrix `B` into the fat matrix `A` which must have the same sparsity pattern as `S`. |
| 11 | +Decompress the narrow matrix `B` into the wide matrix `A` which must have the same sparsity pattern as `S`. |
12 | 12 |
|
13 | 13 | Here, `color` is a column coloring of `S`, while `B` is a compressed representation of matrix `A` obtained by summing the columns that share the same color. |
14 | 14 | """ |
|
63 | 63 | color::AbstractVector{<:Integer} |
64 | 64 | ) where {R<:Real} |
65 | 65 |
|
66 | | -Decompress the thin matrix `B` into a new fat matrix `A` with the same sparsity pattern as `S`. |
| 66 | +Decompress the narrow matrix `B` into a new wide matrix `A` with the same sparsity pattern as `S`. |
67 | 67 |
|
68 | 68 | Here, `color` is a column coloring of `S`, while `B` is a compressed representation of matrix `A` obtained by summing the columns that share the same color. |
69 | 69 | """ |
@@ -158,16 +158,19 @@ end |
158 | 158 | A::AbstractMatrix{R}, |
159 | 159 | S::AbstractMatrix{Bool}, |
160 | 160 | B::AbstractMatrix{R}, |
161 | | - color::AbstractVector{<:Integer} |
| 161 | + color::AbstractVector{<:Integer}, |
| 162 | + [star_set::StarSet], |
162 | 163 | ) where {R<:Real} |
163 | 164 |
|
164 | | -Decompress the thin matrix `B` into the symmetric matrix `A` which must have the same sparsity pattern as `S`. |
| 165 | +Decompress the narrow matrix `B` into the symmetric matrix `A` which must have the same sparsity pattern as `S`. |
165 | 166 |
|
166 | 167 | Here, `color` is a symmetric coloring of `S`, while `B` is a compressed representation of matrix `A` obtained by summing the columns that share the same color. |
167 | 168 |
|
| 169 | +Decompression is faster when a [`StarSet`](@ref) is also provided. |
| 170 | +
|
168 | 171 | # References |
169 | 172 |
|
170 | | -> [_Efficient Computation of Sparse Hessians Using Coloring and Automatic Differentiation_](https://pubsonline.informs.org/doi/abs/10.1287/ijoc.1080.0286), Gebremedhin et al. (2009), Figure 2 |
| 173 | +> [_Efficient Computation of Sparse Hessians Using Coloring and Automatic Differentiation_](https://pubsonline.informs.org/doi/abs/10.1287/ijoc.1080.0286), Gebremedhin et al. (2009), Figures 2 and 3 |
171 | 174 | """ |
172 | 175 | function decompress_symmetric! end |
173 | 176 |
|
@@ -201,35 +204,96 @@ function decompress_symmetric!( |
201 | 204 | return A |
202 | 205 | end |
203 | 206 |
|
| 207 | +function decompress_symmetric!( |
| 208 | + A::AbstractMatrix{R}, |
| 209 | + S::AbstractMatrix{Bool}, |
| 210 | + B::AbstractMatrix{R}, |
| 211 | + color::AbstractVector{<:Integer}, |
| 212 | + star_set::StarSet, |
| 213 | +) where {R<:Real} |
| 214 | + @compat (; star, hub) = star_set |
| 215 | + checksquare(A) |
| 216 | + if !same_sparsity_pattern(A, S) |
| 217 | + throw(DimensionMismatch("`A` and `S` must have the same sparsity pattern.")) |
| 218 | + end |
| 219 | + A .= zero(R) |
| 220 | + for i in axes(A, 1) |
| 221 | + if !iszero(S[i, i]) |
| 222 | + A[i, i] = B[i, color[i]] |
| 223 | + end |
| 224 | + end |
| 225 | + for ((i, j), star_id) in pairs(star) |
| 226 | + i == j && continue |
| 227 | + h = hub[star_id] |
| 228 | + if h == 0 |
| 229 | + # pick arbitrary hub |
| 230 | + h = i |
| 231 | + end |
| 232 | + if h == j |
| 233 | + # i is the spoke |
| 234 | + A[i, j] = A[j, i] = B[i, color[h]] |
| 235 | + elseif h == i |
| 236 | + # j is the spoke |
| 237 | + A[i, j] = A[j, i] = B[j, color[h]] |
| 238 | + end |
| 239 | + end |
| 240 | + return A |
| 241 | +end |
| 242 | + |
204 | 243 | function decompress_symmetric!( |
205 | 244 | A::Symmetric{R}, |
206 | 245 | S::AbstractMatrix{Bool}, |
207 | 246 | B::AbstractMatrix{R}, |
208 | | - colors::AbstractVector{<:Integer}, |
| 247 | + color::AbstractVector{<:Integer}, |
| 248 | +) where {R<:Real} |
| 249 | + # requires parent decompression to handle both upper and lower triangles |
| 250 | + decompress_symmetric!(parent(A), S, B, color) |
| 251 | + return A |
| 252 | +end |
| 253 | + |
| 254 | +function decompress_symmetric!( |
| 255 | + A::Symmetric{R}, |
| 256 | + S::AbstractMatrix{Bool}, |
| 257 | + B::AbstractMatrix{R}, |
| 258 | + color::AbstractVector{<:Integer}, |
| 259 | + star_set::StarSet, |
209 | 260 | ) where {R<:Real} |
210 | 261 | # requires parent decompression to handle both upper and lower triangles |
211 | | - decompress_symmetric!(parent(A), S, B, colors) |
| 262 | + decompress_symmetric!(parent(A), S, B, color, star_set) |
212 | 263 | return A |
213 | 264 | end |
214 | 265 |
|
215 | 266 | """ |
216 | 267 | decompress_symmetric( |
217 | 268 | S::AbstractMatrix{Bool}, |
218 | 269 | B::AbstractMatrix{R}, |
219 | | - colors::AbstractVector{<:Integer} |
| 270 | + color::AbstractVector{<:Integer}, |
| 271 | + [star_set::StarSet], |
220 | 272 | ) where {R<:Real} |
221 | 273 |
|
222 | | -Decompress the thin matrix `B` into a new symmetric matrix `A` with the same sparsity pattern as `S`. |
| 274 | +Decompress the narrow matrix `B` into a new symmetric matrix `A` with the same sparsity pattern as `S`. |
223 | 275 |
|
224 | | -Here, `colors` is a symmetric coloring of `S`, while `B` is a compressed representation of matrix `A` obtained by summing the columns that share the same color. |
| 276 | +Here, `color` is a symmetric coloring of `S`, while `B` is a compressed representation of matrix `A` obtained by summing the columns that share the same color. |
| 277 | +
|
| 278 | +Decompression is faster when a [`StarSet`](@ref) is also provided. |
225 | 279 |
|
226 | 280 | # References |
227 | 281 |
|
228 | | -> [_Efficient Computation of Sparse Hessians Using Coloring and Automatic Differentiation_](https://pubsonline.informs.org/doi/abs/10.1287/ijoc.1080.0286), Gebremedhin et al. (2009) |
| 282 | +> [_Efficient Computation of Sparse Hessians Using Coloring and Automatic Differentiation_](https://pubsonline.informs.org/doi/abs/10.1287/ijoc.1080.0286), Gebremedhin et al. (2009), Figures 2 and 3 |
229 | 283 | """ |
230 | 284 | function decompress_symmetric( |
231 | | - S::AbstractMatrix{Bool}, B::AbstractMatrix{R}, colors::AbstractVector{<:Integer} |
| 285 | + S::AbstractMatrix{Bool}, B::AbstractMatrix{R}, color::AbstractVector{<:Integer} |
| 286 | +) where {R<:Real} |
| 287 | + A = respectful_similar(S, R) |
| 288 | + return decompress_symmetric!(A, S, B, color) |
| 289 | +end |
| 290 | + |
| 291 | +function decompress_symmetric( |
| 292 | + S::AbstractMatrix{Bool}, |
| 293 | + B::AbstractMatrix{R}, |
| 294 | + color::AbstractVector{<:Integer}, |
| 295 | + star_set::StarSet, |
232 | 296 | ) where {R<:Real} |
233 | 297 | A = respectful_similar(S, R) |
234 | | - return decompress_symmetric!(A, S, B, colors) |
| 298 | + return decompress_symmetric!(A, S, B, color, star_set) |
235 | 299 | end |
0 commit comments