@@ -5,11 +5,11 @@ struct GradientCache{CacheType1, CacheType2, CacheType3, fdtype, returntype, inp
55end
66
77function GradientCache (
8- df :: Union{<:Number, AbstractArray{<:Number}} ,
9- x :: Union{<:Number, AbstractArray{<:Number}} ,
10- fdtype :: Type{T1} = Val{:central },
11- returntype :: Type{T2} = eltype (df),
12- inplace :: Type{Val{T3}} = Val{true }) where {T1,T2,T3}
8+ df,
9+ x,
10+ fdtype = Val{:central },
11+ returntype = eltype (df),
12+ inplace = Val{true })
1313
1414 if typeof (x)<: AbstractArray # the vector->scalar case
1515 if fdtype!= Val{:complex } # complex-mode FD only needs one cache, for x+eps*im
@@ -49,74 +49,18 @@ function GradientCache(
4949
5050end
5151
52- function GradientCache (
53- c1 :: Union{Nothing,AbstractArray{<:Number}} ,
54- c2 :: Union{Nothing,AbstractArray{<:Number}} ,
55- fx :: Union{Nothing,<:Number,AbstractArray{<:Number}} = nothing ,
56- fdtype :: Type{T1} = Val{:central },
57- returntype :: Type{T2} = eltype (c1),
58- inplace :: Type{Val{T3}} = Val{true }) where {T1,T2,T3}
59-
60- if fdtype!= Val{:forward } && typeof (fx)!= Nothing
61- @warn (" Pre-computed function values are only useful for fdtype == Val{:forward}." )
62- _fx = nothing
63- else
64- # more runtime sanity checks?
65- _fx = fx
66- end
67-
68- if typeof (x)<: AbstractArray # the vector->scalar case
69- # need cache arrays for x1 (c1) and epsilon (c2) (both only if non-StridedArray)
70- if fdtype!= Val{:complex } # complex-mode FD only needs one cache, for x+eps*im
71- if typeof (x)<: StridedVector
72- if eltype (df)<: Complex && ! (eltype (x)<: Complex )
73- _c1 = zero (Complex{eltype (x)}) .* x
74- _c2 = nothing
75- else
76- _c1 = nothing
77- _c2 = nothing
78- if typeof (c1)!= Nothing || typeof (c2)!= Nothing
79- @warn (" For StridedVectors, neither c1 nor c2 are necessary." )
80- end
81- end
82- else
83- _c1 = c1
84- _c2 = c2
85- end
86- else
87- if ! (returntype<: Real )
88- fdtype_error (returntype)
89- else
90- _c1 = x + zero (eltype (x)) .* im
91- _c2 = nothing
92- end
93- end
94-
95- else # the scalar->vector case
96- # need cache arrays for fx1 and fx2, except in complex mode, which needs one complex array
97- if fdtype != Val{:complex }
98- _c1 = c1
99- _c2 = c2
100- else
101- _c1 = c1
102- _c2 = nothing
103- end
104- end
105- GradientCache {typeof(_fx),typeof(_c1),typeof(_c2),fdtype,returntype,inplace} (_fx,_c1,_c2)
106- end
107-
10852function finite_difference_gradient (
10953 f,
11054 x,
111- fdtype:: Type{T1} = Val{:central },
112- returntype:: Type{T2} = eltype (x),
113- inplace:: Type{Val{T3}} = Val{true },
114- fx:: Union{Nothing,AbstractArray{<:Number}} = nothing ,
115- c1:: Union{Nothing,AbstractArray{<:Number}} = nothing ,
116- c2:: Union{Nothing,AbstractArray{<:Number}} = nothing ;
55+ fdtype = Val{:central },
56+ returntype = eltype (x),
57+ inplace = Val{true },
58+ fx = nothing ,
59+ c1 = nothing ,
60+ c2 = nothing ;
11761 relstep= default_relstep (fdtype, eltype (x)),
11862 absstep= relstep,
119- dir= true ) where {T1,T2,T3}
63+ dir= true )
12064
12165 if typeof (x) <: AbstractArray
12266 df = zero (returntype) .* x
@@ -142,14 +86,14 @@ function finite_difference_gradient!(
14286 df,
14387 f,
14488 x,
145- fdtype:: Type{T1} = Val{:central },
146- returntype:: Type{T2} = eltype (df),
147- inplace:: Type{Val{T3}} = Val{true },
148- fx:: Union{Nothing,AbstractArray{<:Number}} = nothing ,
149- c1:: Union{Nothing,AbstractArray{<:Number}} = nothing ,
150- c2:: Union{Nothing,AbstractArray{<:Number}} = nothing ;
89+ fdtype= Val{:central },
90+ returntype= eltype (df),
91+ inplace= Val{true },
92+ fx= nothing ,
93+ c1= nothing ,
94+ c2= nothing ;
15195 relstep= default_relstep (fdtype, eltype (x)),
152- absstep= relstep) where {T1,T2,T3}
96+ absstep= relstep)
15397
15498 cache = GradientCache (df, x, fdtype, returntype, inplace)
15599 finite_difference_gradient! (df, f, x, cache, relstep= relstep, absstep= absstep)
175119# vector of derivatives of a vector->scalar map by each component of a vector x
176120# this ignores the value of "inplace", because it doesn't make much sense
177121function finite_difference_gradient! (
178- df:: AbstractArray{<:Number} ,
122+ df,
179123 f,
180- x:: AbstractArray{<:Number} ,
124+ x,
181125 cache:: GradientCache{T1,T2,T3,fdtype,returntype,inplace} ;
182126 relstep= default_relstep (fdtype, eltype (x)),
183127 absstep= relstep,
359303# vector of derivatives of a scalar->vector map
360304# this is effectively a vector of partial derivatives, but we still call it a gradient
361305function finite_difference_gradient! (
362- df:: AbstractArray{<:Number} ,
306+ df,
363307 f,
364308 x:: Number ,
365309 cache:: GradientCache{T1,T2,T3,fdtype,returntype,inplace} ;
0 commit comments