Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
Complex Class Reference

Public Member Functions

 Complex (double x=0.f, double y=0.f, bool is_polar=false)
 
 Complex (const Complex &other)
 
double real () const
 
double imag () const
 
double abs () const
 
double arg () const
 
Complex operator+ (const Complex &other)
 
Complex operator- (const Complex &other)
 
Complex operator* (const Complex &other)
 
Complex operator~ () const
 
Complex operator/ (const Complex &other)
 
const Complexoperator= (const Complex &other)
 

Private Attributes

double re
 
double im
 

Detailed Description

Class Complex to represent complex numbers as a field.

Constructor & Destructor Documentation

◆ Complex() [1/2]

Complex::Complex ( double  x = 0.f,
double  y = 0.f,
bool  is_polar = false 
)
inlineexplicit

Complex Constructor which initialises the complex number which takes two arguments.

Parameters
xIf the third parameter is 'true' then this x is the absolute value of the complex number, if the third parameter is 'false' then this x is the real value of the complex number (optional).
yIf the third parameter is 'true' then this y is the argument of the complex number, if the third parameter is 'false' then this y is the imaginary value of the complex number (optional).
is_polar'false' by default. If we want to initialise our complex number using polar form then set this to true, otherwise set it to false to use initialiser which initialises real and imaginary values using the first two parameters (optional).
40  {
41  if (!is_polar) {
42  re = x;
43  im = y;
44  return;
45  }
46 
47  re = x * std::cos(y);
48  im = x * std::sin(y);
49  }
Here is the call graph for this function:

◆ Complex() [2/2]

Complex::Complex ( const Complex other)
inline

Copy Constructor

Parameters
otherThe other number to equate our number to.
55 : re(other.real()), im(other.imag()) {}

Member Function Documentation

◆ abs()

double Complex::abs ( ) const
inline

Member function to which gives the absolute value (modulus) of our complex number

Returns
\( \sqrt{z \dot \bar{z}} \) where \( z \) is our complex number.
73  {
74  return std::sqrt(this->re * this->re + this->im * this->im);
75  }
Here is the call graph for this function:

◆ arg()

double Complex::arg ( ) const
inline

Member function which gives the argument of our complex number.

Returns
Argument of our Complex number in radians.
81 { return std::atan2(this->im, this->re); }
Here is the call graph for this function:

◆ imag()

double Complex::imag ( ) const
inline

Member function (getter) to access the class' im value.

65 { return this->im; }

◆ operator*()

Complex Complex::operator* ( const Complex other)
inline

Operator overload to be able to multiple two complex numbers.

Parameters
otherThe other number to multiply the current number to.
Returns
result current number times other number.
108  {
109  Complex result(this->re * other.re - this->im * other.im,
110  this->re * other.im + this->im * other.re);
111  return result;
112  }

◆ operator+()

Complex Complex::operator+ ( const Complex other)
inline

Operator overload to be able to add two complex numbers.

Parameters
otherThe other number that is added to the current number.
Returns
result current number plus other number
88  {
89  Complex result(this->re + other.re, this->im + other.im);
90  return result;
91  }

◆ operator-()

Complex Complex::operator- ( const Complex other)
inline

Operator overload to be able to subtract two complex numbers.

Parameters
otherThe other number being subtracted from the current number.
Returns
result current number subtract other number
98  {
99  Complex result(this->re - other.re, this->im - other.im);
100  return result;
101  }

◆ operator/()

Complex Complex::operator/ ( const Complex other)
inline

Operator overload to be able to divide two complex numbers. This function would throw an exception if the other number is zero.

Parameters
otherThe other number we divide our number by.
Returns
result Current number divided by other number.
131  {
132  Complex result = *this * ~other;
133  double denominator =
134  other.real() * other.real() + other.imag() * other.imag();
135  if (denominator != 0) {
136  result = Complex(result.real() / denominator,
137  result.imag() / denominator);
138  return result;
139  } else {
140  throw std::invalid_argument("Undefined Value");
141  }
142  }
Here is the call graph for this function:

◆ operator=()

const Complex& Complex::operator= ( const Complex other)
inline

Operator overload to be able to copy RHS instance of Complex to LHS instance of Complex

148  {
149  this->re = other.real();
150  this->im = other.imag();
151  return *this;
152  }
Here is the call graph for this function:

◆ operator~()

Complex Complex::operator~ ( ) const
inline

Operator overload of the BITWISE NOT which gives us the conjugate of our complex number. NOTE: This is overloading the BITWISE operator but its not a BITWISE operation in this definition.

Returns
result The conjugate of our complex number.
120  {
121  Complex result(this->re, -(this->im));
122  return result;
123  }

◆ real()

double Complex::real ( ) const
inline

Member function (getter) to access the class' re value.

60 { return this->re; }

The documentation for this class was generated from the following file:
Complex
Definition: complex_numbers.cpp:19
std::atan2
T atan2(T... args)
std::cos
T cos(T... args)
Complex::Complex
Complex(double x=0.f, double y=0.f, bool is_polar=false)
Definition: complex_numbers.cpp:40
std::sqrt
T sqrt(T... args)
Complex::real
double real() const
Definition: complex_numbers.cpp:60
std::invalid_argument
STL class.
std::sin
T sin(T... args)
Complex::imag
double imag() const
Definition: complex_numbers.cpp:65