Skip to content

Commit 693e02f

Browse files
authored
Update ChebyshevIterationTest.java
1 parent af64607 commit 693e02f

File tree

1 file changed

+25
-108
lines changed

1 file changed

+25
-108
lines changed

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

Lines changed: 25 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,34 @@ class ChebyshevIterationTest {
1212
private static final double M1_A12 = 1.0;
1313
private static final double M1_A21 = 1.0;
1414
private static final double M1_A22 = 3.0;
15-
private static final double[][] M1_A = { { M1_A11, M1_A12 }, { M1_A21, M1_A22 } };
15+
private static final double[][] M1_A = {{M1_A11, M1_A12}, {M1_A21, M1_A22}};
1616

1717
private static final double M1_B1 = 1.0;
1818
private static final double M1_B2 = 2.0;
19-
private static final double[] M1_B = { M1_B1, M1_B2 };
20-
private static final double[] M1_X0 = { 0.0, 0.0 };
19+
private static final double[] M1_B = {M1_B1, M1_B2};
20+
private static final double[] M1_X0 = {0.0, 0.0};
2121

2222
// Eigenvalues are (7 +/- sqrt(5)) / 2
2323
private static final double M1_LAMBDA_MIN = (7.0 - Math.sqrt(5.0)) / 2.0;
2424
private static final double M1_LAMBDA_MAX = (7.0 + Math.sqrt(5.0)) / 2.0;
2525
private static final double M1_EXPECTED_X1 = 1.0 / 11.0;
2626
private static final double M1_EXPECTED_X2 = 7.0 / 11.0;
27-
private static final double[] M1_EXPECTED = { M1_EXPECTED_X1, M1_EXPECTED_X2 };
27+
private static final double[] M1_EXPECTED = {M1_EXPECTED_X1, M1_EXPECTED_X2};
2828

2929
// --- Constants for testSolve3x3System ---
3030
private static final double M2_A11 = 5.0;
3131
private static final double M2_A22 = 2.0;
3232
private static final double M2_A33 = 8.0;
33-
private static final double[][] M2_A = {
34-
{ M2_A11, 0.0, 0.0 },
35-
{ 0.0, M2_A22, 0.0 },
36-
{ 0.0, 0.0, M2_A33 },
37-
};
33+
private static final double[][] M2_A = {{M2_A11, 0.0, 0.0}, {0.0, M2_A22, 0.0}, {0.0, 0.0, M2_A33}};
3834
private static final double M2_B1 = 10.0;
3935
private static final double M2_B2 = -4.0;
4036
private static final double M2_B3 = 24.0;
41-
private static final double[] M2_B = { M2_B1, M2_B2, M2_B3 };
42-
private static final double[] M2_X0 = { 0.0, 0.0, 0.0 };
37+
private static final double[] M2_B = {M2_B1, M2_B2, M2_B3};
38+
private static final double[] M2_X0 = {0.0, 0.0, 0.0};
4339
private static final double M2_E1 = 2.0;
4440
private static final double M2_E2 = -2.0;
4541
private static final double M2_E3 = 3.0;
46-
private static final double[] M2_EXPECTED = { M2_E1, M2_E2, M2_E3 };
42+
private static final double[] M2_EXPECTED = {M2_E1, M2_E2, M2_E3};
4743
private static final double M2_LAMBDA_MIN = 2.0;
4844
private static final double M2_LAMBDA_MAX = 8.0;
4945

@@ -57,16 +53,13 @@ class ChebyshevIterationTest {
5753
private static final double VAL_1_0 = 1.0;
5854
private static final double VAL_1_5 = 1.5;
5955
private static final double VAL_2_0 = 2.0;
60-
private static final double[][] M4_A = { { VAL_1_0, VAL_0_0 }, { VAL_0_0, VAL_1_0 } };
61-
private static final double[] M4_B = { VAL_1_0, VAL_1_0 };
62-
private static final double[] M4_X0 = { VAL_0_0, VAL_0_0 };
63-
private static final double[] M5_B = { VAL_1_0, VAL_1_0, VAL_1_0 };
64-
private static final double[][] M6_A = {
65-
{ VAL_1_0, VAL_0_0, VAL_0_0 },
66-
{ VAL_0_0, VAL_1_0, VAL_0_0 },
67-
};
68-
private static final double[] M6_B = { VAL_1_0, VAL_1_0 };
69-
private static final double[] M6_X0 = { VAL_0_0, VAL_0_0 };
56+
private static final double[][] M4_A = {{VAL_1_0, VAL_0_0}, {VAL_0_0, VAL_1_0}};
57+
private static final double[] M4_B = {VAL_1_0, VAL_1_0};
58+
private static final double[] M4_X0 = {VAL_0_0, VAL_0_0};
59+
private static final double[] M5_B = {VAL_1_0, VAL_1_0, VAL_1_0};
60+
private static final double[][] M6_A = {{VAL_1_0, VAL_0_0, VAL_0_0}, {VAL_0_0, VAL_1_0, VAL_0_0}};
61+
private static final double[] M6_B = {VAL_1_0, VAL_1_0};
62+
private static final double[] M6_X0 = {VAL_0_0, VAL_0_0};
7063

7164
// --- General Constants ---
7265
private static final int MAX_ITERATIONS = 100;
@@ -77,114 +70,38 @@ class ChebyshevIterationTest {
7770

7871
@Test
7972
void testSolveSimple2x2System() {
80-
double[] solution = ChebyshevIteration.solve(
81-
M1_A,
82-
M1_B,
83-
M1_X0,
84-
M1_LAMBDA_MIN,
85-
M1_LAMBDA_MAX,
86-
MAX_ITERATIONS,
87-
TOLERANCE
88-
);
73+
double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_X0, M1_LAMBDA_MIN, M1_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
8974
assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE);
9075
}
9176

9277
@Test
9378
void testSolve3x3System() {
94-
double[] solution = ChebyshevIteration.solve(
95-
M2_A,
96-
M2_B,
97-
M2_X0,
98-
M2_LAMBDA_MIN,
99-
M2_LAMBDA_MAX,
100-
MAX_ITERATIONS,
101-
TOLERANCE
102-
);
79+
double[] solution = ChebyshevIteration.solve(M2_A, M2_B, M2_X0, M2_LAMBDA_MIN, M2_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
10380
assertArrayEquals(M2_EXPECTED, solution, ASSERT_TOLERANCE);
10481
}
10582

10683
@Test
10784
void testAlreadyConverged() {
108-
// Test case where the initial guess is already the solution
109-
double[] solution = ChebyshevIteration.solve(
110-
M1_A,
111-
M1_B,
112-
M1_EXPECTED, // Use expected solution as initial guess
113-
M3_LAMBDA_MIN, // Use approximate eigenvalues
114-
M3_LAMBDA_MAX,
115-
MAX_ITERATIONS,
116-
TOLERANCE
117-
);
85+
86+
double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_EXPECTED, M3_LAMBDA_MIN, M3_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
11887
assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE);
11988
}
12089

12190
@Test
12291
void testInvalidEigenvalues() {
123-
// lambdaMin >= lambdaMax
124-
assertThrows(
125-
IllegalArgumentException.class,
126-
() -> {
127-
ChebyshevIteration.solve(
128-
M4_A,
129-
M4_B,
130-
M4_X0,
131-
VAL_2_0,
132-
VAL_1_0,
133-
TEST_ITERATIONS,
134-
TEST_TOLERANCE
135-
);
136-
}
137-
);
138-
// lambdaMin <= 0
139-
assertThrows(
140-
IllegalArgumentException.class,
141-
() -> {
142-
ChebyshevIteration.solve(
143-
M4_A,
144-
M4_B,
145-
M4_X0,
146-
VAL_0_0,
147-
VAL_2_0,
148-
TEST_ITERATIONS,
149-
TEST_TOLERANCE
150-
);
151-
}
152-
);
92+
93+
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M4_A, M4_B, M4_X0, VAL_2_0, VAL_1_0, TEST_ITERATIONS, TEST_TOLERANCE); });
94+
95+
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M4_A, M4_B, M4_X0, VAL_0_0, VAL_2_0, TEST_ITERATIONS, TEST_TOLERANCE); });
15396
}
15497

15598
@Test
15699
void testMismatchedDimensions() {
157-
assertThrows(
158-
IllegalArgumentException.class,
159-
() -> {
160-
ChebyshevIteration.solve(
161-
M4_A,
162-
M5_B,
163-
M4_X0,
164-
VAL_0_5,
165-
VAL_1_5,
166-
TEST_ITERATIONS,
167-
TEST_TOLERANCE
168-
);
169-
}
170-
);
100+
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M4_A, M5_B, M4_X0, VAL_0_5, VAL_1_5, TEST_ITERATIONS, TEST_TOLERANCE); });
171101
}
172102

173103
@Test
174104
void testNonSquareMatrix() {
175-
assertThrows(
176-
IllegalArgumentException.class,
177-
() -> {
178-
ChebyshevIteration.solve(
179-
M6_A,
180-
M6_B,
181-
M6_X0,
182-
VAL_0_5,
183-
VAL_1_5,
184-
TEST_ITERATIONS,
185-
TEST_TOLERANCE
186-
);
187-
}
188-
);
105+
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M6_A, M6_B, M6_X0, VAL_0_5, VAL_1_5, TEST_ITERATIONS, TEST_TOLERANCE); });
189106
}
190107
}

0 commit comments

Comments
 (0)