Skip to content

Commit 610c62b

Browse files
committed
added special abs fuction so complex step finitie difference can work with abs
1 parent 18425a7 commit 610c62b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/finite_difference.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,33 @@ function finite_difference{T <: Number}(f::Function,
6262
end
6363
end
6464

65+
##############################################################################
66+
##
67+
## Complex Step Finite Differentiation Tools
68+
##
69+
## Martins, Sturdza, and Alonso (2003) suggest the only non-analytic
70+
## fuction of which complex step finite difference approximation
71+
## will fail and finite difference will not is abs().
72+
## They suggest redefining as follows for z = x + im*y
73+
##
74+
## if x < 0
75+
## -x - im * y
76+
## else
77+
## x + im * y
78+
##
79+
## This is provided below as complex_differentiable_abs (renaming encouraged!)
80+
##
81+
## Also, if your fuctions has control flow using < or >, you must compare
82+
## real(z) for your control flow.
83+
##
84+
##############################################################################
85+
86+
function complex_differentiable_abs{T <: Complex}(z::T)
87+
if real(z) < 0
88+
return -real(z) - im * imag(z)
89+
else
90+
return real(z) + im * imag(z)
91+
6592
##############################################################################
6693
##
6794
## Gradient of f: R^n -> R

0 commit comments

Comments
 (0)