Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
complex_numbers.cpp File Reference

An implementation of Complex Number as Objects. More...

#include <cassert>
#include <cmath>
#include <complex>
#include <ctime>
#include <iostream>
#include <stdexcept>
Include dependency graph for complex_numbers.cpp:

Classes

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

Functions

bool operator== (const Complex &a, const Complex &b)
 Operator overload of '==' on Complex class. Logical Equal overload for our Complex class. More...
 
std::ostreamoperator<< (std::ostream &os, const Complex &num)
 Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate the printing of our complex number in their standard form. More...
 
double get_rand ()
 Function to get random numbers to generate our complex numbers for test.
 
void tests ()
 
int main ()
 

Detailed Description

An implementation of Complex Number as Objects.

Author
tjgurwara99

A basic implementation of Complex Number field as a class with operators overloaded to accommodate (mathematical) field operations.

Function Documentation

◆ main()

int main ( )

Main function

267  {
268  tests();
269  return 0;
270 }
Here is the call graph for this function:

◆ operator<<()

std::ostream& operator<< ( std::ostream os,
const Complex num 
)

Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate the printing of our complex number in their standard form.

Parameters
osThe console stream
numThe complex number.
186  {
187  os << "(" << num.real();
188  if (num.imag() < 0) {
189  os << " - " << -num.imag();
190  } else {
191  os << " + " << num.imag();
192  }
193  os << "i)";
194  return os;
195 }
Here is the call graph for this function:

◆ operator==()

bool operator== ( const Complex a,
const Complex b 
)

Operator overload of '==' on Complex class. Logical Equal overload for our Complex class.

Parameters
aLeft hand side of our expression
bRight hand side of our expression
Returns
'True' If real and imaginary parts of a and b are same
'False' Otherwise.
175  {
176  return a.real() == b.real() && a.imag() == b.imag();
177 }
Here is the call graph for this function:

◆ tests()

void tests ( )

Tests Function

205  {
206  std::srand(std::time(nullptr));
207  double x1 = get_rand(), y1 = get_rand(), x2 = get_rand(), y2 = get_rand();
208  Complex num1(x1, y1), num2(x2, y2);
209  std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
210  Complex result;
211  std::complex<double> expected;
212  // Test for addition
213  result = num1 + num2;
214  expected = cnum1 + cnum2;
215  assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
216  "add up \n",
217  (result.real() == expected.real() &&
218  result.imag() == expected.imag())));
219  std::cout << "First test passes." << std::endl;
220  // Test for subtraction
221  result = num1 - num2;
222  expected = cnum1 - cnum2;
223  assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
224  "otherwise. \n",
225  (result.real() == expected.real() &&
226  result.imag() == expected.imag())));
227  std::cout << "Second test passes." << std::endl;
228  // Test for multiplication
229  result = num1 * num2;
230  expected = cnum1 * cnum2;
231  assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
232  "otherwise. \n",
233  (result.real() == expected.real() &&
234  result.imag() == expected.imag())));
235  std::cout << "Third test passes." << std::endl;
236  // Test for division
237  result = num1 / num2;
238  expected = cnum1 / cnum2;
239  assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
240  "otherwise.\n",
241  (result.real() == expected.real() &&
242  result.imag() == expected.imag())));
243  std::cout << "Fourth test passes." << std::endl;
244  // Test for conjugates
245  result = ~num1;
246  expected = std::conj(cnum1);
247  assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
248  "program says otherwise.\n",
249  (result.real() == expected.real() &&
250  result.imag() == expected.imag())));
251  std::cout << "Fifth test passes.\n";
252  // Test for Argument of our complex number
253  assert(((void)"(1 + 1i) has argument PI / 4 but the program differs from "
254  "the std::complex result.\n",
255  (num1.arg() == std::arg(cnum1))));
256  std::cout << "Sixth test passes.\n";
257  // Test for absolute value of our complex number
258  assert(((void)"(1 + 1i) has absolute value sqrt(2) but the program differs "
259  "from the std::complex result. \n",
260  (num1.abs() == std::abs(cnum1))));
261  std::cout << "Seventh test passes.\n";
262 }
Here is the call graph for this function:
std::srand
T srand(T... args)
Complex
Class Complex to represent complex numbers as a field.
Definition: complex_numbers.cpp:20
get_rand
double get_rand()
Function to get random numbers to generate our complex numbers for test.
Definition: complex_numbers.cpp:200
tests
void tests()
Definition: complex_numbers.cpp:205
Complex::real
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
std::cout
std::complex::real
T real(T... args)
Complex::imag
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...
Definition: complex_numbers.cpp:70
std::endl
T endl(T... args)
std::complex
STL class.
std::complex::imag
T imag(T... args)
std::time
T time(T... args)