Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

included complex step finite method for the gradient computation #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/finite_difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
macro complexrule(x, e)
x, e = esc(x), esc(e)
quote
$e = eps($x)
$e = eps($x + 1e-200)
end
end

Expand Down Expand Up @@ -129,8 +129,17 @@ function finite_difference!{S <: Number, T <: Number}(f::Function,
x[i] = oldx
g[i] = (f_xplusdx - f_xminusdx) / (epsilon + epsilon)
end
elseif dtype == :complex
xcomplex = x + 0.*im
for i = 1:n
@complexrule x[i] epsilon
oldxi = xcomplex[i]
xcomplex[i] = oldxi + epsilon * im
g[i] = imag(f(xcomplex)) / epsilon
xcomplex[i] = oldxi
end
else
error("dtype must be :forward or :central")
error("dtype must be :forward, :central or :complex")
end

return
Expand Down