Implementing complex functions with real functions

These notes show how to implement the basic functions of a complex variable using only functions of a real variable. You could use these notes to extend a math library that only supports real functions of a real variable into one that supports complex functions of a complex variable.

In each equation, z = x + iy where x and y are real. All functions on the right-hand sides are evaluated only at real values.

The equations come from the paper Less Complex Elementary Functions by Henry G. Baker. ACM SIGPLAN Notices, vol. 27, no. 11, Nov. 1992, pp. 15–16. DOI: 10.1145/141018.141022

Note that these equations, while exact in theory, may not be the best for numerical implementation. They are not necessarily ideal in terms of efficiency or floating point accuracy.

\begin{align*}
  |z|         &= \sqrt{x^2 + y^2} \\    
  \exp(z)     &= \exp(x)(\cos(y) + i \sin(y)) \\
  \log(z)     &= \log(|z|^2)/2 + i\, \text{atan2}(y, x) \\
  \sqrt{z}    &= \sqrt{\frac{|z| + x}{2}} + i\, \text{sgn}(y) \sqrt{\frac{|z| - x}{2}} \\
  \sin(z)     &= \sin(x) \cosh(y) + i\cos(x) \sinh(y) \\
  \cos(z)     &= \cos(x) \cosh(y) - i\sin(x) \sinh(y) \\
  \tan(z)     &= \frac{\sin(2x)}{\cos(2x) + \cosh(2y)} + i \frac{\tanh(2y)}{1 + \dfrac{\cos(2x)}{\cosh(2y)}} \\
  \sinh(z)    &= \sinh(x) \cos(y) + i\cosh(x)\sin(y) \\
  \cosh(z)    &= \cosh(x) \cos(y) + i\sinh(x)\sin(y) \\
  \tanh(z)    &= \frac{\tanh(2x)}{1 + \dfrac{\cos(2y)}{\cosh(2x)}} + i \frac{\sin(2y)}{\cosh(2x) + \cos(2y)} \\
  \arcsin(z)  &= \frac{\text{sgn}(x)}{2} \arccos\left( \sqrt{(|z|^2 - 1)^2 + 4y^2} - |z|^2\right) + i\frac{\text{sgn}(y)}{2} \text{arccosh}\left(\sqrt{(|z|^2-1)^2 + 4y^2} + |z|^2 \right) \\
  \arccos(z)  &= \frac{\pi}{2} - \arcsin(z)\\
  \arctan(z)  &= \frac{1}{2} \left(
  \begin{cases}
    \text{atan2}(2x, 1 - |z|^2), & x \ne 0, \\
    \pi\, \text{sgn}(y), & |y|>1, \\
    0 & |y| \leq 1  \end{cases} \right) + \frac{i}{2} \text{arctanh}\left(\frac{2y}{1 + |z|^2} \right)\\
  \text{arcsinh}(z) &= \frac{\text{sgn}(x)}{2} \text{arccosh}\left(\sqrt{(|z|^2 -1)^2 + 4x^2} + |z|^2)\right)  = i \frac{\text{sgn}(y)}{2} \arccos(\sqrt{(|z|^2 - 1)^2 + 4x^2} -|z|^2) \\
  \text{arccosh}(z) &= \frac{1}{2} \text{arccosh}\left(\sqrt{(|z|^2 -1)^2 + 4y^2} + |z|^2)\right)  + i \frac{\text{sgn}(y)}{2} \left(\pi - \text{sgn}(x) \arccos(\sqrt{(|z|^2 - 1)^2 + 4y^2} -|z|^2)\right) \\
  \text{arctanh}(z)  &= \frac{1}{2} \text{arctanh}\left(\frac{2x}{1 + |z|^2} \right) + \frac{i}{2}
  \begin{cases}
    \text{atan2}(2y, 1 - |z|^2), & y \ne 0, \\
   - \pi\, \text{sgn}(x), & |x|>1, \\
    0 & |y| \leq 1  \end{cases} \\
    \end{align*}