Abstract
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.
Make it as simple as possible, but no simpler.
Albert Einstein
Access provided by CONRICYT-eBooks. Download chapter PDF
Similar content being viewed by others
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,
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:
15.3 Example 2: Polar Coordinate Example
The second order differential equation:
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
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):
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:
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
If you manage this to your satisfaction, try your skills on the roots of a cubic (see the problems in Chap. 13).
Author information
Authors and Affiliations
Corresponding author
Rights and permissions
Copyright information
© 2018 Springer International Publishing AG, part of Springer Nature
About this chapter
Cite this chapter
Chivers, I., Sleightholme, J. (2018). Complex. In: Introduction to Programming with Fortran. Springer, Cham. https://doi.org/10.1007/978-3-319-75502-1_15
Download citation
DOI: https://doi.org/10.1007/978-3-319-75502-1_15
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-75501-4
Online ISBN: 978-3-319-75502-1
eBook Packages: Computer ScienceComputer Science (R0)