COMPLEX_NUMBERS
Complex Numbers in MATLAB Programming


COMPLEX_NUMBERS is a MATLAB program which demonstrates very briefly some of the features of using complex numbers in a MATLAB program.

The first issue is how to declare a complex variable, including the choice of single precision or double precision, whether the variable is a scalar, vector, or array, and whether the variable is initialized with a value, or assigned one.

A second issue concerns the question of how a complex variable is to be printed out.

Another issue concerns how a complex variable is to operated on by the arithmetic operators of addition, subtraction, multiplication, division, and exponentiation.

The language also provides a number of intrinsic functions that can be applied to a complex variable. The names of these functions can sometimes be easy to forget. Moreover, it is occasionally true that there may be a selection of functions with similar names (say, "exp", "cexp" and "dcexp") which may or may not produce the desired results.

Another issue concerns the details of double precision calculation. Even a single accidental use of a single precision function name in a double precision computation can result in the loss of half the digits of accuracy. Thus, it sometimes really matters whether you use "cmplx" or "dcmplx" to assign values to a double precision complex variable.

One peculiar feature of MATLAB is that the symbols i and j are predefined as functions that return the value of the complex unit. Unless the user redefines them (for example, by using a for statement in which i is an index) then they can be used to set up complex quantities:

        a = 1 + 2 * i;
        b = 1 + 2 * j;
      

Another peculiar feature of MATLAB is that the printf command will only print the real part of a complex number. To get the real and imaginary parts, you must explicitly request them:

        a = 1 + 2 * i;
        fprintf ( 1, '  %f  %f\n', a );  <-- not correct!
        fprintf ( 1, '  %f  %f\n', real ( a ), imag ( a ) );  <--correct!
      

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Licensing:

COMPLEX_NUMBERS is available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:

BLAS1_C, a MATLAB library which contains basic linear algebra routines for vector-vector operations, using single precision complex arithmetic;

BLAS1_Z, a MATLAB library which contains basic linear algebra routines for vector-vector operations, using double precision complex arithmetic.

C8LIB, a MATLAB library which implements certain elementary functions for "C8" or double precision complex variables;

LINPACK_C, a MATLAB library which solves linear systems using single precision complex arithmetic;

Source Code:

Examples and Tests:

You can go up one level to the MATLAB source codes.


Last revised on 07 November 2010.