Mathematics
f(z)    Complex Numbers and Functions     Tech Note
Complex Logarithms and Powers

This page shows how to compute the complex natural logarithm and complex exponential.  A variety of related functions are also explained, including square, integer power, real power, complex power, and roots of complex values.

CLn:  Complex natural logarithm

The easiest way to compute the natural logarithm of a complex value, z, is to convert the value to polar form first.


-p < q £ p

The general logarithmic function, Ln(z), is multi-valued since any multiple of 2p can be added to q.  The value computed, ln(z) above, is said to be the principal branch of Ln(z).

CLn function

// complex natural log, exponential
// z = ln(a)
FUNCTION CLn (CONST a:  TComplex):  TComplex;
BEGIN  // Abramowitz formula 4.1.2 on p. 67
  RESULT := CConvert (a,cfPolar);
  RESULT.form := cfRectangular;
  TRY
    RESULT.x := LN(RESULT.r);
    RESULT.y := FixAngle(RESULT.theta)
  EXCEPT
    ON EZeroDivide DO   // LN(0) raises EZeroDivide
      RAISE EComplexLnZero.Create('CLn(0)')
  END
END {CLn};  // NOTE:  principal value only

One way to test the CLn function is to compute CExp(CLn(z)).  Since CExp and CLn are inverse functions, CExp(CLn(z)) = z.

Examples

Complex natural logarithm:  CLn = LN(z)
 
                              LN(z)
          z                rectangular               EXP( LN(z) ) = z
     ------------  ----------------------------  ----------------------------
 1    0.0 +  0.0i           NAN +          NANi           NAN +          NANi
 2    0.5 +  0.5i  -0.346573590 +  0.785398163i   0.500000000 +  0.500000000i
 3   -0.5 +  0.5i  -0.346573590 +  2.356194490i  -0.500000000 +  0.500000000i
 4   -0.5 -  0.5i  -0.346573590 -  2.356194490i  -0.500000000 -  0.500000000i
 5    0.5 -  0.5i  -0.346573590 -  0.785398163i   0.500000000 -  0.500000000i
 6    1.0 +  0.0i   0.000000000 +  0.000000000i   1.000000000 +  0.000000000i
 7    1.0 +  1.0i   0.346573590 +  0.785398163i   1.000000000 +  1.000000000i
 8    0.0 +  1.0i   0.000000000 +  1.570796327i   0.000000000 +  1.000000000i
 9   -1.0 +  1.0i   0.346573590 +  2.356194490i  -1.000000000 +  1.000000000i
10   -1.0 +  0.0i   0.000000000 +  3.141592654i  -1.000000000 +  0.000000000i
11   -1.0 -  1.0i   0.346573590 -  2.356194490i  -1.000000000 -  1.000000000i
12    0.0 -  1.0i   0.000000000 -  1.570796327i   0.000000000 -  1.000000000i
13    1.0 -  1.0i   0.346573590 -  0.785398163i   1.000000000 -  1.000000000i
14    5.0 +  0.0i   1.609437912 +  0.000000000i   5.000000000 +  0.000000000i
15    5.0 +  3.0i   1.763180262 +  0.540419500i   5.000000000 +  3.000000000i
16    0.0 +  3.0i   1.098612289 +  1.570796327i   0.000000000 +  3.000000000i
17   -5.0 +  3.0i   1.763180262 +  2.601173153i  -5.000000000 +  3.000000000i
18   -5.0 +  0.0i   1.609437912 +  3.141592654i  -5.000000000 +  0.000000000i
19   -5.0 -  3.0i   1.763180262 -  2.601173153i  -5.000000000 -  3.000000000i
20    0.0 -  3.0i   1.098612289 -  1.570796327i   0.000000000 -  3.000000000i
21   -5.0 -  3.0i   1.763180262 -  2.601173153i  -5.000000000 -  3.000000000i
 
 
Notes:  1.  LN(z) is multivalued.
          2.  Any multiple of 2*PI*i could be added to/subtracted from LN(z).
          3.  LN(1)=0; LN(-1)=PI*i; LN(+/-i)=+/-0.5*PI*i.
          4.  LN(1 + i) = LN(SQRT(2)) + i*PI/4.

 

CExp:  Complex exponential

CExp function

// z   = exp(a)
FUNCTION CExp  (CONST a:  TComplex):  TComplex;
  VAR
    aTemp:  TComplex;
    rTemp:  TReal;
BEGIN  // Euler's Formula:  Abramowitz formula 4.3.47 on p. 74
  aTemp := CConvert(a,cfRectangular);
  rTemp := EXP(aTemp.x);
  RESULT := CSet(rTemp*COS(aTemp.y),
                 rTemp*SIN(aTemp.y), cfRectangular)
END {CExp};

One way to test the CExp function is to compute CLn(CExp(z)).  Since CExp and CLn are inverse functions, CLn(CExp(z)) = z.

Examples

Complex exponential:  CExp = EXP(z)

                              EXP(z)
          z                rectangular                 LN( EXP(z) ) = z
     ------------  ----------------------------  ----------------------------
 1    0.0 +  0.0i     1.0000000 +    0.0000000i     0.0000000 +    0.0000000i
 2    0.5 +  0.5i     1.4468890 +    0.7904391i     0.5000000 +    0.5000000i
 3   -0.5 +  0.5i     0.5322807 +    0.2907863i    -0.5000000 +    0.5000000i
 4   -0.5 -  0.5i     0.5322807 -    0.2907863i    -0.5000000 -    0.5000000i
 5    0.5 -  0.5i     1.4468890 -    0.7904391i     0.5000000 -    0.5000000i
 6    1.0 +  0.0i     2.7182818 +    0.0000000i     1.0000000 +    0.0000000i
 7    1.0 +  1.0i     1.4686939 +    2.2873553i     1.0000000 +    1.0000000i
 8    0.0 +  1.0i     0.5403023 +    0.8414710i     0.0000000 +    1.0000000i
 9   -1.0 +  1.0i     0.1987661 +    0.3095599i    -1.0000000 +    1.0000000i
10   -1.0 +  0.0i     0.3678794 +    0.0000000i    -1.0000000 +    0.0000000i
11   -1.0 -  1.0i     0.1987661 -    0.3095599i    -1.0000000 -    1.0000000i
12    0.0 -  1.0i     0.5403023 -    0.8414710i     0.0000000 -    1.0000000i
13    1.0 -  1.0i     1.4686939 -    2.2873553i     1.0000000 -    1.0000000i
14    5.0 +  0.0i   148.4131591 +    0.0000000i     5.0000000 +    0.0000000i
15    5.0 +  3.0i  -146.9279139 +   20.9440662i     5.0000000 +    3.0000000i
16    0.0 +  3.0i    -0.9899925 +    0.1411200i     0.0000000 +    3.0000000i
17   -5.0 +  3.0i    -0.0066705 +    0.0009509i    -5.0000000 +    3.0000000i
18   -5.0 +  0.0i     0.0067379 +    0.0000000i    -5.0000000 +    0.0000000i
19   -5.0 -  3.0i    -0.0066705 -    0.0009509i    -5.0000000 -    3.0000000i
20    0.0 -  3.0i    -0.9899925 -    0.1411200i     0.0000000 -    3.0000000i
21   -5.0 -  3.0i    -0.0066705 -    0.0009509i    -5.0000000 -    3.0000000i

 

Complex Powers and Roots

CSqr:  Complex square

CSqr function

// z = SQR(a)
FUNCTION CSqr  (CONST a:  TComplex):  TComplex;
  VAR
    aTemp:  TComplex;
BEGIN
  aTemp := CConvert (a,cfRectangular);
  RESULT.form := cfRectangular;
  RESULT.x := SQR(aTemp.x) - SQR(aTemp.y);   // real part
  RESULT.y := 2*aTemp.x*aTemp.y              // imaginary part
END {CSqr};

Examples

Let  u =   1.000000000 +  1.000000000i  =   1.414213562 * CIS(  0.785398163)
     v =   1.732050808 -  1.000000000i  =   2.000000000 * CIS( -0.523598776)
 
u^2    =   0.000000000 +  2.000000000i  =   2.000000000 * CIS(  1.570796327)
v^2    =   2.000000000 -  3.464101615i  =   4.000000000 * CIS( -1.047197551)

 

CIntPower:  Complex value to integer power

Because there is significant overhead in CPower (see below) due to the complex functions involved, there are more efficient ways to compute powers of a complex value when the exponent is a real number or an integer.  DeMoivre’s theorem defines an integer power of a complex value:

The angle nq is kept in the “standard” range, -p < nq £ p, by adding (or subtracting) a multiple of 2p.

The computation of rn can be made using the math library function IntPower.

CIntPower function

// z = a^n
// CIntPower directly applies DeMoivre's theorem to calculate
// an integer power of a complex number.  The formula holds
// for both positive and negative values of 'n'.
FUNCTION CIntPower (CONST a:  TComplex; CONST n:  INTEGER):  TComplex;
  VAR
    aTemp:  TComplex;
BEGIN
  IF   CAbsSqr(a) = 0.0
  THEN
    IF    n = 0
    THEN  RAISE EComplexZeroToZero.Create('IntPower')
    ELSE  RESULT := ComplexZero    // 0^n = 0, except for 0^0
  ELSE BEGIN
    aTemp := CConvert (a,cfPolar);
    RESULT.form := cfPolar;
{$IFDEF MATH}
    RESULT.r := IntPower(aTemp.r,n);
{$ELSE}
    RESULT.r := Power(aTemp.r,n);
{$ENDIF}
    RESULT.theta := FixAngle(n*aTemp.theta)
  END
END {CIntPower};

Examples

Complex integer powers using DeMoivre's theorem:  CIntPower = z^n
 
                               z^3                             z^3
          z                rectangular                        polar
     ------------  ----------------------------   -----------------------------
 1    0.0 +  0.0i     0.0000000 +    0.0000000i     0.0000000 * CIS( 0.0000000)
 2    0.5 +  0.5i    -0.2500000 +    0.2500000i     0.3535534 * CIS( 2.3561945)
 3   -0.5 +  0.5i     0.2500000 +    0.2500000i     0.3535534 * CIS( 0.7853982)
 4   -0.5 -  0.5i     0.2500000 -    0.2500000i     0.3535534 * CIS(-0.7853982)
 5    0.5 -  0.5i    -0.2500000 -    0.2500000i     0.3535534 * CIS(-2.3561945)
 6    1.0 +  0.0i     1.0000000 +    0.0000000i     1.0000000 * CIS( 0.0000000)
 7    1.0 +  1.0i    -2.0000000 +    2.0000000i     2.8284271 * CIS( 2.3561945)
 8    0.0 +  1.0i     0.0000000 -    1.0000000i     1.0000000 * CIS(-1.5707963)
 9   -1.0 +  1.0i     2.0000000 +    2.0000000i     2.8284271 * CIS( 0.7853982)
10   -1.0 +  0.0i    -1.0000000 +    0.0000000i     1.0000000 * CIS( 3.1415927)
11   -1.0 -  1.0i     2.0000000 -    2.0000000i     2.8284271 * CIS(-0.7853982)
12    0.0 -  1.0i     0.0000000 +    1.0000000i     1.0000000 * CIS( 1.5707963)
13    1.0 -  1.0i    -2.0000000 -    2.0000000i     2.8284271 * CIS(-2.3561945)
14    5.0 +  0.0i   125.0000000 +    0.0000000i   125.0000000 * CIS( 0.0000000)
15    5.0 +  3.0i   -10.0000000 +  198.0000000i   198.2523644 * CIS( 1.6212585)
16    0.0 +  3.0i     0.0000000 -   27.0000000i    27.0000000 * CIS(-1.5707963)
17   -5.0 +  3.0i    10.0000000 +  198.0000000i   198.2523644 * CIS( 1.5203342)
18   -5.0 +  0.0i  -125.0000000 +    0.0000000i   125.0000000 * CIS( 3.1415927)
19   -5.0 -  3.0i    10.0000000 -  198.0000000i   198.2523644 * CIS(-1.5203342)
20    0.0 -  3.0i     0.0000000 +   27.0000000i    27.0000000 * CIS( 1.5707963)
21   -5.0 -  3.0i    10.0000000 -  198.0000000i   198.2523644 * CIS(-1.5203342)

 

CRealPower:  Complex value to real exponent

While normally integers are used with DeMoivre’s theorem, real values work quite nicely, also.

The computation of rx can be made using the math library function Power.

CRealPower function

// z = a^x
FUNCTION CRealPower (CONST a:  TComplex; CONST x:  TReal):  TComplex;
  VAR
    aTemp:  TComplex;
BEGIN
  IF   CAbsSqr(a) = 0.0
  THEN
    IF    Defuzz(x) = 0.0
    THEN  RAISE EComplexZeroToZero.Create('RealPower')
    ELSE  RESULT := ComplexZero    // 0^n = 0, except for 0^0
  ELSE BEGIN
    aTemp := CConvert (a,cfPolar);
    RESULT.form := cfPolar;
    RESULT.r := Power(aTemp.r,x);
    RESULT.theta := FixAngle(x*aTemp.theta)
  END
END {CRealPower};

Examples

Complex real powers using DeMoivre's theorem:  CRealPower = z^x
 
                               z^1.5                           z^1.5
          z                rectangular                         polar
     ------------  ----------------------------   -----------------------------
 1    0.0 +  0.0i     0.0000000 +    0.0000000i     0.0000000 * CIS( 0.0000000)
 2    0.5 +  0.5i     0.2275449 +    0.5493421i     0.5946036 * CIS( 1.1780972)
 3   -0.5 +  0.5i    -0.5493421 -    0.2275449i     0.5946036 * CIS(-2.7488936)
 4   -0.5 -  0.5i    -0.5493421 +    0.2275449i     0.5946036 * CIS( 2.7488936)
 5    0.5 -  0.5i     0.2275449 -    0.5493421i     0.5946036 * CIS(-1.1780972)
 6    1.0 +  0.0i     1.0000000 +    0.0000000i     1.0000000 * CIS( 0.0000000)
 7    1.0 +  1.0i     0.6435943 +    1.5537740i     1.6817928 * CIS( 1.1780972)
 8    0.0 +  1.0i    -0.7071068 +    0.7071068i     1.0000000 * CIS( 2.3561945)
 9   -1.0 +  1.0i    -1.5537740 -    0.6435943i     1.6817928 * CIS(-2.7488936)
10   -1.0 +  0.0i     0.0000000 -    1.0000000i     1.0000000 * CIS(-1.5707963)
11   -1.0 -  1.0i     1.5537740 -    0.6435943i     1.6817928 * CIS(-0.3926991)
12    0.0 -  1.0i    -0.7071068 -    0.7071068i     1.0000000 * CIS(-2.3561945)
13    1.0 -  1.0i     0.6435943 -    1.5537740i     1.6817928 * CIS(-1.1780972)
14    5.0 +  0.0i    11.1803399 +    0.0000000i    11.1803399 * CIS( 0.0000000)
15    5.0 +  3.0i     9.7018649 +   10.2042237i    14.0802118 * CIS( 0.8106293)
16    0.0 +  3.0i    -3.6742346 +    3.6742346i     5.1961524 * CIS( 2.3561945)
17   -5.0 +  3.0i   -10.2042237 -    9.7018649i    14.0802118 * CIS(-2.3814256)
18   -5.0 +  0.0i     0.0000000 -   11.1803399i    11.1803399 * CIS(-1.5707963)
19   -5.0 -  3.0i   -10.2042237 +    9.7018649i    14.0802118 * CIS( 2.3814256)
20    0.0 -  3.0i    -3.6742346 -    3.6742346i     5.1961524 * CIS(-2.3561945)
21   -5.0 -  3.0i   -10.2042237 +    9.7018649i    14.0802118 * CIS( 2.3814256)

 

CPower:  Complex value to complex power

Computation of CPower involves using the complex functions CExp and CLn.

CPower function

// z = a^b
// Exception raised for 0^0.  Sometimes this limit is appropriate as
// a value:  lim a^a = 1 as a -> 0
FUNCTION CPower  (CONST a,b:  TComplex):  TComplex;
  VAR
    blna,lna:  TComplex;
BEGIN  // Abramowitz formula 4.2.7 on p. 69
  CDeFuzz (a);
  CDeFuzz (b);
  IF   CAbsSqr(a) = 0.0
  THEN
    IF    CAbsSqr(b) = 0.0
    THEN  RAISE EComplexZeroToZero.Create('Power')
    ELSE  RESULT := ComplexZero    // 0^b = 0, b <> 0
  ELSE BEGIN
    lna := CLn(a);
    blna := CMult(b, lna);
    RESULT := CExp(blna)
  END
END {CPower};

Examples

Complex powers:  CPower = z1^z2
 
                             z^(-1+i)                        z^(-1+i)
          z                rectangular                        polar
     ------------  ----------------------------   --------------------------------
 1    0.0 +  0.0i   0.000000000 +  0.000000000i   0.000000000 * CIS(  0.000000000)
 2    0.5 +  0.5i   0.273957254 -  0.583700759i   0.644793884 * CIS( -1.131971754)
 3   -0.5 +  0.5i  -0.121339466 -  0.056950118i   0.134039479 * CIS( -2.702768080)
 4   -0.5 -  0.5i  -6.339560605 + 13.507239848i  14.920977079 * CIS(  2.009620900)
 5    0.5 -  0.5i   2.807879297 +  1.317865173i   3.101766394 * CIS(  0.438824573)
 6    1.0 +  0.0i   1.000000000 +  0.000000000i   1.000000000 * CIS(  0.000000000)
 7    1.0 +  1.0i   0.291850379 -  0.136978627i   0.322396942 * CIS( -0.438824573)
 8    0.0 +  1.0i   0.000000000 -  0.207879576i   0.207879576 * CIS( -1.570796327)
 9   -1.0 +  1.0i  -0.028475059 -  0.060669733i   0.067019740 * CIS( -2.009620900)
10   -1.0 +  0.0i  -0.043213918 +  0.000000000i   0.043213918 * CIS( -3.141592654)
11   -1.0 -  1.0i  -6.753619924 +  3.169780303i   7.460488539 * CIS(  2.702768080)
12    0.0 -  1.0i   0.000000000 +  4.810477381i   4.810477381 * CIS(  1.570796327)
13    1.0 -  1.0i   0.658932586 +  1.403939649i   1.550883197 * CIS(  1.131971754)
14    5.0 +  0.0i  -0.007726394 +  0.199850701i   0.200000000 * CIS(  1.609437912)
15    5.0 +  3.0i   0.034070593 +  0.093909115i   0.099898585 * CIS(  1.222760762)
16    0.0 +  3.0i   0.061710926 -  0.031516790i   0.069293192 * CIS( -0.472184038)
17   -5.0 +  3.0i   0.008511047 -  0.009456941i   0.012722879 * CIS( -0.837992891)
18   -5.0 +  0.0i   0.000333888 -  0.008636332i   0.008642784 * CIS( -1.532154741)
19   -5.0 -  3.0i  -0.788417122 -  2.173121965i   2.311722438 * CIS( -1.918831892)
20    0.0 -  3.0i  -1.428033572 +  0.729320361i   1.603492460 * CIS(  2.669408615)
21   -5.0 -  3.0i  -0.788417122 -  2.173121965i   2.311722438 * CIS( -1.918831892)

 

CRoot:  The roots of a complex value


k = 0, 1, 2, …, n-1

CRoot function

// z = a^(1/n), n > 0
// CRoot can calculate all 'n' roots of 'a' by varying 'k' from 0..n-1.
// This is another application of DeMoivre's theorem.  See CIntPower.
FUNCTION CRoot (CONST a:  TComplex; CONST k,n:  WORD):  TComplex;
  VAR
    aTemp:  TComplex;
BEGIN
  IF   CAbs(a) = 0.0
  THEN RESULT := ComplexZero   // 0^z = 0, except 0^0 is undefined
  ELSE BEGIN
    aTemp := CConvert (a,cfPolar);
    RESULT.form := cfPolar;
    RESULT.r := Power(aTemp.r,1.0/n);
    RESULT.theta := FixAngle((aTemp.theta + k*2.0*PI)/n)
  END
END {CRoot};

Examples

Complex roots (CRoot) from DeMoivre's theorem:  Square Roots
 
                             SQRT(z)                         SQRT(z)
          z                  root 1                          root 2
     ------------  ----------------------------  ----------------------------
 1    0.0 +  0.0i   0.000000000 +  0.000000000i   0.000000000 +  0.000000000i
 2    0.5 +  0.5i   0.776886987 +  0.321797126i  -0.776886987 -  0.321797126i
 3   -0.5 +  0.5i   0.321797126 +  0.776886987i  -0.321797126 -  0.776886987i
 4   -0.5 -  0.5i   0.321797126 -  0.776886987i  -0.321797126 +  0.776886987i
 5    0.5 -  0.5i   0.776886987 -  0.321797126i  -0.776886987 +  0.321797126i
 6    1.0 +  0.0i   1.000000000 +  0.000000000i  -1.000000000 +  0.000000000i
 7    1.0 +  1.0i   1.098684113 +  0.455089861i  -1.098684113 -  0.455089861i
 8    0.0 +  1.0i   0.707106781 +  0.707106781i  -0.707106781 -  0.707106781i
 9   -1.0 +  1.0i   0.455089861 +  1.098684113i  -0.455089861 -  1.098684113i
10   -1.0 +  0.0i   0.000000000 +  1.000000000i   0.000000000 -  1.000000000i
11   -1.0 -  1.0i  -0.455089861 +  1.098684113i   0.455089861 -  1.098684113i
12    0.0 -  1.0i   0.707106781 -  0.707106781i  -0.707106781 +  0.707106781i
13    1.0 -  1.0i   1.098684113 -  0.455089861i  -1.098684113 +  0.455089861i
14    5.0 +  0.0i   2.236067977 +  0.000000000i  -2.236067977 +  0.000000000i
15    5.0 +  3.0i   2.327117519 +  0.644574237i  -2.327117519 -  0.644574237i
16    0.0 +  3.0i   1.224744871 +  1.224744871i  -1.224744871 -  1.224744871i
17   -5.0 +  3.0i   0.644574237 +  2.327117519i  -0.644574237 -  2.327117519i
18   -5.0 +  0.0i   0.000000000 +  2.236067977i   0.000000000 -  2.236067977i
19   -5.0 -  3.0i   0.644574237 -  2.327117519i  -0.644574237 +  2.327117519
20    0.0 -  3.0i   1.224744871 -  1.224744871i  -1.224744871 +  1.224744871i
21   -5.0 -  3.0i   0.644574237 -  2.327117519i  -0.644574237 +  2.327117519i
 
--------------------------------------------------------------------------------

 Complex roots:  Cube Roots
 
                             z^(1/3)                         z^(1/3)
          z                rectangular                        polar
     ------------  ----------------------------   -------------------------------
 0   -1.0 +  1.0i   0.793700526 +  0.793700526i   1.122462048 * CIS( 0.785398163)
 1   -1.0 +  1.0i  -1.084215081 +  0.290514556i   1.122462048 * CIS( 2.879793266)
 2   -1.0 +  1.0i   0.290514556 -  1.084215081i   1.122462048 * CIS(-1.308996939)
 
From Schaum's Outline Series "Complex Variables," 1964, p. 18, problem 29.

 

CSqrt:  Complex Square Root

The two complex square roots can be obtained using the CRoot function.  One of the square roots can be computed using the CRealPower function, since

CSqrt function

// z = SQRT(a)
FUNCTION CSqrt (CONST a:  TComplex):  TComplex;
BEGIN
  RESULT := CRealPower(a, 0.5)
END {CSqrt};

 


Download ComplexMathLibrary


Updated 18 Feb 2002


since 24 June 2001