-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.cpp
162 lines (142 loc) · 3.78 KB
/
complex.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
#include "complex.h"
complex::complex() {
re=0;
im=0;
r=0;
g=0;
b=0;
}
complex::complex(double x, double y) {
re=x;
im=y;
r=0;
g=0;
b=0;
}
complex::complex(double x) {
re=x;
im=0;
r=0;
g=0;
b=0;
}
complex::complex (const complex &z) {
re=z.re;
im=z.im;
r=z.r;
g=z.g;
b=z.b;
}
complex::complex (const complex &z, double _r, double _g, double _b) {
re=z.re;
im=z.im;
r=_r;
g=_g;
b=_b;
}
complex::complex(double x, double y, double _r, double _g, double _b) {
re=x;
im=y;
r=_r;
g=_g;
b=_b;
}
complex::complex(std::string s) {
std::cout << "HEYY " << s << std::endl;
bool hasimaginary = false;
int signcounter = 0;
int lastsign = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i]=='+' || s[i]=='-') {
signcounter++;
lastsign = i;
}
else if (s[i]=='i') {
if (i<s.length()-1) {
throw "Wrong format of creating complex number";
}
else {
hasimaginary = true;
}
}
}
if (signcounter > 2) throw "Wrong format of creating complex number";
else {
if (lastsign > 0) {
if (hasimaginary) {
re = std::stod(s.substr(0,lastsign));
std::string strim = s.substr(lastsign,s.length()-lastsign-1);
//std::cout << "asd " << strim+"1";
strim = strim.length() > 1 ? strim : strim+"1";
//std::cout << "asd " << strim;
im = std::stod(strim);
}
else {
throw "Complex number has more than 1 real part";
}
}
else {
if (hasimaginary) {
//std::cout << 43534535 << std::endl;
re = 0;
std::string strim = s.substr(0,s.length()-1);
//std::cout << "asd " << strim << std::endl;
strim = strim.length() > 1 ? strim : strim+"1";
//std::cout << "asdsadfd "<< strim << std::endl;
im = std::stod(strim);
}
else {
re = std::stod(s);
im = 0;
}
}
}
}
double complex::mod() {
double res = sqrt(pow(this->im,2)+pow(this->re,2));
return res;
}
double complex::arg() {
double res = atan2(this->im,this->re);
return res;
}
complex complex::ln() {
complex res(log(sqrt(pow(this->im,2)+pow(this->re,2))),atan2(this->im,this->re));
return res;
}
complex complex::ex() {
complex res(exp(this->re)*cos(this->im), exp(this->re)*sin(this->im));
return res;
}
complex complex::sn() {
complex res(sin(re)*cosh(im),cos(re)*sinh(im));
//std::cout << sin(re)*cosh(im) << "+i*" << cos(re)*sinh(im) << std::endl;
return res;
}
complex complex::cs() {
complex res(cos(re)*cosh(im),-sin(re)*sinh(im));
return res;
}
complex complex::operator + (const complex &z2) {
complex res(this->re+z2.re, this->im+z2.im);
return res;
}
complex complex::operator - (const complex &z2) {
complex res(this->re-z2.re, this->im-z2.im);
return res;
}
complex complex::operator * (const complex &z2) {
complex res(this->re*z2.re-this->im*z2.im, this->re*z2.im+this->im*z2.re);
return res;
}
complex complex::operator / (const complex &z2) {
complex res((this->re*z2.re+this->im*z2.im)/(z2.re*z2.re+z2.im*z2.im), (z2.re*this->im-this->re*z2.im)/(z2.re*z2.re+z2.im*z2.im));
return res;
}
complex complex::operator ^ (const complex &a) {
complex res(pow(this->mod(),a.re*cos(a.re*this->arg())), pow(this->mod(),a.re*sin(a.re*this->arg())));
return res;
}
std::ostream &operator<<(std::ostream &os, complex const &z) {
return os << z.re << "+" << z.im << "i";
}