-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-sampled-Jacobian.jl
More file actions
130 lines (120 loc) · 3.55 KB
/
api-sampled-Jacobian.jl
File metadata and controls
130 lines (120 loc) · 3.55 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
function NLPModels.jprod_residual!(
nls::SampledNLSModel,
x::AbstractVector,
v::AbstractVector,
Jv::AbstractVector,
)
NLPModels.@lencheck nls.meta.nvar x v
NLPModels.@lencheck length(nls.sample) Jv
increment!(nls, :neval_jprod_residual)
nls.jprod_resid!(Jv, x, v; sample = nls.sample)
Jv
end
function NLPModels.jtprod_residual!(
nls::SampledNLSModel,
x::AbstractVector,
v::AbstractVector,
Jtv::AbstractVector,
)
NLPModels.@lencheck nls.meta.nvar x Jtv
NLPModels.@lencheck length(nls.sample) v
increment!(nls, :neval_jtprod_residual)
nls.jtprod_resid!(Jtv, x, v; sample = nls.sample)
Jtv
end
function NLPModels.jac_op_residual!(
nls::SampledNLSModel,
x::AbstractVector,
Jv::AbstractVector,
Jtv::AbstractVector,
)
@lencheck nls.meta.nvar x Jtv
@lencheck length(nls.sample) Jv
prod! = @closure (res, v, α, β) -> begin
jprod_residual!(nls, x, v, Jv)
if β == 0
@. res = α * Jv
else
@. res = α * Jv + β * res
end
return res
end
ctprod! = @closure (res, v, α, β) -> begin
jtprod_residual!(nls, x, v, Jtv)
if β == 0
@. res = α * Jtv
else
@. res = α * Jtv + β * res
end
return res
end
return LinearOperator{eltype(x)}(
length(nls.sample),
nls_meta(nls).nvar,
false,
false,
prod!,
ctprod!,
ctprod!,
)
end
#=function NLPModels.jac_op_residual!(
nls::SampledADNLSModel_BA,
rows::AbstractVector{<:Integer},
cols::AbstractVector{<:Integer},
vals::AbstractVector,
Jv::AbstractVector,
Jtv::AbstractVector,
)
@lencheck length(rows) rows cols vals
@lencheck 2*length(nls.sample) Jv
@lencheck nls.meta.nvar Jtv
prod! = @closure (res, v, α, β) -> begin
jprod_residual!(nls.adnls, rows, cols, vals, v, Jv)
if β == 0
@. res = α * Jv
else
@. res = α * Jv + β * res
end
return res
end
ctprod! = @closure (res, v, α, β) -> begin
jtprod_residual!(nls.adnls, rows, cols, vals, v, Jtv)
if β == 0
@. res = α * Jtv
else
@. res = α * Jtv + β * res
end
return res
end
return LinearOperator{eltype(vals)}(
2*length(nls.sample),
nls_meta(nls).nvar,
false,
false,
prod!,
ctprod!,
ctprod!,
)
end=#
function NLPModels.jac_op_residual(nls::SampledNLSModel{T, S, R, J, Jt}, x::AbstractVector{T}) where {T, S, R, J, Jt}
@lencheck nls.meta.nvar x
Jv = S(undef, length(nls.sample))
Jtv = S(undef, nls.meta.nvar)
return NLPModels.jac_op_residual!(nls, x, Jv, Jtv)
end
#function NLPModels.jac_structure_residual! end
#sp_sample must be a sample adapted to the sparse structure of the Jacobian, obtained with sp_sample(rows::AbstractVector{T}, sample::AbstractVector{<:Integer}) where {T} function
function NLPModels.jac_structure_residual(nls::SampledNLSModel{T, S, R, J, Jt}, sp_sample::AbstractVector{<:Integer}) where {T, S, R, J, Jt}
rows = Vector{Int}(undef, nls.nls_meta.nnzj)
cols = Vector{Int}(undef, nls.nls_meta.nnzj)
jac_structure_residual!(nls, rows, cols)
rows[sp_sample], cols[sp_sample]
end
#function NLPModels.jac_coord_residual! end
function jac_coord_residual(nls::SampledNLSModel{T, S, R, J, Jt}, sp_sample::AbstractVector{<:Integer}) where {T, S, R, J, Jt}
@lencheck nls.meta.nvar x
vals = S(undef, nls.nls_meta.nnzj)
jac_coord_residual!(nls, x, vals)
vals[sp_sample]
end