TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 20 of file complex_numbers.cpp.

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).

Definition at line 43 of file complex_numbers.cpp.

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 }

◆ Complex() [2/2]

Complex::Complex ( const Complex & other)
inline

Copy Constructor.

Parameters
otherThe other number to equate our number to.

Definition at line 58 of file complex_numbers.cpp.

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...
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...

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.

Definition at line 79 of file complex_numbers.cpp.

79 {
80 return std::sqrt(this->re * this->re + this->im * this->im);
81 }

◆ arg()

double Complex::arg ( ) const
inline

Member function to give the argument of our complex number.

Returns
Argument of our Complex number in radians.

Definition at line 87 of file complex_numbers.cpp.

87{ return std::atan2(this->im, this->re); }

◆ 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.

Definition at line 70 of file complex_numbers.cpp.

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.

Definition at line 117 of file complex_numbers.cpp.

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.
uint64_t result(uint64_t n)

◆ 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

Definition at line 95 of file complex_numbers.cpp.

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

Definition at line 106 of file complex_numbers.cpp.

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.

Definition at line 142 of file complex_numbers.cpp.

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.

◆ 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.

Definition at line 160 of file complex_numbers.cpp.

160 {
161 this->re = other.real();
162 this->im = other.imag();
163 return *this;
164 }

◆ 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.

Definition at line 130 of file complex_numbers.cpp.

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.

Definition at line 64 of file complex_numbers.cpp.

64{ return this->re; }

Member Data Documentation

◆ im

double Complex::im
private

Definition at line 24 of file complex_numbers.cpp.

◆ re

double Complex::re
private

Definition at line 22 of file complex_numbers.cpp.


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