-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.cpp
More file actions
160 lines (150 loc) · 4.27 KB
/
complex.cpp
File metadata and controls
160 lines (150 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "complex.h"
complex::complex(long double _re,long double _im){
re =_re ;
im = _im ;
}
complex::complex(const complex&src){
re = src.re ;
im = src.im ;
}
complex::complex(const int _re){
re=_re ;
im = 0;
}
complex::~complex(){
}
long double complex::get_re(void)const{
return re ;
}
long double complex::get_im(void)const{
return im ;
}
void complex:: operator=(const complex&src){
re = src.re ;
im = src.im ;
}
complex complex:: operator+(const complex&c2)const{
return complex(re+c2.re,im+c2.im) ;
}
complex complex:: operator-(const complex&c2)const{
return complex(re-c2.re,im-c2.im) ;
}
complex complex:: operator*(const complex&c2)const{
return complex((re*c2.re-im*c2.im),(re*c2.im+im*c2.re));
}
complex complex:: operator/(const complex&c2)const{
return (*this*conjugate(c2))/(abs(c2)*abs(c2)) ;
}
complex complex:: operator*(const long double &scalar)const{
return complex(scalar*re,scalar*im);
}
complex complex::operator/(const long double &scalar)const{
return complex(re/scalar,im/scalar);
}
void complex:: operator+=(const complex&c2){
*this = *this+c2 ;
}
void complex::operator-=(const complex&c2) {
*this = *this-c2 ;
}
void complex::operator*=(const complex&c2) {
*this = *this*c2 ;
}
void complex::operator/=(const complex&c2){
*this = *this/c2 ;
}
void complex::operator^=(const long double &power){
*this = *this^power;
}
void complex::operator*=(const long double &scalar){
*this = *this*scalar;
}
void complex::operator/=(const long double &scalar){
*this = *this/scalar;
}
complex complex:: operator^(const long double &power) const{
long double rad = pow(abs(*this),power) ;
long double the = power*theta() ;
return complex(rad*cos(the),rad*sin(the)) ;
}
long double complex::theta(void)const{
return atan2(im,re);
}
long double abs(const complex&c){
return sqrt(c.get_re()*c.get_re()+c.get_im()*c.get_im()) ;
}
bool complex::operator>(const complex&c2)const{
return abs(*this)>abs(c2) ;
}
bool complex::operator<(const complex&c2)const{
return abs(*this)<abs(c2) ;
}
bool complex::operator==(const complex&c2)const{
return (abs(re-c2.re)<tolerance&&abs(im-c2.im)<tolerance);
}
bool complex::operator!=(const complex&c2)const{
return !(*this==c2) ;
}
std::ostream& operator<<(std::ostream& os, const complex& c) {
os << c.re;
if(abs(c.im)>tolerance){
if(c.im>0){
os << "+";
}
os << c.im << "j";
}
return os;
}
string to_string(complex&c){
string ret_str =to_string(c.get_re()) ;
if(c.get_im()>0){
ret_str+="+";
}
ret_str+=to_string(c.get_im()) ;
return ret_str ;
}
// Template specialization for your complex type
complex conjugate(const complex& val) {
// Replace with your own implementation for complex numbers
return complex(val.get_re(),val.get_im()*-1);
}
unsigned char conjugate(const unsigned char &val){
return val ;
}
char conjugate(const char &val){
return val ;
}
unsigned int conjugate(const unsigned int &val){
return val ;
}
int conjugate(const int &val){
return val ;
}
float conjugate(const float &val){
return val ;
}
long double conjugate(const long double &val){
return val ;
}
unsigned long conjugate(const unsigned long &val){
return val ;
}
long conjugate(const long &val){
return val ;
}
long long conjugate(const long long &val){
return val ;
}
unsigned long long conjugate(const unsigned long long &val){
return val ;
}
double conjugate(const double&val){
return val ;
}
complex pow(const complex &c,int power){
return c^power ;
}
void complex ::operator=(const long double & val) {
re = val ;
im = 0;
}