TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
complex_numbers.cpp
Go to the documentation of this file.
1
10#include <cassert>
11#include <cmath>
12#include <complex>
13#include <ctime>
14#include <iostream>
15#include <stdexcept>
16
20class Complex {
21 // The real value of the complex number
22 double re;
23 // The imaginary value of the complex number
24 double im;
25
26 public:
43 explicit Complex(double x = 0.f, double y = 0.f, bool is_polar = false) {
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 }
53
58 Complex(const Complex &other) : re(other.real()), im(other.imag()) {}
59
64 double real() const { return this->re; }
65
70 double imag() const { return this->im; }
71
79 double abs() const {
80 return std::sqrt(this->re * this->re + this->im * this->im);
81 }
82
87 double arg() const { return std::atan2(this->im, this->re); }
88
95 Complex operator+(const Complex &other) {
96 Complex result(this->re + other.re, this->im + other.im);
97 return result;
98 }
99
106 Complex operator-(const Complex &other) {
107 Complex result(this->re - other.re, this->im - other.im);
108 return result;
109 }
110
117 Complex operator*(const Complex &other) {
118 Complex result(this->re * other.re - this->im * other.im,
119 this->re * other.im + this->im * other.re);
120 return result;
121 }
122
131 Complex result(this->re, -(this->im));
132 return result;
133 }
134
142 Complex operator/(const Complex &other) {
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 }
154
160 const Complex &operator=(const Complex &other) {
161 this->re = other.real();
162 this->im = other.imag();
163 return *this;
164 }
165};
166
175bool operator==(const Complex &a, const Complex &b) {
176 return a.real() == b.real() && a.imag() == b.imag();
177}
178
186std::ostream &operator<<(std::ostream &os, const Complex &num) {
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}
196
201double get_rand() { return (std::rand() % 100 - 50) / 100.f; }
202
206void tests() {
207 std::srand(std::time(nullptr));
208 double x1 = get_rand(), y1 = get_rand(), x2 = get_rand(), y2 = get_rand();
209 Complex num1(x1, y1), num2(x2, y2);
210 std::complex<double> cnum1(x1, y1), cnum2(x2, y2);
211 Complex result;
212 std::complex<double> expected;
213 // Test for addition
214 result = num1 + num2;
215 expected = cnum1 + cnum2;
216 assert(((void)"1 + 1i + 1 + 1i is equal to 2 + 2i but the addition doesn't "
217 "add up \n",
218 (result.real() == expected.real() &&
219 result.imag() == expected.imag())));
220 std::cout << "First test passes." << std::endl;
221 // Test for subtraction
222 result = num1 - num2;
223 expected = cnum1 - cnum2;
224 assert(((void)"1 + 1i - 1 - 1i is equal to 0 but the program says "
225 "otherwise. \n",
226 (result.real() == expected.real() &&
227 result.imag() == expected.imag())));
228 std::cout << "Second test passes." << std::endl;
229 // Test for multiplication
230 result = num1 * num2;
231 expected = cnum1 * cnum2;
232 assert(((void)"(1 + 1i) * (1 + 1i) is equal to 2i but the program says "
233 "otherwise. \n",
234 (result.real() == expected.real() &&
235 result.imag() == expected.imag())));
236 std::cout << "Third test passes." << std::endl;
237 // Test for division
238 result = num1 / num2;
239 expected = cnum1 / cnum2;
240 assert(((void)"(1 + 1i) / (1 + 1i) is equal to 1 but the program says "
241 "otherwise.\n",
242 (result.real() == expected.real() &&
243 result.imag() == expected.imag())));
244 std::cout << "Fourth test passes." << std::endl;
245 // Test for conjugates
246 result = ~num1;
247 expected = std::conj(cnum1);
248 assert(((void)"(1 + 1i) has a conjugate which is equal to (1 - 1i) but the "
249 "program says otherwise.\n",
250 (result.real() == expected.real() &&
251 result.imag() == expected.imag())));
252 std::cout << "Fifth test passes.\n";
253 // Test for Argument of our complex number
254 assert(((void)"(1 + 1i) has argument PI / 4 but the program differs from "
255 "the std::complex result.\n",
256 (num1.arg() == std::arg(cnum1))));
257 std::cout << "Sixth test passes.\n";
258 // Test for absolute value of our complex number
259 assert(((void)"(1 + 1i) has absolute value sqrt(2) but the program differs "
260 "from the std::complex result. \n",
261 (num1.abs() == std::abs(cnum1))));
262 std::cout << "Seventh test passes.\n";
263}
264
268int main() {
269 tests();
270 return 0;
271}
Class Complex to represent complex numbers as a field.
double real() const
Member function to get real value of our complex number. Member function (getter) to access the class...
Complex operator-(const Complex &other)
Operator overload of '-' on Complex class. Operator overload to be able to subtract two complex numbe...
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.
const Complex & operator=(const Complex &other)
Operator overload of '=' on Complex class. Operator overload to be able to copy RHS instance of Compl...
Complex operator+(const Complex &other)
Operator overload of '+' on Complex class. Operator overload to be able to add two complex numbers.
Complex operator~() const
Operator overload of '~' on Complex class. Operator overload of the BITWISE NOT which gives us the co...
Complex operator*(const Complex &other)
Operator overload of '*' on Complex class. Operator overload to be able to multiple two complex numbe...
Complex operator/(const Complex &other)
Operator overload of '/' on Complex class. Operator overload to be able to divide two complex numbers...
double arg() const
Member function to give the argument of our complex number.
double abs() const
Member function to give the modulus of our complex number. Member function to which gives the absolut...
double imag() const
Member function to get imaginary value of our complex number. Member function (getter) to access the ...
std::ostream & operator<<(std::ostream &os, const Complex &num)
Operator overload of '<<' of ostream for Complex class. Overloaded insersion operator to accommodate ...
bool operator==(const Complex &a, const Complex &b)
Operator overload of '==' on Complex class. Logical Equal overload for our Complex class.
double get_rand()
Function to get random numbers to generate our complex numbers for test.
void tests()
int main()