Cpp Libs Complex
Title: C++ Standard Library |
In mathematics and engineering calculations, we often need to deal with complex numbers, such as in signal processing, Fourier transforms, circuit analysis, etc.
The C++ standard library provides the `` header file, 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
Complex numbers are a combination of real and imaginary numbers, typically represented as `a + bi`, where `a` is the real part, `b` is the imaginary part, and `i` is the imaginary unit, satisfying `i^2 = -1`.
In C++, the complex number type is represented by `std::complex`, where `T` can be any arithmetic type, such as `float`, `double`, or `long double`.
To use the `` library, you first need to include this header file in your C++ program:
#include #include
## Example
#include
#include // complex header file
int main(){
std::complex z1(3.0, 4.0);// 3 + 4i
std::complex 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 a Complex Number
std::complex c(5.0, 3.0); // create a complex number 5 + 3i
### Accessing Real and Imaginary Parts
double realPart = c.real(); // get real part
double imagPart = c.imag(); // get imaginary part
### 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. Getting Real and Imaginary Parts**
std::cout << "Real part: " << z1.real() << std::endl; // 3
std::cout << "Imaginary part: " << z1.imag() << std::endl; // 4
**2. Four 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**
The header file `` provides many mathematical functions related to complex numbers.
## Example
#include // some math functions required
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 complex numbers using polar coordinates (modulus + argument).
## Example
// create complex number from polar coordinates
double r =5.0;// modulus
double theta = M_PI /4;// 45 degrees
std::complex 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 that supports different numeric types:
* `std::complex`
* `std::complex` (commonly used)
* `std::complex`
## Example
std::complexzf(1.0f, 2.0f);
std::complex zd(1.0, 2.0);
## Example
Below is a simple example using the `` header file, including creating complex numbers, basic operations, and outputting results.
## Example
#include
#include
int main(){
// create two complex numbers
std::complex c1(5.0, 3.0);// 5 + 3i
std::complex 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 addition
std::complex sum = c1 + c2;
std::cout<<"Sum: "<< sum << std::endl;// 7 - i
// complex subtraction
std::complex diff = c1 - c2;
std::cout<<"Difference: "<< diff << std::endl;// 3 + 7i
// complex multiplication
std::complex product = c1 * c2;
std::cout<<"Product: "<< product << std::endl;// 22 - 14i
// complex division
std::complex quotient = c1 / c2;
std::cout<<"Quotient: "<< quotient << std::endl;// -0.1 + 1.3i
// conjugate of complex number
std::complex conjugate = std::conj(c1);
std::cout<<"Conjugate of c1: "<< conjugate << std::endl;// 5 - 3i
// modulus of complex number
double modulus = std::abs(c1);
std::cout<<"
YouTip