Complex numbers are an extension of the real number system and play a crucial role in many areas of science and engineering. A complex number has both a real part and an imaginary part. In Fortran, complex variables are supported natively, making computations with complex numbers efficient and straightforward.
1. Introduction to Complex Numbers
A complex number is defined as:
z = a + bi
ais the real part.bis the imaginary part.iis the imaginary unit, satisfyingi^2 = -1.
Complex numbers are widely used in fields such as:
- Electrical Engineering – Representing AC circuits with impedance.
- Quantum Physics – Representing wave functions and probability amplitudes.
- Signal Processing – Fourier transforms, filters, and frequency domain analysis.
Fortran provides a native complex data type to handle such numbers efficiently.
2. Declaring Complex Variables
In Fortran, a complex variable is declared using the complex keyword. Optionally, you can specify precision.
complex :: z
z = (2.0, 3.0) ! 2 + 3i
print *, "Complex number:", z
Explanation:
zis a complex variable.(2.0, 3.0)represents a complex number with real part2.0and imaginary part3.0.- The program prints
2.0 + 3.0i.
Complex variables can also be initialized directly in the declaration:
complex :: z = (1.0, -4.0)
3. Accessing Real and Imaginary Parts
Fortran provides built-in functions to access the real and imaginary components of a complex variable:
real(z)– Returns the real part.aimag(z)– Returns the imaginary part.
Example:
complex :: z
z = (2.0, 3.0)
print *, "Real part:", real(z)
print *, "Imaginary part:", aimag(z)
Output:
Real part: 2.0
Imaginary part: 3.0
These functions are useful when performing operations that require the individual components.
4. Basic Arithmetic with Complex Numbers
Complex numbers support all standard arithmetic operations: addition, subtraction, multiplication, and division.
Addition and Subtraction
complex :: z1, z2, zsum, zdiff
z1 = (2.0, 3.0)
z2 = (1.0, -1.0)
zsum = z1 + z2
zdiff = z1 - z2
print *, "Sum:", zsum
print *, "Difference:", zdiff
Explanation:
- Addition and subtraction are done component-wise:
(a + bi) + (c + di) = (a+c) + (b+d)i (a + bi) - (c + di) = (a-c) + (b-d)i
Multiplication
complex :: z1, z2, zprod
z1 = (2.0, 3.0)
z2 = (1.0, -1.0)
zprod = z1 * z2
print *, "Product:", zprod
Explanation:
- Multiplication of complex numbers uses the formula:
(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
Division
complex :: z1, z2, zquot
z1 = (2.0, 3.0)
z2 = (1.0, -1.0)
zquot = z1 / z2
print *, "Quotient:", zquot
Explanation:
- Division of complex numbers is done using the formula:
(a + bi) / (c + di) = [(ac + bd) + (bc - ad)i] / (c^2 + d^2)
5. Complex Conjugate
The complex conjugate of a number z = a + bi is conjg(z) = a - bi. In Fortran, the conjg function returns the conjugate.
complex :: z, zconj
z = (2.0, 3.0)
zconj = conjg(z)
print *, "Conjugate:", zconj
Output:
Conjugate: (2.0, -3.0)
The conjugate is used in many applications, such as calculating the magnitude squared of a complex number or performing inner products in quantum mechanics.
6. Magnitude and Phase
Two important properties of complex numbers are magnitude and phase:
- Magnitude (absolute value) is computed using
abs(z). - Phase (angle) is computed using
atan2(aimag(z), real(z)).
Example:
complex :: z
real :: magnitude, angle
z = (3.0, 4.0)
magnitude = abs(z)
angle = atan2(aimag(z), real(z))
print *, "Magnitude:", magnitude
print *, "Phase (radians):", angle
Output:
Magnitude: 5.0
Phase (radians): 0.927295
Explanation:
- Magnitude is calculated as
sqrt(a^2 + b^2). - Phase is the angle formed by the complex number with the real axis.
7. Polar Form of Complex Numbers
Complex numbers can also be represented in polar form:
z = r * exp(iθ)
ris the magnitude.θis the phase angle.
In Fortran, you can convert between rectangular and polar forms using abs, atan2, and exp:
complex :: z
real :: r, theta
z = (3.0, 4.0)
r = abs(z)
theta = atan2(aimag(z), real(z))
print *, "Polar form: r =", r, ", θ =", theta
8. Built-in Complex Functions
Fortran provides several functions specifically for complex numbers:
real(z)– Returns the real part.aimag(z)– Returns the imaginary part.abs(z)– Returns the magnitude.conjg(z)– Returns the conjugate.exp(z)– Complex exponential.log(z)– Complex natural logarithm.sqrt(z)– Complex square root.
Example of complex exponential:
complex :: z, zexp
z = (0.0, 3.14159)
zexp = exp(z)
print *, "exp(z):", zexp
This is particularly useful in quantum mechanics, signal processing, and other applications involving complex exponentials.
9. Complex Arrays
You can declare arrays of complex numbers for computations involving multiple elements:
complex, dimension(3) :: arr
arr(1) = (1.0, 2.0)
arr(2) = (3.0, 4.0)
arr(3) = (5.0, 6.0)
print *, "Array elements:"
do i = 1, 3
print *, arr(i)
end do
Explanation:
- Complex arrays allow operations like vector addition, multiplication, and transformations.
- Arrays are widely used in simulations, quantum computations, and linear algebra.
10. Complex Matrices
Complex matrices are common in linear algebra, physics, and engineering applications.
Example:
complex, dimension(2,2) :: mat
mat(1,1) = (1.0, 1.0)
mat(1,2) = (2.0, -1.0)
mat(2,1) = (-1.0, 2.0)
mat(2,2) = (0.0, 3.0)
print *, "Matrix elements:"
do i = 1, 2
do j = 1, 2
print *, mat(i,j)
end do
end do
Explanation:
- Complex matrices are essential for transformations in quantum mechanics, control systems, and electrical circuit analysis.
- Operations such as matrix multiplication, determinant, and eigenvalues can be performed using specialized libraries.
11. Applications of Complex Variables
- Electrical Engineering: Impedance in AC circuits:
Z = R + jXRis resistance,Xis reactance.- Complex numbers simplify circuit analysis using phasors.
- Quantum Physics: Wave functions and probability amplitudes are complex-valued.
- Signal Processing: Fourier transforms and filters use complex numbers for frequency domain representation.
- Control Systems: Poles and zeros of transfer functions are often complex.
12. Best Practices
- Always use complex literals
(a,b)instead of trying to manually encode real and imaginary parts separately. - Use built-in functions like
abs,aimag,conjg,expfor accurate calculations. - For arrays and matrices, use loops or vectorized operations to handle multiple complex numbers efficiently.
- Document the meaning of real and imaginary parts in context to avoid confusion in engineering applications.
Leave a Reply