1
1
package com .thealgorithms .maths ;
2
2
3
-
4
3
public class ComplexNumberUtil {
5
4
6
- public static class ComplexNumber
7
- {
5
+ public static class ComplexNumber {
8
6
public final double REAL ;
9
7
public final double IMAGINARY ;
10
8
11
- public ComplexNumber (double real , double imaginary )
12
- {
9
+ public ComplexNumber (double real , double imaginary ) {
13
10
REAL = real ;
14
11
IMAGINARY = imaginary ;
15
12
}
16
13
17
14
@ Override
18
15
public boolean equals (Object obj ) {
19
- if (obj instanceof ComplexNumber num )
20
- {
16
+ if (obj instanceof ComplexNumber num ) {
21
17
return this .REAL == num .REAL && this .IMAGINARY == num .IMAGINARY ;
22
18
}
23
19
@@ -30,20 +26,19 @@ public String toString() {
30
26
}
31
27
}
32
28
33
- public final static ComplexNumber ZERO = new ComplexNumber (0 ,0 );
34
- public final static ComplexNumber ONE = new ComplexNumber (1 ,0 );
35
- public final static ComplexNumber TWO = new ComplexNumber (2 ,0 );
36
- public final static ComplexNumber PLUS_I = new ComplexNumber (0 ,1 );
37
- public final static ComplexNumber MINUS_I = new ComplexNumber (0 ,-1 );
29
+ public final static ComplexNumber ZERO = new ComplexNumber (0 , 0 );
30
+ public final static ComplexNumber ONE = new ComplexNumber (1 , 0 );
31
+ public final static ComplexNumber TWO = new ComplexNumber (2 , 0 );
32
+ public final static ComplexNumber PLUS_I = new ComplexNumber (0 , 1 );
33
+ public final static ComplexNumber MINUS_I = new ComplexNumber (0 , -1 );
38
34
39
35
/**
40
36
* add two complex numbers
41
37
* @param num1 the first complex number
42
38
* @param num2 the second complex number
43
39
* @return the sum of num1 and num2
44
40
*/
45
- public static ComplexNumber add (ComplexNumber num1 , ComplexNumber num2 )
46
- {
41
+ public static ComplexNumber add (ComplexNumber num1 , ComplexNumber num2 ) {
47
42
return new ComplexNumber (num1 .REAL + num2 .REAL , num1 .IMAGINARY + num2 .IMAGINARY );
48
43
}
49
44
@@ -54,8 +49,7 @@ public static ComplexNumber add(ComplexNumber num1, ComplexNumber num2)
54
49
* @param num2 the second complex number
55
50
* @return the result of subtracting num2 from num1
56
51
*/
57
- public static ComplexNumber subtract (ComplexNumber num1 , ComplexNumber num2 )
58
- {
52
+ public static ComplexNumber subtract (ComplexNumber num1 , ComplexNumber num2 ) {
59
53
return new ComplexNumber (num1 .REAL - num2 .REAL , num1 .IMAGINARY - num2 .IMAGINARY );
60
54
}
61
55
@@ -67,12 +61,8 @@ public static ComplexNumber subtract(ComplexNumber num1, ComplexNumber num2)
67
61
* @return the product of num1 and num2
68
62
* @link <a href="https://en.wikipedia.org/wiki/Complex_number#Multiplication">...</a>
69
63
*/
70
- public static ComplexNumber multiply (ComplexNumber num1 , ComplexNumber num2 )
71
- {
72
- return new ComplexNumber (
73
- num1 .REAL *num2 .REAL - num1 .IMAGINARY *num2 .IMAGINARY ,
74
- num1 .REAL *num2 .IMAGINARY + num1 .IMAGINARY *num2 .REAL
75
- );
64
+ public static ComplexNumber multiply (ComplexNumber num1 , ComplexNumber num2 ) {
65
+ return new ComplexNumber (num1 .REAL * num2 .REAL - num1 .IMAGINARY * num2 .IMAGINARY , num1 .REAL * num2 .IMAGINARY + num1 .IMAGINARY * num2 .REAL );
76
66
}
77
67
78
68
/**
@@ -84,18 +74,13 @@ public static ComplexNumber multiply(ComplexNumber num1, ComplexNumber num2)
84
74
* @throws RuntimeException if the divisor (num2) is zero
85
75
* @link <a href="https://en.wikipedia.org/wiki/Complex_number#Complex_conjugate,_absolute_value_and_argument">...</a>
86
76
*/
87
- public static ComplexNumber divide (ComplexNumber num1 , ComplexNumber num2 )
88
- {
89
- final double divisor = num2 .REAL *num2 .REAL + num2 .IMAGINARY *num2 .IMAGINARY ;
90
- if (divisor == 0 )
91
- {
77
+ public static ComplexNumber divide (ComplexNumber num1 , ComplexNumber num2 ) {
78
+ final double divisor = num2 .REAL * num2 .REAL + num2 .IMAGINARY * num2 .IMAGINARY ;
79
+ if (divisor == 0 ) {
92
80
throw new RuntimeException ("Cannot divide by zero" );
93
81
}
94
82
95
- return new ComplexNumber (
96
- (num1 .REAL *num2 .REAL + num1 .IMAGINARY *num2 .IMAGINARY ) / divisor ,
97
- (num1 .IMAGINARY *num2 .REAL - num1 .REAL *num2 .IMAGINARY ) / divisor
98
- );
83
+ return new ComplexNumber ((num1 .REAL * num2 .REAL + num1 .IMAGINARY * num2 .IMAGINARY ) / divisor , (num1 .IMAGINARY * num2 .REAL - num1 .REAL * num2 .IMAGINARY ) / divisor );
99
84
}
100
85
101
86
/**
@@ -105,9 +90,8 @@ public static ComplexNumber divide(ComplexNumber num1, ComplexNumber num2)
105
90
* @return the absolute value of num
106
91
* @link <a href="https://en.wikipedia.org/wiki/Complex_number#Complex_conjugate,_absolute_value_and_argument">...</a>
107
92
*/
108
- public static double abs (ComplexNumber num )
109
- {
110
- return Math .sqrt (num .REAL *num .REAL + num .IMAGINARY *num .IMAGINARY );
93
+ public static double abs (ComplexNumber num ) {
94
+ return Math .sqrt (num .REAL * num .REAL + num .IMAGINARY * num .IMAGINARY );
111
95
}
112
96
113
97
/**
@@ -117,13 +101,9 @@ public static double abs(ComplexNumber num)
117
101
* @return e raised to the power of num
118
102
* @link <a href="https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex">...</a>
119
103
*/
120
- public static ComplexNumber exp (ComplexNumber num )
121
- {
104
+ public static ComplexNumber exp (ComplexNumber num ) {
122
105
final double coefficient = Math .exp (num .REAL );
123
- return new ComplexNumber (
124
- coefficient * Math .cos (num .IMAGINARY ),
125
- coefficient * Math .sin (num .IMAGINARY )
126
- );
106
+ return new ComplexNumber (coefficient * Math .cos (num .IMAGINARY ), coefficient * Math .sin (num .IMAGINARY ));
127
107
}
128
108
129
109
/**
@@ -134,17 +114,12 @@ public static ComplexNumber exp(ComplexNumber num)
134
114
* @throws RuntimeException if num is zero
135
115
* @link <a href="https://en.wikipedia.org/wiki/Complex_logarithm#Calculating_the_principal_value">...</a>
136
116
*/
137
- public static ComplexNumber ln (ComplexNumber num )
138
- {
139
- if (num .equals (ZERO ))
140
- {
117
+ public static ComplexNumber ln (ComplexNumber num ) {
118
+ if (num .equals (ZERO )) {
141
119
throw new RuntimeException ("Cannot take the logarithm of zero" );
142
120
}
143
121
144
- return new ComplexNumber (
145
- Math .log (abs (num )),
146
- Math .atan2 (num .IMAGINARY ,num .REAL )
147
- );
122
+ return new ComplexNumber (Math .log (abs (num )), Math .atan2 (num .IMAGINARY , num .REAL ));
148
123
}
149
124
150
125
/**
@@ -155,14 +130,12 @@ public static ComplexNumber ln(ComplexNumber num)
155
130
* @return num1 raised to the power of num2
156
131
* link <a href="https://en.wikipedia.org/wiki/Exponentiation#Complex_exponents_with_a_positive_real_base">...</a>
157
132
*/
158
- public static ComplexNumber pow (ComplexNumber num1 , ComplexNumber num2 )
159
- {
160
- if (num1 .equals (ZERO ))
161
- {
133
+ public static ComplexNumber pow (ComplexNumber num1 , ComplexNumber num2 ) {
134
+ if (num1 .equals (ZERO )) {
162
135
return num2 .equals (ZERO ) ? ONE : ZERO ;
163
136
}
164
137
165
- return exp (multiply (ln (num1 ),num2 ));
138
+ return exp (multiply (ln (num1 ), num2 ));
166
139
}
167
140
168
141
/**
@@ -171,9 +144,8 @@ public static ComplexNumber pow(ComplexNumber num1, ComplexNumber num2)
171
144
* @param num the complex number
172
145
* @return the square root of num
173
146
*/
174
- public static ComplexNumber sqrt (ComplexNumber num )
175
- {
176
- return pow (num ,new ComplexNumber (0.5 ,0 ));
147
+ public static ComplexNumber sqrt (ComplexNumber num ) {
148
+ return pow (num , new ComplexNumber (0.5 , 0 ));
177
149
}
178
150
179
151
/**
@@ -183,11 +155,10 @@ public static ComplexNumber sqrt(ComplexNumber num)
183
155
* @return the sine of num
184
156
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Relationship_to_exponential_function_">...</a>(Euler's_formula)
185
157
*/
186
- public static ComplexNumber sin (ComplexNumber num )
187
- {
188
- ComplexNumber exp1 = exp (multiply (num ,PLUS_I ));
189
- ComplexNumber exp2 = exp (multiply (num ,MINUS_I ));
190
- return divide (subtract (exp1 ,exp2 ),multiply (new ComplexNumber (2 ,0 ),PLUS_I ));
158
+ public static ComplexNumber sin (ComplexNumber num ) {
159
+ ComplexNumber exp1 = exp (multiply (num , PLUS_I ));
160
+ ComplexNumber exp2 = exp (multiply (num , MINUS_I ));
161
+ return divide (subtract (exp1 , exp2 ), multiply (new ComplexNumber (2 , 0 ), PLUS_I ));
191
162
}
192
163
193
164
/**
@@ -197,11 +168,10 @@ public static ComplexNumber sin(ComplexNumber num)
197
168
* @return the cosine of num
198
169
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Relationship_to_exponential_function_">...</a>(Euler's_formula)
199
170
*/
200
- public static ComplexNumber cos (ComplexNumber num )
201
- {
202
- ComplexNumber exp1 = exp (multiply (num ,PLUS_I ));
203
- ComplexNumber exp2 = exp (multiply (num ,MINUS_I ));
204
- return divide (add (exp1 ,exp2 ),TWO );
171
+ public static ComplexNumber cos (ComplexNumber num ) {
172
+ ComplexNumber exp1 = exp (multiply (num , PLUS_I ));
173
+ ComplexNumber exp2 = exp (multiply (num , MINUS_I ));
174
+ return divide (add (exp1 , exp2 ), TWO );
205
175
}
206
176
207
177
/**
@@ -212,14 +182,12 @@ public static ComplexNumber cos(ComplexNumber num)
212
182
* @throws RuntimeException if <code>num.real = pi*(n+0.5)</code>
213
183
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Right-angled_triangle_definitions">...</a>
214
184
*/
215
- public static ComplexNumber tan (ComplexNumber num )
216
- {
217
- if (num .REAL % Math .PI == Math .PI / 2 )
218
- {
185
+ public static ComplexNumber tan (ComplexNumber num ) {
186
+ if (num .REAL % Math .PI == Math .PI / 2 ) {
219
187
throw new RuntimeException ("Cannot take the tan of a number where the real part can be expressed as pi*(n+0.5)" );
220
188
}
221
189
222
- return divide (sin (num ),cos (num ));
190
+ return divide (sin (num ), cos (num ));
223
191
}
224
192
225
193
/**
@@ -230,14 +198,12 @@ public static ComplexNumber tan(ComplexNumber num)
230
198
* @throws RuntimeException if <code>num.real = pi*n</code>
231
199
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Right-angled_triangle_definitions">...</a>
232
200
*/
233
- public static ComplexNumber cot (ComplexNumber num )
234
- {
235
- if (num .REAL % Math .PI == 0 )
236
- {
201
+ public static ComplexNumber cot (ComplexNumber num ) {
202
+ if (num .REAL % Math .PI == 0 ) {
237
203
throw new RuntimeException ("Cannot take the cot of number with real part dividable by pi" );
238
204
}
239
205
240
- return divide (cos (num ),sin (num ));
206
+ return divide (cos (num ), sin (num ));
241
207
}
242
208
243
209
/**
@@ -247,10 +213,9 @@ public static ComplexNumber cot(ComplexNumber num)
247
213
* @return the arcsine of num
248
214
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
249
215
*/
250
- public static ComplexNumber arcsin (ComplexNumber num )
251
- {
252
- ComplexNumber temp = sqrt (subtract (ONE ,multiply (num ,num )));
253
- return multiply (MINUS_I ,ln (add (multiply (PLUS_I ,num ),temp )));
216
+ public static ComplexNumber arcsin (ComplexNumber num ) {
217
+ ComplexNumber temp = sqrt (subtract (ONE , multiply (num , num )));
218
+ return multiply (MINUS_I , ln (add (multiply (PLUS_I , num ), temp )));
254
219
}
255
220
256
221
/**
@@ -260,10 +225,9 @@ public static ComplexNumber arcsin(ComplexNumber num)
260
225
* @return the arccosine of num
261
226
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
262
227
*/
263
- public static ComplexNumber arccos (ComplexNumber num )
264
- {
265
- ComplexNumber temp = sqrt (subtract (ONE ,multiply (num ,num )));
266
- return multiply (MINUS_I ,ln (add (num ,multiply (temp ,PLUS_I ))));
228
+ public static ComplexNumber arccos (ComplexNumber num ) {
229
+ ComplexNumber temp = sqrt (subtract (ONE , multiply (num , num )));
230
+ return multiply (MINUS_I , ln (add (num , multiply (temp , PLUS_I ))));
267
231
}
268
232
269
233
/**
@@ -274,14 +238,12 @@ public static ComplexNumber arccos(ComplexNumber num)
274
238
* @throws RuntimeException if <code>num=i</code> or <code>num=-i</code>
275
239
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
276
240
*/
277
- public static ComplexNumber arctan (ComplexNumber num )
278
- {
279
- if (num .equals (PLUS_I ) || num .equals (MINUS_I ))
280
- {
241
+ public static ComplexNumber arctan (ComplexNumber num ) {
242
+ if (num .equals (PLUS_I ) || num .equals (MINUS_I )) {
281
243
throw new RuntimeException ("Cannot take the arctan of " + num );
282
244
}
283
245
284
- return multiply (divide (MINUS_I ,TWO ),ln (divide (subtract (PLUS_I ,num ),add (PLUS_I ,num ))));
246
+ return multiply (divide (MINUS_I , TWO ), ln (divide (subtract (PLUS_I , num ), add (PLUS_I , num ))));
285
247
}
286
248
287
249
/**
@@ -292,14 +254,12 @@ public static ComplexNumber arctan(ComplexNumber num)
292
254
* @throws RuntimeException if <code>num=i</code> or <code>num=-i</code>
293
255
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
294
256
*/
295
- public static ComplexNumber arccot (ComplexNumber num )
296
- {
297
- if (num .equals (PLUS_I ) || num .equals (MINUS_I ))
298
- {
257
+ public static ComplexNumber arccot (ComplexNumber num ) {
258
+ if (num .equals (PLUS_I ) || num .equals (MINUS_I )) {
299
259
throw new RuntimeException ("Cannot take the arccot of " + num );
300
260
}
301
261
302
- return multiply (divide (MINUS_I ,TWO ),ln (divide (add (num ,PLUS_I ),subtract (num ,PLUS_I ))));
262
+ return multiply (divide (MINUS_I , TWO ), ln (divide (add (num , PLUS_I ), subtract (num , PLUS_I ))));
303
263
}
304
264
305
265
public static void main (final String [] args ) {
0 commit comments