Table of Contents

<complex>

<complex> provides std::complex<T> for complex numbers with arithmetic operations, trigonometric functions, and conversions. It's useful for signal processing, control theory, and wave simulations.

Operations like abs(), arg(), conj(), polar() work on complex numbers just as you'd expect, and they compose with <cmath> functions.

Example

This example performs arithmetic on complex numbers, calculates magnitude and phase, and constructs a complex number from polar form.

// compile: g++ -std=c++11 -o complexexample complexexample.cpp
// run: ./complexexample
// description: complex arithmetic and polar form
 
#include <complex>
#include <iostream>
#include <cmath>
 
int main() {
    std::complex<double> z1(3, 4);
    std::complex<double> z2(1, 2);
 
    auto product = z1 * z2;
    std::cout << "product: " << product << "\n";
 
    std::cout << "magnitude of z1: " << std::abs(z1) << "\n";
    std::cout << "phase of z1: " << std::arg(z1) << " radians\n";
 
    auto polar = std::polar(2.0, M_PI / 4);
    std::cout << "polar(2, pi/4): " << polar << "\n";
 
    return 0;
}