-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomplex.cpp
50 lines (38 loc) · 1.13 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
#include <cstdio>
#include "complex.h"
Complex Complex::operator+(Complex z) {
return Complex(re + z.re, im + z.im);
}
Complex Complex::operator+(double lambda) {
return Complex(re + lambda, im);
}
Complex Complex::operator-(double lambda) {
return Complex(re - lambda, im);}
Complex Complex::operator-(Complex z) {
return Complex(re - z.re, im - z.im);
}
Complex Complex::operator*(Complex z) {
return Complex(re * z.re - im * z.im, re * z.im + im * z.re);
}
Complex Complex::operator/(Complex z) {
return (neg(z) * Complex(re, im)) * Complex(1 / (abs(z)*abs(z)), 0);
}
bool Complex::operator==(Complex z) {
double a = std::abs(re - z.re);
double b = std::abs(im -z.im);
if (a < 1e-6 && b < 1e-6){
return true;
}
else {return false;}
}
Complex inverse(Complex z){
return Complex(z.re / (z.re * z.re + z.im * z.im), - z.im / (z.re * z.re + z.im * z.im));
}
Complex neg (Complex z) {
return Complex(z.re, -z.im);
}
double abs (Complex z) {
return sqrt(z.re * z.re + z.im * z.im);
}
int arg(Complex z) {return atan2(z.re, z.im);}
void print(Complex z) { printf("%f + %fi", z.re, z.im);}