You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/decompression.jl
+28-17Lines changed: 28 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ end
31
31
colors::AbstractVector{<:Integer}
32
32
) where {R<:Real}
33
33
34
-
Decompress the thin matrix `C` into a fat matrix `A` with the same sparsity pattern as `S`.
34
+
Decompress the thin matrix `C` into the fat matrix `A` which must have the same sparsity pattern as `S`.
35
35
36
36
Here, `colors` is a column coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
37
37
"""
@@ -44,11 +44,12 @@ function decompress_columns!(
44
44
colors::AbstractVector{<:Integer},
45
45
) where {R<:Real}
46
46
A .=zero(R)
47
-
@viewsfor j inaxes(A, 2)
47
+
for j inaxes(A, 2)
48
48
k = colors[j]
49
-
rows_j =map(!iszero, S[:, j])
50
-
copyto!(A[rows_j, j], C[rows_j, k])
51
-
A[rows_j, j] .= C[rows_j, k]
49
+
rows_j = (!iszero).(view(S, :, j))
50
+
Aj =view(A, rows_j, j)
51
+
Cj =view(C, rows_j, k)
52
+
copyto!(Aj, Cj)
52
53
end
53
54
return A
54
55
end
@@ -59,14 +60,18 @@ function decompress_columns!(
59
60
C::AbstractMatrix{R},
60
61
colors::AbstractVector{<:Integer},
61
62
) where {R<:Real}
62
-
# assume A and S have the same pattern
63
+
ifnnz(parent(A)) !=nnz(parent(S))
64
+
throw(DimensionMismatch("`A` and `S` must have the same sparsity pattern."))
65
+
end
63
66
Anz, Arv =nonzeros(A), rowvals(A)
64
67
Anz .=zero(R)
65
-
@viewsfor j inaxes(A, 2)
68
+
for j inaxes(A, 2)
66
69
k = colors[j]
67
70
nzrange_j =nzrange(A, j)
68
-
rows_j = Arv[nzrange_j]
69
-
copyto!(Anz[nzrange_j], C[rows_j, k])
71
+
rows_j =view(Arv, nzrange_j)
72
+
Aj =view(Anz, nzrange_j)
73
+
Cj =view(C, rows_j, k)
74
+
copyto!(Aj, Cj)
70
75
end
71
76
return A
72
77
end
@@ -99,7 +104,7 @@ end
99
104
colors::AbstractVector{<:Integer}
100
105
) where {R<:Real}
101
106
102
-
Decompress the small matrix `C` into the tall matrix `A` with the same sparsity pattern as `S`.
107
+
Decompress the small matrix `C` into the tall matrix `A` which must have the same sparsity pattern as `S`.
103
108
104
109
Here, `colors` is a row coloring of `S`, while `C` is a compressed representation of matrix `A` obtained by summing the columns that share the same color.
105
110
"""
@@ -112,10 +117,12 @@ function decompress_rows!(
112
117
colors::AbstractVector{<:Integer},
113
118
) where {R<:Real}
114
119
A .=zero(R)
115
-
@viewsfor i inaxes(A, 1)
120
+
for i inaxes(A, 1)
116
121
k = colors[i]
117
-
cols_i =map(!iszero, S[i, :])
118
-
copyto!(A[i, cols_i], C[k, cols_i])
122
+
cols_i = (!iszero).(view(S, i, :))
123
+
Ai =view(A, i, cols_i)
124
+
Ci =view(C, k, cols_i)
125
+
copyto!(Ai, Ci)
119
126
end
120
127
return A
121
128
end
@@ -126,15 +133,19 @@ function decompress_rows!(
126
133
C::AbstractMatrix{R},
127
134
colors::AbstractVector{<:Integer},
128
135
) where {R<:Real}
129
-
# assume A and S have the same pattern
136
+
ifnnz(parent(A)) !=nnz(parent(S))
137
+
throw(DimensionMismatch("`A` and `S` must have the same sparsity pattern."))
0 commit comments