Skip to content

Commit 4796867

Browse files
authored
Update ChebyshevIterationTest.java
1 parent de0b3af commit 4796867

File tree

1 file changed

+16
-70
lines changed

1 file changed

+16
-70
lines changed

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

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,101 +7,47 @@
77

88
class ChebyshevIterationTest {
99

10-
// --- Constants for testSolveSimple2x2System ---
11-
private static final double M1_A11 = 4.0;
12-
private static final double M1_A12 = 1.0;
13-
private static final double M1_A21 = 1.0;
14-
private static final double M1_A22 = 3.0;
15-
private static final double[][] M1_A = {{M1_A11, M1_A12}, {M1_A21, M1_A22}};
16-
17-
private static final double M1_B1 = 1.0;
18-
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};
21-
22-
// Eigenvalues are (7 +/- sqrt(5)) / 2
10+
private static final double[][] M1_A = { { 4.0, 1.0 }, { 1.0, 3.0 } };
11+
private static final double[] M1_B = { 1.0, 2.0 };
12+
private static final double[] M1_X0 = { 0.0, 0.0 };
2313
private static final double M1_LAMBDA_MIN = (7.0 - Math.sqrt(5.0)) / 2.0;
2414
private static final double M1_LAMBDA_MAX = (7.0 + Math.sqrt(5.0)) / 2.0;
25-
private static final double M1_EXPECTED_X1 = 1.0 / 11.0;
26-
private static final double M1_EXPECTED_X2 = 7.0 / 11.0;
27-
private static final double[] M1_EXPECTED = {M1_EXPECTED_X1, M1_EXPECTED_X2};
15+
private static final double[] M1_EXPECTED = { 1.0 / 11.0, 7.0 / 11.0 };
2816

29-
// --- Constants for testSolve3x3System ---
30-
private static final double M2_A11 = 5.0;
31-
private static final double M2_A22 = 2.0;
32-
private static final double M2_A33 = 8.0;
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}};
34-
private static final double M2_B1 = 10.0;
35-
private static final double M2_B2 = -4.0;
36-
private static final double M2_B3 = 24.0;
37-
private static final double[] M2_B = {M1_B1, M2_B2, M2_B3};
38-
private static final double[] M2_X0 = {0.0, 0.0, 0.0};
39-
private static final double M2_E1 = 2.0;
40-
private static final double M2_E2 = -2.0;
41-
private static final double M2_E3 = 3.0;
42-
private static final double[] M2_EXPECTED = {M2_E1, M2_E2, M2_E3};
17+
private static final double[][] M2_A = { { 5.0, 0.0, 0.0 }, { 0.0, 2.0, 0.0 }, { 0.0, 0.0, 8.0 } };
18+
private static final double[] M2_B = { 10.0, -4.0, 24.0 };
19+
private static final double[] M2_X0 = { 0.0, 0.0, 0.0 };
20+
private static final double[] M2_EXPECTED = { 2.0, -2.0, 3.0 };
4321
private static final double M2_LAMBDA_MIN = 2.0;
4422
private static final double M2_LAMBDA_MAX = 8.0;
4523

46-
// --- Constants for testAlreadyConverged ---
47-
private static final double M3_LAMBDA_MIN = 2.38;
48-
private static final double M3_LAMBDA_MAX = 4.62;
49-
50-
// --- Constants for Invalid/Dimension Tests ---
51-
private static final double VAL_0_0 = 0.0;
52-
private static final double VAL_0_5 = 0.5;
53-
private static final double VAL_1_0 = 1.0;
54-
private static final double VAL_1_5 = 1.5;
55-
private static final double VAL_2_0 = 2.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};
63-
64-
// --- General Constants ---
65-
private static final int MAX_ITERATIONS = 100;
24+
private static final double MAX_ITERATIONS = 100;
6625
private static final double TOLERANCE = 1e-10;
6726
private static final double ASSERT_TOLERANCE = 1e-9;
68-
private static final int TEST_ITERATIONS = 10;
69-
private static final double TEST_TOLERANCE = 1e-5;
7027

7128
@Test
7229
void testSolveSimple2x2System() {
73-
double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_X0, M1_LAMBDA_MIN, M1_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
30+
double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_X0, M1_LAMBDA_MIN, M1_LAMBDA_MAX, 100, TOLERANCE);
7431
assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE);
7532
}
7633

7734
@Test
7835
void testSolve3x3System() {
79-
double[] solution = ChebyshevIteration.solve(M2_A, M2_B, M2_X0, M2_LAMBDA_MIN, M2_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
36+
double[] solution = ChebyshevIteration.solve(M2_A, M2_B, M2_X0, M2_LAMBDA_MIN, M2_LAMBDA_MAX, 100, TOLERANCE);
8037
assertArrayEquals(M2_EXPECTED, solution, ASSERT_TOLERANCE);
8138
}
8239

8340
@Test
8441
void testAlreadyConverged() {
85-
// Test case where the initial guess is already the solution
86-
double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_EXPECTED, M3_LAMBDA_MIN, M3_LAMBDA_MAX, MAX_ITERATIONS, TOLERANCE);
42+
double[] solution = ChebyshevIteration.solve(M1_A, M1_B, M1_EXPECTED, M1_LAMBDA_MIN, M1_LAMBDA_MAX, 100, TOLERANCE);
8743
assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE);
8844
}
8945

9046
@Test
9147
void testInvalidEigenvalues() {
92-
// lambdaMin >= lambdaMax
93-
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M4_A, M4_B, M4_X0, VAL_2_0, VAL_1_0, TEST_ITERATIONS, TEST_TOLERANCE); });
94-
// lambdaMin <= 0
95-
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M4_A, M4_B, M4_X0, VAL_0_0, VAL_2_0, TEST_ITERATIONS, TEST_TOLERANCE); });
96-
}
97-
98-
@Test
99-
void testMismatchedDimensions() {
100-
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M4_A, M5_B, M4_X0, VAL_0_5, VAL_1_5, TEST_ITERATIONS, TEST_TOLERANCE); });
101-
}
102-
103-
@Test
104-
void testNonSquareMatrix() {
105-
assertThrows(IllegalArgumentException.class, () -> { ChebyshevIteration.solve(M6_A, M6_B, M6_X0, VAL_0_5, VAL_1_5, TEST_ITERATIONS, TEST_TOLERANCE); });
48+
assertThrows(IllegalArgumentException.class,
49+
() -> ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 2.0, 1.0, 10, 1e-5));
50+
assertThrows(IllegalArgumentException.class,
51+
() -> ChebyshevIteration.solve(M1_A, M1_B, M1_X0, 0.0, 2.0, 10, 1e-5));
10652
}
10753
}

0 commit comments

Comments
 (0)