-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComplexNumber.java
314 lines (273 loc) · 11.2 KB
/
ComplexNumber.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package Mandelbrot;
public class ComplexNumber {
// the real part of a complex number
double real;
// the imaginary part of a complex number
double imaginary;
// ------------ Constructors: -------------
// initializes real and imaginary to zero
ComplexNumber() {
this.real = 0;
this.imaginary = 0;
}
// initializes the private variables real and imaginary to r and i respectively
ComplexNumber(double r, double i) {
this.real = r;
this.imaginary = i;
}
// ------------- Methods: --------------
// For all the examples below assume c1 and c2 are complex numbers that have
// already been declared and initialized
// Example of how to use this method: c1.addTo (c2) ;
// The above line (example) updates c1 with the result of adding c1 (the caller)
// with c2
void addTo(ComplexNumber c) {
this.real += c.real;
this.imaginary += c.imaginary;
}
// Example of how to use this method: c1.subFrom (c2) ;
// The above line (example) updates c1 with the result of subtracting c2 from c1
void subFrom(ComplexNumber c) {
this.real = c.real - this.real;
this.imaginary = c.imaginary - this.imaginary;
}
// Example of how to use this method: c1.multBy (c2) ;
// The above line (example) updates c1 with the result of multiplying c1 with c2
void multBy(ComplexNumber c) {
double temp = Double.valueOf(real); // store val of real immutably before it gets changed
this.real = this.real * c.real - this.imaginary * c.imaginary;
this.imaginary = temp * c.imaginary + c.real * this.imaginary;
}
// Example of how to use this method: c1.squareThisCN() ;
// The above line (example) updates c1 with the result of squaring c1
void squareThisCN() {
ComplexNumber c = new ComplexNumber();
c.real = this.real;
c.imaginary = this.imaginary;
multBy(c);
}
// Example of how to use this method: ComplexNumber myCN = c1.add(c2);
// The above line (example) initializes the complex number myCN with the result
// of adding c1 with c2
// Note: this method does not change c1 (the caller) in any way (as opposed to
// addTo).
ComplexNumber add(ComplexNumber c) {
ComplexNumber n = new ComplexNumber();
n.real = this.real + c.real;
n.imaginary = this.imaginary + c.imaginary;
return n;
}
// Example of how to use this method: ComplexNumber myCN = c1.sub(c2);
// The above line (example) initializes the complex number myCN with the result
// of subtracting c2 from c1
// Note: this method does not change c1 (the caller) in any way (as opposed to
// subFrom).
ComplexNumber sub(ComplexNumber c) {
ComplexNumber n = new ComplexNumber();
n.real = c.real - this.real;
n.imaginary = c.imaginary - this.imaginary;
return n;
}
// Example of how to use this method: ComplexNumber myCN = c1.mult(c2);
// The above line (example) initializes the complex number myCN with the result
// of multiplying c1 with c2
// Note: this method does not change c1 (the caller) in any way (as opposed to
// multBy).
ComplexNumber mult(ComplexNumber c) {
ComplexNumber n = new ComplexNumber();
n.real = this.real * c.real - this.imaginary * c.imaginary;
n.imaginary = this.real * c.imaginary + c.real * this.imaginary;
return n;
}
// Example of how to use this method: ComplexNumber myCN = c1.square();
// The above line (example) initializes the complex number myCN with the result
// of squaring c1
// Note: this method does not change c1 (the caller) in any way (as opposed to
// squareThisCN).
ComplexNumber square() {
ComplexNumber c = new ComplexNumber();
c.real = this.real;
c.imaginary = this.imaginary;
// c.squareThisCN();
// return c;
return c.mult(c);
}
// Example of how to use this method: if ( c1.equals(c2) ) { ... } else ...
// The above line uses the method equals as a condition for an if statement.
// This method returns true if c1 equals c2, otherwise this method returns false
// Note: this method does not change c1 (the caller) in any way.
boolean equals(ComplexNumber c) {
return this.real == c.real && this.imaginary == c.imaginary;
}
// Example of how to use this method: double absValue = c1.modulus();
// The above line uses the method modulus to calculate the absolute (or modulus)
// value of the complex number c1 .
// Note: this method does not change c1 (the caller) in any way.
double modulus() {
double r = Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imaginary, 2));
return r;
}
// Example of how to use this method: double absValue = c1.modSquare();
// The above line uses the method modSquare to calculate square of the absolute
// (or modulus) value of the complex number c1.
// Note: this method does not change c1 (the caller) in any way.
double modSquare() {
double mod = this.modulus();
return Math.pow(mod, 2);
}
// This method prints the complex number to the screen. Below is an example of
// how you can use this method :
// ComplexNumber cn = new ComplexNumber (2, 3);
// cn.display();
// The above example should display the following to the console:
// 2 + 3i
void display() {
String positiveOrNegative = this.imaginary > 0 ? " +" : " ";
System.out.println(this.real + positiveOrNegative + this.imaginary + "i");
}
private boolean isEven(int n) {
return n % 2 == 0;
}
// This method returns the val of i^n if imaginary number is 1
void iToThePow(int n) {
// ComplexNumber c = new ComplexNumber();
int quotient = n / 2;
int remainder = n % 2;
if (isEven(quotient)) {
if (remainder == 0) {
System.out.println("i ^ " + n + " : 1");
} else {
System.out.println("i ^ " + n + " : i");
}
} else {
if (remainder == 0) {
System.out.println("i ^ " + n + " : -1");
} else {
System.out.println("i ^ " + n + " : -i");
}
}
}
// Convert mandelbrot X to Java X
int mandelbrotToJavaCoordX(int javaOriginX, double mandelbrotX, float scale) {
int javaX = (int) (javaOriginX + mandelbrotX * scale);
return javaX;
}
// Convert mandelbrot Y to Java Y
int mandelbrotToJavaCoordY(int javaOriginY, double mandelbrotY, float scale) {
int javaY = (int) (javaOriginY + mandelbrotY * scale);
return javaY;
}
// Convert Java X to mandelbrot X
void javaCoordToMandelbrotX(int javaOriginX, int javaX, float scale) {
// ComplexNumber num = new ComplexNumber();
// num.real = (javaX - javaOriginX) / scale;
// return num;
this.real = (javaX - javaOriginX) / scale;
}
// Convert Java Y to mandelbrot Y
void javaCoordToMandelbrotY(int javaOriginY, int javaY, float scale) {
// ComplexNumber num = new ComplexNumber();
// num.imaginary = (javaY - javaOriginY) / scale;
// return num;
this.imaginary = (javaY - javaOriginY) / scale;
}
// int inMandelbrotSetRange(ComplexNumber c, ComplexNumber constant, int count) {
// if (count >= 425)
// return count;
// c.squareThisCN();
// c.addTo(constant);
// double mod = c.modulus();
// if (mod >= 2) {
// // System.out.println(count);
// // c.display();
// return count;
// }
// count++;
// int result = inMandelbrotSetRange(c, constant, count);
// return result;
// }
// int inMandelbrotSetRange(ComplexNumber constant) {
// ComplexNumber c = new ComplexNumber();
// int result = inMandelbrotSetRange(c, constant, 1);
// return result;
// }
// boolean inMandelbrotSet(ComplexNumber c, ComplexNumber constant, int count) {
// if (count > 15)
// return true;
// c.squareThisCN();
// c.addTo(constant);
// double mod = c.modulus();
// if (mod >= 2) {
// // System.out.println(count);
// // c.display();
// return false;
// }
// count++;
// boolean result = inMandelbrotSet(c, constant, count);
// return result;
// }
// boolean inMandelbrotSet(ComplexNumber constant) {
// ComplexNumber c = new ComplexNumber();
// boolean result = inMandelbrotSet(c, constant, 1);
// return result;
// }
public static void main(String[] args) {
// ComplexNumber a = new ComplexNumber(-2, 0);
// int jX = a.mandelbrotToJavaCoordX(400, a.real, 200);
// System.out.println(jX);
// ComplexNumber c = new ComplexNumber(0, 0.7);
// boolean r = c.inMandelbrotSet(c);
// int r = c.inMandelbrotSetRange(c);
// System.out.println(r);
// Testing using Worksheet:
// ComplexNumber a = new ComplexNumber();
// System.out.println("\n1. ");
// a.iToThePow(5);
// System.out.println("\n2. ");
// a.iToThePow(32);
// System.out.println("\n3. ");
// a.iToThePow(102);
// ComplexNumber d = new ComplexNumber(2, 3);
// ComplexNumber d2 = new ComplexNumber(-5, 6);
// ComplexNumber dAns = d.add(d2);
// d.addTo(d2);
// System.out.println("\n4. ");
// dAns.display();
// d.display();
// ComplexNumber e = new ComplexNumber(-6, -8);
// ComplexNumber e2 = new ComplexNumber(9, 4);
// ComplexNumber eAns = e2.sub(e);
// e2.subFrom(e);
// System.out.println("\n5. ");
// eAns.display();
// e2.display();
// ComplexNumber f = new ComplexNumber(4, 2);
// ComplexNumber f2 = new ComplexNumber(6, -12);
// ComplexNumber fAns = f.mult(f2);
// f.multBy(f2);
// System.out.println("\n6. ");
// fAns.display();
// f.display();
// ComplexNumber g = new ComplexNumber(7, -3);
// ComplexNumber g2 = new ComplexNumber(5, -10);
// ComplexNumber gAns = g.mult(g2);
// g.multBy(g2);
// System.out.println("\n7. ");
// gAns.display();
// g.display();
// ComplexNumber h = new ComplexNumber(-5, 3);
// ComplexNumber hAns = h.square();
// h.squareThisCN();
// System.out.println("\n8. ");
// hAns.display();
// h.display();
// ComplexNumber i = new ComplexNumber(8, -9);
// double iAns = i.modulus();
// System.out.println("\n9. ");
// System.out.println(iAns);
// ComplexNumber j = new ComplexNumber(3, 4);
// double jAns = j.modSquare();
// System.out.println("\n10. ");
// System.out.println(jAns);
}
}