Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
Complex Class Reference

Class Complex to represent complex numbers as a field. More...

Public Member Functions

 Complex (double x=0.f, double y=0.f, bool is_polar=false)
 Complex Constructor which initialises our complex number.
 
 Complex (const Complex &other)
 Copy Constructor.
 
double real () const
 Member function to get real value of our complex number. Member function (getter) to access the class' re value.
 
double imag () const
 Member function to get imaginary value of our complex number. Member function (getter) to access the class' im value.
 
double abs () const
 Member function to give the modulus of our complex number. Member function to which gives the absolute value (modulus) of our complex number.
 
double arg () const
 Member function to give the argument of our complex number.
 
Complex operator+ (const Complex &other)
 Operator overload of '+' on Complex class. Operator overload to be able to add two complex numbers.
 
Complex operator- (const Complex &other)
 Operator overload of '-' on Complex class. Operator overload to be able to subtract two complex numbers.
 
Complex operator* (const Complex &other)
 Operator overload of '*' on Complex class. Operator overload to be able to multiple two complex numbers.
 
Complex operator~ () const
 Operator overload of '~' on Complex class. 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.
 
Complex operator/ (const Complex &other)
 Operator overload of '/' on Complex class. Operator overload to be able to divide two complex numbers. This function would throw an exception if the other number is zero.
 
const Complexoperator= (const Complex &other)
 Operator overload of '=' on Complex class. Operator overload to be able to copy RHS instance of Complex to LHS instance of Complex.
 

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 our complex number.

Complex Constructor which initialises the complex number which takes three 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).
43 {
44 if (!is_polar) {
45 re = x;
46 im = y;
47 return;
48 }
49
50 re = x * std::cos(y);
51 im = x * std::sin(y);
52 }
T cos(T... args)
T sin(T... args)
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.
58: re(other.real()), im(other.imag()) {}
double real() const
Member function to get real value of our complex number. Member function (getter) to access the class...
Definition complex_numbers.cpp:64
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...
Definition complex_numbers.cpp:70

Member Function Documentation

◆ abs()

double Complex::abs ( ) const
inline

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

Returns
\( \sqrt{z \bar{z}} \) where \( z \) is our complex number.
79 {
80 return std::sqrt(this->re * this->re + this->im * this->im);
81 }
T sqrt(T... args)
Here is the call graph for this function:

◆ arg()

double Complex::arg ( ) const
inline

Member function to give the argument of our complex number.

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

◆ imag()

double Complex::imag ( ) const
inline

Member function to get imaginary value of our complex number. Member function (getter) to access the class' im value.

70{ return this->im; }

◆ operator*()

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

Operator overload of '*' on Complex class. 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.
117 {
118 Complex result(this->re * other.re - this->im * other.im,
119 this->re * other.im + this->im * other.re);
120 return result;
121 }
Class Complex to represent complex numbers as a field.
Definition complex_numbers.cpp:20
uint64_t result(uint64_t n)
Definition fibonacci_sum.cpp:76

◆ operator+()

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

Operator overload of '+' on Complex class. 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
95 {
96 Complex result(this->re + other.re, this->im + other.im);
97 return result;
98 }

◆ operator-()

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

Operator overload of '-' on Complex class. 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
106 {
107 Complex result(this->re - other.re, this->im - other.im);
108 return result;
109 }

◆ operator/()

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

Operator overload of '/' on Complex class. 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.
142 {
143 Complex result = *this * ~other;
144 double denominator =
145 other.real() * other.real() + other.imag() * other.imag();
146 if (denominator != 0) {
147 result = Complex(result.real() / denominator,
148 result.imag() / denominator);
149 return result;
150 } else {
151 throw std::invalid_argument("Undefined Value");
152 }
153 }
Complex(double x=0.f, double y=0.f, bool is_polar=false)
Complex Constructor which initialises our complex number.
Definition complex_numbers.cpp:43
Here is the call graph for this function:

◆ operator=()

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

Operator overload of '=' on Complex class. Operator overload to be able to copy RHS instance of Complex to LHS instance of Complex.

160 {
161 this->re = other.real();
162 this->im = other.imag();
163 return *this;
164 }
Here is the call graph for this function:

◆ operator~()

Complex Complex::operator~ ( ) const
inline

Operator overload of '~' on Complex class. 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.
130 {
131 Complex result(this->re, -(this->im));
132 return result;
133 }

◆ real()

double Complex::real ( ) const
inline

Member function to get real value of our complex number. Member function (getter) to access the class' re value.

64{ return this->re; }

The documentation for this class was generated from the following file: