Complex numbers
Fortran has a complex type in the language, not in a library. A complex value is a pair of reals of the same kind, the four arithmetic operators work on the pair with the rules of complex arithmetic, and a handful of intrinsics take it apart. This lesson covers the literal, the operators, the intrinsics, and the reason a complex number appears in a program at all: a rotation.
The literal and the type
A complex literal is a parenthesised pair of reals, real part first: (3.0, 4.0) is 3 + 4i. A declaration is complex or complex(dp), and the kind of the value follows the kind of the components: a plain complex is single precision, and a program that wants double precision writes every literal with the _dp suffix as well, or builds the pair with cmplx(a, b, dp).
There is no i constant. The imaginary unit is written (0.0, 1.0), and a program that needs it a lot gives it a name.
The arithmetic
Addition and subtraction work component by component, multiplication follows (a + bi)(c + di) = (ac - bd) + (ad + bc)i, and division multiplies by the conjugate of the divisor. The program below prints all four for one pair of values, and the results are the ones a hand calculation gives.
The file ops.f90:
program complex_ops
implicit none
complex :: a, b
a = (3.0, 4.0)
b = (1.0, -2.0)
print *, "a + b :", a + b
print *, "a - b :", a - b
print *, "a * b :", a * b
print *, "a / b :", a / b
print *, "abs(a):", abs(a)
print *, "conjg(a):", conjg(a)
print *, "real(a), aimag(a):", real(a), aimag(a)
end program complex_ops
gfortran -Wall -Wextra -std=f2018 ops.f90 -o ops
./ops
a + b : (4.00000000,2.00000000)
a - b : (2.00000000,6.00000000)
a * b : (11.0000000,-2.00000000)
a / b : (-1.00000000,2.00000000)
abs(a): 5.00000000
conjg(a): (3.00000000,-4.00000000)
real(a), aimag(a): 3.00000000 4.00000000
Check the division by hand: multiply 3 + 4i by the conjugate of 1 - 2i, which is 1 + 2i. The numerator is 3 + 6i + 4i + 8i², that is -5 + 10i, and the divisor times its conjugate is 5, so the quotient is -1 + 2i, which is what the program printed.
Mixing a real and a complex in one expression promotes the real to a complex with a zero imaginary part, so a + 1.0 is legal and means a + (1.0, 0.0). Mixing a single precision complex with a double precision real promotes the whole expression to single, which is the same trap as in the real case and the reason every literal in a double precision program carries its suffix.
REAL, AIMAG, CONJG and ABS
- real(z) and aimag(z) are the two components.
- conjg(z) flips the sign of the imaginary part, which is the operation a division and a power spectrum are built from.
- abs(z) is the modulus, sqrt(real(z)**2 + aimag(z)**2), computed without the overflow that square would cause for a large value.
abs is the reason a complex array is a natural home for a Fourier transform: the amplitude of each frequency is the modulus of a complex coefficient, and the phase is atan2(aimag(z), real(z)). The lesson on FFT basics builds one.
A quarter turn
The useful picture is geometric. Multiplication by exp(i θ) rotates a complex number by θ and leaves its modulus alone, so a rotation of a vector is one multiplication. With θ = π/2 the factor is exp(iπ/2), which is i.
The file rotate.f90:
program rotate
implicit none
complex :: v, quarter_turn
real, parameter :: pi = 3.14159265
v = (1.0, 0.0)
quarter_turn = exp(cmplx(0.0, pi / 2.0))
print *, "quarter_turn :", quarter_turn
print *, "v * q :", v * quarter_turn
print *, "v * q ** 2 :", v * quarter_turn ** 2
print *, "abs(v * q**2):", abs(v * quarter_turn ** 2)
end program rotate
gfortran -Wall -Wextra -std=f2018 rotate.f90 -o rotate
./rotate
quarter_turn : (-4.371138829E-08,1.00000000)
v * q : (-4.371138829E-08,1.00000000)
v * q ** 2 : (-1.00000000,-8.742277657E-08)
abs(v * q**2): 1.00000000
The quarter turn came out as -4.37e-08 + 1.0i and not as exactly i, because pi is a decimal literal and exp rounds. Two quarter turns give -1 with the same small error in the imaginary part, and the modulus of the result is 1 to seven digits: the rotation preserved the length and the error stayed in the angle. That is the behaviour a rotation in a loop keeps, and the reason a phase accumulator is sometimes renormalised by dividing by abs.
How complex values print
List-directed output prints a complex value as a parenthesised pair with a comma, and the field is wide: about twenty characters per value, with the width chosen for the worst case of both components. In a formatted write a complex takes two real edit descriptors in succession, not one, which is the detail that breaks a format written for a real array. The lesson on formatted output shows the pair.
Exercises
- Print (0.0, 1.0) ** 2 and (0.0, 1.0) ** 3. Which one is a real number?
- Compute abs((3.0, 4.0)) by hand and compare it with the program.
- Rotate (1.0, 0.0) by π instead of π/2. What are the two components, and what should they be?
- Divide (1.0, 0.0) by (0.0, 1.0) and explain the sign of the result.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How is a complex literal written?
Answer
(3.0, 4.0)
A parenthesised pair of reals is the literal; there is no i suffix in Fortran.
-
What is
abs((3.0, 4.0))?Answer
5.0
The modulus is sqrt(9 + 16), which is 5.
-
What does
conjg(a)do?Answer
It flips the sign of the imaginary part
The conjugate of a + bi is a - bi, and division is built on it.
-
What does multiplying by
exp(cmplx(0.0, pi))do to a complex number?Answer
It rotates it by pi and keeps the modulus
Multiplication by a unit phasor is a rotation by its angle.
-
What does this program print?
program p implicit none print *, real((3.0, 4.0)) + aimag((3.0, 4.0)) end program pAnswer
7.00000000
real gives 3.0 and aimag gives 4.0, and their sum is 7.0.
-
What is
(0.0, 1.0) ** 2?Answer
(-1.00000000,0.00000000)
i squared is -1, and the imaginary part of the result is zero.
-
What happens in
a + 1.0withacomplex?Answer
The real is promoted to a complex with a zero imaginary part
A real and a complex mix by promoting the real part.
-
What is
(1.0, -2.0) * (1.0, 2.0)?Answer
(5.00000000,0.00000000)
The divisor conjugates: (1 - 2i)(1 + 2i) = 1 + 4 = 5.
-
How does list-directed output print a complex value?
Answer
A parenthesised pair with a comma, in a wide field
The components are printed inside parentheses, separated by a comma.
-
Why is a complex array a natural home for a Fourier transform?
Answer
The amplitude of a frequency is the modulus of its coefficient
Each frequency has an amplitude and a phase, which one complex value holds.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev