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

complexComplex number class (class template)

Functions

Complex values:
realReturn real part of complex (function template)
imagReturn imaginary part of complex (function template)
absReturn absolute value of complex (function template)
argReturn phase angle of complex (function template )
normReturn norm of complex number (function template)
conjReturn complex conjugate (function template)
polarReturn complex from polar components (function template )

Transcendentals overloads:
cosReturn cosine of complex (function template)
coshReturn hyperbolic cosine of complex (function template)
expReturn exponential of complex (function template)
logReturn natural logarithm of complex (function template)
log10Return common logarithm of complex (function template)
powReturn complex power (function template)
sinReturn sine of complex (function template)
sinhReturn hyperbolic sine of complex (function template)
sqrtReturn square root of complex (function template)
tanReturn tangent of complex (function template)
tanhReturn hyperbolic tangent of complex (function template)

Operator overloads:
complex operatorsComplex 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::imagReturn imaginary part (public member function)
complex::realReturn real part (public member function)
complex operatorsComplex number operators (functions)

The class also includes an alias type of the template argument:
complex::value_typeValue 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值