-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexLib.java
292 lines (232 loc) · 7.02 KB
/
ComplexLib.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
ComplexLib is a simple library for working with complex numbers in Java.
It currently implements the following operators and functions:
Addition
Subtraction
Multiplication
Reciprocation
Division
Exponentiation (limited to natural exponents)
Normalization
Conjugates
Polar coordinates
Square roots
Written by lachsdachs.
LICENSE:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
//base class representing complex numbers. Implements some operators/functions for convenience.
public class Complex {
public double r; //real part
public double i; //imaginary part
public Complex(double r_, double i_) {
r = r_;
i = i_;
}
//get real dude, smh rn
public double getReal() {
return r;
}
public double getImaginary() {
return i;
}
public double getMagnitude() {
return Math.sqrt( r*r + i*i );
}
public boolean equals(Complex c) {
return r == c.r && i == c.i;
}
public String toString() {
return "("+r+" + "+i+"i)";
}
//this works apparently
public void normalize() {
r /= getMagnitude();
i /= getMagnitude();
}
public void add(Complex c) {
r += c.r;
i += c.i;
}
public void subtract(Complex c) {
r -= c.r;
i -= c.i;
}
public void multiply(double a) {
r *= a;
i *= a;
}
public void multiply(Complex c) {
r = r * c.r - i * c.i;
i = r * c.i + i * c.r;
}
public double getPhase() {
return Math.atan2(r, i);
}
public Complex getConjugate() {
return complex(r, -i);
}
public Complex getReciprocal() {
double s = r*r + i*i;
return complex(r/s, -i/s);
}
public void square() {
multiply(complex(r, i));
}
public void raise(int exponent) {
if (exponent < 0) {
println("WARNING: ComplexLib: Exponentiation with a negative exponent is not supported and always yields 0r + 0i");
r = 0;
i= 0;
return;
}
exponent--;
while (exponent > 0) {
multiply(this);
exponent--;
}
}
}
public final Complex cZero = new Complex(0, 0);
public boolean cEquals(Complex a, Complex b) {
return a.r == b.r && a.i == b.i;
}
public String cToString(Complex c) {
return c.toString();
}
//shorthand for "new Complex()"
public Complex complex(double r, double i) {
return new Complex(r, i);
}
//useful when you want to do things like adding a real number to a complex number even though there is no function that explicitly supports it
public Complex complex(double r) {
return complex(r, 0);
}
public double cPhase(Complex input) {
return Math.atan2(input.r, input.i);
}
public Complex cConjugate(Complex c) {
return c.getConjugate();
}
public Complex cReciprocal(Complex c) {
return c.getReciprocal();
}
public Complex cAdd(Complex a, Complex b) {
return complex(a.r+b.r, a.i+b.i);
}
public Complex cAdd(Complex a, Complex b, Complex c) {
return complex(a.r+b.r+c.r, a.i+b.i+c.i);
}
public Complex cSum(Complex[] input) {
double rSum = 0;
double iSum = 0;
for (int i = 0; i < input.length; i++) {
rSum += input[i].r;
iSum += input[i].i;
}
Complex output = complex(rSum, iSum);
return output;
}
public Complex cSubtract(Complex a, Complex b) {
return complex(a.r-b.r, a.i-b.i);
}
public Complex cMultiply(Complex c, double alpha) {
return complex(c.r*alpha, c.i*alpha);
}
public Complex cMultiply(double alpha, Complex c) {
return complex(c.r*alpha, c.i*alpha);
}
public Complex cMultiply(Complex a, Complex b) {
double r = a.r * b.r - a.i * b.i;
double i = a.r * b.i + a.i * b.r;
return complex(r, i);
}
public Complex cDivide(Complex a, double b) {
return complex(a.r/b, a.i/b);
}
public Complex cDivide(Complex a, Complex b) {
return cMultiply(a, cReciprocal(b));
}
public Complex cSquare(Complex input) {
return cMultiply(input, input);
}
//can only do exponentiation with a natural exponent.
public Complex cRaise(Complex base, int exponent) {
if (exponent < 0) {
println("WARNING: ComplexLib: Exponentiation with a negative exponent is not supported and always yields 0r + 0i");
return cZero;
}
exponent--; //this seems to work but it's really awful
Complex result = base;
while (exponent > 0) {
result = cMultiply(result, base);
exponent--;
}
return result;
}
//TESTME
public Complex cSqrt(Complex input) {
double r = Math.sqrt(input.getMagnitude());
double theta = cPhase(input)/2;
return complex(r*Math.cos(theta), r*Math.sin(theta));
}
//TESTME
public Complex normalize(Complex c) {
return complex(c.r /= c.getMagnitude(), c.i /= c.getMagnitude());
}
//--------------TESTING ONLY--------------//
double precision = 0.00001; //how much the expected and the calculated result are allowed to diverge
//i used symbolab.com/solver/complex-numbers-calculator and WolframAlpha to calculate these
void cTest() {
println("starting test...");
test("Equals", cEquals(c(1, 23), c(1, 23)));
test("Addition", eq( cAdd(c(5, 6), c(2, 1)), c(7, 7)) );
test("Subtraction", eq( cSubtract(c(12.4, 5), c(11.2, 4.9)), c(1.2, 0.1)) );
test("Multiplication", eq( cMultiply(c(5.1, 6.3), c(2, -1)), c(16.5, 7.5)));
test("Reciprocation", eq( complex(3, -2).getReciprocal(), c(0.230769230, 0.1538461538) ));
test("Division", eq( cDivide(c(1, 2), c(2, 3)), c(0.615384615, 0.07692)));
test("Exponentiation", eq(cRaise(c(5, 7), 3), c(-610, 182)));
println("\n");
println(total + " tests have been performed. " + passed + " passed, " + failed + " failed.");
}
//shorthands for testing
Complex c(double r, double i) {
return complex(r, i);
}
boolean eq(Complex a, Complex b) {
return Math.abs(a.r-b.r)<precision&&Math.abs(a.i-b.i)<precision;
}
//weird generic test thingy
int passed = 0;
int failed = 0;
int total = 0;
void test(String name, boolean b) { //i tried to refactor it a bit and now it's even less readable. success!
total++;
print("\ntestcase \"" + name);
if (!b) {
print("\" failed");
failed++;
return;
}
passed++;
print("\" passed");
}