STL Complex Numbers

Download: Reese.zip

C++ finally has complex numbers. They come as a class template that can be parameterized by the floating-point data types, float, double, or long double.

See:

test_complex.cpp
numeric_complex.cpp

Reference

Greg Reese, C++ Standard Library Practical Tips Charles River Media, 2006. ISBN 1-58450-400-5. p. 394-398.


test_complex.cpp

test_complex.cpp


#include <iostream>
#include <iomanip> // for setprecision()
#include <complex>
using namespace std;

int main()
{
	complex<double> c1(7.63, 9.88); // create from rectangular coordinates
	complex<double> c2( polar(10.04, 0.77) ); // from polar coordinates (phasor)

	complex<double> c3 = c1 + c2; // -, *, and / also available

	cout << setprecision(4);
	cout << "Sum: " << c3 << "  magnitude: " << abs(c3);
	cout << "  Angle: " << arg(c3) << endl;
	cout << "Norm: " << norm(c3)  << "  Conjugate: " << conj(c3)<< endl;
	cout << "Real: " << c3.real() << "  Imag: " << c3.imag() << endl;
	cout << "Sqrt: " << sqrt(c3) << "  Log: " << log(c3) <<endl;
	cout << "Sine: " << sin(c3) << "  hyperboic sine: " << sinh(c3) << endl;
}
			


Results

Sum: (14.84,16.87)  magnitude: 22.47  Angle: 0.8494
Norm: 504.7  Conjugate: (14.84,-16.87)
Real: 14.84  Imag: 16.87
Sqrt: (4.319,1.953)  Log: (3.112,0.8494)
Sine: (8.1e+006,-6.832e+006)  hyperboic sine: (-5.534e+005,-1.275e+006)


numeric_complex.cpp

This test code computes the impedance of an RLC circuit as a function of frequency.


// numeric_complex.cpp

#include <complex>
#include <iomanip>
#include <iostream>
#include <cmath>

using namespace std;

// compute the complex impedance of a parallel RLC circuit
complex<double> parallel_RLC_impedance( double frequency,
   double resistance, double inductance, double capacitance );

int main( )
{
   const double R = 1000.0;   // R in ohms
   const double L = 0.2;      // L in henries
   const double C = 10.0e-9;  // C in farads
   double frequency;

   // set the resonant frequency
   frequency = 1.0 / sqrt( L * C );

   // compute the impedance at the resonant frequency
   complex<double> impedance =
      parallel_RLC_impedance( frequency, R, L, C );

   cout << setprecision( 2 ) << fixed
      << "AT RESONANT FREQUENCY\nImpedance = "
      << impedance << "   Magnitude = " << abs( impedance )
      << "   Phase = " << arg( impedance ) << endl;

   // compute the impedance at one tenth of the resonant frequency
   impedance = parallel_RLC_impedance( frequency / 10, R, L, C );

   cout << "\nAT ONE TENTH OF RESONANT FREQUENCY\nImpedance = "
      << impedance << "   Magnitude = " << abs( impedance )
      << "   Phase = " << arg( impedance ) << endl;

   // compute the impedance at ten times the resonant frequency
   impedance = parallel_RLC_impedance( frequency * 10, R, L, C );

   cout << "\nAT TEN TIMES THE RESONANT FREQUENCY\nImpedance = "
      << 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;
}


Results

AT RESONANT FREQUENCY
Impedance = (1000.00,-0.00)   Magnitude = 1000.00   Phase = -0.00

AT ONE TENTH OF RESONANT FREQUENCY
Impedance = (169.48,375.17)   Magnitude = 411.68   Phase = 1.15

AT TEN TIMES THE RESONANT FREQUENCY
Impedance = (169.48,-375.17)   Magnitude = 411.68   Phase = -1.15


Maintained by John Loomis, updated Wed Feb 14 23:28:29 2007