A small complex package

I have written a small (inefficient; why is it?) package which you can use to compute with complex numbers. Fortran has a built-in datatype for complex number, for C has not.

Here is a list of the prototypes and a short description of the routines.

To make the prototypes (and the definition of the type Complex) available to your program include the following line in the beginning:

#include "defs.h"

 

Complex Add(Complex z, Complex w)

Returns z + w.

Complex Sub(Complex z, Complex w)

Returns z - w.

Complex Mul(Complex z, Complex w)

Returns z * w.

Complex Div(Complex z, Complex w)

Returns z / w.

 

In the following calls a is not complex but real.

Complex Adds(double a, Complex z)

Returns a + z.

Complex Muls(double a, Complex z)

Returns a * z.

Complex Divs(double a, Complex z)

Returns a / z.

void PrintC(Complex z)

Print z.

double Cabs(Complex z, Complex w)

Returns |real(z) - real(w)| + |imag(z) - imag(w)|. Can be used to terminate the iteration (if two iterands are close enough, or the iterand is close to a root).


Complex Exp(Complex z);

Returns exp(z).

Complex Double2Complex(double re, double im)

Return the complex number having real part re and imaginary part im.

A complex number can be created using  the following syntax (using the function Double2Complex):

Complex z, w = {1.0, -2.5};

z = Double2Complex(2.3, 4.5);

A small example program.


Back