Add square root of two complex numbers and print the result
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#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);
cout << "c1: " << c1 << endl;
/* create complex number from polar coordinates
* - magnitude: 5.0
* - phase angle: 0.75
*/
complex<float> c2(polar(5.0,0.75));
// add square root of c1 to c1 and print the result
cout << "c1 += sqrt(c1): " << (c1 += sqrt(c1)) << endl;
}
c1: (4,3)
c1 += sqrt(c1): (6.12132,3.70711)
Complex conjugates
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#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);
cout << "c1: " << c1 << endl;
/* create complex number from polar coordinates
* - magnitude: 5.0
* - phase angle: 0.75
*/
complex<float> c2(polar(5.0,0.75));
// print complex conjugates
cout << "c1 conjugated: " << conj(c1) << endl;
cout << "c2 conjugated: " << conj(c2) << endl;
}
c1: (4,3)
c1 conjugated: (4,-3)
c2 conjugated: (3.65844,-3.40819)
Complex + integer
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> cmpx1(1, 0);
complex<double> cmpx2(1, 1);
cout << cmpx1 << " " << cmpx2 << endl;
cmpx1 += 10;
cmpx2 += 10;
cout << cmpx1 << " " << cmpx2 << endl;
return 0;
}
(1,0) (1,1)
(11,0) (11,1)
Complex number + float number
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#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);
cout << "c1: " << c1 << endl;
/* create complex number from polar coordinates
* - magnitude: 5.0
* - phase angle: 0.75
*/
complex<float> c2(polar(5.0,0.75));
// print result of a computation
cout << "4.4 + c1 * 1.8: " << 4.4 + c1 * 1.8 << endl;
}
c1: (4,3)
4.4 + c1 * 1.8: (11.6,5.4)
Complex number: magnitude, squared magnitude and phase angle
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#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);
cout << "c1: " << c1 << endl;
/* create complex number from polar coordinates
* - magnitude: 5.0
* - phase angle: 0.75
*/
complex<float> c2(polar(5.0,0.75));
// 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;
}
c1: (4,3)
c1: magnitude: 5 (squared magnitude: 25) phase angle: 0.643501
c2: magnitude: 5 (squared magnitude: 25) phase angle: 0.75
Computing an Impedance with Complex Numbers
#include <complex>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
complex<double> parallel_RLC_impedance( double frequency, double resistance, double inductance, double capacitance );
int main( )
{
const double R = 1000.0;
const double L = 0.2;
const double C = 10.0e-9;
double frequency;
frequency = 1.0 / sqrt( L * C );
complex<double> impedance = parallel_RLC_impedance( frequency, R, L, C );
cout << impedance << " Magnitude = " << abs( impedance ) << " Phase = " << arg( impedance ) << endl;
impedance = parallel_RLC_impedance( frequency / 10, R, L, C );
cout << impedance << " Magnitude = " << abs( impedance ) << " Phase = " << arg( impedance ) << endl;
impedance = parallel_RLC_impedance( frequency * 10, R, L, C );
cout << impedance << " Magnitude = " << abs( impedance ) << " Phase = " << arg( impedance ) << endl;
}
inline
complex<double> parallel_RLC_impedance( double frequency,double resistance, double inductance, double capacitance )
{
complex<double> impedance_inverse( 1.0 / resistance,frequency * capacitance - 1.0 / ( frequency * inductance ) );
return 1.0 / impedance_inverse;
}
Create complex numbers based on double value
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> cmpx1(1, 0);
complex<double> cmpx2(1, 1);
cout << cmpx1 << " " << cmpx2 << endl;
return 0;
}
(1,0) (1,1)
Plus two complex numbers together
#include <iostream>
#include <complex>
using namespace std;
int main()
{
complex<double> cmpx1(1, 0);
complex<double> cmpx2(1, 1);
cout << cmpx1 << " " << cmpx2 << endl;
complex<double> cmpx3 = cmpx1 + cmpx2;
cout << cmpx3 << endl;
return 0;
}
(1,0) (1,1)
(2,1)
print sum of two complex numbers
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#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);
cout << "c1: " << c1 << endl;
/* create complex number from polar coordinates
* - magnitude: 5.0
* - phase angle: 0.75
*/
complex<float> c2(polar(5.0,0.75));
/* print sum of c1 and c2:
* - note: different types
*/
cout << "c1 + c2: "
<< c1 + complex<double>(c2.real(),c2.imag()) << endl;
}
c1: (4,3)
c1 + c2: (7.65844,6.40819)
The << and >> operators are overloaded relative to complex numbers.
#include <iostream>
#include <complex>
using namespace std;
main(void)
{
complex<double> num(10, 1);
cout << num;
return 0;
}
Use polar to create complex number
#include <complex>
#include <iostream>
using namespace std;
int main( ) {
double rho = 3.0; // magnitude
double theta = 3.141592 / 2; // angle
complex<double> coord = polar(rho, theta);
cout << "rho = " << abs(coord) << ", theta = " << arg(coord) << endl;
coord += polar(4.0, 0.0);
cout << "rho = " << abs(coord) << ", theta = " << arg(coord) << endl;
}
rho = 3, theta = 1.5708
rho = 5, theta = 0.643501