Using Complex Numbers


Introduction.

A number of students have asked how to perform mathematical operations with complex numbers. C does not include a standard library for complex number arithmetic; someone has to write such functions.

All files discussed in the following are on this page. You are encouraged to copy routines as required to your floppy.

I wrote a number of functions which will accommodate most of your needs. Refer to cplx.c.

Do not let these routines intimidate you. A type Complex is defined which consists of .r (real) and .I (imaginary) components. Refer to function C_add. Note that it simply adds the real and imaginary components and returns the result. Each function is pretty simple.

Carefully review the description of the various functions at the top of cplx.c. Note that all angles are in radians.

A Project.

Note that cplx.c is simply a collection of functions; there is no main. Of course, you could add a main to this file so as to use the functions in the same manner as you have been doing.

However, there is a problem. As you develop more and more standard functions, including them all in your program leads to programs with thousands and thousands of lines of code which you really are not concerned with. You just want to use a function; you don't really care how the function works.

The solution is a PROJECT. CPLX.C is a stand alone routine which consists of simply functions. cplx.h is a header file which consists of the declarations contained in cplx.c.

Refer to tst_cplx.c which is quite short. Note that it uses struct Complex and functions C_para, C_add, C_div, C_mul and C_Mag. How does the compiler know about these functions.

Two ways. Note;

#include "a:cplx.h"

The < > are used to indicate that the .h file is in the \turboc\include directory. The quotes indicate it is somewhere else; in this case on the a: drive.

The presence of this #include statement lets you compile tst_cplx.c. The compiler doesn't know what the functions actually do, but the declarations are there to indicate what each function expects and returns which is sufficient for the compiler to compile tst_cplx.c.

In addition, you prepare a project file, having any name you desire with a .prj extension; for example xxx.prj. The file consists of two lines;

     a:cplx.c
     a:tst_cplx.c

You then tell the compiler what the project file is by using the alt-P command, which prompts you for the project file name.

You then go ahead and compile pretty much as you have been doing, except when you alt-C, you Make rather than Compile. The compiler then compiles a:cplx.c and a:tst_cplx.c and links the two together as if they were all in one program.

This may sound difficult and confusing. In fact, it is not and once you have learned it, you have a powerful tool, for you can then go ahead and develop your own libraries of useful functions.

tst_cplx.c.

Program tst_cplx.c calculates the magnitude response of a network consisting of a series 1.0K R and shunt parallel C=1590 pFd and L=16 uH over a the frequency range 0.75 MHZ to 1.25 MHZ. At each frequency, the complex impedance of the shunt parallel L-C is calculated. Voltage division is then used to determine V_out.

You might correctly note that you can do the same thing using SPICE. Life isn't always that easy. What if the problem were to determine the response as a function of L at 100 points with a fixed frequency. This type of C program permits you to vary anything; not simply the frequency.

Note that in tst_cplx.c, the output is sent to the terminal and also to a file. In writing to the file, the values are separated by commas. This permits you to later import import the file into QPRO.