Prev Next ComplexStep

A Complex Step Method Function Object
Syntax ComplexStep<FunTypecomStep(
Fun  &f   , 
Type &h   )
Type comStep(const Type &x)

The object f has type Fun and the syntax
must return a std::complex<Type> object where the argument x has prototype const std::complex<Type> &x.

The binary operations + and / must supported by this type. In addition, there must be a constructor of the form Type(double x).

Step Size
The argument h specifies the step size for the forward difference operation.

The constructor syntax
FunTypecomStep(fh) creates the function object comStep from the specified function and step size.

The syntax
returns the following complex step method approximation for the derivative: @[@ {\rm Imag} [ f(x + {\rm i} h) ] / h @]@ as a Type object.

Source Code
     template <typename Fun, typename Type>
     class ComplexStep {
          // constructor
          ComplexStep(const Fun &f_, const Type &h_) 
          : z(0.), f(f_), h(h_)
          { }

          // function object value
          Type operator()(const Type &x) const
          {using std::complex;

          complex<Type> cx = complex<Type>(x, z);
          complex<Type> ch = complex<Type>(z, h);
          return imag( f(x + ch) ) / h;

          Type z;
          Type h;
          Fun  f;

Input File: ComplexStep.h