Skip to content

Commit d267150

Browse files
committed
Fix sparse and complex
1 parent 12d98ed commit d267150

1 file changed

Lines changed: 60 additions & 13 deletions

File tree

  • DifferentiationInterfaceTest/src/scenarios

DifferentiationInterfaceTest/src/scenarios/sparse.jl

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ function sparse_vec_to_vec_scenarios(x::AbstractVector)
3434
f! = diffsquare!
3535
y = f(x)
3636
jac = diffsquare_jacobian(x)
37+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
3738

3839
scens = Scenario[]
3940
for pl_op in (:out, :in)
4041
append!(
4142
scens,
4243
[
43-
Scenario{:jacobian,pl_op}(f, x; res1=jac),
44-
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
44+
Scenario{:jacobian,pl_op}(
45+
f, x; prep_args=(; x=x_prep, contexts=()), res1=jac
46+
),
47+
Scenario{:jacobian,pl_op}(
48+
f!, y, x; prep_args=(; y=zero(y), x=x_prep, contexts=()), res1=jac
49+
),
4550
],
4651
)
4752
end
@@ -70,14 +75,19 @@ function sparse_mat_to_vec_scenarios(x::AbstractMatrix)
7075
f! = diffsquarecube_matvec!
7176
y = f(x)
7277
jac = diffsquarecube_matvec_jacobian(x)
78+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
7379

7480
scens = Scenario[]
7581
for pl_op in (:out, :in)
7682
append!(
7783
scens,
7884
[
79-
Scenario{:jacobian,pl_op}(f, x; res1=jac),
80-
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
85+
Scenario{:jacobian,pl_op}(
86+
f, x; prep_args=(; x=x_prep, contexts=()), res1=jac
87+
),
88+
Scenario{:jacobian,pl_op}(
89+
f!, y, x; prep_args=(; y=zero(y), x=x_prep, contexts=()), res1=jac
90+
),
8191
],
8292
)
8393
end
@@ -103,14 +113,19 @@ function sparse_vec_to_mat_scenarios(x::AbstractVector)
103113
f! = diffsquarecube_vecmat!
104114
y = f(x)
105115
jac = diffsquarecube_vecmat_jacobian(vec(x))
116+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
106117

107118
scens = Scenario[]
108119
for pl_op in (:out, :in)
109120
append!(
110121
scens,
111122
[
112-
Scenario{:jacobian,pl_op}(f, x; res1=jac),
113-
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
123+
Scenario{:jacobian,pl_op}(
124+
f, x; prep_args=(; x=x_prep, contexts=()), res1=jac
125+
),
126+
Scenario{:jacobian,pl_op}(
127+
f!, y, x; prep_args=(; y=zero(y), x=x_prep, contexts=()), res1=jac
128+
),
114129
],
115130
)
116131
end
@@ -138,14 +153,19 @@ function sparse_mat_to_mat_scenarios(x::AbstractMatrix)
138153
f! = diffsquarecube_matmat!
139154
y = f(x)
140155
jac = diffsquarecube_matmat_jacobian(x)
156+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
141157

142158
scens = Scenario[]
143159
for pl_op in (:out, :in)
144160
append!(
145161
scens,
146162
[
147-
Scenario{:jacobian,pl_op}(f, x; res1=jac),
148-
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
163+
Scenario{:jacobian,pl_op}(
164+
f, x; prep_args=(; x=x_prep, contexts=()), res1=jac
165+
),
166+
Scenario{:jacobian,pl_op}(
167+
f!, y, x; prep_args=(; y=zero(y), x=x_prep, contexts=()), res1=jac
168+
),
149169
],
150170
)
151171
end
@@ -180,10 +200,18 @@ function sparse_vec_to_num_scenarios(x::AbstractVector)
180200
f = sumdiffcube
181201
grad = sumdiffcube_gradient(x)
182202
hess = sumdiffcube_hessian(x)
203+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
183204

184205
scens = Scenario[]
185206
for pl_op in (:out, :in)
186-
append!(scens, [Scenario{:hessian,pl_op}(f, x; res1=grad, res2=hess)])
207+
append!(
208+
scens,
209+
[
210+
Scenario{:hessian,pl_op}(
211+
f, x; prep_args=(; x=x_prep, contexts=()), res1=grad, res2=hess
212+
),
213+
],
214+
)
187215
end
188216
return scens
189217
end
@@ -204,10 +232,18 @@ function sparse_mat_to_num_scenarios(x::AbstractMatrix)
204232
f = sumdiffcube_mat
205233
grad = sumdiffcube_mat_gradient(x)
206234
hess = sumdiffcube_mat_hessian(x)
235+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
207236

208237
scens = Scenario[]
209238
for pl_op in (:out, :in)
210-
append!(scens, [Scenario{:hessian,pl_op}(f, x; res1=grad, res2=hess)])
239+
append!(
240+
scens,
241+
[
242+
Scenario{:hessian,pl_op}(
243+
f, x; prep_args=(; x=x_prep, contexts=()), res1=grad, res2=hess
244+
),
245+
],
246+
)
211247
end
212248
return scens
213249
end
@@ -251,12 +287,17 @@ function squarelinearmap_scenarios(x::AbstractVector, band_sizes)
251287
f! = f
252288
y = f(x)
253289
jac = sparse(squarelinearmap_jacobian(x, A))
290+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
254291
for pl_op in (:out, :in)
255292
append!(
256293
scens,
257294
[
258-
Scenario{:jacobian,pl_op}(f, x; res1=jac),
259-
Scenario{:jacobian,pl_op}(f!, y, x; res1=jac),
295+
Scenario{:jacobian,pl_op}(
296+
f, x; prep_args=(; x=x_prep, contexts=()), res1=jac
297+
),
298+
Scenario{:jacobian,pl_op}(
299+
f!, y, x; prep_args=(; y=zero(y), x=x_prep, contexts=()), res1=jac
300+
),
260301
],
261302
)
262303
end
@@ -306,12 +347,18 @@ end
306347
function squarequadraticform_scenarios(x::AbstractVector, band_sizes)
307348
n = length(x)
308349
scens = Scenario[]
350+
x_prep = reshape(eltype(x).(1:length(x)) .^ 3, size(x))
309351
for A in banded_matrix.(eltype(x), n, band_sizes)
310352
f = SquareQuadraticForm(A)
311353
grad = squarequadraticform_gradient(x, A)
312354
hess = sparse(squarequadraticform_hessian(x, A))
313355
for pl_op in (:out, :in)
314-
push!(scens, Scenario{:hessian,pl_op}(f, x; res1=grad, res2=hess))
356+
push!(
357+
scens,
358+
Scenario{:hessian,pl_op}(
359+
f, x; prep_args=(; x=x_prep, contexts=()), res1=grad, res2=hess
360+
),
361+
)
315362
end
316363
end
317364
return scens

0 commit comments

Comments
 (0)