-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsparse.jl
More file actions
352 lines (294 loc) · 9.09 KB
/
sparse.jl
File metadata and controls
352 lines (294 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
## Vector to vector
diffsquare(x::AbstractVector)::AbstractVector = diff(x) .^ 2
diffcube(x::AbstractVector)::AbstractVector = diff(x) .^ 3
function diffsquare!(y::AbstractVector, x::AbstractVector)
x1 = @view x[1:(end - 1)]
x2 = @view x[2:end]
y .= x2 .- x1
y .^= 2
return nothing
end
function diffcube!(y::AbstractVector, x::AbstractVector)
x1 = @view x[1:(end - 1)]
x2 = @view x[2:end]
y .= x2 .- x1
y .^= 3
return nothing
end
function diffsquare_jacobian(x)
n = length(x)
return spdiagm(n - 1, n, 0 => -2 * diff(x), 1 => 2 * diff(x))
end
function diffcube_jacobian(x)
n = length(x)
return spdiagm(n - 1, n, 0 => -3 * diff(x) .^ 2, 1 => 3 * diff(x) .^ 2)
end
function sparse_vec_to_vec_scenarios(x::AbstractVector)
f = diffsquare
f! = diffsquare!
y = f(x)
jac = diffsquare_jacobian(x)
scens = Scenario[]
for pl_op in (:out, :in)
append!(
scens,
[
Scenario{:jacobian,pl_op}(f, x; res1=jac),
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
],
)
end
return scens
end
## Matrix to vector
function diffsquarecube_matvec(x::AbstractMatrix)::AbstractVector
return vcat(diffsquare(vec(x)), diffcube(vec(x)))
end
function diffsquarecube_matvec!(y::AbstractVector, x::AbstractMatrix)
m, n = size(x)
diffsquare!(view(y, 1:(m * n - 1)), vec(x))
diffcube!(view(y, (m * n):(2(m * n) - 2)), vec(x))
return nothing
end
function diffsquarecube_matvec_jacobian(x)
return vcat(diffsquare_jacobian(vec(x)), diffcube_jacobian(vec(x)))
end
function sparse_mat_to_vec_scenarios(x::AbstractMatrix)
f = diffsquarecube_matvec
f! = diffsquarecube_matvec!
y = f(x)
jac = diffsquarecube_matvec_jacobian(x)
scens = Scenario[]
for pl_op in (:out, :in)
append!(
scens,
[
Scenario{:jacobian,pl_op}(f, x; res1=jac),
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
],
)
end
return scens
end
## Vector to matrix
diffsquarecube_vecmat(x::AbstractVector)::AbstractMatrix = hcat(diffsquare(x), diffcube(x))
function diffsquarecube_vecmat!(y::AbstractMatrix, x::AbstractVector)
diffsquare!(view(y, :, 1), x)
diffcube!(view(y, :, 2), x)
return nothing
end
function diffsquarecube_vecmat_jacobian(x::AbstractVector)
return vcat(diffsquare_jacobian(x), diffcube_jacobian(x))
end
function sparse_vec_to_mat_scenarios(x::AbstractVector)
f = diffsquarecube_vecmat
f! = diffsquarecube_vecmat!
y = f(x)
jac = diffsquarecube_vecmat_jacobian(vec(x))
scens = Scenario[]
for pl_op in (:out, :in)
append!(
scens,
[
Scenario{:jacobian,pl_op}(f, x; res1=jac),
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
],
)
end
return scens
end
## Matrix to matrix
function diffsquarecube_matmat(x::AbstractMatrix)::AbstractMatrix
return hcat(diffsquare(vec(x)), diffcube(vec(x)))
end
function diffsquarecube_matmat!(y::AbstractMatrix, x::AbstractMatrix)
diffsquare!(view(y, :, 1), vec(x))
diffcube!(view(y, :, 2), vec(x))
return nothing
end
function diffsquarecube_matmat_jacobian(x::AbstractMatrix)
return vcat(diffsquare_jacobian(vec(x)), diffcube_jacobian(vec(x)))
end
function sparse_mat_to_mat_scenarios(x::AbstractMatrix)
f = diffsquarecube_matmat
f! = diffsquarecube_matmat!
y = f(x)
jac = diffsquarecube_matmat_jacobian(x)
scens = Scenario[]
for pl_op in (:out, :in)
append!(
scens,
[
Scenario{:jacobian,pl_op}(f, x; res1=jac),
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
],
)
end
return scens
end
## Vector to scalar
sumdiffcube(x::AbstractVector)::Number = sum(diffcube(x))
function sumdiffcube_gradient(x::AbstractVector)
g = similar(x)
for j in eachindex(x)
if j == firstindex(x)
g[j] = -3(x[j + 1] - x[j])^2
elseif j == lastindex(x)
g[j] = +3(x[j] - x[j - 1])^2
else
g[j] = 3(x[j] - x[j - 1])^2 - 3(x[j + 1] - x[j])^2
end
end
return g
end
function sumdiffcube_hessian(x::AbstractVector)
T = eltype(x)
d = 6 * diff(x)
return spdiagm(0 => vcat(d, zero(T)) + vcat(zero(T), d), 1 => -d, -1 => -d)
end
function sparse_vec_to_num_scenarios(x::AbstractVector)
f = sumdiffcube
grad = sumdiffcube_gradient(x)
hess = sumdiffcube_hessian(x)
scens = Scenario[]
for pl_op in (:out, :in)
append!(scens, [Scenario{:hessian,pl_op}(f, x; res1=grad, res2=hess)])
end
return scens
end
## Matrix to scalar
sumdiffcube_mat(x::AbstractMatrix)::Number = sum(diffcube(vec(x)))
sumdiffcube_mat_gradient(x::AbstractMatrix) = reshape(sumdiffcube_gradient(vec(x)), size(x))
function sumdiffcube_mat_hessian(x::AbstractMatrix)
T = eltype(x)
d = 6 * diff(vec(x))
return spdiagm(0 => vcat(d, zero(T)) + vcat(zero(T), d), 1 => -d, -1 => -d)
end
function sparse_mat_to_num_scenarios(x::AbstractMatrix)
f = sumdiffcube_mat
grad = sumdiffcube_mat_gradient(x)
hess = sumdiffcube_mat_hessian(x)
scens = Scenario[]
for pl_op in (:out, :in)
append!(scens, [Scenario{:hessian,pl_op}(f, x; res1=grad, res2=hess)])
end
return scens
end
## Various matrices
function banded_matrix(::Type{T}, n, b) where {T}
@assert b <= n
pairs = [k => rand(T, n - k) for k in 0:b]
return spdiagm(n, n, pairs...)
end
### Linear map
struct SquareLinearMap{M<:AbstractMatrix}
A::M
end
function Base.show(io::IO, s::SquareLinearMap{M}) where {M}
return print(io, "SquareLinearMap{$M - $(size(s.A)) with $(mynnz(s.A)) nonzeros}")
end
function (s::SquareLinearMap)(x::AbstractArray)
return s.A * abs2.(vec(x))
end
function (s::SquareLinearMap)(y::AbstractArray, x::AbstractArray)
vec(y) .= s.A * abs2.(vec(x))
return nothing
end
function squarelinearmap_jacobian(x::AbstractArray, A::AbstractMatrix)
return 2 .* A .* transpose(vec(x))
end
function squarelinearmap_scenarios(x::AbstractVector, band_sizes)
n = length(x)
scens = Scenario[]
for A in banded_matrix.(eltype(x), n, band_sizes)
f = SquareLinearMap(A)
f! = f
y = f(x)
jac = sparse(squarelinearmap_jacobian(x, A))
for pl_op in (:out, :in)
append!(
scens,
[
Scenario{:jacobian,pl_op}(f, x; res1=jac),
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
],
)
end
end
return scens
end
### Quadratic form
struct SquareQuadraticForm{M<:AbstractMatrix}
A::M
end
function Base.show(io::IO, s::SquareQuadraticForm{M}) where {M}
return print(io, "SquareQuadraticForm{$M - $(size(s.A)) with $(mynnz(s.A)) nonzeros}")
end
function (s::SquareQuadraticForm)(x::AbstractArray)
v = abs2.(vec(x))
return dot(v, s.A, v)
end
function squarequadraticform_gradient(x::AbstractArray, A::AbstractMatrix)
g = similar(x)
for i in eachindex(g)
g[i] =
4 * A[i, i] * x[i]^3 +
2 * sum((A[i, j] + A[j, i]) * x[i] * x[j]^2 for j in eachindex(g) if j != i)
end
return g
end
function squarequadraticform_hessian(x::AbstractArray, A::AbstractMatrix)
H = similar(x, length(x), length(x))
for i in axes(H, 1), j in axes(H, 2)
if i == j
H[i, i] =
12 * A[i, i] * x[i]^2 +
2 * sum((A[i, j2] + A[j2, i]) * x[j2]^2 for j2 in axes(H, 2) if j2 != i)
else
H[i, j] = 4 * (A[i, j] + A[j, i]) * x[i] * x[j]
end
end
return H
end
function squarequadraticform_scenarios(x::AbstractVector, band_sizes)
n = length(x)
scens = Scenario[]
for A in banded_matrix.(eltype(x), n, band_sizes)
f = SquareQuadraticForm(A)
grad = squarequadraticform_gradient(x, A)
hess = sparse(squarequadraticform_hessian(x, A))
for pl_op in (:out, :in)
push!(scens, Scenario{:hessian,pl_op}(f, x; res1=grad, res2=hess))
end
end
return scens
end
## Gather
"""
sparse_scenarios()
Create a vector of [`Scenario`](@ref)s with sparse array types, focused on sparse Jacobians and Hessians.
"""
function sparse_scenarios(;
band_sizes=[5, 10, 20], include_constantified=false, include_cachified=false
)
x_6 = float.(1:6)
x_2_3 = float.(reshape(1:6, 2, 3))
x_50 = float.(range(1, 2, 50))
scens = vcat(
sparse_vec_to_vec_scenarios(x_6),
sparse_vec_to_mat_scenarios(x_6),
sparse_mat_to_vec_scenarios(x_2_3),
sparse_mat_to_mat_scenarios(x_2_3),
sparse_vec_to_num_scenarios(x_6),
sparse_mat_to_num_scenarios(x_2_3),
)
if !isempty(band_sizes)
append!(scens, squarelinearmap_scenarios(x_50, band_sizes))
append!(scens, squarequadraticform_scenarios(x_50, band_sizes))
end
final_scens = Scenario[]
append!(final_scens, scens)
include_constantified && append!(final_scens, constantify(scens))
include_cachified && append!(final_scens, cachify(scens))
return final_scens
end