This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathjacvec_operators.jl
232 lines (212 loc) · 6.23 KB
/
jacvec_operators.jl
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
### Operator Implementation
mutable struct JacVecOperator{T,F,T1,T2,uType,P,tType,O} <:
DiffEqBase.AbstractDiffEqLinearOperator{T}
f::F
cache1::T1
cache2::T2
u::uType
p::P
t::tType
autodiff::Bool
ishermitian::Bool
opnorm::O
function JacVecOperator{T}(
f,
p = nothing,
t::Union{Nothing,Number} = nothing;
autodiff = true,
ishermitian = false,
opnorm = true,
) where {T}
p === nothing ? P = Any : P = typeof(p)
t === nothing ? tType = Any : tType = typeof(t)
new{T,typeof(f),Nothing,Nothing,Any,P,tType,typeof(opnorm)}(
f,
nothing,
nothing,
nothing,
nothing,
nothing,
autodiff,
ishermitian,
)
end
function JacVecOperator{T}(
f,
u::AbstractArray,
p = nothing,
t::Union{Nothing,Number} = nothing;
autodiff = true,
ishermitian = false,
opnorm = true,
) where {T}
if autodiff
cache1 = ForwardDiff.Dual{typeof(ForwardDiff.Tag(SparseDiffTools.DeivVecTag(),eltype(u))),eltype(u),1}.(u, ForwardDiff.Partials.(tuple.(u)))
cache2 = ForwardDiff.Dual{typeof(ForwardDiff.Tag(SparseDiffTools.DeivVecTag(),eltype(u))),eltype(u),1}.(u, ForwardDiff.Partials.(tuple.(u)))
else
cache1 = similar(u)
cache2 = similar(u)
end
p === nothing ? P = Any : P = typeof(p)
t === nothing ? tType = Any : tType = typeof(t)
new{
T,
typeof(f),
typeof(cache1),
typeof(cache2),
typeof(u),
P,
tType,
typeof(opnorm),
}(
f,
cache1,
cache2,
u,
p,
t,
autodiff,
ishermitian,
opnorm,
)
end
function JacVecOperator(f, u, args...; kwargs...)
JacVecOperator{eltype(u)}(f, u, args...; kwargs...)
end
end
LinearAlgebra.opnorm(L::JacVecOperator, p::Real = 2) = L.opnorm
LinearAlgebra.ishermitian(L::JacVecOperator) = L.ishermitian
Base.size(L::JacVecOperator) = (length(L.cache1), length(L.cache1))
Base.size(L::JacVecOperator, i::Int) = length(L.cache1)
function update_coefficients!(L::JacVecOperator, u, p, t)
L.u = u
L.p = p
L.t = t
!L.autodiff && L.cache1 !== nothing && L.f(L.cache1, L.u, L.p, L.t)
end
# Interpret the call as df/du * u
function (L::JacVecOperator)(u, p, t::Number)
update_coefficients!(L, u, p, t)
L * u
end
function (L::JacVecOperator)(du, u, p, t::Number)
update_coefficients!(L, u, p, t)
mul!(du, L, u)
end
function Base.:*(L::JacVecOperator, x::AbstractVector)
if hasmethod(L.f, typeof.((L.u, L.p, L.t)))
return vec(
L.autodiff ? auto_jacvec(_u -> L.f(_u, L.p, L.t), L.u, x) :
num_jacvec(_u -> L.f(_u, L.p, L.t), L.u, x),
)
end
return mul!(similar(vec(L.u)), L, x)
end
function LinearAlgebra.mul!(
du::AbstractVector,
L::JacVecOperator,
x::AbstractVector,
)
du = reshape(du, size(L.u))
let p = L.p, t = L.t
if L.cache1 === nothing
if L.autodiff
auto_jacvec!(du, (_du, _u) -> L.f(_du, _u, p, t), L.u, x)
else
num_jacvec!(
du,
(_du, _u) -> L.f(_du, _u, p, t),
L.u,
x;
compute_f0 = true,
)
end
else
if L.autodiff
auto_jacvec!(
du,
(_du, _u) -> L.f(_du, _u, p, t),
L.u,
x,
L.cache1,
L.cache2,
)
else
num_jacvec!(
du,
(_du, _u) -> L.f(_du, _u, p, t),
L.u,
x,
L.cache1,
L.cache2;
compute_f0 = true,
)
end
end
end
return vec(du)
end
function Base.resize!(J::JacVecOperator, i)
resize!(J.cache1, i)
resize!(J.cache2, i)
end
### AnalyticalOperator Implementation
mutable struct AnalyticalJacVecOperator{T,F,uType,P,tType,O} <:
DiffEqBase.AbstractDiffEqLinearOperator{T}
f::F
u::uType
p::P
t::tType
ishermitian::Bool
opnorm::O
function AnalyticalJacVecOperator{T}(
f,
u = nothing,
p = nothing,
t::Union{Nothing,Number} = nothing;
ishermitian = false,
opnorm = true,
) where {T}
u === nothing ? uType = Any : uType = typeof(u)
p === nothing ? P = Any : P = typeof(p)
t === nothing ? tType = Any : tType = typeof(t)
new{T,typeof(f),uType,P,tType,typeof(opnorm)}(
f,
u,
p,
t,
ishermitian,
opnorm,
)
end
function AnalyticalJacVecOperator(f, u, args...; kwargs...)
AnalyticalJacVecOperator{eltype(u)}(f, u, args...; kwargs...)
end
end
LinearAlgebra.opnorm(L::AnalyticalJacVecOperator, p::Real = 2) = L.opnorm
LinearAlgebra.ishermitian(L::AnalyticalJacVecOperator) = L.ishermitian
Base.size(L::AnalyticalJacVecOperator) = (length(L.u), length(L.u))
Base.size(L::AnalyticalJacVecOperator, i::Int) = length(L.u)
function update_coefficients!(L::AnalyticalJacVecOperator, u, p, t)
L.u = u
L.p = p
L.t = t
return L
end
# Interpret the call as df/du * u
function (L::AnalyticalJacVecOperator)(u, p, t::Number)
update_coefficients!(L, u, p, t)
L * u
end
function (L::AnalyticalJacVecOperator)(du, u, p, t::Number)
update_coefficients!(L, u, p, t)
mul!(du, L, u)
end
Base.:*(L::AnalyticalJacVecOperator, x::AbstractVector) = L.f(x, L.u, L.p, L.t)
function LinearAlgebra.mul!(
du::AbstractVector,
L::AnalyticalJacVecOperator,
x::AbstractVector,
)
L.f(du, x, L.u, L.p, L.t)
end