88function GradientCache (
99 df,
1010 x,
11- fdtype = Val{ :central } ,
11+ fdtype = Val ( :central ) ,
1212 returntype = eltype (df),
13- inplace = Val{ true } )
13+ inplace = Val ( true ) )
1414
15+ fdtype isa Type && (fdtype = fdtype ())
16+ inplace isa Type && (inplace = inplace ())
1517 if typeof (x)<: AbstractArray # the vector->scalar case
16- if fdtype!= Val{ :complex } # complex-mode FD only needs one cache, for x+eps*im
18+ if fdtype!= Val ( :complex ) # complex-mode FD only needs one cache, for x+eps*im
1719 if typeof (x)<: StridedVector
1820 if eltype (df)<: Complex && ! (eltype (x)<: Complex )
1921 _c1 = zero (Complex{eltype (x)}) .* x
@@ -37,7 +39,7 @@ function GradientCache(
3739 _c3 = similar (x)
3840 else # the scalar->vector case
3941 # need cache arrays for fx1 and fx2, except in complex mode, which needs one complex array
40- if fdtype != Val{ :complex }
42+ if fdtype != Val ( :complex )
4143 _c1 = similar (df)
4244 _c2 = similar (df)
4345 else
5557function finite_difference_gradient (
5658 f,
5759 x,
58- fdtype = Val{ :central } ,
60+ fdtype = Val ( :central ) ,
5961 returntype = eltype (x),
60- inplace = Val{ true } ,
62+ inplace = Val ( true ) ,
6163 fx = nothing ,
6264 c1 = nothing ,
6365 c2 = nothing ;
6466 relstep= default_relstep (fdtype, eltype (x)),
6567 absstep= relstep,
6668 dir= true )
6769
70+ inplace isa Type && (inplace = inplace ())
6871 if typeof (x) <: AbstractArray
6972 df = zero (returntype) .* x
7073 else
71- if inplace == Val{ true }
74+ if inplace == Val ( true )
7275 if typeof (fx)== Nothing && typeof (c1)== Nothing && typeof (c2)== Nothing
7376 error (" In the scalar->vector in-place map case, at least one of fx, c1 or c2 must be provided, otherwise we cannot infer the return size." )
7477 else
@@ -89,9 +92,9 @@ function finite_difference_gradient!(
8992 df,
9093 f,
9194 x,
92- fdtype= Val{ :central } ,
95+ fdtype= Val ( :central ) ,
9396 returntype= eltype (df),
94- inplace= Val{ true } ,
97+ inplace= Val ( true ) ,
9598 fx= nothing ,
9699 c1= nothing ,
97100 c2= nothing ;
@@ -133,12 +136,12 @@ function finite_difference_gradient!(
133136 # NOTE: in this case epsilon is a vector, we need two arrays for epsilon and x1
134137 # c1 denotes x1, c2 is epsilon
135138 fx, c1, c2, c3 = cache. fx, cache. c1, cache. c2, cache. c3
136- if fdtype != Val{ :complex } && ArrayInterface. fast_scalar_indexing (c2)
139+ if fdtype != Val ( :complex ) && ArrayInterface. fast_scalar_indexing (c2)
137140 @. c2 = compute_epsilon (fdtype, x, relstep, absstep, dir)
138141 copyto! (c1,x)
139142 end
140143 copyto! (c3,x)
141- if fdtype == Val{ :forward }
144+ if fdtype == Val ( :forward )
142145 @inbounds for i ∈ eachindex (x)
143146 if ArrayInterface. fast_scalar_indexing (c2)
144147 epsilon = ArrayInterface. allowed_getindex (c2,i)* dir
@@ -168,7 +171,7 @@ function finite_difference_gradient!(
168171 ArrayInterface. allowed_setindex! (c1,c1_old,i)
169172 end
170173 end
171- elseif fdtype == Val{ :central }
174+ elseif fdtype == Val ( :central )
172175 @inbounds for i ∈ eachindex (x)
173176 if ArrayInterface. fast_scalar_indexing (c2)
174177 epsilon = ArrayInterface. allowed_getindex (c2,i)* dir
@@ -191,7 +194,7 @@ function finite_difference_gradient!(
191194 ArrayInterface. allowed_setindex! (c1,c1_old, i)
192195 ArrayInterface. allowed_setindex! (c3,x_old,i)
193196 end
194- elseif fdtype == Val{ :complex } && returntype <: Real
197+ elseif fdtype == Val ( :complex ) && returntype <: Real
195198 copyto! (c1,x)
196199 epsilon_complex = eps (real (eltype (x)))
197200 # we use c1 here to avoid typing issues with x
@@ -219,13 +222,13 @@ function finite_difference_gradient!(
219222 # c1 is x1 if we need a complex copy of x, otherwise Nothing
220223 # c2 is Nothing
221224 fx, c1, c2, c3 = cache. fx, cache. c1, cache. c2, cache. c3
222- if fdtype != Val{ :complex }
225+ if fdtype != Val ( :complex )
223226 if eltype (df)<: Complex && ! (eltype (x)<: Complex )
224227 copyto! (c1,x)
225228 end
226229 end
227230 copyto! (c3,x)
228- if fdtype == Val{ :forward }
231+ if fdtype == Val ( :forward )
229232 for i ∈ eachindex (x)
230233 epsilon = compute_epsilon (fdtype, x[i], relstep, absstep, dir)
231234 x_old = x[i]
@@ -262,7 +265,7 @@ function finite_difference_gradient!(
262265 df[i] -= im * imag (dfi)
263266 end
264267 end
265- elseif fdtype == Val{ :central }
268+ elseif fdtype == Val ( :central )
266269 @inbounds for i ∈ eachindex (x)
267270 epsilon = compute_epsilon (fdtype, x[i], relstep, absstep, dir)
268271 x_old = x[i]
@@ -289,7 +292,7 @@ function finite_difference_gradient!(
289292 df[i] -= im* imag (dfi / (2 * im* epsilon))
290293 end
291294 end
292- elseif fdtype== Val{ :complex } && returntype<: Real && eltype (df)<: Real && eltype (x)<: Real
295+ elseif fdtype== Val ( :complex ) && returntype<: Real && eltype (df)<: Real && eltype (x)<: Real
293296 copyto! (c1,x)
294297 epsilon_complex = eps (real (eltype (x)))
295298 # we use c1 here to avoid typing issues with x
@@ -320,40 +323,40 @@ function finite_difference_gradient!(
320323 # c1 denotes fx1, c2 is fx2, sizes guaranteed by the cache constructor
321324 fx, c1, c2 = cache. fx, cache. c1, cache. c2
322325
323- if inplace == Val{ true }
326+ if inplace == Val ( true )
324327 _c1, _c2 = c1, c2
325328 end
326329
327- if fdtype == Val{ :forward }
328- epsilon = compute_epsilon (Val{ :forward } , x, relstep, absstep, dir)
329- if inplace == Val{ true }
330+ if fdtype == Val ( :forward )
331+ epsilon = compute_epsilon (Val ( :forward ) , x, relstep, absstep, dir)
332+ if inplace == Val ( true )
330333 f (c1, x+ epsilon)
331334 else
332335 _c1 = f (x+ epsilon)
333336 end
334337 if typeof (fx) != Nothing
335338 @. df = (_c1 - fx) / epsilon
336339 else
337- if inplace == Val{ true }
340+ if inplace == Val ( true )
338341 f (c2, x)
339342 else
340343 _c2 = f (x)
341344 end
342345 @. df = (_c1 - _c2) / epsilon
343346 end
344- elseif fdtype == Val{ :central }
345- epsilon = compute_epsilon (Val{ :central } , x, relstep, absstep, dir)
346- if inplace == Val{ true }
347+ elseif fdtype == Val ( :central )
348+ epsilon = compute_epsilon (Val ( :central ) , x, relstep, absstep, dir)
349+ if inplace == Val ( true )
347350 f (c1, x+ epsilon)
348351 f (c2, x- epsilon)
349352 else
350353 _c1 = f (x+ epsilon)
351354 _c2 = f (x- epsilon)
352355 end
353356 @. df = (_c1 - _c2) / (2 * epsilon)
354- elseif fdtype == Val{ :complex } && returntype <: Real
357+ elseif fdtype == Val ( :complex ) && returntype <: Real
355358 epsilon_complex = eps (real (eltype (x)))
356- if inplace == Val{ true }
359+ if inplace == Val ( true )
357360 f (c1, x+ im* epsilon_complex)
358361 else
359362 _c1 = f (x+ im* epsilon_complex)
0 commit comments