Skip to content

Commit 58b4e72

Browse files
Merge pull request #133 from ArnoStrouwen/master
Uniform variable names
2 parents 86b8b3b + 9c4b478 commit 58b4e72

File tree

4 files changed

+122
-122
lines changed

4 files changed

+122
-122
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ Suppose we had the function
1919

2020
```julia
2121
fcalls = 0
22-
function f(dx,x) # in-place
22+
function f(y,x) # in-place
2323
global fcalls += 1
2424
for i in 2:length(x)-1
25-
dx[i] = x[i-1] - 2x[i] + x[i+1]
25+
y[i] = x[i-1] - 2x[i] + x[i+1]
2626
end
27-
dx[1] = -2x[1] + x[2]
28-
dx[end] = x[end-1] - 2x[end]
27+
y[1] = -2x[1] + x[2]
28+
y[end] = x[end-1] - 2x[end]
2929
nothing
3030
end
3131

3232
function g(x) # out-of-place
3333
global fcalls += 1
34-
dx = zero(x)
34+
y = zero(x)
3535
for i in 2:length(x)-1
36-
dx[i] = x[i-1] - 2x[i] + x[i+1]
36+
y[i] = x[i-1] - 2x[i] + x[i+1]
3737
end
38-
dx[1] = -2x[1] + x[2]
39-
dx[end] = x[end-1] - 2x[end]
40-
dx
38+
y[1] = -2x[1] + x[2]
39+
y[end] = x[end-1] - 2x[end]
40+
y
4141
end
4242
```
4343

@@ -85,8 +85,8 @@ forwarddiff_color_jacobian!(jac, f, x, colorvec = colors)
8585
If one only needs to compute products, one can use the operators. For example,
8686

8787
```julia
88-
u = rand(30)
89-
J = JacVec(f,u)
88+
x = rand(30)
89+
J = JacVec(f,x)
9090
```
9191

9292
makes `J` into a matrix-free operator which calculates `J*v` products. For
@@ -219,14 +219,14 @@ the function signature for Jacobians is `f!(du,u)`, while out-of-place has
219219
The functions for Jacobians are:
220220

221221
```julia
222-
auto_jacvec!(du, f, x, v,
222+
auto_jacvec!(dy, f, x, v,
223223
cache1 = ForwardDiff.Dual{DeivVecTag}.(x, v),
224224
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v))
225225

226226
auto_jacvec(f, x, v)
227227

228228
# If compute_f0 is false, then `f(cache1,x)` will be computed
229-
num_jacvec!(du,f,x,v,cache1 = similar(v),
229+
num_jacvec!(dy,f,x,v,cache1 = similar(v),
230230
cache2 = similar(v);
231231
compute_f0 = true)
232232
num_jacvec(f,x,v,f0=nothing)
@@ -235,21 +235,21 @@ num_jacvec(f,x,v,f0=nothing)
235235
For Hessians, the following are provided:
236236

237237
```julia
238-
num_hesvec!(du,f,x,v,
238+
num_hesvec!(dy,f,x,v,
239239
cache1 = similar(v),
240240
cache2 = similar(v),
241241
cache3 = similar(v))
242242

243243
num_hesvec(f,x,v)
244244

245-
numauto_hesvec!(du,f,x,v,
245+
numauto_hesvec!(dy,f,x,v,
246246
cache = ForwardDiff.GradientConfig(f,v),
247247
cache1 = similar(v),
248248
cache2 = similar(v))
249249

250250
numauto_hesvec(f,x,v)
251251

252-
autonum_hesvec!(du,f,x,v,
252+
autonum_hesvec!(dy,f,x,v,
253253
cache1 = similar(v),
254254
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v),
255255
cache3 = ForwardDiff.Dual{DeivVecTag}.(x, v))
@@ -258,17 +258,17 @@ autonum_hesvec(f,x,v)
258258
```
259259

260260
In addition,
261-
the following forms allow you to provide a gradient function `g(dx,x)` or `dx=g(x)`
261+
the following forms allow you to provide a gradient function `g(dy,x)` or `dy=g(x)`
262262
respectively:
263263

264264
```julia
265-
num_hesvecgrad!(du,g,x,v,
265+
num_hesvecgrad!(dy,g,x,v,
266266
cache2 = similar(v),
267267
cache3 = similar(v))
268268

269269
num_hesvecgrad(g,x,v)
270270

271-
auto_hesvecgrad!(du,g,x,v,
271+
auto_hesvecgrad!(dy,g,x,v,
272272
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v),
273273
cache3 = ForwardDiff.Dual{DeivVecTag}.(x, v))
274274

@@ -287,14 +287,14 @@ optimized these will likely be the fastest.
287287
```julia
288288
using Zygote # Required
289289

290-
numback_hesvec!(du,f,x,v,
290+
numback_hesvec!(dy,f,x,v,
291291
cache1 = similar(v),
292292
cache2 = similar(v))
293293

294294
numback_hesvec(f,x,v)
295295

296296
# Currently errors! See https://github.com/FluxML/Zygote.jl/issues/241
297-
autoback_hesvec!(du,f,x,v,
297+
autoback_hesvec!(dy,f,x,v,
298298
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v),
299299
cache3 = ForwardDiff.Dual{DeivVecTag}.(x, v))
300300

@@ -308,9 +308,9 @@ Jacobian-vector and Hessian-vector products where the differentiation takes
308308
place at the vector `u`:
309309

310310
```julia
311-
JacVec(f,u::AbstractArray;autodiff=true)
312-
HesVec(f,u::AbstractArray;autodiff=true)
313-
HesVecGrad(g,u::AbstractArray;autodiff=false)
311+
JacVec(f,x::AbstractArray;autodiff=true)
312+
HesVec(f,x::AbstractArray;autodiff=true)
313+
HesVecGrad(g,x::AbstractArray;autodiff=false)
314314
```
315315

316316
These all have the same interface, where `J*v` utilizes the out-of-place
Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
struct DeivVecTag end
22

33
# J(f(x))*v
4-
function auto_jacvec!(du, f, x, v,
4+
function auto_jacvec!(dy, f, x, v,
55
cache1 = ForwardDiff.Dual{DeivVecTag}.(x, v),
66
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v))
77
cache1 .= Dual{DeivVecTag}.(x, v)
88
f(cache2,cache1)
9-
du .= partials.(cache2, 1)
9+
dy .= partials.(cache2, 1)
1010
end
1111
function auto_jacvec(f, x, v)
1212
partials.(f(Dual{DeivVecTag}.(x, v)), 1)
1313
end
1414

15-
function num_jacvec!(du,f,x,v,cache1 = similar(v),
15+
function num_jacvec!(dy,f,x,v,cache1 = similar(v),
1616
cache2 = similar(v);
1717
compute_f0 = true)
1818
compute_f0 && (f(cache1,x))
@@ -22,7 +22,7 @@ function num_jacvec!(du,f,x,v,cache1 = similar(v),
2222
@. x += ϵ*v
2323
f(cache2,x)
2424
@. x -= ϵ*v
25-
@. du = (cache2 - cache1)/ϵ
25+
@. dy = (cache2 - cache1)/ϵ
2626
end
2727

2828
function num_jacvec(f,x,v,f0=nothing)
@@ -33,7 +33,7 @@ function num_jacvec(f,x,v,f0=nothing)
3333
(f(x.+ϵ.*v) .- _f0)./ϵ
3434
end
3535

36-
function num_hesvec!(du,f,x,v,
36+
function num_hesvec!(dy,f,x,v,
3737
cache1 = similar(v),
3838
cache2 = similar(v),
3939
cache3 = similar(v))
@@ -48,7 +48,7 @@ function num_hesvec!(du,f,x,v,
4848
g(cache2,x)
4949
@. x -= 2ϵ*v
5050
g(cache3,x)
51-
@. du = (cache2 - cache3)/(2ϵ)
51+
@. dy = (cache2 - cache3)/(2ϵ)
5252
end
5353

5454
function num_hesvec(f,x,v)
@@ -63,7 +63,7 @@ function num_hesvec(f,x,v)
6363
(gxp - gxm)/(2ϵ)
6464
end
6565

66-
function numauto_hesvec!(du,f,x,v,
66+
function numauto_hesvec!(dy,f,x,v,
6767
cache = ForwardDiff.GradientConfig(f,v),
6868
cache1 = similar(v),
6969
cache2 = similar(v))
@@ -77,7 +77,7 @@ function numauto_hesvec!(du,f,x,v,
7777
g(cache1,x)
7878
@. x -= 2ϵ*v
7979
g(cache2,x)
80-
@. du = (cache1 - cache2)/(2ϵ)
80+
@. dy = (cache1 - cache2)/(2ϵ)
8181
end
8282

8383
function numauto_hesvec(f,x,v)
@@ -92,22 +92,22 @@ function numauto_hesvec(f,x,v)
9292
(gxp - gxm)/(2ϵ)
9393
end
9494

95-
function autonum_hesvec!(du,f,x,v,
95+
function autonum_hesvec!(dy,f,x,v,
9696
cache1 = ForwardDiff.Dual{DeivVecTag}.(x, v),
9797
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v))
9898
cache = FiniteDiff.GradientCache(v[1],cache1,Val{:central})
9999
g = (dx,x) -> FiniteDiff.finite_difference_gradient!(dx,f,x,cache)
100100
cache1 .= Dual{DeivVecTag}.(x, v)
101101
g(cache2,cache1)
102-
du .= partials.(cache2, 1)
102+
dy .= partials.(cache2, 1)
103103
end
104104

105105
function autonum_hesvec(f,x,v)
106106
g = (x) -> FiniteDiff.finite_difference_gradient(f,x)
107107
partials.(g(Dual{DeivVecTag}.(x, v)), 1)
108108
end
109109

110-
function num_hesvecgrad!(du,g,x,v,
110+
function num_hesvecgrad!(dy,g,x,v,
111111
cache2 = similar(v),
112112
cache3 = similar(v))
113113
T = eltype(x)
@@ -117,7 +117,7 @@ function num_hesvecgrad!(du,g,x,v,
117117
g(cache2,x)
118118
@. x -= 2ϵ*v
119119
g(cache3,x)
120-
@. du = (cache2 - cache3)/(2ϵ)
120+
@. dy = (cache2 - cache3)/(2ϵ)
121121
end
122122

123123
function num_hesvecgrad(g,x,v)
@@ -131,12 +131,12 @@ function num_hesvecgrad(g,x,v)
131131
(gxp - gxm)/(2ϵ)
132132
end
133133

134-
function auto_hesvecgrad!(du,g,x,v,
134+
function auto_hesvecgrad!(dy,g,x,v,
135135
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, v),
136136
cache3 = ForwardDiff.Dual{DeivVecTag}.(x, v))
137137
cache2 .= Dual{DeivVecTag}.(x, v)
138138
g(cache3,cache2)
139-
du .= partials.(cache3, 1)
139+
dy .= partials.(cache3, 1)
140140
end
141141

142142
function auto_hesvecgrad(g,x,v)
@@ -145,98 +145,98 @@ end
145145

146146
### Operator Forms
147147

148-
struct JacVec{F,T1,T2,uType}
148+
struct JacVec{F,T1,T2,xType}
149149
f::F
150150
cache1::T1
151151
cache2::T2
152-
u::uType
152+
x::xType
153153
autodiff::Bool
154154
end
155155

156-
function JacVec(f,u::AbstractArray;autodiff=true)
156+
function JacVec(f,x::AbstractArray;autodiff=true)
157157
if autodiff
158-
cache1 = ForwardDiff.Dual{DeivVecTag}.(u, u)
159-
cache2 = ForwardDiff.Dual{DeivVecTag}.(u, u)
158+
cache1 = ForwardDiff.Dual{DeivVecTag}.(x, x)
159+
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, x)
160160
else
161-
cache1 = similar(u)
162-
cache2 = similar(u)
161+
cache1 = similar(x)
162+
cache2 = similar(x)
163163
end
164-
JacVec(f,cache1,cache2,u,autodiff)
164+
JacVec(f,cache1,cache2,x,autodiff)
165165
end
166166

167167
Base.size(L::JacVec) = (length(L.cache1),length(L.cache1))
168168
Base.size(L::JacVec,i::Int) = length(L.cache1)
169-
Base.:*(L::JacVec,x::AbstractVector) = L.autodiff ? auto_jacvec(_u->L.f(_u),L.u,x) : num_jacvec(_u->L.f(_u),L.u,x)
169+
Base.:*(L::JacVec,v::AbstractVector) = L.autodiff ? auto_jacvec(_x->L.f(_x),L.x,v) : num_jacvec(_x->L.f(_x),L.x,v)
170170

171-
function LinearAlgebra.mul!(du::AbstractVector,L::JacVec,v::AbstractVector)
171+
function LinearAlgebra.mul!(dy::AbstractVector,L::JacVec,v::AbstractVector)
172172
if L.autodiff
173-
auto_jacvec!(du,(_du,_u)->L.f(_du,_u),L.u,v,L.cache1,L.cache2)
173+
auto_jacvec!(dy,(_y,_x)->L.f(_y,_x),L.x,v,L.cache1,L.cache2)
174174
else
175-
num_jacvec!(du,(_du,_u)->L.f(_du,_u),L.u,v,L.cache1,L.cache2)
175+
num_jacvec!(dy,(_y,_x)->L.f(_y,_x),L.x,v,L.cache1,L.cache2)
176176
end
177177
end
178178

179-
struct HesVec{F,T1,T2,uType}
179+
struct HesVec{F,T1,T2,xType}
180180
f::F
181181
cache1::T1
182182
cache2::T2
183183
cache3::T2
184-
u::uType
184+
x::xType
185185
autodiff::Bool
186186
end
187187

188-
function HesVec(f,u::AbstractArray;autodiff=true)
188+
function HesVec(f,x::AbstractArray;autodiff=true)
189189
if autodiff
190-
cache1 = ForwardDiff.GradientConfig(f,u)
191-
cache2 = similar(u)
192-
cache3 = similar(u)
190+
cache1 = ForwardDiff.GradientConfig(f,x)
191+
cache2 = similar(x)
192+
cache3 = similar(x)
193193
else
194-
cache1 = similar(u)
195-
cache2 = similar(u)
196-
cache3 = similar(u)
194+
cache1 = similar(x)
195+
cache2 = similar(x)
196+
cache3 = similar(x)
197197
end
198-
HesVec(f,cache1,cache2,cache3,u,autodiff)
198+
HesVec(f,cache1,cache2,cache3,x,autodiff)
199199
end
200200

201201
Base.size(L::HesVec) = (length(L.cache2),length(L.cache2))
202202
Base.size(L::HesVec,i::Int) = length(L.cache2)
203-
Base.:*(L::HesVec,x::AbstractVector) = L.autodiff ? numauto_hesvec(L.f,L.u,x) : num_hesvec(L.f,L.u,x)
203+
Base.:*(L::HesVec,v::AbstractVector) = L.autodiff ? numauto_hesvec(L.f,L.x,v) : num_hesvec(L.f,L.x,v)
204204

205-
function LinearAlgebra.mul!(du::AbstractVector,L::HesVec,v::AbstractVector)
205+
function LinearAlgebra.mul!(dy::AbstractVector,L::HesVec,v::AbstractVector)
206206
if L.autodiff
207-
numauto_hesvec!(du,L.f,L.u,v,L.cache1,L.cache2,L.cache3)
207+
numauto_hesvec!(dy,L.f,L.x,v,L.cache1,L.cache2,L.cache3)
208208
else
209-
num_hesvec!(du,L.f,L.u,v,L.cache1,L.cache2,L.cache3)
209+
num_hesvec!(dy,L.f,L.x,v,L.cache1,L.cache2,L.cache3)
210210
end
211211
end
212212

213213
struct HesVecGrad{G,T1,T2,uType}
214214
g::G
215215
cache1::T1
216216
cache2::T2
217-
u::uType
217+
x::uType
218218
autodiff::Bool
219219
end
220220

221-
function HesVecGrad(g,u::AbstractArray;autodiff=false)
221+
function HesVecGrad(g,x::AbstractArray;autodiff=false)
222222
if autodiff
223-
cache1 = ForwardDiff.Dual{DeivVecTag}.(u, u)
224-
cache2 = ForwardDiff.Dual{DeivVecTag}.(u, u)
223+
cache1 = ForwardDiff.Dual{DeivVecTag}.(x, x)
224+
cache2 = ForwardDiff.Dual{DeivVecTag}.(x, x)
225225
else
226-
cache1 = similar(u)
227-
cache2 = similar(u)
226+
cache1 = similar(x)
227+
cache2 = similar(x)
228228
end
229-
HesVecGrad(g,cache1,cache2,u,autodiff)
229+
HesVecGrad(g,cache1,cache2,x,autodiff)
230230
end
231231

232232
Base.size(L::HesVecGrad) = (length(L.cache2),length(L.cache2))
233233
Base.size(L::HesVecGrad,i::Int) = length(L.cache2)
234-
Base.:*(L::HesVecGrad,x::AbstractVector) = L.autodiff ? auto_hesvecgrad(L.g,L.u,x) : num_hesvecgrad(L.g,L.u,x)
234+
Base.:*(L::HesVecGrad,v::AbstractVector) = L.autodiff ? auto_hesvecgrad(L.g,L.x,v) : num_hesvecgrad(L.g,L.x,v)
235235

236-
function LinearAlgebra.mul!(du::AbstractVector,L::HesVecGrad,v::AbstractVector)
236+
function LinearAlgebra.mul!(dy::AbstractVector,L::HesVecGrad,v::AbstractVector)
237237
if L.autodiff
238-
auto_hesvecgrad!(du,L.g,L.u,v,L.cache1,L.cache2)
238+
auto_hesvecgrad!(dy,L.g,L.x,v,L.cache1,L.cache2)
239239
else
240-
num_hesvecgrad!(du,L.g,L.u,v,L.cache1,L.cache2)
240+
num_hesvecgrad!(dy,L.g,L.x,v,L.cache1,L.cache2)
241241
end
242242
end

0 commit comments

Comments
 (0)