|
7 | 7 |
|
8 | 8 | class ChebyshevIterationTest { |
9 | 9 |
|
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 }; |
23 | 13 | private static final double M1_LAMBDA_MIN = (7.0 - Math.sqrt(5.0)) / 2.0; |
24 | 14 | 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 }; |
28 | 16 |
|
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 }; |
43 | 21 | private static final double M2_LAMBDA_MIN = 2.0; |
44 | 22 | private static final double M2_LAMBDA_MAX = 8.0; |
45 | 23 |
|
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; |
66 | 25 | private static final double TOLERANCE = 1e-10; |
67 | 26 | 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; |
70 | 27 |
|
71 | 28 | @Test |
72 | 29 | 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); |
74 | 31 | assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE); |
75 | 32 | } |
76 | 33 |
|
77 | 34 | @Test |
78 | 35 | 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); |
80 | 37 | assertArrayEquals(M2_EXPECTED, solution, ASSERT_TOLERANCE); |
81 | 38 | } |
82 | 39 |
|
83 | 40 | @Test |
84 | 41 | 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); |
87 | 43 | assertArrayEquals(M1_EXPECTED, solution, ASSERT_TOLERANCE); |
88 | 44 | } |
89 | 45 |
|
90 | 46 | @Test |
91 | 47 | 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)); |
106 | 52 | } |
107 | 53 | } |
0 commit comments