-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.pde
89 lines (81 loc) · 1.6 KB
/
Complex.pde
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
public static class Complex {
float a, b, m, p;
public static Complex fromMP(float m, float p) {
float a = m * cos(p);
float b = m * sin(p);
return new Complex(a, b);
}
public static Complex zero() {
return new Complex(0);
}
public static Complex one() {
return new Complex(1);
}
public static float mod(float a, float b) {
while (a < 0) a += b;
return a % b;
}
public static float mod(float a) {
return mod(a,TWO_PI);
}
Complex(float a_, float b_) {
a = a_;
b = b_;
m = sqrt(
a * a +
b * b
);
p = mod(atan2(b,a),TWO_PI);
}
Complex(float a) {
this(a, 0.0);
}
Complex() {
this(0.0, 0.0);
}
Complex add(Complex other) {
return new Complex(
a + other.a,
b + other.b
);
}
Complex sub(Complex other) {
return new Complex(
a - other.a,
b - other.b
);
}
Complex scale(float s) {
return new Complex(a * s, b * s);
}
Complex mult(Complex other) {
return new Complex(
a*other.a - b*other.b,
a*other.b + b*other.a
);
}
Complex div(Complex other) {
Complex c = other.conjugate();
Complex numerator = mult(c);
float denomenator = other.mult(c).a;
return new Complex(
numerator.a / denomenator,
numerator.b / denomenator
);
}
Complex conjugate() {
return new Complex(a, -b);
}
Complex sq() {
return mult(this);
}
float magSq() {
return m * m;
}
String strAB() {
return String.format("%.3f+%.3fi",a,b);
}
String strMP() {
return String.format("%.3fe^(%.3fi)",m,p);
}
}