C++ : Complex numbers library

Complex numbers library

The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them:

Classes

complex Complex number class (class template)

Functions

Complex values:
real Return real part of complex (function template)
imag Return imaginary part of complex (function template)
abs Return absolute value of complex (function template)
arg Return phase angle of complex (function template )
norm Return norm of complex number (function template)
conj Return complex conjugate (function template)
polar Return complex from polar components (function template )

Transcendentals overloads:
cos Return cosine of complex (function template)
cosh Return hyperbolic cosine of complex (function template)
exp Return exponential of complex (function template)
log Return natural logarithm of complex (function template)
log10 Return common logarithm of complex (function template)
pow Return complex power (function template)
sin Return sine of complex (function template)
sinh Return hyperbolic sine of complex (function template)
sqrt Return square root of complex (function template)
tan Return tangent of complex (function template)
tanh Return hyperbolic tangent of complex (function template)

Operator overloads:
complex operators Complex number operators (functions)

 

complex

class template
<complex>
template <class T> class complex;

Complex number class

The complex class is designed to hold two components of the same type, that conform a complex number.

A complex number is formed by adding an imaginary part to a real number:

x + y * i

The imaginary part (y*i) is a factor of i, known as the imaginary unit, and which satisfies that:

i2 = -1

In this class, complex numbers have two components: real (corresponding to x in the above example) and imag (corresponding to y). This way of referring to complex numbers by two real components is known and cartesian, because this way they have the ability to be easily representable on a cartesian axis.

The class has been implemented to provide as similar a functionality as the one of a numerical type when this was possible, therefore, 
complexobjects can be assigned, compared, inserted and extracted, as well as several arithmetical operators have been overloaded to be used on them directly.

Members

(constructor) Complex number constructor (public member function)
complex::imag Return imaginary part (public member function)
complex::real Return real part (public member function)
complex operators Complex number operators (functions)

The class also includes an alias type of the template argument:
complex::value_type Value type (public member type)

complex specializations

complex is specialized for the three fundamental floating-point types: float, double and long double.

These specializations have the same members as the template, but optimize its implementation for these fundamental types, as well as they allow operations with other instantiations of complex (complex objects with different template argument).

 

complex::complex

public member function
 
complex (const T& re = T(), const T& im = T());
complex (const complex& cmplx);
template<class X>
  complex (const complex<X>& cmplx);

Complex number constructor

Constructs a complex object.

It may be constructed from two values (re and im) or from another complex number.

Parameters

re, im
Real and imaginary parts, respectively, of complex number.
T is complex's template type.
cmplx
A complex object.
If constructed from a 
complex object with a different template parameter (X), the apropriate conversions are performed.


 

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// complex constructor example
#include <iostream>
#include <complex>
using namespace std;

int main ()
{
  complex<double> first (2.0,2.0);
  complex<double> second (first);
  complex<long double> third (second);

  cout << third;
  
  return 0;
} 



Output:


(2,2)

 

complex::real

public member function
 
T real() const;

Return real part

Returns the real part of the complex number.

A global function exists, 
real, with the same behavior.

Parameters

none

Return value

Real part.
T is complex's template type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// complex::real example
#include <iostream>
#include <complex>
using namespace std;

int main ()
{
  complex<double> mycomplex (10.0,1.0);

  cout << "Real part: " << mycomplex.real() << endl;

  return 0;
} 


Output:

Real part: 10

 

complex::imag

public member function
 
T imag() const;

Return imaginary part

Returns the imaginary part of the complex number.
The imaginary part is the factor by which the imaginary unit is multiplied.

A global function exists, 
imag, with the same behavior.

Parameters

none

Return value

Imaginary part.
T is complex's template type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
// complex::imag example
#include <iostream>
#include <complex>
using namespace std;

int main ()
{
  complex<double> mycomplex (20.0,2.0);

  cout << "Imaginary part: " << mycomplex.imag() << endl;

  return 0;
} 


Output:

Imaginary part: 2

posted @ 2011-09-22 21:27  Rabbit Nick  阅读(411)  评论(0编辑  收藏  举报