Aims

The aims of this chapter are:

  • To introduce the last predefined numeric data type in Fortran.

  • To illustrate with examples how to use this type.

15.1 Introduction

This variable type reflects an extension of the real data type available in Fortran — the complex data type, where we can store and manipulate complex variables. Problems that require this data type are restricted to certain branches of mathematics, physics and engineering. Complex numbers are defined as having a real and imaginary part, i.e., \( a = x + iy \) where i is the square root of –1.

They are not supported in many programming languages as a base type which makes Fortran the language of first choice for many people.

To use this variable type we have to write the number as two parts, the real and imaginary elements of the number, for example,

figure a

represents the complex number \(1 + i2\). Note that the complex number is enclosed in brackets. We can do arithmetic on variables like this, and most of the intrinsic functions such as log, sin, cos, etc., accept a complex data type as argument.

All the usual rules about mixing different variable types, like reals and integers, also apply to complex. Complex numbers are read in and written out in a similar way to real numbers, but with the provision that, for each single complex value, two format descriptors must be given. You may use either E or F formats (or indeed, mix them), as long as there are enough of them. Although you use brackets around the pairs of numbers in a program, these must not appear in any input, nor will they appear on the output.

15.2 Example 1: Use of cmplx, aimag and conjg

There are a number of intrinsic functions to enable complex calculations to be performed. The program below uses some of them:

figure b

15.3 Example 2: Polar Coordinate Example

The second order differential equation:

$$\begin{aligned} \frac{d^2y}{dt^2} + 2 \frac{dy}{dt} + y = x(t) \end{aligned}$$

could describe the behaviour of an electrical system, where x(t) is the input voltage and y(t) is the output voltage and dy/dt is the current. The complex ratio

$$\begin{aligned} \frac{y(w)}{x(w)} = 1 / (-w^2 + 2jw +1) \end{aligned}$$

is called the frequency response of the system because it describes the relationship between input and output for sinusoidal excitation at a frequency of w and where j is \( \sqrt{(}-1) \) The following program reads in a value of w and evaluates the frequency response for this value of w together with its polar form (magnitude and phase):

figure c

15.4 Complex and Kind Type

The standard requires that there be a minimum of two kind types for real numbers and this is also true of the complex data type. Chapter 5 must be consulted for a full coverage of real kind types. We would therefore use something like the following to select a complex kind type other than the default:

figure d

Chapter 21 includes a good example of how to use modules to define and use precision throughout a program and subprogram units.

15.5 Summary

Complex is used to store and manipulate complex numbers: those with a real and an imaginary part. There are standard functions which allow conversion between the numerical data types — cmplx, real and int.

15.6 Problem

15.1

The program used in Chap. 13 which calculated the roots of a quadratic had to abandon the calculation if the roots were complex. You should now be able to remedy this, remembering that it is necessary to declare any complex variables. Instead of raising the expression to the power 0.5 in order to take its square root, use the function sqrt. The formulae for the complex roots are

$$ \frac{-b}{2a} \pm i \frac{ \sqrt{-(b^2-4ac)} }{2a} $$

If you manage this to your satisfaction, try your skills on the roots of a cubic (see the problems in Chap. 13).