Complex
Works with complex numbers where each component is of some specified type
Definition
The complex template class is defined in the standard header <complex>, and in the nonstandard backward-compatibility header <complex.h>.
namespace std {
template <class T>
class complex;
}The template parameter T is used as the scalar type of both the real and the imaginary parts of the complex number.
Description
The template class std::complex operates on complex numbers.
A complex number is a number which can be put in the form a + bi, where a and b are real numbers and i is called the imaginary unit, where
Complex Numbers Operations
Create, Copy, and Assign Operations
| Operation | Effect |
| complex c | Creates a complex number with 0 as the real part and 0 as the imaginary part |
| complex c(1.1) | Creates a complex number with 1.1 as the real part and 0 as the imaginary part |
| complex c(1.1,2.2) | Creates a complex number with 1.1 as the real part and 1.2 as the imaginary part |
| complex c1(c2) | Creates c1 as a copy of c2 |
| polar (2. 2) | Creates a temporary complex number from polar coordinates (2.2 as magnitude rho and 0 as phase angle theta) |
| polar (2. 2, 0.77) | Creates a temporary complex number from polar coordinates (2.2 as magnitude rho and 0.77 as phase angle theta) |
| conj (c) | Creates a temporary complex number that is the conjugated complex number of c |
| c1 = c2 | Assigns the values of c2 to c1 |
| c1 += c2 | Adds the value of c2 to c1 |
| c1 -= c2 | Subtracts the value of c2 from c1 |
| c1 *= c2 | Multiplies the value of c2 by c1 |
| c1 /= c2 | Divides the value of c2 into c1 |
Value Access
| Operation | Effect |
| c.real() | Returns the value of the real part (member function) |
| real(c) | Returns the value of the real part (global function) |
| c.imag() | Returns the value of the imaginary part (member function) |
| imag(c) | Returns the value of the imaginary part (global function) |
| abs(c) | Returns the absolute value of c |
| norm(c) | Returns the squared absolute value of c(c.real()^2 + c.imag()^2) |
| arg(c) | Returns the angle of the polar representation of c |
Comparison Operations
| Operation | Effect |
| c1 == c2 | Returns if c1 is equal to c2 |
| c1 != c2 | Returns if c1 differs from c2 |
Arithmetic Operations
| Operation | Effect |
| c1 + c2 | Returns the sum of c1 and c2 |
| c1 - c2 | Returns the difference between c1 and c2 |
| c1 * c2 | Returns the product of c1 and c2 |
| c1 / c2 | Returns the quotient of c1 and c2 |
| -c | Returns the negated value of c |
| + c | Returns c |
| c1 += c2 | Same with c1 = c1 + c2 |
| c1 -= c2 | Same with c1 = c1 - c2 |
| c1 *= c2 | Same with c1 = c1 * c2 |
| c1 /= c2 | Same with c1 = c1 / c2 |
Input/Output Operations
| Operation | Effect |
| strm << c | Writes the complex number c to the ostream strm |
| strm >> c | Reads the complex number c from the istream strm |
Transcendental Functions
| Operation | Effect |
| pow(c1, c2) | Complex power c1^c2 |
| exp(c) | Base e exponential of c (e^c) |
| sqrt(c) | Square root of c |
| log(c) | Complex natural logarithm of c with base e (ln c) |
| log10(c) | Complex common logarithm of c with base 10 (lg c) |
| sin(c) | Sine of c |
| cos(c) | Cosine of c |
| tan(c) | Tangent of c |
| sinh(c) | Hyperbolic sine of c |
| cosh(c) | Hyperbolic cosine of c |
| tanh(c) | Hyperbolic tangent of c |
References
- Nicolai M. Josuttis: "The C++ Standard Library"
Example 1
The following program performs some common operations on complex numbers.
#include <iostream>
#include <complex>
using namespace std;
int main()
{
/*complex number with real and imaginary parts
*-real part: 4.0
*-imaginary part: 3.0
*/
complex<double> c1(4.0,3.0);
/*create complex number from polar coordinates
*-magnitude:5.0
*-phase angle:0.75
*/
complex<float> c2(polar(5.0,0.75));
// print complex numbers with real and imaginary parts
cout << "c1: " << c1 << endl;
cout << "c2: " << c2 << endl;
//print complex numbers as polar coordinates
cout << "c1: magnitude: " << abs (c1)
<< " (squared magnitude: " << norm(c1) << ") "
<< " phase angle: " << arg(c1) << endl;
cout << "c2: magnitude: " << abs(c2)
<< " (squared magnitude: " << norm (c2) << ") "
<< " phase angle: " << arg(c2) << endl;
//print complex conjugates
cout << "c1 conjugated: " << conj(c1) << endl;
cout << "c2 conjugated: " << conj(c2) << endl;
//print result of a computation
cout << "4.4 + c1 * 1.8: " << 4.4 + c1 * 1.8 << endl;
/*print sum of c1 and c2:
*-note: different types
*/
cout << "c1 + c2: "
<< c1 + complex<double>(c2.real(),c2.imag()) << endl;
// add square root of c1 to c1 and print the result
cout << "c1 += sqrt(c1): " << (c1 += sqrt(c1)) << endl;
return 0;
}Output:
c1: (4,3)
c2: (3.65844,3.40819)
c1: magnitude: 5 (squared magnitude: 25) phase angle: 0.643501
c2: magnitude: 5 (squared magnitude: 25) phase angle: 0.75
c1 conjugated: (4,-3)
c2 conjugated: (3.65844,-3.40819)
4.4 + c1 * 1.8: (11.6,5.4)
c1 + c2: (7.65844,6.40819)
c1 += sqrt(c1): (6.12132,3.70711)
- Nicolai M. Josuttis: "The C++ Standard Library"