-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexe.cpp
229 lines (182 loc) · 5.01 KB
/
Complexe.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "Complexe.hpp"
using namespace std;
Complexe::Complexe(){ a = 0; b = 0; }
Complexe::Complexe(double _a, double _b){ a = _a; b = _b; }
Complexe::Complexe(double _a){ a = _a; b = 0; }
Complexe::Complexe(const Complexe& c){ a = c.a; b = c.b; }
const string Complexe::toString() const
{
ostringstream res;
if(a == 0){
if(b > 0){
if(b == 1)
res << "i";
else
res << b << "i";
}if(b < 0){
if(b == -1)
res << "- i";
else
res << "- " << double(0) - b << "i";
}if(b == 0)
res << "0";
}else if(b == 0){
if(a > 0)
res << a;
if(a < 0)
res << "-" << double(0) - a;
}else{// a!=0 && b != 0
if(a > 0 && b > 0){
if(b == 1)
res << a << " + i";
else
res << a << " + " << b << "i";
}else if(a > 0 && b < 0){
if(b == -1)
res << a << " - i";
else
res << a << " - " << double(0) - b << "i";
}else if(a < 0 && b > 0){
if(b == 1)
res <<"-"<< double(0) - a << " + i";
else
res <<"-"<< double(0) - a << " + " << b << "i";
}else if(a < 0 && b < 0){
if(b == -1)
res <<"-"<< double(0) - a << " - i";
else
res <<"-"<< double(0) - a << " - " << double(0) - b << "i";
}
}
return res.str();
}
void Complexe::setRe(double _a){a = _a;}
void Complexe::setIm(double _b){b = _b;}
double Complexe::getRe() const{return a;}
double Complexe::getIm() const{return b;}
Complexe& Complexe::operator=(const Complexe& c){
a = c.a;
b = c.b;
return *this;
}
Complexe& Complexe::operator+=(const Complexe& c){
a += c.a;
b += c.b;
return *this;
}
Complexe& Complexe::operator-=(const Complexe& c){
a -= c.a;
b -= c.b;
return *this;
}
Complexe Complexe::operator+(const Complexe& c){
return Complexe(a + c.a, b + c.b);
}
Complexe Complexe::operator-(const Complexe& c){
return Complexe(a - c.a, b - c.b);
}
bool Complexe::operator==(const Complexe& c) const{return a == c.a && b == c.b;}
bool Complexe::operator!=(const Complexe& c) const{return a != c.a && b != c.b;}
bool Complexe::operator<(const Complexe& c) const{//lexicographic order
if (a == c.a)
return b < c.b;
return a < c.a;
}
bool Complexe::operator>(const Complexe& c) const{//lexicographic order
if (a == c.a)
return b > c.b;
return a > c.a;
}
bool Complexe::operator<=(const Complexe& c) const{//lexicographic order
if (a == c.a)
return b <= c.b;
return a <= c.a;
}
bool Complexe::operator>=(const Complexe& c) const{//lexicographic order
if (a == c.a)
return b >= c.b;
return a >= c.a;
}
Complexe& Complexe::operator+=(double d_a){a+=d_a; return *this;}
Complexe& Complexe::operator-=(double d_a){a-=d_a; return *this;}
Complexe& Complexe::operator*=(double d_a){a*=d_a; b*=d_a; return *this;}
Complexe& Complexe::operator/=(double d_a){a/=d_a; b/=d_a; return *this;}
Complexe& Complexe::operator*=(const Complexe& c){
a = a*c.a - b*c.b;
b = a*c.b + b*c.a;
return *this;
}
Complexe Complexe::operator*(const Complexe& c){
return Complexe(a*c.a - b*c.b, a*c.b + b*c.a);
}
Complexe& Complexe::operator/=(const Complexe& c){
if(c.a == 0 && c.b == 0)
throw std::runtime_error("Division by zero");
Complexe * res = new Complexe();
res->a = (c.a*a - c.b*b) / (c.a*c.a + c.b*c.b);
res->b = (c.a*b + c.b*a) / (c.a*c.a + c.b*c.b);
return *res;
}
Complexe Complexe::operator/(const Complexe& c) {
return Complexe((c.a*1.0*a +c.b*1.0*b) / (c.a*1.0*c.a + c.b*1.0*c.b), (c.a*1.0*b - c.b*1.0*a) / (c.a*1.0*c.a + c.b*1.0*c.b));
}
Complexe Complexe::operator+(double d_a){
return Complexe(a + d_a, b);
}
Complexe Complexe::operator-(double d_b){
return Complexe(a - d_b, b);
}
Complexe Complexe::operator*(double d_c){
return Complexe(a * d_c, b*d_c);
}
Complexe Complexe::operator/(double d_c){
if(d_c == 0)
throw std::runtime_error("Division by zero");
return Complexe(a / d_c, b / d_c);
}
Complexe Complexe::conjugate() const{return Complexe(a, double() - b);}
double Complexe::abs() const {return sqrt(a*a + b*b);}
double Complexe::arg() const {return atan(b*1.0/a);}
double Complexe::distance(const Complexe& c) const{return sqrt((c.a - a) * (c.a - a) + (c.b - b) * (c.b - b));}
bool Complexe::translate(const Complexe& c){
a += c.a;
b += c.b;
return true;
}
bool Complexe::translate(double _a, double _b){
a += _a;
b += _b;
return true;
}
bool Complexe::translate(double _a){
a += _a;
return true;
}
bool Complexe::dilation(double zoom){
if(zoom == 0.0)
return false;
a *= zoom;
b *= zoom;
return true;
}
bool Complexe::rotate(double angle){
Complexe rotation_center(cos(angle), sin(angle));
(*this) *= rotation_center;
return true;
}
Complexe Complexe::operator^(double exp){
if(exp == 0.0)
return Complexe(1, 0);
return Complexe(pow(abs(), exp)*cos(arg() * exp), pow(abs(), exp)*sin(arg() * exp));
}
Complexe Complexe::exp(){
if(b == 0.0)
return Complexe(exp10(a), 0);
return Complexe(exp10(a)*cos(b), exp10(a)*sin(b)); //replace exp10 by exp ?
}
double Complexe::distance(const Complexe& c, const Complexe& d){
return sqrt((c.a - d.a)*(c.a - d.a) + (c.b - d.b)*(c.b - d.b));
}
std::ostream& operator << (std::ostream& os, const Complexe& x) {
return os << x.toString();
}