-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcomplex.cpp
More file actions
134 lines (125 loc) · 2.86 KB
/
Copy pathcomplex.cpp
File metadata and controls
134 lines (125 loc) · 2.86 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
/*
Credits: github.com/likecs/Competitive-Coding
*/
//Implementation of Complex library with operator overloading
#include <bits/stdc++.h>
using namespace std;
//Quite faster than inbuilt-complex C++ library as contains only the functions required
template<typename T> class cmplx {
private:
T x, y;
public:
cmplx () : x(0.0), y(0.0) {}
cmplx (T a) : x(a), y(0.0) {}
cmplx (T a, T b) : x(a), y(b) {}
friend ostream &operator << (ostream &output, const cmplx& a) {
if (a.y >= 0) output << a.x << "+" << a.y << "i\n";
else output << a.x << a.y << "i\n";
return output;
}
friend istream &operator >> (istream &input, cmplx& a) {
input >> a.x >> a.y;
return input;
}
T get_real() { return this->x; }
T get_img() { return this->y; }
T norm() { return this->x * this->x + this->y * this->y; }
T abs() { return sqrt(this->x * this->x + this->y * this->y); }
T arg() { return atan2(this->y, this->x) * 180.0 / M_PI; }
cmplx conj () { return cmplx(this->x, -(this->y)); }
cmplx operator = (const cmplx& a) {
this->x = a.x;
this->y = a.y;
return *this;
}
cmplx operator + (const cmplx& b) {
return cmplx(this->x + b.x, this->y + b.y);
}
cmplx operator - (const cmplx& b) {
return cmplx(this->x - b.x, this->y - b.y);
}
cmplx operator * (const T& num) {
return cmplx(this->x * num, this->y * num);
}
cmplx operator * (const cmplx& b) {
return cmplx(this->x * b.x - this->y * b.y, this->y * b.x + this->x * b.y);
}
cmplx operator / (const T& num) {
return cmplx(this->x / num, this->y / num);
}
cmplx operator / (const cmplx& b) {
cmplx temp(b.x, -b.y);
cmplx n = (*this) * temp;
T d = b.x * b.x + b.y * b.y;
cmplx res = n / d;
return res;
}
cmplx operator += (const cmplx& a) {
this->x += a.x;
this->y += a.y;
return *this;
}
cmplx operator -= (const cmplx& a) {
this->x -= a.x;
this->y -= a.y;
return *this;
}
cmplx operator *= (const T& a) {
cmplx temp = (*this) * a;
this->x = temp.x;
this->y = temp.y;
return *this;
}
cmplx operator *= (const cmplx& a) {
cmplx temp = (*this) * a;
this->x = temp.x;
this->y = temp.y;
return *this;
}
cmplx operator /= (const T& a) {
cmplx temp = (*this) / a;
this->x = temp.x;
this->y = temp.y;
return *this;
}
cmplx operator /= (const cmplx& a) {
cmplx temp = (*this) / a;
this->x = temp.x;
this->y = temp.y;
return *this;
}
};
//Sample program to use above library
int main() {
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
#endif
cmplx<double> a, b, c;
cin >> a >> b;
cout << a.get_real() << " " << a.get_img() << "\n";
cout << a;
cout << b;
cout << a+b;
cout << a-b;
cout << a*b;
cout << a/b;
cout << b.abs() << "\n";
cout << b.norm() << "\n";
cout << b.arg() << "\n";
cout << a.conj();
c = a;
cout << c;
c += b;
cout << c;
c -= b;
cout << c;
c *= b;
cout << c;
c /= b;
cout << c;
c *= 2.0;
cout << c;
c /= 2.5;
cout << c;
return 0;
}