Previous: 15.1 Matrices
Up: 15 Engineering Programming Examples
Next: 15.3 Integrals
Previous Page: 15.1.2 Matrix Operations: Sums and Products
Next Page: 15.2.1 Complex Numbers and Vectors
Complex numbers are encountered in many mathematical applications. In this section, we will first review complex numbers and operations involving complex numbers. We will then represent complex numbers using structure types and implement many of the basic complex number operations.
The squares of a real number, either positive or negative,
is a positive number. Numbers whose
squares are negative cannot be real numbers; they are, therefore, called
imaginary numbers.
Thus, imaginary numbers are the square roots of negative numbers.
For example, consider:
Here
is the absolute value of x,
and thus
is a square root of a negative number, i.e. an imaginary number. Imaginary
numbers are written in a normalized manner as follows:
where, , and
.
Square root of -1 is represented by the
special symbol,
in mathematics or
in Electrical Engineering.
Thus, an imaginary number is represented by
times a
real number
.
A complex number is a number that is sums of both a real and
an imaginary number.
Both
and
are real numbers, and
is a complex number.
Either of the real
numbers
or
can, of course, be zero;
in which case, the complex number reduces to
either a real or an imaginary number.
The number
is called the real part of
, and
is
called the imaginary part.
Remember that both the real part,
, and the imaginary
part,
, are real numbers. It is
that is an imaginary number,
not
.
Complex numbers can be visualized geometrically as points on a two dimensional
plane with rectangular axes, real and imag.
Then, the real part of a number is
the projection of the point onto the real axis, and the imaginary part of the
number is the projection onto the imaginary axis imag
(see Figure 15.8. For these
reasons, the complex number representation as a sum of real and imaginary parts
is called representation in rectangular coordinates.
Addition, subtraction, multiplication, and division operators for complex
numbers are defined in terms of the same operator symbols as for real numbers,
viz. +, -, *, /.
The sum of two complex numbers is simply the sum of their
real parts plus times the sum of their imaginary parts. Thus, if
then the sum of z1 and z2 is given by:
The product of z1 and z2 is obtained by
multiplying the two numbers, replacing
by
,
and collecting the real terms and the imaginary terms. Thus,
Division of two complex numbers is a little more involved. First, we define the
complex conjugate, , of a number,
,
as one with the same real part,
,
but whose imaginary part is
. Thus, the complex conjugate of
is:
Observe that the product of
and
is real:
Now, we can divide two complex numbers:
To separate the result into real and imaginary parts, we first make the
denominator real by
multiplying both the numerator
and the denominator by
.
With this description of complex numbers and operations on them, we would like to develop programs that can work with them. Complex number is not a native data type in C, but we would like to represent complex numbers in a program as if it were. We will define an abstract data type, complex, using typedef and define functions to serve as operators on complex numbers.
We will represent complex numbers as ordered pairs of real and imaginary parts,
(using rectangular form defined above),
and implement the ordered pairs as structures.
We will use typedef to define a data type, rect,
for this structure.
(We choose the name rect because complex data type
with an identical structure is already defined in math.h.
We can, of course,
use the complex type defined in math.h,
but we define a rect type to illustrate
the use of typedef).
Figure 15.9 shows this definition and
the functions for addition and multiplication of complex numbers.
We use type double in the structure rect for greater precision in computation. In a similar manner, it is easy to write the remaining functions for subtraction and division of two complex numbers. Implementation of these functions is left as an exercise.