sokobo
Loading...
Searching...
No Matches
complex_number.h
1#pragma once
2#include <complex>
3#include <ostream>
4#include <string>
5#include <vector>
6
8{
9private:
10 std::complex<double> value;
11 double real;
12 double img;
13
14public:
15 ComplexNumber(double real = 0, double imag = 0)
16 : value(real, imag) {};
17
18 ComplexNumber(std::complex<double> val);
19
20 ComplexNumber operator-() const
21 {
22 return ComplexNumber(-value.real(), -value.imag());
23 }
24
25 bool operator>=(const ComplexNumber& other) const
26 {
27 return this->magnitude() >= other.magnitude();
28 }
29
30 bool operator>(const ComplexNumber& other) const
31 {
32 return this->magnitude() > other.magnitude();
33 }
34
35 bool operator<(const ComplexNumber& other) const
36 {
37 return this->magnitude() < other.magnitude();
38 }
39
40 friend std::ostream& operator<<(std::ostream& os, const ComplexNumber& c)
41 {
42 os << c.toString();
43 return os;
44 }
45
46 ComplexNumber& operator/=(const ComplexNumber& other)
47 {
48 this->value /= other.value; // usa std::complex<double>::operator/=
49 return *this;
50 }
51
52 ComplexNumber& operator-=(const ComplexNumber& other)
53 {
54 this->value -= other.value; // usa std::complex<double>::operator-=
55 return *this;
56 }
57
58 ComplexNumber& operator+=(const ComplexNumber& other)
59 {
60 this->value += other.value; // usa std::complex<double>::operator-=
61 return *this;
62 }
63
64 ComplexNumber& operator*=(const ComplexNumber& other)
65 {
66 this->value *= other.value; // usa std::complex<double>::operator-=
67 return *this;
68 }
69
70
71
72
73 // Basic operations
74 ComplexNumber operator+(const ComplexNumber& other) const;
75 ComplexNumber operator-(const ComplexNumber& other) const;
76 ComplexNumber operator*(const ComplexNumber& other) const;
77 ComplexNumber operator/(const ComplexNumber& other) const;
78 ComplexNumber operator^(int n) const;
79
80 // Complex functions
81 ComplexNumber conjugate() const;
82 double magnitude() const;
83 double phase() const;
84 ComplexNumber exp() const;
85 ComplexNumber log() const;
86 ComplexNumber sin() const;
87 ComplexNumber cos() const;
88 ComplexNumber tan() const;
89 ComplexNumber sqrt() const;
90
91 // Roots of unity
92 static std::vector<ComplexNumber> rootsOfUnity(int n);
93
94 std::string toString() const;
95 std::string toPolarString() const;
96
97 std::complex<double> getValue() const { return value; }
98};