The functions described in this chapter provide support for complex numbers.  The algorithms take care to avoid unnecessary intermediate underflows and overflows, allowing the functions to be evaluated over as much of the complex plane as possible.

For multiple-valued functions the branch cuts have been chosen to follow the conventions of Abramowitz and Stegun in the Handbook of Mathematical Functions. The functions return principal values which are the same as those in GNU Calc, which in turn are the same as those in Common Lisp, The Language (Second Edition) (Note that the first edition uses different definitions.) and the HP-28/48 series of calculators.

The complex types are defined in the header file gsl_complex.h, while the corresponding complex functions and arithmetic operations are defined in gsl_complex_math.h.

Complex numbers
===============

Complex numbers are represented using the type gsl_complex. The internal representation of this type may vary across platforms and should not be accessed directly. The functions and macros described below allow complex numbers to be manipulated in a portable way.

For reference, the default form of the gsl_complex type is given by the following struct,

     typedef struct
     {
       double dat[2];
     } gsl_complex;

The real and imaginary part are stored in contiguous elements of a two element array. This eliminates any padding between the real and imaginary parts, dat[0] and dat[1], allowing the struct to be mapped correctly onto packed complex arrays.

 -- Function: gsl_complex gsl_complex_rect (double X, double Y)
     This function uses the rectangular cartesian components (X,Y) to return the complex number z = x + i y.

 -- Function: gsl_complex gsl_complex_polar (double R, double THETA)
     This function returns the complex number z = r \exp(i \theta) = r (\cos(\theta) + i \sin(\theta)) from the polar representation (R,THETA).

 -- Macro: GSL_REAL (Z)
 -- Macro: GSL_IMAG (Z)
     These macros return the real and imaginary parts of the complex number Z.

 -- Macro: GSL_SET_COMPLEX (ZP, X, Y)
     This macro uses the cartesian components (X,Y) to set the real and imaginary parts of the complex number pointed to by ZP. For example,

          GSL_SET_COMPLEX(&z, 3, 4)

     sets Z to be 3 + 4i.

 -- Macro: GSL_SET_REAL (ZP,X)
 -- Macro: GSL_SET_IMAG (ZP,Y)
     These macros allow the real and imaginary parts of the complex number pointed to by ZP to be set independently.

Properties of complex numbers
=============================

 -- Function: double gsl_complex_arg (gsl_complex Z)
     This function returns the argument of the complex number Z, \arg(z), where $-\pi < \arg(z) \leq \pi$ -\pi < \arg(z) <= \pi.

 -- Function: double gsl_complex_abs (gsl_complex Z)
     This function returns the magnitude of the complex number Z, |z|.

 -- Function: double gsl_complex_abs2 (gsl_complex Z)
     This function returns the squared magnitude of the complex number Z, |z|^2.

 -- Function: double gsl_complex_logabs (gsl_complex Z)
     This function returns the natural logarithm of the magnitude of the complex number Z, \log|z|.  It allows an accurate evaluation of \log|z| when |z| is close to one. The direct evaluation of log(gsl_complex_abs(z)) would lead to a loss of precision in this case.

Complex arithmetic operators
============================

 -- Function: gsl_complex gsl_complex_add (gsl_complex A, gsl_complex B)
     This function returns the sum of the complex numbers A and B, z=a+b.

 -- Function: gsl_complex gsl_complex_sub (gsl_complex A, gsl_complex B)
     This function returns the difference of the complex numbers A and B, z=a-b.

 -- Function: gsl_complex gsl_complex_mul (gsl_complex A, gsl_complex B)
     This function returns the product of the complex numbers A and B, z=ab.

 -- Function: gsl_complex gsl_complex_div (gsl_complex A, gsl_complex B)
     This function returns the quotient of the complex numbers A and B, z=a/b.

 -- Function: gsl_complex gsl_complex_add_real (gsl_complex A, double X)
     This function returns the sum of the complex number A and the real number X, z=a+x.

 -- Function: gsl_complex gsl_complex_sub_real (gsl_complex A, double X)
     This function returns the difference of the complex number A and the real number X, z=a-x.

 -- Function: gsl_complex gsl_complex_mul_real (gsl_complex A, double X)
     This function returns the product of the complex number A and the real number X, z=ax.

 -- Function: gsl_complex gsl_complex_div_real (gsl_complex A, double X)
     This function returns the quotient of the complex number A and the real number X, z=a/x.

 -- Function: gsl_complex gsl_complex_add_imag (gsl_complex A, double Y)
     This function returns the sum of the complex number A and the imaginary number iY, z=a+iy.

 -- Function: gsl_complex gsl_complex_sub_imag (gsl_complex A, double Y)
     This function returns the difference of the complex number A and the imaginary number iY, z=a-iy.

 -- Function: gsl_complex gsl_complex_mul_imag (gsl_complex A, double Y)
     This function returns the product of the complex number A and the imaginary number iY, z=a*(iy).

 -- Function: gsl_complex gsl_complex_div_imag (gsl_complex A, double Y)
     This function returns the quotient of the complex number A and the imaginary number iY, z=a/(iy).

 -- Function: gsl_complex gsl_complex_conjugate (gsl_complex Z)
     This function returns the complex conjugate of the complex number Z, z^* = x - i y.

 -- Function: gsl_complex gsl_complex_inverse (gsl_complex Z)
     This function returns the inverse, or reciprocal, of the complex number Z, 1/z = (x - i y)/(x^2 + y^2).

 -- Function: gsl_complex gsl_complex_negative (gsl_complex Z)
     This function returns the negative of the complex number Z, -z = (-x) + i(-y).

Elementary Complex Functions
============================

 -- Function: gsl_complex gsl_complex_sqrt (gsl_complex Z)
     This function returns the square root of the complex number Z, \sqrt z. The branch cut is the negative real axis. The result always lies in the right half of the complex plane.

 -- Function: gsl_complex gsl_complex_sqrt_real (double X)
     This function returns the complex square root of the real number X, where X may be negative.

 -- Function: gsl_complex gsl_complex_pow (gsl_complex Z, gsl_complex A)
     The function returns the complex number Z raised to the complex power A, z^a. This is computed as \exp(\log(z)*a) using complex logarithms and complex exponentials.

 -- Function: gsl_complex gsl_complex_pow_real (gsl_complex Z, double X)
     This function returns the complex number Z raised to the real power X, z^x.

 -- Function: gsl_complex gsl_complex_exp (gsl_complex Z)
     This function returns the complex exponential of the complex number Z, \exp(z).

 -- Function: gsl_complex gsl_complex_log (gsl_complex Z)
     This function returns the complex natural logarithm (base e) of the complex number Z, \log(z).  The branch cut is the negative real axis.

 -- Function: gsl_complex gsl_complex_log10 (gsl_complex Z)
     This function returns the complex base-10 logarithm of the complex number Z, $\log_10(z)$ \log_10 (z).

 -- Function: gsl_complex gsl_complex_log_b (gsl_complex Z, gsl_complex B)
     This function returns the complex base-B logarithm of the complex number Z, \log_b(z). This quantity is computed as the ratio \log(z)/\log(b).

Complex Trigonometric Functions
===============================

 -- Function: gsl_complex gsl_complex_sin (gsl_complex Z)
     This function returns the complex sine of the complex number Z, \sin(z) = (\exp(iz) - \exp(-iz))/(2i).

 -- Function: gsl_complex gsl_complex_cos (gsl_complex Z)
     This function returns the complex cosine of the complex number Z, \cos(z) = (\exp(iz) + \exp(-iz))/2.

 -- Function: gsl_complex gsl_complex_tan (gsl_complex Z)
     This function returns the complex tangent of the complex number Z, \tan(z) = \sin(z)/\cos(z).

 -- Function: gsl_complex gsl_complex_sec (gsl_complex Z)
     This function returns the complex secant of the complex number Z, \sec(z) = 1/\cos(z).

 -- Function: gsl_complex gsl_complex_csc (gsl_complex Z)
     This function returns the complex cosecant of the complex number Z, \csc(z) = 1/\sin(z).

 -- Function: gsl_complex gsl_complex_cot (gsl_complex Z)
     This function returns the complex cotangent of the complex number Z, \cot(z) = 1/\tan(z).

Inverse Complex Trigonometric Functions
=======================================

 -- Function: gsl_complex gsl_complex_arcsin (gsl_complex Z)
     This function returns the complex arcsine of the complex number Z, \arcsin(z). The branch cuts are on the real axis, less than -1 and greater than 1.

 -- Function: gsl_complex gsl_complex_arcsin_real (double Z)
     This function returns the complex arcsine of the real number Z, \arcsin(z). For z between -1 and 1, the function returns a real value in the range [-\pi/2,\pi/2]. For z less than -1 the result has a real part of -\pi/2 and a positive imaginary part.  For z greater than 1 the result has a real part of \pi/2 and a negative imaginary part.

 -- Function: gsl_complex gsl_complex_arccos (gsl_complex Z)
     This function returns the complex arccosine of the complex number Z, \arccos(z). The branch cuts are on the real axis, less than -1 and greater than 1.

 -- Function: gsl_complex gsl_complex_arccos_real (double Z)
     This function returns the complex arccosine of the real number Z, \arccos(z). For z between -1 and 1, the function returns a real value in the range [0,\pi]. For z less than -1 the result has a real part of \pi and a negative imaginary part.  For z greater than 1 the result is purely imaginary and positive.

 -- Function: gsl_complex gsl_complex_arctan (gsl_complex Z)
     This function returns the complex arctangent of the complex number Z, \arctan(z). The branch cuts are on the imaginary axis, below -i and above i.

 -- Function: gsl_complex gsl_complex_arcsec (gsl_complex Z)
     This function returns the complex arcsecant of the complex number Z, \arcsec(z) = \arccos(1/z).

 -- Function: gsl_complex gsl_complex_arcsec_real (double Z)
     This function returns the complex arcsecant of the real number Z, \arcsec(z) = \arccos(1/z).

 -- Function: gsl_complex gsl_complex_arccsc (gsl_complex Z)
     This function returns the complex arccosecant of the complex number Z, \arccsc(z) = \arcsin(1/z).

 -- Function: gsl_complex gsl_complex_arccsc_real (double Z)
     This function returns the complex arccosecant of the real number Z, \arccsc(z) = \arcsin(1/z).

 -- Function: gsl_complex gsl_complex_arccot (gsl_complex Z)
     This function returns the complex arccotangent of the complex number Z, \arccot(z) = \arctan(1/z).

Complex Hyperbolic Functions
============================

 -- Function: gsl_complex gsl_complex_sinh (gsl_complex Z)
     This function returns the complex hyperbolic sine of the complex number Z, \sinh(z) = (\exp(z) - \exp(-z))/2.

 -- Function: gsl_complex gsl_complex_cosh (gsl_complex Z)
     This function returns the complex hyperbolic cosine of the complex number Z, \cosh(z) = (\exp(z) + \exp(-z))/2.

 -- Function: gsl_complex gsl_complex_tanh (gsl_complex Z)
     This function returns the complex hyperbolic tangent of the complex number Z, \tanh(z) = \sinh(z)/\cosh(z).

 -- Function: gsl_complex gsl_complex_sech (gsl_complex Z)
     This function returns the complex hyperbolic secant of the complex number Z, \sech(z) = 1/\cosh(z).

 -- Function: gsl_complex gsl_complex_csch (gsl_complex Z)
     This function returns the complex hyperbolic cosecant of the complex number Z, \csch(z) = 1/\sinh(z).

 -- Function: gsl_complex gsl_complex_coth (gsl_complex Z)
     This function returns the complex hyperbolic cotangent of the complex number Z, \coth(z) = 1/\tanh(z).

Inverse Complex Hyperbolic Functions
====================================

 -- Function: gsl_complex gsl_complex_arcsinh (gsl_complex Z)
     This function returns the complex hyperbolic arcsine of the complex number Z, \arcsinh(z).  The branch cuts are on the imaginary axis, below -i and above i.

 -- Function: gsl_complex gsl_complex_arccosh (gsl_complex Z)
     This function returns the complex hyperbolic arccosine of the complex number Z, \arccosh(z).  The branch cut is on the real axis, less than 1.  Note that in this case we use the negative square root in formula 4.6.21 of Abramowitz & Stegun giving $\arccosh(z)=\log(z-\sqrtz^2-1)$ \arccosh(z)=\log(z-\sqrt{z^2-1}).

 -- Function: gsl_complex gsl_complex_arccosh_real (double Z)
     This function returns the complex hyperbolic arccosine of the real number Z, \arccosh(z).

 -- Function: gsl_complex gsl_complex_arctanh (gsl_complex Z)
     This function returns the complex hyperbolic arctangent of the complex number Z, \arctanh(z).  The branch cuts are on the real axis, less than -1 and greater than 1.

 -- Function: gsl_complex gsl_complex_arctanh_real (double Z)
     This function returns the complex hyperbolic arctangent of the real number Z, \arctanh(z).

 -- Function: gsl_complex gsl_complex_arcsech (gsl_complex Z)
     This function returns the complex hyperbolic arcsecant of the complex number Z, \arcsech(z) = \arccosh(1/z).

 -- Function: gsl_complex gsl_complex_arccsch (gsl_complex Z)
     This function returns the complex hyperbolic arccosecant of the complex number Z, \arccsch(z) = \arcsin(1/z).

 -- Function: gsl_complex gsl_complex_arccoth (gsl_complex Z)
     This function returns the complex hyperbolic arccotangent of the complex number Z, \arccoth(z) = \arctanh(1/z).

References and Further Reading
==============================

The implementations of the elementary and trigonometric functions are based on the following papers,

  T. E. Hull, Thomas F. Fairgrieve, Ping Tak Peter Tang, ``Implementing Complex Elementary Functions Using Exception Handling'', ACM Transactions on Mathematical Software, Volume 20 (1994), pp 215--244, Corrigenda, p553

  T. E. Hull, Thomas F. Fairgrieve, Ping Tak Peter Tang, ``Implementing the complex arcsin and arccosine functions using exception handling'', ACM Transactions on Mathematical Software, Volume 23 (1997) pp 299--335

The general formulas and details of branch cuts can be found in the following books,

  Abramowitz and Stegun, Handbook of Mathematical Functions, ``Circular Functions in Terms of Real and Imaginary Parts'', Formulas 4.3.55--58, ``Inverse Circular Functions in Terms of Real and Imaginary Parts'', Formulas 4.4.37--39, ``Hyperbolic Functions in Terms of Real and Imaginary Parts'', Formulas 4.5.49--52, ``Inverse Hyperbolic Functions---relation to Inverse Circular Functions'', Formulas 4.6.14--19.

  Dave Gillespie, Calc Manual, Free Software Foundation, ISBN 1-882114-18-3