-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.h
More file actions
65 lines (58 loc) · 1.42 KB
/
complex.h
File metadata and controls
65 lines (58 loc) · 1.42 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
#include<cmath>
class Complex{
private:
double real,imag;
public:
Complex(double x=0,double y=0){real=x;imag=y;}
double x() const{return real;}
double y() const{return imag;}
Complex operator +(Complex const &obj){
return Complex(real+obj.real,imag+obj.imag);
}
Complex operator -(Complex const &obj){
return Complex(real-obj.real,imag-obj.imag);
}
Complex operator *(Complex const &obj){
return Complex(real*obj.real-imag*obj.imag,real*obj.imag+imag*obj.real);
}
Complex operator /(Complex const &obj){
return Complex( (real*obj.real+imag*obj.imag)/(obj.mag()*obj.mag()),(imag*obj.real-real*obj.imag)/(obj.mag()*obj.mag()) );
}
Complex pow(int x) const;
double mag() const;
double arg() const;
Complex cjg() const;
void out() const{
printf("%.6lf%c%.6lfi",real,imag>=0?'+':'-',fabs(imag));
}
};
Complex Complex::pow(int x) const{
Complex res(1,0);
Complex p(real,imag);
for(int i=1;i<=abs(x);i++){
res=res*p;
}
if(x<0) res=Complex(1,0)/res;
return res;
}
double Complex::mag() const{
return sqrt(real*real+imag*imag);
}
double Complex::arg() const{
double res=acos(real/mag());
if(imag>0) return res;
else return -res;
}
Complex Complex::cjg() const{
return Complex(real,-imag);
}
Complex dis(Complex a,Complex b){
return Complex((a-b).mag(),0.0);
}
Complex Sqrt(double x){
double y=sqrt(fabs(x));
Complex res;
if(x>0) res=Complex(y,0.0);
else res=Complex(0.0,y);
return res;
}