-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComplex.java
More file actions
167 lines (121 loc) · 4.14 KB
/
Complex.java
File metadata and controls
167 lines (121 loc) · 4.14 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
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
162
163
164
165
166
167
package ckks;
import java.math.BigInteger;
public class Complex {
public double re, im;
// c'tors
public Complex(double re, double im) {
this.re = re;
this.im = im;
}
public Complex(double re) {
this(re, 0);
}
public Complex(BigInteger re) {
this(re.doubleValue(), 0);
}
public Complex() {
this(0, 0);
}
public Complex(Complex src) {
this(src.re, src.im);
}
// basic arithmetics
public Complex add(Complex that) {
return new Complex(this.re + that.re, this.im + that.im);
}
public void add_inplace(Complex that) {
this.re += that.re;
this.im += that.im;
}
public Complex sub(Complex that) {
return new Complex(this.re - that.re, this.im - that.im);
}
public void sub_inplace(Complex that) {
this.re -= that.re;
this.im -= that.im;
}
public Complex mult(Complex that) {
return new Complex(this.re * that.re - this.im * that.im, this.re * that.im + this.im * that.re);
}
public void mult_inplace(Complex that) {
double new_re = this.re * that.re - this.im * that.im;
double new_im = this.re * that.im + this.im * that.re;
this.re = new_re;
this.im = new_im;
}
public Complex divide(Complex that) {
Complex res = new Complex(this.mult(that.conj()));
double thatNormSquared = that.mult(that.conj()).re;
res.re /= thatNormSquared;
res.im /= thatNormSquared;
return res;
}
public void divide_inplace(Complex that) {
double thatNormSquared = that.mult(that.conj()).re;
this.mult_inplace(that.conj());
this.re /= thatNormSquared;
this.im /= thatNormSquared;
}
public Complex conj() {
return new Complex(this.re, -this.im);
}
public void conj_inplace() {
this.im = -this.im;
}
public double norm() {
return Math.sqrt(this.re * this.re + this.im * this.im);
}
public boolean equal(Complex other) {
return this.re == other.re && this.im == other.im;
}
// print
@Override
public String toString() {
return "(" + this.re + ", " + this.im + ")";
}
// vector operations
static public Complex hermitianProduct(Complex[] a, Complex[] b) {
if (a.length != b.length)
throw new IllegalArgumentException("Both vectors must be of the same size.");
Complex res = new Complex(0, 0);
for (int i = 0; i < a.length; i++)
res.add_inplace(a[i].mult(b[i].conj()));
return res;
}
static public Complex[] multVectorByScalar(Complex[] vec, Complex scalar) {
Complex[] res = deepClone(vec);
for (int i = 0; i < res.length; i++)
res[i].mult_inplace(scalar);
return res;
}
static public Complex[] multVectorByScalar(Complex[] vec, double scalar) {
return multVectorByScalar(vec, new Complex(scalar));
}
static public void multVectorByScalar_inplace(Complex[] vec, Complex scalar) {
for (int i = 0; i < vec.length; i++)
vec[i].mult_inplace(scalar);
}
static public void multVectorByScalar_inplace(Complex[] vec, double scalar) {
multVectorByScalar_inplace(vec, new Complex(scalar));
}
static public Complex[] addVectors(Complex[] a, Complex[] b) {
if (a.length != b.length)
throw new IllegalArgumentException("Both vectors must be of the same size.");
Complex[] res = deepClone(a);
for (int i = 0; i < a.length; i++)
res[i].add_inplace(b[i]);
return res;
}
static public void addVectors_inplace(Complex[] dest, Complex[] other) {
if (dest.length != other.length)
throw new IllegalArgumentException("Both vectors must be of the same size.");
for (int i = 0; i < dest.length; i++)
dest[i].add_inplace(other[i]);
}
static public Complex[] deepClone(Complex[] src) {
Complex[] clone = new Complex[src.length];
for (int i = 0; i < src.length; i++)
clone[i] = new Complex(src[i]);
return clone;
}
}