Skip to content

Commit 61b3cc5

Browse files
committed
format the code using clang
1 parent 6ce45ca commit 61b3cc5

File tree

2 files changed

+65
-109
lines changed

2 files changed

+65
-109
lines changed

src/main/java/com/thealgorithms/maths/ComplexNumberUtil.java

+54-94
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package com.thealgorithms.maths;
22

3-
43
public class ComplexNumberUtil {
54

6-
public static class ComplexNumber
7-
{
5+
public static class ComplexNumber {
86
public final double REAL;
97
public final double IMAGINARY;
108

11-
public ComplexNumber(double real, double imaginary)
12-
{
9+
public ComplexNumber(double real, double imaginary) {
1310
REAL = real;
1411
IMAGINARY = imaginary;
1512
}
1613

1714
@Override
1815
public boolean equals(Object obj) {
19-
if (obj instanceof ComplexNumber num)
20-
{
16+
if (obj instanceof ComplexNumber num) {
2117
return this.REAL == num.REAL && this.IMAGINARY == num.IMAGINARY;
2218
}
2319

@@ -30,20 +26,19 @@ public String toString() {
3026
}
3127
}
3228

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);
3834

3935
/**
4036
* add two complex numbers
4137
* @param num1 the first complex number
4238
* @param num2 the second complex number
4339
* @return the sum of num1 and num2
4440
*/
45-
public static ComplexNumber add(ComplexNumber num1, ComplexNumber num2)
46-
{
41+
public static ComplexNumber add(ComplexNumber num1, ComplexNumber num2) {
4742
return new ComplexNumber(num1.REAL + num2.REAL, num1.IMAGINARY + num2.IMAGINARY);
4843
}
4944

@@ -54,8 +49,7 @@ public static ComplexNumber add(ComplexNumber num1, ComplexNumber num2)
5449
* @param num2 the second complex number
5550
* @return the result of subtracting num2 from num1
5651
*/
57-
public static ComplexNumber subtract(ComplexNumber num1, ComplexNumber num2)
58-
{
52+
public static ComplexNumber subtract(ComplexNumber num1, ComplexNumber num2) {
5953
return new ComplexNumber(num1.REAL - num2.REAL, num1.IMAGINARY - num2.IMAGINARY);
6054
}
6155

@@ -67,12 +61,8 @@ public static ComplexNumber subtract(ComplexNumber num1, ComplexNumber num2)
6761
* @return the product of num1 and num2
6862
* @link <a href="https://en.wikipedia.org/wiki/Complex_number#Multiplication">...</a>
6963
*/
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);
7666
}
7767

7868
/**
@@ -84,18 +74,13 @@ public static ComplexNumber multiply(ComplexNumber num1, ComplexNumber num2)
8474
* @throws RuntimeException if the divisor (num2) is zero
8575
* @link <a href="https://en.wikipedia.org/wiki/Complex_number#Complex_conjugate,_absolute_value_and_argument">...</a>
8676
*/
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) {
9280
throw new RuntimeException("Cannot divide by zero");
9381
}
9482

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);
9984
}
10085

10186
/**
@@ -105,9 +90,8 @@ public static ComplexNumber divide(ComplexNumber num1, ComplexNumber num2)
10590
* @return the absolute value of num
10691
* @link <a href="https://en.wikipedia.org/wiki/Complex_number#Complex_conjugate,_absolute_value_and_argument">...</a>
10792
*/
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);
11195
}
11296

11397
/**
@@ -117,13 +101,9 @@ public static double abs(ComplexNumber num)
117101
* @return e raised to the power of num
118102
* @link <a href="https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex">...</a>
119103
*/
120-
public static ComplexNumber exp(ComplexNumber num)
121-
{
104+
public static ComplexNumber exp(ComplexNumber num) {
122105
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));
127107
}
128108

129109
/**
@@ -134,17 +114,12 @@ public static ComplexNumber exp(ComplexNumber num)
134114
* @throws RuntimeException if num is zero
135115
* @link <a href="https://en.wikipedia.org/wiki/Complex_logarithm#Calculating_the_principal_value">...</a>
136116
*/
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)) {
141119
throw new RuntimeException("Cannot take the logarithm of zero");
142120
}
143121

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));
148123
}
149124

150125
/**
@@ -155,14 +130,12 @@ public static ComplexNumber ln(ComplexNumber num)
155130
* @return num1 raised to the power of num2
156131
* link <a href="https://en.wikipedia.org/wiki/Exponentiation#Complex_exponents_with_a_positive_real_base">...</a>
157132
*/
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)) {
162135
return num2.equals(ZERO) ? ONE : ZERO;
163136
}
164137

165-
return exp(multiply(ln(num1),num2));
138+
return exp(multiply(ln(num1), num2));
166139
}
167140

168141
/**
@@ -171,9 +144,8 @@ public static ComplexNumber pow(ComplexNumber num1, ComplexNumber num2)
171144
* @param num the complex number
172145
* @return the square root of num
173146
*/
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));
177149
}
178150

179151
/**
@@ -183,11 +155,10 @@ public static ComplexNumber sqrt(ComplexNumber num)
183155
* @return the sine of num
184156
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Relationship_to_exponential_function_">...</a>(Euler's_formula)
185157
*/
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));
191162
}
192163

193164
/**
@@ -197,11 +168,10 @@ public static ComplexNumber sin(ComplexNumber num)
197168
* @return the cosine of num
198169
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Relationship_to_exponential_function_">...</a>(Euler's_formula)
199170
*/
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);
205175
}
206176

207177
/**
@@ -212,14 +182,12 @@ public static ComplexNumber cos(ComplexNumber num)
212182
* @throws RuntimeException if <code>num.real = pi*(n+0.5)</code>
213183
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Right-angled_triangle_definitions">...</a>
214184
*/
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) {
219187
throw new RuntimeException("Cannot take the tan of a number where the real part can be expressed as pi*(n+0.5)");
220188
}
221189

222-
return divide(sin(num),cos(num));
190+
return divide(sin(num), cos(num));
223191
}
224192

225193
/**
@@ -230,14 +198,12 @@ public static ComplexNumber tan(ComplexNumber num)
230198
* @throws RuntimeException if <code>num.real = pi*n</code>
231199
* @link <a href="https://en.wikipedia.org/wiki/Trigonometric_functions#Right-angled_triangle_definitions">...</a>
232200
*/
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) {
237203
throw new RuntimeException("Cannot take the cot of number with real part dividable by pi");
238204
}
239205

240-
return divide(cos(num),sin(num));
206+
return divide(cos(num), sin(num));
241207
}
242208

243209
/**
@@ -247,10 +213,9 @@ public static ComplexNumber cot(ComplexNumber num)
247213
* @return the arcsine of num
248214
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
249215
*/
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)));
254219
}
255220

256221
/**
@@ -260,10 +225,9 @@ public static ComplexNumber arcsin(ComplexNumber num)
260225
* @return the arccosine of num
261226
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
262227
*/
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))));
267231
}
268232

269233
/**
@@ -274,14 +238,12 @@ public static ComplexNumber arccos(ComplexNumber num)
274238
* @throws RuntimeException if <code>num=i</code> or <code>num=-i</code>
275239
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
276240
*/
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)) {
281243
throw new RuntimeException("Cannot take the arctan of " + num);
282244
}
283245

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))));
285247
}
286248

287249
/**
@@ -292,14 +254,12 @@ public static ComplexNumber arctan(ComplexNumber num)
292254
* @throws RuntimeException if <code>num=i</code> or <code>num=-i</code>
293255
* @link <a href="https://en.wikipedia.org/wiki/Inverse_trigonometric_functions#Extension_to_the_complex_plane">...</a>
294256
*/
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)) {
299259
throw new RuntimeException("Cannot take the arccot of " + num);
300260
}
301261

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))));
303263
}
304264

305265
public static void main(final String[] args) {

src/test/java/com/thealgorithms/maths/ComplexNumberUtilTest.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.thealgorithms.maths;
22

33
import static org.junit.jupiter.api.Assertions.*;
4-
import org.junit.jupiter.api.Test;
4+
55
import com.thealgorithms.maths.ComplexNumberUtil.ComplexNumber;
6+
import org.junit.jupiter.api.Test;
67

78
public class ComplexNumberUtilTest {
89

9-
private boolean checkIfEqual(ComplexNumber expected, ComplexNumber actual)
10-
{
10+
private boolean checkIfEqual(ComplexNumber expected, ComplexNumber actual) {
1111
final double LIM = 0.0001;
12-
if (Math.abs(actual.REAL-expected.REAL) > LIM || Math.abs(actual.IMAGINARY-expected.IMAGINARY) > LIM)
13-
{
12+
if (Math.abs(actual.REAL - expected.REAL) > LIM || Math.abs(actual.IMAGINARY - expected.IMAGINARY) > LIM) {
1413
fail("Expected " + expected + " but got " + actual);
1514
}
1615

@@ -76,9 +75,7 @@ public void testDivide() {
7675
public void testDivideByZero() {
7776
ComplexNumber c1 = new ComplexNumber(1, 1);
7877
ComplexNumber c2 = new ComplexNumber(0, 0);
79-
assertThrows(RuntimeException.class, () -> {
80-
ComplexNumberUtil.divide(c1, c2);
81-
});
78+
assertThrows(RuntimeException.class, () -> { ComplexNumberUtil.divide(c1, c2); });
8279
}
8380

8481
@Test
@@ -114,17 +111,16 @@ public void testPow1() {
114111
public void testPow2() {
115112
ComplexNumber c1 = new ComplexNumber(-3, 4);
116113
ComplexNumber c2 = new ComplexNumber(2, -5);
117-
ComplexNumber result = ComplexNumberUtil.pow(c1,c2);
118-
ComplexNumber expected = new ComplexNumber(-1428309.3755404,738159.21728509);
119-
checkIfEqual(expected,result);
114+
ComplexNumber result = ComplexNumberUtil.pow(c1, c2);
115+
ComplexNumber expected = new ComplexNumber(-1428309.3755404, 738159.21728509);
116+
checkIfEqual(expected, result);
120117
}
121118

122119
@Test
123-
public void testSqrt()
124-
{
125-
ComplexNumber c = new ComplexNumber(-2,3);
120+
public void testSqrt() {
121+
ComplexNumber c = new ComplexNumber(-2, 3);
126122
ComplexNumber result = ComplexNumberUtil.sqrt(c);
127-
checkIfEqual(new ComplexNumber(0.8959774,1.6741492),result);
123+
checkIfEqual(new ComplexNumber(0.8959774, 1.6741492), result);
128124
}
129125

130126
@Test

0 commit comments

Comments
 (0)