@@ -33,8 +33,10 @@ function GradientCache(
33
33
if typeof (c2)!= Void && x<: StridedVector
34
34
warn (" c2 cache isn't necessary when x<:StridedVector." )
35
35
end
36
- if typeof (c2)== Void || eltype (c2)!= real (eltype (x)) && ! (x <: StridedVector )
36
+ if ( typeof (c2)== Void || eltype (c2)!= real (eltype (x))) && ! (typeof (x) <: StridedVector )
37
37
_c2 = zeros (real (eltype (x)), size (x))
38
+ elseif typeof (x)<: StridedArray
39
+ _c2 = nothing
38
40
else
39
41
_c2 = c2
40
42
end
@@ -176,7 +178,7 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
176
178
end
177
179
df
178
180
end
179
- #=
181
+
180
182
function finite_difference_gradient! (df:: StridedVector{<:Number} , f, x:: StridedVector{<:Number} ,
181
183
cache:: GradientCache{T1,T2,T3,fdtype,returntype,inplace} ) where {T1,T2,T3,fdtype,returntype,inplace}
182
184
@@ -189,24 +191,24 @@ function finite_difference_gradient!(df::StridedVector{<:Number}, f, x::StridedV
189
191
if fdtype == Val{:forward }
190
192
@inbounds for i ∈ eachindex (x)
191
193
epsilon = compute_epsilon (fdtype, x[i], epsilon_factor)
192
- c2_old = c2 [i]
193
- c2 [i] += epsilon
194
+ c1_old = c1 [i]
195
+ c1 [i] += epsilon
194
196
if typeof (fx) != Void
195
- df[i] = (f(c2 ) - fx) / epsilon
197
+ df[i] = (f (c1 ) - fx) / epsilon
196
198
else
197
- df[i] = (f(c2 ) - f(x)) / epsilon
199
+ df[i] = (f (c1 ) - f (x)) / epsilon
198
200
end
199
- c2 [i] = c2_old
201
+ c1 [i] = c1_old
200
202
end
201
203
elseif fdtype == Val{:central }
202
204
@inbounds for i ∈ eachindex (x)
203
205
epsilon = compute_epsilon (fdtype, x[i], epsilon_factor)
204
- c2_old = c2 [i]
205
- c2 [i] += epsilon
206
+ c1_old = c1 [i]
207
+ c1 [i] += epsilon
206
208
x_old = x[i]
207
209
x[i] -= epsilon
208
- df[i] = (f(c2 ) - f(x)) / (2*epsilon)
209
- c2 [i] = c2_old
210
+ df[i] = (f (c1 ) - f (x)) / (2 * epsilon)
211
+ c1 [i] = c1_old
210
212
x[i] = x_old
211
213
end
212
214
elseif fdtype == Val{:complex } && returntype <: Real
@@ -224,7 +226,7 @@ function finite_difference_gradient!(df::StridedVector{<:Number}, f, x::StridedV
224
226
end
225
227
df
226
228
end
227
- =#
229
+
228
230
# vector of derivatives of a scalar->vector map
229
231
# this is effectively a vector of partial derivatives, but we still call it a gradient
230
232
function finite_difference_gradient! (df:: AbstractArray{<:Number} , f, x:: Number ,
0 commit comments