The C++ standard library
In mathematical and engineering computations, we often need to handle complex numbers, such as in signal processing, Fourier transforms, and circuit analysis.
The C++ standard library provides the header, allowing you to easily manipulate complex numbers just like 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 and an imaginary number, usually expressed as a + bi, where a is the real part,b is the imaginary part,i is the imaginary unit, satisfying i^2 = -1.
In C++, the complex type is represented by std::complex , where T can be any arithmetic type, such as float, double or long double.
To use library, you first need to include this header in your C++ program:
#include
#include
Example
#include
#include
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
Creating complex numbers
std::complex c(5.0, 3.0); // 创建一个复数 5 + 3i
Accessing the real and imaginary parts
double realPart = c.real(); // 获取实部
double imagPart = c.imag(); // 获取虚部
Basic operations on complex numbers
The C++ standard library supports the following basic operations:
- Addition:
operator+ - Subtraction:
operator- - Multiplication:
operator* - Division:
operator/ - Conjugate:
conj - Modulus:
abs - Argument:
arg
1. Get the real and imaginary parts
std::cout << "实部: " << z1.real() << std::endl; // 3
std::cout << "虚部: " << z1.imag() << std::endl; // 4
2. Arithmetic operations:
Example
auto
z3
=
z1
z2
;
// (4, 2)
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 provides many math functions related to complex numbers.
Example
#include
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 representation
Sometimes we need to represent a complex number using polar coordinates (modulus + phase angle).
Example
// Generate a complex number from polar coordinates
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 is a template class supporting different numeric types:
std::complexstd::complex(commonly used)std::complex
Example
std
::
complex
<
float
zf
(
1.0f
,
2.0f
);
std
::
complex
<
double
zd
(
1.0
,
2.0
);
Example
Below is an example using header's simple example, including creating complex numbers, basic operations, and output results.
Example
#include
#include
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 the complex number
std
::
cout
<<
"c1: "
<<
c1
<<
std
::
endl
;
// (5,3)
std
::
cout
<<
"c2: "
<<
c2
<<
std
::
endl
;
// (2,-4)
// Complex addition
std
::
complex
<
double
sum
=
c1
c2;
std
::
cout
<<
"Sum: "
<<
sum
<<
std
::
endl
;
// 7 - i
// Complex subtraction
std
::
complex
<
double
diff
=
c1
c2;
std
::
cout
<<
"Difference: "
<<
diff
<<
std
::
endl
;
// 3 + 7i
// Complex multiplication
std
::
complex
<
double
product
=
c1
c2;
std
::
cout
<<
"Product: "
<<
product
<<
std
::
endl
;
// 22 - 14i
// Complex division
std
::
complex
<
double
quotient
=
c1
/
c2;
std
::
cout
<<
"Quotient: "
<<
quotient
<<
std
::
endl
;
// -0.1 + 1.3i
// Conjugate of the complex number
std
::
complex
<
double
conjugate
=
std::
conj
(
c1
)
;
std
::
cout
<<
"Conjugate of c1: "
<<
conjugate
<<
std
::
endl
;
// 5 - 3i
// Modulus of the complex number
double
modulus
=
std
::
abs
(
c1
)
;
std
::
cout
<<
"Modulus of c1: "
<<
modulus
<<
std
::
endl
;
// sqrt(34) ≈ 5.83095
// Argument of the complex number (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 program above, 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.54042