Prev Next ComplexStep

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

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

Type
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.

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

Evaluation
The syntax
     
comStep(x)
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 {
     public:
          // 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;
     }

     private:
          Type z;
          Type h;
          Fun  f;
     };

Input File: ComplexStep.h