C++ Standard Library<complex>
In mathematics and engineering computations, we often need to deal with complex numbers, such as in signal processing, Fourier transforms, circuit analysis, etc.
The C++ standard library provides<complex>The header file allows you to operate on complex numbers as easily as ordinary numbers.
A complex number consists of a real part and an imaginary part, in the form:
z = a + bi
A complex number is a combination of a real number and an imaginary number, usually represented asa + bi, whereais the real part,bis the imaginary part,iis the imaginary unit, satisfyingi^2 = -1。
In C++, the complex type isstd::complex<T>represented, whereTcan be any arithmetic type, such asfloat、doubleorlong double。
To use<complex>For the library, you first need to include this header file in your C++ program:
#include <iostream> #include <complex>
Example
#include <complex> // complex number header file
int main() {
std::complex<double> z1(3.0, 4.0); // 3 + 4i
std::complex<double> z2(1.0, -2.0); // 1 - 2i
std::cout << "z1 = " << z1 << std::endl;
std::cout << "z2 = " << z2 << std::endl;
}
Output:
z1 = (3,4) z2 = (1,-2)
Basic Syntax
Create complex numbers
std::complex<double> c(5.0, 3.0); // 创建一个复数 5 + 3i
Accessing Real and Imaginary Parts
double realPart = c.real(); // 获取实部 double imagPart = c.imag(); // 获取虚部
Basic Operations on Complex Numbers
C++ Standard Library<complex>supports the following basic operations:
- Addition:
operator+ - Subtraction:
operator- - Multiplication:
operator* - Division:
operator/ - Conjugate:
conj - Modulus:
abs - Argument:
arg
1. Getting the real and imaginary parts
std::cout << "实部: " << z1.real() << std::endl; // 3 std::cout << "虚部: " << z1.imag() << std::endl; // 4
2. Four Basic Arithmetic Operations:
Example
auto z4 = z1 - z2; // (2, 6)
auto z5 = z1 * z2; // (11, -2)
auto z6 = z1 / z2; // (-1, 2)
std::cout << "z1 + z2 = " << z3 << std::endl;
std::cout << "z1 * z2 = " << z5 << std::endl;
3. Common Functions
Header file<complex>provides many mathematical functions related to complex numbers.
Example
std::cout << "Modulus |z1| = " << std::abs(z1) << std::endl; // 5
std::cout << "Argument arg(z1) = " << std::arg(z1) << std::endl; // 0.927 (radians)
std::cout << "Conjugate conjugate(z1) = " << std::conj(z1) << std::endl; // (3,-4)
std::cout << "exp(z1) = " << std::exp(z1) << std::endl; // e^(3+4i)
std::cout << "sin(z1) = " << std::sin(z1) << std::endl;
std::cout << "cos(z1) = " << std::cos(z1) << std::endl;
4. Polar Coordinate Representation
Sometimes we need to represent complex numbers in polar coordinates (magnitude + phase angle).
Example
double r = 5.0; // Modulus
double theta = M_PI / 4; // 45 degrees
std::complex<double> z = std::polar(r, theta);
std::cout << "Polar complex number z = " << z << std::endl; // (3.53553,3.53553)
5. Template Parameters
std::complex<T>It is a template class that supports different numeric types:
std::complex<float>std::complex<double>(commonly used)std::complex<long double>
Example
std::complex<double> zd(1.0, 2.0);
Example
Below is an example using<complex>A simple example of the header file, including creating complex numbers, basic operations, and output results.
Example
#include <complex>
int main() {
// Create two complex numbers
std::complex<double> c1(5.0, 3.0); // 5 + 3i
std::complex<double> c2(2.0, -4.0); // 2 - 4i
// Output complex numbers
std::cout << "c1: " << c1 << std::endl; // (5,3)
std::cout << "c2: " << c2 << std::endl; // (2,-4)
// Complex number addition
std::complex<double> sum = c1 + c2;
std::cout << "Sum: " << sum << std::endl; // 7 - i
// Complex number subtraction
std::complex<double> diff = c1 - c2;
std::cout << "Difference: " << diff << std::endl; // 3 + 7i
// Complex number multiplication
std::complex<double> product = c1 * c2;
std::cout << "Product: " << product << std::endl; // 22 - 14i
// Complex number division
std::complex<double> quotient = c1 / c2;
std::cout << "Quotient: " << quotient << std::endl; // -0.1 + 1.3i
// Complex conjugate
std::complex<double> conjugate = std::conj(c1);
std::cout << "Conjugate of c1: " << conjugate << std::endl; // 5 - 3i
// Complex modulus
double modulus = std::abs(c1);
std::cout << "Modulus of c1: " << modulus << std::endl; // sqrt(34) ≈ 5.83095
// Complex argument (radians)
double argument = std::arg(c1);
std::cout << "Argument of c1: " << argument << std::endl; // atan(3/5) ≈ 0.54042 rad
return 0;
}
When you run the above program, you will get the following output:
c1: (5,3) c2: (2,-4) Sum: (7,-1) Difference: (3,7) Product: (22,-14) Quotient: (-0.1,1.3) Conjugate of c1: (5,-3) Modulus of c1: 5.83095 Argument of c1: 0.54042other extensions