-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
135 lines (117 loc) · 3.65 KB
/
Complex.java
File metadata and controls
135 lines (117 loc) · 3.65 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
135
package ImageAnalysisLibrary;
public class Complex {
public double Real;
public double Imaginary;
public Complex() {
this.Real=0;
this.Imaginary=0;
}
public Complex(double Real,double Imaginray) {
this.Real=Real;
this.Imaginary=Imaginray;
}
public Complex(Complex Copy) {
this.Real=Copy.Real;
this.Imaginary=Copy.Imaginary;
}
public void Conjugate() {
this.Imaginary*=-1;
}
public Complex Add(Complex B) {
return new Complex(this.Real + B.Real, this.Imaginary + B.Imaginary);
}
public Complex Subtract(Complex B) {
return new Complex(this.Real - B.Real, this.Imaginary - B.Imaginary);
}
public Complex Multiplication(Complex b) {
return new Complex(this.Real * b.Real - this.Imaginary * b.Imaginary,
this.Real * b.Imaginary + this.Imaginary * b.Real);
}
public void Multiplication(double b) {
this.Real *= b;
this.Imaginary *=b;
}
public void Power(int power) {
for (int i = 1; i < power; i++) {
this.Multiplication(this);
}
}
public void Divide(Complex B) {
double divisor = B.Real*B.Real + B.Imaginary*B.Imaginary;
Complex t = new Complex(B);
t.Conjugate();
this.Multiplication(t);
this.Real /= divisor;
this.Imaginary /= divisor;
}
public String toString() {
if(Imaginary == 0) {
return "" + Real;
}
if(Imaginary > 0) {
return " " + Real + " + " + Imaginary + "I";
}else {
return " " + Real + Imaginary + "I";
}
}
public double Abs() {
if (Math.abs(Real) < Math.abs(Imaginary)) {
if (Imaginary == 0.0) {
return Math.abs(Real);
}
double q = Real / Imaginary;
return Math.abs(Imaginary) * Math.sqrt(1 + q * q);
} else {
if (Real == 0.0) {
return Math.abs(Imaginary);
}
double q = Imaginary / Real;
return Math.abs(Real) * Math.sqrt(1 + q * q);
}
}
public Complex Log() {
return new Complex(Math.log(Abs()),
Math.atan2(Imaginary, Real));
}
public static int bitReverse(int n, int bits) {
int reversedN = n;
int count = bits - 1;
n >>= 1;
while (n > 0) {
reversedN = (reversedN << 1) | (n & 1);
count--;
n >>= 1;
}
return ((reversedN << count) & ((1 << bits) - 1));
}
static void FFT(Complex[] Values) {
int bits = (int) (Math.log(Values.length) / Math.log(2));
for (int j = 1; j < Values.length / 2; j++) {
int swapPos = bitReverse(j, bits);
Complex temp = Values[j];
Values[j] = Values[swapPos];
Values[swapPos] = temp;
}
for (int N = 2; N <= Values.length; N <<= 1) {
for (int i = 0; i < Values.length; i += N) {
for (int k = 0; k < N / 2; k++) {
int evenIndex = i + k;
int oddIndex = i + k + (N / 2);
Complex even = Values[evenIndex];
Complex odd = Values[oddIndex];
double term = (-2 * Math.PI * k) / (double) N;
Complex exp = (new Complex(Math.cos(term), Math.sin(term)).Multiplication(odd));
Values[evenIndex] = even.Add(exp);
Values[oddIndex] = even.Subtract(exp);
}
}
}
}
public Complex Exp() {
double expReal = Math.exp(Real);
return new Complex(expReal * Math.cos(Imaginary),expReal * Math.sin(Imaginary));
}
public Complex Power(Complex x) {
return this.Log().Multiplication(x).Exp();
}
}