# Howo to implement complex step derivative for complex functions?

I have a complex analytic function of which I want to take the numerical derivative.

\begin{align} f(z) &\equiv f(x,y) = u(x,y) + i v(x,y) \\ \frac{d f(z)}{d z} & = \lim_{h \to 0} \frac{f(z + h) - f(h)}{h} = \frac{\partial u(x,y)}{\partial x} + i\frac{\partial v(x,y)}{\partial x} \end{align}

Now, I can do the partial derivatives via the complex step method, as $$\frac{\partial u(x,y)}{\partial x} = \lim_{h \to 0} \frac{\Im{u(x + i h,y)}}{h} \\ \frac{\partial v(x,y)}{\partial x} = \lim_{h \to 0} \frac{\Im{v(x + i h,y)}}{h} \\ \frac{d f(z)}{d z} = \lim_{h \to 0} \left[ \frac{\Im{u(x + i h,y)}}{h} + i \frac{\Im{v(x + i h,y)}}{h}\right]$$

Here, $$h$$ is a real infinitesimally small number.

I want to implement this for a general complex analytic function. I am starting with a complex function as

#include<complex>
#include<iostream>
#include<limits>
using namespace std;

complex <double> f(complex< double > z) { return z*z; }
complex <double> u(complex< double > z) { return real(f(z)); }
complex <double> v(complex< double > z) { return imag(f(z)); }

double h = std::numeric_limits<double>::epsilon();
double h_inv = 1./std::numeric_limits<double>::epsilon();
int main()
{
//Calculating derivative at z = 1 + I;
double x = 1; y = 1;
complex<double> xh{x.,h};
complex<double> z = xh+complex<double>(0,y);
complex<double> dudx = imag(u(z))*h_inv;
complex<double> dvdx = imag(v(z))*h_inv;
complex<double> dfdz{dudx,dvdx};
return 0;
}


This doesn't work as I am taking the real part and imaginary part of $$f$$, both of which are real-valued functions, and I can't get any imaginary part in the complex step method, so dfdz = 0.

• I want this whole process to be numerical so that I don't have to analytically calculate real and imaginary part and plug in the code explicitly

• How to implement a complex step method in this case?

• Well, the second equation doesn't seem right to me. In fact, we have: $$df = du + i dv = \frac{\partial u}{\partial x} dx + \frac{\partial u}{\partial y} dy + i (\frac{\partial v}{\partial x} dx + \frac{\partial v}{\partial y} dy) = (\frac{\partial u}{\partial x} + i \frac{\partial v}{\partial x}) dx + (\frac{\partial u}{\partial y} + i \frac{\partial v}{\partial y}) dy$$ So it seems that you are confusing $\frac{\partial f}{\partial x}$ with $\frac{d f}{dz}$. May 5 at 18:34
• For analytic functions it doesn't matter how you choose your $dz$ it can be $dz = dx + I dy$ or just $dz = dx$ or so on, For example have a look at www1.spms.ntu.edu.sg/~ydchong/teaching/… equation 23 May 5 at 18:45
• Have you heard about directional derivatives? No, it really matters in which direction you want to do differentiation. taking $dz = dx$ is one choice from many possible choices and it is equal to taking gradient along the x direction. May 5 at 18:47
• Sure, but in complex analysis, it really doesn't matter what is your $dz$, if it would have been mattered then the limit in the first principle of derivative (2nd equation in the post) would not exist and also the Cauchy's theorem wouldn't be valid. May 5 at 18:58
• Well, sorry but no. We know $$\frac{df}{dz} = \frac{du + i dv} {dx + i dy} = \frac{(du + i dv) \wedge (dx - i dy)}{(dx + i dy) \wedge (dx - i dy)} = \frac{du \wedge dx - dv \wedge dy + i (du \wedge dy + dv \wedge dx)}{i 2 dx \wedge dy} = \frac{-(\frac{\partial u}{\partial y}+\frac{\partial v}{\partial x}) dx \wedge dy + i (\frac{\partial u}{\partial x} - \frac{\partial v}{\partial y}) dx \wedge dy}{i 2 dx \wedge dy} = \frac{1}{2} (\frac{\partial u}{\partial x} - \frac{\partial v}{\partial y}) + \frac{i}{2} (\frac{\partial u}{\partial y}+\frac{\partial v}{\partial x})$$ May 5 at 19:20