1
1
#=
2
- Sinple -point derivatives of scalar->scalar maps.
2
+ Single -point derivatives of scalar->scalar maps.
3
3
=#
4
4
function finite_difference_derivative (f, x:: T , fdtype:: DataType = Val{:central },
5
5
returntype:: DataType = eltype (x), f_x:: Union{Void,T} = nothing ) where T<: Number
6
6
7
- epsilon = compute_epsilon (fdtype, real (x) )
7
+ epsilon = compute_epsilon (fdtype, x )
8
8
if fdtype== Val{:forward }
9
9
return (f (x+ epsilon) - f (x)) / epsilon
10
10
elseif fdtype== Val{:central }
73
73
function DerivativeCache (
74
74
x :: AbstractArray{<:Number} ,
75
75
fx :: Union{Void,AbstractArray{<:Number}} = nothing ,
76
- epsilon :: Union{Void,AbstractArray{<:Number }} = nothing ,
76
+ epsilon :: Union{Void,AbstractArray{<:Real }} = nothing ,
77
77
fdtype :: DataType = Val{:central },
78
78
returntype :: DataType = eltype (x))
79
79
@@ -99,8 +99,8 @@ function DerivativeCache(
99
99
if typeof (epsilon)== Void || eltype (epsilon)!= real (eltype (x))
100
100
epsilon = zeros (real (eltype (x)), size (x))
101
101
end
102
- epsilon_factor = compute_epsilon_factor (fdtype, real ( eltype (x) ))
103
- @. epsilon = compute_epsilon (fdtype, real (x) , epsilon_factor)
102
+ epsilon_factor = compute_epsilon_factor (fdtype, eltype (x))
103
+ @. epsilon = compute_epsilon (fdtype, x , epsilon_factor)
104
104
_epsilon = epsilon
105
105
end
106
106
DerivativeCache {typeof(_fx),typeof(_epsilon),fdtype,returntype} (_fx,_epsilon)
@@ -115,7 +115,7 @@ function finite_difference_derivative(
115
115
fdtype :: DataType = Val{:central },
116
116
returntype :: DataType = eltype (x), # return type of f
117
117
fx :: Union{Void,AbstractArray{<:Number}} = nothing ,
118
- epsilon :: Union{Void,AbstractArray{<:Real}} = nothing )
118
+ epsilon :: Union{Void,AbstractArray{<:Real}} = nothing )
119
119
120
120
df = zeros (returntype, size (x))
121
121
finite_difference_derivative! (df, f, x, fdtype, returntype, fx, epsilon)
@@ -160,10 +160,10 @@ Optimized implementations for StridedArrays.
160
160
Essentially, the only difference between these and the AbstractArray case
161
161
is that here we can compute the epsilon one by one in local variables and avoid caching it.
162
162
=#
163
- function _finite_difference_derivative ! (df:: StridedArray , f, x:: StridedArray ,
163
+ function finite_difference_derivative ! (df:: StridedArray , f, x:: StridedArray ,
164
164
cache:: DerivativeCache{T1,T2,fdtype,returntype} ) where {T1,T2,fdtype,returntype}
165
165
166
- epsilon_factor = compute_epsilon_factor (fdtype, real ( eltype (x) ))
166
+ epsilon_factor = compute_epsilon_factor (fdtype, eltype (x))
167
167
if fdtype == Val{:forward }
168
168
fx = cache. fx
169
169
@inbounds for i ∈ eachindex (x)
0 commit comments